鉴于该类已经存在于类路径中并且没有通过 JDT API 进行实质性更改,因此我自己实现了一些东西。
例如,IMethodBinding
可以Method
使用以下代码将 a 转换为 a:
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
Class<?> clazz = retrieveTypeClass(methodBinding.getDeclaringClass());
Class<?>[] paramClasses = new Class<?>[methodInvocation.arguments().size()];
for (int idx = 0; idx < methodInvocation.arguments().size(); idx++) {
ITypeBinding paramTypeBinding = methodBinding.getParameterTypes()[idx];
paramClasses[idx] = retrieveTypeClass(paramTypeBinding);
}
String methodName = methodInvocation.getName().getIdentifier();
Method method;
try {
method = clazz.getMethod(methodName, paramClasses);
} catch (Exception exc) {
throw new RuntimeException(exc);
}
private Class<?> retrieveTypeClass(Object argument) {
if (argument instanceof SimpleType) {
SimpleType simpleType = (SimpleType) argument;
return retrieveTypeClass(simpleType.resolveBinding());
}
if (argument instanceof ITypeBinding) {
ITypeBinding binding = (ITypeBinding) argument;
String className = binding.getBinaryName();
if ("I".equals(className)) {
return Integer.TYPE;
}
if ("V".equals(className)) {
return Void.TYPE;
}
try {
return Class.forName(className);
} catch (Exception exc) {
throw new RuntimeException(exc);
}
}
if (argument instanceof IVariableBinding) {
IVariableBinding variableBinding = (IVariableBinding) argument;
return retrieveTypeClass(variableBinding.getType());
}
if (argument instanceof SimpleName) {
SimpleName simpleName = (SimpleName) argument;
return retrieveTypeClass(simpleName.resolveBinding());
}
throw new UnsupportedOperationException("Retrieval of type " + argument.getClass() + " not implemented yet!");
}
请注意,该方法retrieveTypeClass
还解决了第二个问题。希望这对任何人都有帮助。