Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Reduce available JvmTypes
Reduce available JvmTypes [message #1759730] Mon, 17 April 2017 18:54 Go to next message
Marco Ullrich is currently offline Marco UllrichFriend
Messages: 14
Registered: December 2016
Location: Bayreuth
Junior Member
Hello everyone,

i am currently writing a beginner friendly OO-Language based on Xbase.
I decided to use Xbase for this purpose, hence most of my DSL concepts can easily be mapped to Java and it ships with a lot of nice features (like debugging).

My problem is, that Xbase provides too many types (e.g. StringBuilder etc...), which can be quiet overwhelming for beginners.

I only want to provide some primitive types (like int, String, boolean and double) and some datastructures like arrays and lists (and user-defined classes of course).

I tried to override the different Xbase grammarrules to achieve this, e.g:

JvmParameterizedTypeReference:
type=[JvmPrimitiveType] ;

But with no luck. : /

So my question is: How can i reduce the amount of available JvmTypes?
Where should i start / what is the best approach for that?

Thanks in advance and kind regards,
Marco
Re: Reduce available JvmTypes [message #1759790 is a reply to message #1759730] Tue, 18 April 2017 06:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
I think you should make use of some filtering of the proposals e.g. as in https://kthoms.wordpress.com/2012/03/14/how-to-limit-proposed-java-types-to-implementors-of-an-interface/

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Reduce available JvmTypes [message #1759819 is a reply to message #1759730] Tue, 18 April 2017 08:47 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

You can also restrict the types with JDT Type Filters preference setting. That does not require any customization, just preference configuration.
Re: Reduce available JvmTypes [message #1759829 is a reply to message #1759819] Tue, 18 April 2017 17:31 Go to previous messageGo to next message
Marco Ullrich is currently offline Marco UllrichFriend
Messages: 14
Registered: December 2016
Location: Bayreuth
Junior Member
Hello Christian and Karsten,
thank you for your fast reply!

I tried your approaches and they work well for the proposed types, but it wont prevent a user from using/writing types like StringBuilder in a program.
Is there a way to only allow the mentioned primitive types and collections on a grammar level?

Kind regards,
Marco
Re: Reduce available JvmTypes [message #1759830 is a reply to message #1759829] Tue, 18 April 2017 17:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
That would be scoping not grammar
(See XbaseBatchScopeProvider for entry point
And
IJvmTypeProvider)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Reduce available JvmTypes [message #1759897 is a reply to message #1759829] Wed, 19 April 2017 08:10 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Right. I would wrap the scope with a FilteringScope and only accept allowed types.
Re: Reduce available JvmTypes [message #1759911 is a reply to message #1759730] Wed, 19 April 2017 20:12 Go to previous messageGo to next message
Marco Ullrich is currently offline Marco UllrichFriend
Messages: 14
Registered: December 2016
Location: Bayreuth
Junior Member
Thank you both for your support, now I am back on track. Smile

I decided to extend and customize the JdtTypeProvider, hence it should only provide some types.

Now I have one question left:
The scope provider now uses my implementation of the type provider but all types are still proposed.
I thought the proposals were build depending on the content of of the scope, aren't they?
As a workaround, i could customize the proposals as mentioned in Karstens blog, but I wonder why it still proposes all types....

Here my solution so far ,just in case someone else has the same problem / requirement:

In the UI-Plugin add a factory...
@Singleton
class MyFilteredJdtTypeProviderFactory extends JdtTypeProviderFactory {
	
	@Inject
	IWorkingCopyOwnerProvider copyOwnerProvider;
	
	@Inject
	TypeResourceServices typeResourceServices;

	override protected IJdtTypeProvider createJdtTypeProvider(IJavaProject javaProject, ResourceSet resourceSet) {
		if (javaProject == null){
			return new NullJdtTypeProvider(resourceSet)		
		}
		
		var WorkingCopyOwner wco= null
		if(copyOwnerProvider==null)		
			wco = DefaultWorkingCopyOwner.PRIMARY 
		else
			wco = copyOwnerProvider.getWorkingCopyOwner(javaProject, resourceSet)
		
		return new MyFilteredJdtTypeProvider(javaProject, resourceSet, getIndexedJvmTypeAccess(), wco, typeResourceServices);
	}
}

...and the provider...
class MyFilteredJdtTypeProvider extends JdtTypeProvider {
	
	public new(IJavaProject javaProject, ResourceSet resourceSet, IndexedJvmTypeAccess indexedJvmTypeAccess, WorkingCopyOwner workingCopyOwner, TypeResourceServices services) {
		super(javaProject, resourceSet, indexedJvmTypeAccess, workingCopyOwner, services)
	}
	
	private val HashSet<String> supportedJdtTypes ={
		val hs = new HashSet()		
		hs.add("int")
		hs.add("double")
		hs.add("boolean")
		hs.add("java.lang.String")

               // Required by the compiler to process method bodys right 
                hs.add("void") 

		// Required, otherwise operators do not work
		hs.add("org.eclipse.xtext.xbase.lib.StringExtensions")
		hs.add("org.eclipse.xtext.xbase.lib.IntegerExtensions")
		hs.add("org.eclipse.xtext.xbase.lib.DoubleExtensions")
		hs.add("org.eclipse.xtext.xbase.lib.BooleanExtensions")		
		return hs
	}
		
	public override JvmType findTypeByName(String name) {
		if(name.isTypeSupported){
			return doFindTypeByName(name, false)			
		}else{
			return null
		}
	}
	
	public override JvmType findTypeByName(String name, boolean binaryNestedTypeDelimiter) {
		if(name.isTypeSupported){			
			return super.findTypeByName(name,binaryNestedTypeDelimiter)	
		}else{
			return null
		}
	}
	
	def isTypeSupported(String name){
		supportedJdtTypes.contains(name)	
	}
}

... and tell the framework (in UiModule.xtend) to use our factory instead of its default impl:
	override Class<? extends IJvmTypeProvider.Factory> bindIJvmTypeProvider$Factory() {
		return MyFilteredJdtTypeProviderFactory;
	}

[Updated on: Thu, 20 April 2017 18:38]

Report message to a moderator

Re: Reduce available JvmTypes [message #1759914 is a reply to message #1759911] Wed, 19 April 2017 20:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
No they are not. There is an exception for jvm types regard scope = content assist thus my first hint on ca filtering

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Reduce available JvmTypes [message #1759948 is a reply to message #1759730] Thu, 20 April 2017 10:01 Go to previous message
Marco Ullrich is currently offline Marco UllrichFriend
Messages: 14
Registered: December 2016
Location: Bayreuth
Junior Member
Okay, I See, thanks again. Smile

In case someone should be interested, this is my approach (iam filtering the types of the ITypesProposalProvider / JdtTypesProposalProvider):

class MyDslProposalProvider extends AbstractExpressionsProposalProvider {
    override protected completeJavaTypes(ContentAssistContext context, EReference reference, boolean forced,IValueConverter<String> valueConverter, ITypesProposalProvider.Filter filter,ICompletionProposalAcceptor acceptor) {		
		val extendedFilter = TypeMatchFilters.and(filter,new SupportedTypesFilter())	
		super.completeJavaTypes(context, reference, forced, valueConverter, extendedFilter, acceptor)
	}
          	 
	private static class SupportedTypesFilter extends AbstractFilter {
		override boolean accept(int modifiers, char[] packageName, char[] simpleTypeName,char[][] enclosingTypeNames, String path) {			
			// Add further predicates to filter for java types here
			packageName.join.equals("java.lang")&& simpleTypeName.join.equals("String")						
		}
	}
}
Previous Topic:Is it possible to call @Check method only once in validation
Next Topic:Active Annotation Concept in a XBase and JvmModel based DSL
Goto Forum:
  


Current Time: Fri Apr 19 19:40:56 GMT 2024

Powered by FUDForum. Page generated in 0.04163 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top