I have the code
private static class MyVisitor extends VoidVisitorAdapter<Object> {
@Override
public void visit(MethodCallExpr exp, Object arg) {
System.out.println("Scope: " + exp.getScope());
System.out.println("Method: " + exp.getName());
if(exp.getArgs() != null)
for(Expression e : exp.getArgs()) {
System.out.println("\tArgument: " + e.toString());
}
System.out.println();
}
}
And
CompilationUnit cu = JavaParser.parse(new File("Test.java"));
for(TypeDeclaration type : cu.getTypes()) {
for(BodyDeclaration dec : type.getMembers()) {
if(dec instanceof MethodDeclaration) {
MethodDeclaration mdec = (MethodDeclaration) dec;
BlockStmt block = mdec.getBody();
for(Statement stmt : block.getStmts()) {
MyVisitor visitor = new MyVisitor();
s.accept(visitor, null);
}
}
}
}
I have two problems:
- How to convert Statement to Expression? I want to check is that Method Call. I tried to check that they are incompatible types by this way :
if(stmt instanceof MethodCallExp)
but got error. I entered the code I use now. Is that the one way to perform check? System.out.println("Scope: " + exp.getScope());
will be displayed the object/package/package+class name. But how to get its type? For example for the codeSystem.out.println("Hello world");
Should be outputted
Type: java.io.PrintStream