JDT - AST - Exemplo básico de uso

    public static void main(String[] args) {
try {
App app = new App();
ASTParser parser = ASTParser.newParser(AST.JLS3);
try {
parser.setSource(app.readFileToString("C:/programa-java/MetodoLiu/src/main/java/weiEtal/App.java").toCharArray());
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
for (Object declaration : cu.types()) {
if (declaration instanceof TypeDeclaration) {
MethodDeclaration[] metodos = ((TypeDeclaration) declaration).getMethods();
for (MethodDeclaration method : metodos) {
String nomeMetodo = method.getName().getFullyQualifiedName().toString();
if(nomeMetodo.equals("metodo1")) {
MethodDeclaration metodo = method;
List<Statement> statements = metodo.getBody().statements();
for (Statement stmnt : statements) {
if(stmnt instanceof IfStatement){
IfStatement ifStmt = (IfStatement) stmnt;
System.out.println(ifStmt.getElseStatement());
System.out.println(ifStmt.getElseStatement());
}
}
}
System.out.println(" - " + nomeMetodo);
}
}
}
//System.out.println(cu.types().get(0));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


} catch (Exception e) {
e.printStackTrace();
}
}

 

Comentários

Postagens mais visitadas deste blog

Instalação NetBeans

Calcular frete pelos correios via PHP