Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [JET] How to load a java file with a fully qualified name?
[JET] How to load a java file with a fully qualified name? [message #664185] Thu, 07 April 2011 18:50 Go to next message
yongjiezheng is currently offline yongjiezhengFriend
Messages: 10
Registered: March 2011
Location: California
Junior Member
Hi,

I know we can use the c:load tag to load a (java) model, but the problem is that we have to specify its physical address (relative to workspace or a project). What if I have a fully qualified name, and I know this java file is somewhere in an Eclipse project? Its physical address in the project may be different from project to project, but it is on the classpath of the project.

Does anyone know how to do this?

Thanks,
William
Re: [JET] How to load a java file with a fully qualified name? [message #664215 is a reply to message #664185] Thu, 07 April 2011 20:56 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
William:

JET has not tags to do this out of the box, but it would not be too difficult to create a custom JET tag that did what you want.

It might look something like:

<myjava:loadClass project="project name" fqn="..." var="varToSet"/>


The tag implementation would have to do a number of things:

1) resolve the project name into first an IProject and then a IJavaProject:

IProject proj = ResourcesPlugin.getWorkspace.getProject(prjName);
IJavaProject jProj = JavaCore.create(proj);


2) find the Java type (IType) corresponding to the fully qualified class name:

IType type = jProj.findType(fqClassName);


3) Use the ASTParser to get a DOM:

ASTParser parser = new ASTParser(AST.JLS3);
if(type.isBinary) {
  parser.setSource( type.getClassFile() );
} else {
  parser.setSource( type.getCompilationUnit() );
}
CompilationUnit cu = (CompilationUnit) parser.createAST(null);


4) Set the JET variable:

context.setVariable( variableName, cu );


You could get fancier, but see if you can make that work first.

Paul
Re: [JET] How to load a java file with a fully qualified name? [message #664233 is a reply to message #664185] Thu, 07 April 2011 23:17 Go to previous messageGo to next message
yongjiezheng is currently offline yongjiezhengFriend
Messages: 10
Registered: March 2011
Location: California
Junior Member
Hi Paul,

Following what you suggested, I created a custom tag whose implementation is like this:

public class LoadClassTag extends AbstractFunctionTag {

public LoadClassTag() {
// TODO Auto-generated constructor stub
}

public String doFunction(TagInfo td, JET2Context context, String bodyContent)
throws JET2TagException {
// TODO Auto-generated method stub
IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(td.getAt tribute( "project"));
IJavaProject jProj = JavaCore.create(proj);

try {
IType type = jProj.findType(td.getAttribute("fqn"));
ASTParser parser = ASTParser.newParser(AST.JLS3);
if(type.isBinary()) {
parser.setSource( type.getClassFile() );
} else {
parser.setSource( type.getCompilationUnit() );
}
CompilationUnit cu = (CompilationUnit) parser.createAST(null);

context.setVariable(td.getAttribute("var"), cu);

} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}


And in my template file, I wrote something like the following:

<mytag:loadClass project="{$org.eclipse.jet.resource.project.name}" fqn=" {//interfaceType[@id=$typeID]/implementation/mainClass/javaC lassName} " var="root" />

When I tried to execute the template, i got the error like "Variable root is not defined".

Any idea about what's wrong with this?

Many thanks,
William
Re: [JET] How to load a java file with a fully qualified name? [message #664240 is a reply to message #664185] Fri, 08 April 2011 00:13 Go to previous messageGo to next message
yongjiezheng is currently offline yongjiezhengFriend
Messages: 10
Registered: March 2011
Location: California
Junior Member
JET engine seems not recognize the tag I defined. It treats the tag as output text, and does not process it at all. I developed this tag in my transformation project, instead of another plug-in project. I don't know if this is fine.
Re: [JET] How to load a java file with a fully qualified name? [message #664949 is a reply to message #664240] Tue, 12 April 2011 15:02 Go to previous message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
William:

Several things might be going wrong.

1) The tag isn't imported into the template. Two ways to do this:
i) an <%@taglib %> directive, with the fully qualified id of the tag library (plugin-id + '.' + id-used-in-the-declaring-plug.xml)

ii) import the tag library (with the same id as above) via the hosting JET project's plugin.xml. Need an 'importLibrary' tag with the autoImport attribute set to 'true'.

2) You developed your tag in a separate plug-in project (from the JET project), and you are trying to run the JET project from the workspace. This won't work. JET does some magic to load the JET project out of the workspace - it does not extend this ability to referenced plug-ins. The solution? Launch a runtime workbench that includes both the JET project and the plug-in project defining the tag, and test from there. OR, declare the tag in the sample plug-in as the JET templates

Paul
Previous Topic:List or something
Next Topic:[Acceleo] How to create a generator for composed metamodels
Goto Forum:
  


Current Time: Sat Nov 09 01:45:28 GMT 2024

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

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

Back to the top