- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想获取给定 MethodDeclaration 对象的 java 方法签名的字节码。我正在使用 Eclipse jdt 解析 java 类并迭代 MethodDeclaration,如下所示:
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();
}
}
最佳答案
MethodDeclaration
实例是表示源代码语法的AST的一部分。它需要先解析源代码中找到的类型名称,然后才能为方法创建签名。
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);
}
关于java - 如何获取java MethodDeclaration对应的Byte code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18943361/
我正在尝试创建 Java 文件的解析器,但无法获取每个方法的正确行号。这是我现在的代码: ASTParser parser = ASTParser.newParser(AST.JLS4); parse
我正在为读取源代码的 Java 解析器使用 MethodDeclaration,但我遇到了一些问题。我看不到任何给我所属类的方法。是否存在用于此的方法,或者我是否需要查看 java 解析器以尝试创建一
我有一个 JDT AST MethodDeclaration 定义如下: MethodDeclaration md = ast.newMethodDeclaration(); SimpleName s
有什么方法可以比较 MethodDeclaration 对象和 IMethod 对象并检查它们是否相似?或者有什么方法可以将 MethodDeclaration 对象转换为 IMethod 对象(IM
假设我已经从 Java 文件中解析出一个 CompilationUnit 单元。现在,我想向 Java 文件中添加一些新的复杂方法。用简单的方法比如 public static void main(S
我正在实现一个 ASTVisitor,当我访问一个 MethodInvocation 节点时,我想访问它对应的 MethodDeclaration 节点。以下示例显示了我需要的内容: public b
我是一名优秀的程序员,十分优秀!