1

I want to get the byte code for a java method signature given MethodDeclaration object. I'm parsing the java class using Eclipse jdt and iterating over the MethodDeclaration like the following:

private static void processJavaFile(String javaFilePath) {
    List<MethodDeclaration> methodDeclarations = new ArrayList<MethodDeclaration>();
    FileInputStream reader = null;
    try {
        File javaFile = new File(javaFilePath);
        reader = new FileInputStream(javaFile);

        byte[] bs = new byte[reader.available()];
        reader.read(bs, 0, reader.available());
        String javaContent = new String(bs);

        CompilationUnit unit = ASTUtil.getCompilationUnit(javaContent, 4);
        MethodVisitor methodVisitor = new MethodVisitor();
        unit.accept(methodVisitor);
        methodDeclarations = methodVisitor.getMethods();
        for (MethodDeclaration methodDeclaration :methodDeclarations){
            ////////////////////////////////////////////////////////////////////////
            // ???? I want to get the byte code of the method signature here ???? //
            ////////////////////////////////////////////////////////////////////////
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

} 
9
  • If you've got the Eclipse source, you can look thru (manually) and try and find the compiler/ bytecode generator. I assume it's a Visitor or similar, but there may require multiple steps to run before you can generate the bytecode. Commented Sep 22, 2013 at 11:40
  • 1
    What do you mean by bytecode of the signature? Commented Sep 22, 2013 at 13:58
  • @Antimony i mean some thing like that: java.lang.Object.equals(Ljava/lang/Object;)Z Commented Sep 22, 2013 at 15:38
  • 1
    I can only repeat Antimony: what do your mean with “bytecode of the signature”? Commented Sep 23, 2013 at 10:12
  • 2
    So why are you asking for “byte code” when you just want the signature? Ask for the signature if you want the signature. Or ask for “JVM internal signature” if you think “signature” is too ambiguous. Commented Sep 23, 2013 at 10:42

1 Answer 1

1

The MethodDeclaration instance is part of the AST representing the syntax of the source code. It requires resolving the type names found in the source code before you can create a signature for a method.

for (MethodDeclaration methodDeclaration :methodDeclarations){
  // the next line requires that the project is setup correctly
  IMethodBinding resolved = methodDeclaration.resolveBinding();
  // then you can create a method signature
  ITypeBinding[] pType = resolved.getParameterTypes();
  String[] pTypeName=new String[pType.length];
  for(int ix = 0; ix < pType.length; ix++)
    pTypeName[ix]=pType[ix].getBinaryName().replace('.', '/');
  String rTypeName=resolved.getReturnType().getBinaryName().replace('.', '/');
  //org.eclipse.jdt.core.Signature
  String signature = Signature.createMethodSignature(pTypeName, rTypeName);
  System.out.println(signature);
}
Sign up to request clarification or add additional context in comments.

5 Comments

methodDeclaration.resolveBinding() returns null how ever i have already set resolveBindings & bindingsRecovery of the parser to true, any idea?
As said, the project needs to be set up. E.g. if you used setEnvironment(String[], String[], String[], boolean) you need also to use setUnitName(String) to provide a file name as you are not using an API method that specifies a File. That’s the minimum setup as far as I know.
But i'm not inside an Eclipse Plugin project, I'm inside Maven project and using using jdt as Maven dependency, any idea how to configure the jdt to resolve bindings?
Use the methods on the ASTParser instance. I gave you an example. With “project” I mean just the minimum information required for resolving, e.g. class path and source path. You can use empty arrays and use a value of true for includeRunningVMBootclasspath if your source code has no further dependencies.
it works now with some small modifications, I searched how to use setEnvironment(String[], String[], String[], boolean) and found this example of how to use AST in stand alone application dev.eclipse.org/svnroot/tools/org.eclipse.objectteams/trunk/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.