Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Trying to use types that are only imported via import statement
Trying to use types that are only imported via import statement [message #1405048] Wed, 06 August 2014 12:58 Go to next message
Pavlina Temelakieva is currently offline Pavlina TemelakievaFriend
Messages: 10
Registered: July 2014
Junior Member
Hey guys,

I am trying to write java interfaces -like language. I want to be able to use only types that are explicitly imported via import statement.

Here is my grammar:

DomainModel:
packageDeclaration=PackageDeclaration;

PackageDeclaration:
"package" name=QualifiedName ";" importDeclarations+=ImportDeclaration*
abstractTypeDeclaration=AbstractTypeDeclaration;

ImportDeclaration:
"import" importedNamespace=TypeQualifiedName ";";

AbstractTypeDeclaration:
modifier+=ClassOrInterfaceModifier* typeDeclaration=TypeDeclaration;

TypeDeclaration:
/*EnumDeclaration |*/ InterfaceDeclaration
;

TypeQualifiedName:
type=[TypeDeclaration|QualifiedName];

Type:
type=[TypeDeclaration|ID];

InterfaceDeclaration:
"interface" name=ValidID parameters+=TypeParameters? body=InterfaceBody;

InterfaceBody:
"{" interfaceDeclaration+=InterfaceBodyDeclaration+ "}";

InterfaceBodyDeclaration:
modifiers+=ClassOrInterfaceModifier | memberDeclaration=InterfaceMemberDeclaration;

InterfaceMemberDeclaration:
InterfaceMethodDeclaration
;

InterfaceMethodDeclaration:
(type=Type | "void")? name=ValidID parameters=FormalParameters ("throws"
exceptions=QualifiedNameList)? ";";


I've also implemented ImportedNamespaceAwareLocalScopeProvider in order to dynamically load imported namespaces depending on the current file.

@Override
protected List<ImportNormalizer> internalGetImportedNamespaceResolvers(
EObject context, boolean ignoreCase) {
if (!(context instanceof DomainModel)) {
return Collections.emptyList();
}

List<ImportNormalizer> importedNamespaces = Lists.newArrayList();
DomainModel domainModel = (DomainModel) context;
PackageDeclaration packageDeclaration = domainModel
.getPackageDeclaration();
EList<ImportDeclaration> importDeclarations = packageDeclaration
.getImportDeclarations();
for (ImportDeclaration importDeclaration : importDeclarations) {
// String value = importDeclaration.getImportedNamespace();
String value = null;
TypeQualifiedName typeQualifiedName = importDeclaration
.getImportedNamespace();
TypeDeclaration typeDeclaration = typeQualifiedName.getType();
if (typeDeclaration instanceof EnumDeclaration) {
EnumDeclaration enumDeclaration = (EnumDeclaration) typeDeclaration;
PackageDeclaration packageDecl = (PackageDeclaration) enumDeclaration
.eContainer().eContainer();
String name = enumDeclaration.getName();
value = packageDecl.getName().concat(DOT_SEPARATOR)
.concat(name);
} else if (typeDeclaration instanceof InterfaceDeclaration) {
InterfaceDeclaration interfaceDeclaration = (InterfaceDeclaration) typeDeclaration;
PackageDeclaration packageDecl = (PackageDeclaration) interfaceDeclaration
.eContainer().eContainer();
String name = interfaceDeclaration.getName();
value = packageDecl.getName().concat(DOT_SEPARATOR)
.concat(name);
}
if (value != null) {
ImportNormalizer resolver = createImportedNamespaceResolver(
value, ignoreCase);
if (resolver != null) {
importedNamespaces.add(resolver);
}
}
}

return importedNamespaces;
}

I can successfully build the list of imports but when I create interface and have method who is referencing an imported type it doesn't work.

On the other hand if the importedNamespace=QualifiedName is used instead in the grammar definition it works but I prefer to be able to validate imports before use them in the file. Like this the validation is perfomed when I use the imported type somehere in the model (interface). here is what I mean

Demo.java
package com.example.simple;
import com.example.enums.Foo; //OK (I want to trigger compilation error here that Foo is not valid)
import com.example.enums.Days;
public interface Demo {
void someMethod(Days days); // OK
void someMethod(Foo foo); // NOT OK - Foo is not a valid Type
}

Any help would be much appreciated!
Re: Trying to use types that are only imported via import statement [message #1405213 is a reply to message #1405048] Wed, 06 August 2014 21:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13270
Registered: July 2009
Senior Member
Hi,

what about using imported namespaces and simply validate them.

for index access e.g. have a look at https://www.eclipse.org/forums/index.php/m/766678/#msg_766678


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Trying to use types that are only imported via import statement [message #1406904 is a reply to message #1405213] Mon, 11 August 2014 12:08 Go to previous messageGo to next message
Pavlina Temelakieva is currently offline Pavlina TemelakievaFriend
Messages: 10
Registered: July 2014
Junior Member
Thank you very much Christian. It helped a lot!

Aside from this question I want to ask for directions regarding imports, again:). In my specific language I want to import types from 3rd party jars that are in the project's classpath (those jars conain class files of java projects that are not part of my model). I can easily import types that are defined in model files via type reference e.g. ref=[Entity], but I don't know what kind of type reference to provide for types in 3rd party jars, since as I understand, they are not subtypes of EObject?

[Updated on: Mon, 11 August 2014 12:15]

Report message to a moderator

Re: Trying to use types that are only imported via import statement [message #1406907 is a reply to message #1406904] Mon, 11 August 2014 12:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13270
Registered: July 2009
Senior Member
what is this third party stuff then? java classes? simple files ....?

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Trying to use types that are only imported via import statement [message #1406910 is a reply to message #1406907] Mon, 11 August 2014 12:19 Go to previous messageGo to next message
Pavlina Temelakieva is currently offline Pavlina TemelakievaFriend
Messages: 10
Registered: July 2014
Junior Member
Yep, those are java classes but not part of Java SE so I cannot use the built-in JvmTypeReference.

I debugged StateBasedContainerManager . getVisibleContainers (...) method and it didn't return the container related to the jar from the classpath containing the types I want to use as imports, because the container.isEmpty() method returned true. Does that say something to you?

[Updated on: Mon, 11 August 2014 13:10]

Report message to a moderator

Re: Trying to use types that are only imported via import statement [message #1406956 is a reply to message #1406910] Mon, 11 August 2014 14:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13270
Registered: July 2009
Senior Member
HI,

you can reference ALL classes on the classpath via JvmTypeReference


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Trying to use types that are only imported via import statement [message #1406977 is a reply to message #1406956] Mon, 11 August 2014 15:21 Go to previous message
Pavlina Temelakieva is currently offline Pavlina TemelakievaFriend
Messages: 10
Registered: July 2014
Junior Member
Ohhhh... thank you very much! I lost so much time with this.
Previous Topic:spurious type conflicts when importing two related packages in an Xtext grammar
Next Topic:Manually add errors to a resource
Goto Forum:
  


Current Time: Tue Dec 10 22:04:31 GMT 2019

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

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

Back to the top