I need to integrate Eclipse JDT into some existing API that is based on java.lang.reflect. My questions are: Is there an existing interface or adapter? What is the best way to do this? Can anyone point me to a tutorial to do this?
我需要将Eclipse JDT集成到一些基于java.lang.reflect的现有API中。我的问题是:是否有现有的接口或适配器?做这个的最好方式是什么?任何人都可以指点我这样做的教程吗?
For instance I need to retrieve the java.lang.reflect.Method
from a org.eclipse.jdt.core.dom.IMethodBinding
.
例如,我需要从org.eclipse.jdt.core.dom.IMethodBinding中检索java.lang.reflect.Method。
Similarly I need to get the java.lang.Class
from a org.eclipse.jdt.core.dom.Type
or org.eclipse.jdt.core.dom.ITypeBinding
. I found that this can be achieved by:
类似地,我需要从org.eclipse.jdt.core.dom.Type或org.eclipse.jdt.core.dom.ITypeBinding获取java.lang.Class。我发现这可以通过以下方式实现:
Class<?> clazz = Class.forName(typeBinding.getBinaryName());
Of course this is a very simple solution that assumes that the class already exists on the classpath and is not changed via the JDT API -- so it is far from perfect. But it should be noted that these two assumptions do hold for my specific situation.
当然,这是一个非常简单的解决方案,假设该类已经存在于类路径中,并且不会通过JDT API进行更改 - 因此它远非完美。但应该指出的是,这两个假设确实适用于我的具体情况。
1 个解决方案
#1
0
Given that the class already exists on the classpath and is not changes substantially via the JDT API, I implemented something myself.
鉴于类已经存在于类路径中并且实际上没有通过JDT API进行更改,我自己实现了一些东西。
For instance an IMethodBinding
can be transformed to a Method
with the following code:
例如,可以使用以下代码将IMethodBinding转换为Method:
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!");
}
Note that the method retrieveTypeClass
also solves the second problem. Hope this helps anyone.
请注意,方法retrieveTypeClass也解决了第二个问题。希望这有助于任何人。
#1
0
Given that the class already exists on the classpath and is not changes substantially via the JDT API, I implemented something myself.
鉴于类已经存在于类路径中并且实际上没有通过JDT API进行更改,我自己实现了一些东西。
For instance an IMethodBinding
can be transformed to a Method
with the following code:
例如,可以使用以下代码将IMethodBinding转换为Method:
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!");
}
Note that the method retrieveTypeClass
also solves the second problem. Hope this helps anyone.
请注意,方法retrieveTypeClass也解决了第二个问题。希望这有助于任何人。