Having a problem with the ast.newSimpleName() method.
I am not able to create a SimpleName of the sort 'SomeJava.class'. But the method works fine for names like 'SWT.None' or 'SomeJava.None'.
Here is the code :
MethodInvocation loggerInstance = ast.newMethodInvocation();
loggerInstance.setExpression(ast.newSimpleName("Logger"));
loggerInstance.setName(ast.newSimpleName("getLogger"));
String[] name1 = {className.replace(".java", ""),"None"};
String[] name2 = {className.replace(".java", ""), "class"};
loggerInstance.arguments().add(ast.newName(name1)); // This works
loggerInstance.arguments().add(ast.newName(name2)); // This doesn't
Should i use any thing else other than SimpleName for this. Thanks in advance.
Edit : This is the statement i want to construct:
Logger.getLogger(ClientTest.class);
During my analysis, i found out that the problem arises when using the "class" literal. Not sure how to overcome this.
ast.newName("class");
ast.newSimpleName("class");
Use ASTView plugin (http://www.eclipse.org/jdt/ui/astview/index.php) to see what is the type of node for 'ClientTest.class' and then construct that type of node.
Got it from ASTView. Finally downloaded and installed it. :)
It should be a generated as a TypeLiteral, not as a SimpleName/Name.
TypeLiteral typeLiteral = ast.newTypeLiteral();
typeLiteral.setType(ast.newSimpleType(ast.newSimpleName(className)));
Related
Hi (most probably Peter), I am having troubles to figure out how to make parametrization of my RUTA project.
First of all I have several scripts that make kind of chain:
Project Adjectives.ruta
Project Anatomy.ruta (contains "SCRIPT Adjectives;" and "Document{->CALL(Adjectives)};")
Project Anamnesis.ruta (contains "SCRIPT Anatomy;" and "Document{->CALL(Anatomy)};")
For the result I am calling this:
File specFile = new File("C:/.../.../pipelines/AnamnesisEngine.xml");
String path = new File(specFile.toURI()).getParentFile().getAbsolutePath();
AnalysisEngineDescription desc = null;
String[] VarNames = {"Name1", "Name2"};
String[] VarValues = {"Value1", "Value2"};
try {
desc = AnalysisEngineFactory.createEngineDescriptionFromPath(
specFile.getAbsolutePath(), RutaEngine.PARAM_SCRIPT_PATHS, path+"/script",
RutaEngine.PARAM_DESCRIPTOR_PATHS, path+"/descriptor",
RutaEngine.PARAM_RESOURCE_PATHS,path+"/resources",
RutaEngine.PARAM_VAR_NAMES, VarNames,
RutaEngine.PARAM_VAR_VALUES, VarValues); ..... End so on (Those parameters (VarNames and VarValues) are filled from query, but that is not so important right now)
Everything works fine and I am getting nice JSON output. But now I am having troubles with those parameters (VarNames, VarValues) and I can't figure this out.
When I make something like this in script Anamnesis.ruta
STRING Name1;
Anamnesis{->SETFEATURE("Lemma",Name1)};
Everything works perfectly and I can see in my output that lemma for Anamnesis annotation is set to Value1...
However I also need to work with those variables in projects Adjectives.ruta and Anatomy.ruta. I suspect that those projects are controlled by their own descriptors (AdjectivesEngine.xml and AnatomyEngine.xml). Is there way to set the parameters for those projects and use them while creating ae from AnamnesisEngine.xml?
When I try to add this to Anatomy.ruta (And again call AnamnesisEngine.xml)
STRING Name1;
Anatomy{->SETFEATURE("Lemma",Name1)};
There is no Lemma at all in the output. Which kind of makes sense but I was hoping that maybe that whole chain can be controlled by AnamnesisEngine.xml and those first two projects would be able to "find", assign and work with those variables... Well I was wrong...
Please what would be the best way to achieve this?
If somebody is ever interested, I managed to achieve this with "Aggregate Engine Type" - which let's you import other descriptors into it and propagates variables into them... Epic!
lets say i have written "doSomthing()" in a text file. Does anybody know if it is possible to have that text doSomthing() without having to wirite:
if(txt.equals("doSomthing()"){
doSomthing();
}
the answer was indeed in the reflection chapter and it's straight forward. thanx for that StephaneM :
Method method = MyClass.class.getMethod("doSometing", String.Object);
Object returnValue = method.invoke(null, "parameter-value1");
I want to ask, is it possible to get full line method using AST Parser in java file?
example:
public double getAverage(int[] data) {
}
i only get method name (getAverage) using MethodDeclaration, while i hope full line (public double getAverage(int[] data) {
The second question, how to read closing of the method ( } ) ?
Thanks :)
There is no direct way to do that but you can get all the required information and build the string yourself.
You can use MethodDeclaration.getModifiers() to get the modifier information which will tell you whether it is public or private.
You can use MethodDeclaration.getReturnType2().resolveBinding().getName() to get the name of the return type
and MethodDeclaration.parameters() will give you information about parameters.
one more trick to do is :
String signature= MethodDeclaration.toString().split("{")[0];
but this may not be an efficient way to do.
Thank you
I'm trying to figure a way to do create a class with only the class's name in PHP.
E.g.:
$class = "MyClass";
if(class_exists($class))
$unit = new $class($param1, $param2);
else
$unit = new Unit($param1, $param2);
Is there a way to do this in PHP? If possible, I'd also like to know if this is possible in Java.
Cheers! thanks in advance.
I don't know about PHP (haven't used it in years), but in Java you can do:
MyClass obj = (MyClass) Class.forName("MyClass").newInstance( );
Yep, it should work fine in PHP. I would write that like this in order to avoid duplicating all the parameters to the constructor (if they are the same, of course):
$class = 'MyClass';
if (! class_exists($class)) {
$class = 'Unit';
}
$unit = new $class($param1, $param2);
you can use double $ signs in PHP to make a variable well.. variable.
i.e.
$$class($param1,$param2);
I have not come across such a capability in Java.
Note: you probably don't want to call your class "class" as it is a reserved word ;)
I'm looking for a Java class that for a given string "the ${animal} jumped over the ${target}." is able to pull out the variable names, ie 'animal' and 'target'.
I had hoped StrSubstitutor in Commons Lang had a method
getVariables(String str) : List<String>
... but no such luck.
Yes I could write this myself, but I'm certain there must be a 3rd party library that exists out there that does this.
You could just do a simple regex match if you really just want to pull out those values and avoid the dependency of an entire library:
public List<String> getVariableNames(String source) {
List<String> vs = new ArrayList<String>();
Pattern p = Pattern.compile("\\$\\{(\\w+)\\}");
Matcher m = p.matcher(source);
while (m.find()) {
vs.add(m.group(1));
}
return vs;
}
Storing the pattern as a member variable will improve performance.
Actually I was also looking for a similar library while developing DSL Adapter for my GenericFixture.
After looking at some libraries and template based framework I eventually decided to write code by myself. Feel free to check the code I used for this purpose in DSLAdapter class from GenericFixture available on sourceforge.