Unni Kris
Unni Kris

Reputation: 3095

Not able to create SimpleName for '.class' string pattern

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");

Upvotes: 1

Views: 130

Answers (2)

Unni Kris
Unni Kris

Reputation: 3095

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)));

Upvotes: 0

Deepak Azad
Deepak Azad

Reputation: 7923

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.

Upvotes: 1

Related Questions