I have a 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 an expression to an expression? I want to check that method call. I tried to check that they are incompatible types:
if(stmt instanceof MethodCallExp) but got an error. I have entered the code that I am using now. Is this the only way to check? System.out.println("Scope: " + exp.getScope()); the name of the object / package / package + class will be displayed. But how to get its type? For example, for the code System.out.println("Hello world"); Must be issued
Type: java.io.PrintStream
source share