I am implementing a java-like language that starts with 'package' and 'imports' .It uses xbase and it works fairly well. However there are two issues:
1) Classes in the same package are not imported by default.
2) Importing native java classes or interfaces does not work properly (the classes are visible but I can't call methods on them).
I am trying to implement this 'out of the box', that is by using the magic 'importedNamespace' name in the grammar, like this:
Is this possible? That is can I fix the two issues above without adding custom fragments or guice imports (which I don't know how to do). I am not using @Inject extension IQualifiedNameProvider.
Notes:
Apart from the grammar file and the generator (myJvmModelInferrer) file everything else is left with the default file contents. (so I am using QualifiedNamesFragment).
I can get round issue 1 like this:
package myPackage
import myPackage.*
but I would like to avoid the need for the additional import.
I can use methods on the built-in java library such as 'String' but not java code that I have written myself (in the same eclipse project).
I would appreciate any information about this or pointers to further reading that I could do.
(I have already read this: blogs.itemis.de/stundzig/archives/773)
You can fix the first by changing the scope provider:
package yourdsl.scoping;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.scoping.impl.ImportNormalizer;
import org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider;
import ru.focusmedia.fire.idl.idl.Model;
import com.google.common.collect.ImmutableList;
public class YourDslImportedNamespaceScopeProvider extends
ImportedNamespaceAwareLocalScopeProvider {
// automatically import all types from the package we are in
@Override
protected List<ImportNormalizer> internalGetImportedNamespaceResolvers(
EObject context, boolean ignoreCase) {
List<ImportNormalizer> importedNamespaceResolvers = super
.internalGetImportedNamespaceResolvers(context, ignoreCase);
String ownNamespace = EcoreUtil2.getContainerOfType(context,
Model.class).getName()
+ ".*";
importedNamespaceResolvers.add(createImportedNamespaceResolver(
ownNamespace, ignoreCase));
return importedNamespaceResolvers;
}
// can also add implicit imports, etc.
}
and changing your module to use it:
/*
* generated by Xtext
*/
package yourdsl;
import org.eclipse.xtext.generator.IGenerator;
import com.google.inject.Binder;
import com.google.inject.name.Names;
/**
* Use this class to register components to be used at runtime / without the Equinox extension registry.
*/
public class YourDslRuntimeModule extends AbstractYourDslRuntimeModule {
@Override
public void configureIScopeProviderDelegate(Binder binder) {
binder.bind(org.eclipse.xtext.scoping.IScopeProvider.class).annotatedWith(Names.named(org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider.NAMED_DELEGATE)).to(YourDslImportedNamespaceScopeProvider.class);
}
}
Thanks very much for your reply, I think its almost there but its causing a problem at runtime. I did the following:
A) I created a scoping directory. For some reason when I run mwe2 on an xbase project it does not build a scoping directory even though it has the following line:
fragment = scoping.ImportNamespacesScopingFragment {}
B) In that directory I constructed a class called: EditorImportedNamespaceScopeProvider
I then copy and pasted your code and changed 'YourDsl' to the name of my project.
I changed: import ru.focusmedia.fire.idl.idl.Model; to the name of my model.
I changed Model.class).getName() to Model.class).getImportedNamespace()
C) I edited the already existing file: EditorRuntimeModule which contained an empty class. I copy/pasted your code into it and again changed the name to correspond with my project.
This code then compiled without errors, but when I start the runtime version of eclipse I get a continuous sequence of errors, I have put a sample below:
Thanks, Martin
190993 [main] ERROR org.eclipse.xtext.linking.lazy.LazyLinkingResource - resolution of uriFragment 'xtextLink_::0.2.0.7.14.4.1.1::1::/0' failed.
java.lang.NullPointerException
at com.euclideanspace.euclid.scoping.EditorImportedNamespaceScopeProvider.internalGetImportedNamespaceResolvers(EditorImportedNamespaceScopeProvider.java:23)
at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider$1.get(ImportedNamespaceAwareLocalScopeProvider.java:123)
at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider$1.get(ImportedNamespaceAwareLocalScopeProvider.java:1)
at org.eclipse.xtext.util.OnChangeEvictingCache.get(OnChangeEvictingCache.java:75)
at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getImportedNamespaceResolvers(ImportedNamespaceAwareLocalScopeProvider.java:121)
at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getLocalElementsScope(ImportedNamespaceAwareLocalScopeProvider.java:174)
at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getScope(ImportedNamespaceAwareLocalScopeProvider.java:91)
at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getScope(ImportedNamespaceAwareLocalScopeProvider.java:87)
at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getScope(ImportedNamespaceAwareLocalScopeProvider.java:87)
at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getScope(ImportedNamespaceAwareLocalScopeProvider.java:87)
at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getScope(ImportedNamespaceAwareLocalScopeProvider.java:87)
at org.eclipse.xtext.xbase.scoping.XtypeScopeProvider.getJvmTypeScope(XtypeScopeProvider.java:37)
at org.eclipse.xtext.xbase.scoping.XtypeScopeProvider.getScope(XtypeScopeProvider.java:31)
at org.eclipse.xtext.xbase.scoping.XbaseScopeProvider.createTypeScope(XbaseScopeProvider.java:199)
at org.eclipse.xtext.xbase.scoping.XbaseScopeProvider.getScope(XbaseScopeProvider.java:185)
at org.eclipse.xtext.xbase.linking.XbaseLinkingScopeProvider.getScope(XbaseLinkingScopeProvider.java:42)
at org.eclipse.xtext.linking.impl.DefaultLinkingService.getScope(DefaultLinkingService.java:59)
at org.eclipse.xtext.linking.impl.DefaultLinkingService.getLinkedObjects(DefaultLinkingService.java:119)
at org.eclipse.xtext.linking.lazy.LazyLinkingResource.getEObject(LazyLinkingResource.java:179)
at org.eclipse.xtext.xbase.resource.XbaseResource.access$0(XbaseResource.java:1)
at org.eclipse.xtext.xbase.resource.XbaseResource$2.exec(XbaseResource.java:239)
at org.eclipse.xtext.xbase.resource.XbaseResource$2.exec(XbaseResource.java:1)
at org.eclipse.xtext.util.OnChangeEvictingCache.execWithoutCacheClear(OnChangeEvictingCache.java:124)
at org.eclipse.xtext.xbase.resource.XbaseResource.getEObject(XbaseResource.java:237)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getEObject(ResourceSetImpl.java:223)
at org.eclipse.emf.ecore.util.EcoreUtil.resolve(EcoreUtil.java:197)
at org.eclipse.emf.ecore.util.EcoreUtil.resolve(EcoreUtil.java:257)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eResolveProxy(BasicEObjectImpl.java:1473)
at org.eclipse.xtext.common.types.impl.JvmTypeReferenceImplCustom.eResolveProxy(JvmTypeReferenceImplCustom.java:37)
at org.eclipse.xtext.common.types.impl.JvmParameterizedTypeReferenceImpl.getType(JvmParameterizedTypeReferenceImpl.java:110)
at org.eclipse.xtext.common.types.util.Primitives$1.doVisitParameterizedTypeReference(Primitives.java:134)
at org.eclipse.xtext.common.types.util.Primitives$1.doVisitParameterizedTypeReference(Primitives.java:1)
at
<snip> lots more like this </snip>
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
!ENTRY org.apache.log4j 4 0 2012-11-06 08:59:13.917
!MESSAGE org.eclipse.xtext.linking.lazy.LazyLinkingResource - resolution of uriFragment 'xtextLink_::0.2.0.7.14.4.1.1::1::/0' failed.
!STACK 0
java.lang.NullPointerException
at com.euclideanspace.euclid.scoping.EditorImportedNamespaceScopeProvider.internalGetImportedNamespaceResolvers(EditorImportedNamespaceScopeProvider.java:23)
at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider$1.get(ImportedNamespaceAwareLocalScopeProvider.java:123)
at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider$1.get(ImportedNamespaceAwareLocalScopeProvider.java:1)
at org.eclipse.xtext.util.OnChangeEvictingCache.get(OnChangeEvictingCache.java:75)
at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getImportedNamespaceResolvers(ImportedNamespaceAwareLocalScopeProvider.java:121)
at
<snip>
Please ignore my last message, it was adding '.*. to imports rather than the package. I have now fixed that and it now recognises stuff in the same package! Thanks.
However it has caused another problem. It no longer recognises the built-in library classes such as 'String'