Question

I want to print all the method invocations within all methods of a Class. I am using ASTParser. Following is my code

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import java .io.*;

public class ASTParserDemo {
public static void main(String[] args) {


    ASTParserDemo demo = new ASTParserDemo();
    String rawContent = demo.readFile();

    //String rawContent = "public class HelloWorld { public String s = \"hello\"; public static void main(String[] args) { HelloWorld hw = new HelloWorld(); String s1 = hw.s; } }";
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(rawContent.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    AST ast = cu.getAST();
    IdentifierVisitor iv = new IdentifierVisitor();
    cu.accept(iv);

}

public String readFile() {
    StringBuffer fileContent = new StringBuffer();
    BufferedReader br = null;
    try {
        String sCurrentLine;
        br = new BufferedReader(new FileReader("C:\\research\\android-projects\\AsyncSearch.java"));

        while ((sCurrentLine = br.readLine()) != null) {
            //System.out.println(sCurrentLine);
            fileContent.append(sCurrentLine);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return fileContent.toString();
}

}

import org.eclipse.jdt.core.dom.*;

import java.util.*;

public class IdentifierVisitor extends ASTVisitor {

private Vector<String> identifiers = new Vector<String>();

public Vector<String> getIdentifiers(){
    return identifiers; 
}


public boolean visit(MethodDeclaration m){
    System.out.println("METHOD DECLARATION : " + m);
    return true;
}

public boolean visit(MethodInvocation m){
    System.out.println("METHOD INVOCATION : " + m);
    return true;
}

}

the output is showing only one method declaration. Please let me know how do I print all method invocations within all declared methods. Thanks

Was it helpful?

Solution

You're not using a good method to retrieve the string representation of your source code. You can use an alternative method for read a file from your path and return a string representation of source:

public static String readFileToString(String filePath) throws IOException {
    StringBuilder fileData = new StringBuilder(1000);
    BufferedReader reader = new BufferedReader(new FileReader(filePath));

    char[] buf = new char[10];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        //          System.out.println(numRead);
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
        buf = new char[1024];
    }
    reader.close();
    return  fileData.toString();    
}

Remember to always check whether it is an actual file before calling readFileToString(filePath) eg:

String filePath = file.getAbsolutePath();
if (file.isFile ())) 
     String source = readFileToString(filePath) 

Alternatively you can print the contents of rawContent returned from your method readFile and check that the code you want to parse is actually the same as what you mean.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top