Eclipse JDT 适配器到 java.lang.reflect

发布于 2024-11-09 10:17:52 字数 823 浏览 0 评论 0 原文

我需要集成 Eclipse JDT 到一些基于 java.lang.reflect。我的问题是:是否有现有的接口或适配器?最好的方法是什么?谁能指点我一个教程来做到这一点?

例如,我需要从 org.eclipse.jdt.core.dom.IMethodBinding 检索 java.lang.reflect.Method

同样,我需要从 org.eclipse.jdt.core.dom.Typeorg.eclipse.jdt.core 获取 java.lang.Class。 dom.ITypeBinding。我发现这可以通过以下方式实现:

Class<?> clazz = Class.forName(typeBinding.getBinaryName());

当然,这是一个非常简单的解决方案,假设该类已经存在于类路径中,并且没有通过 JDT API 进行更改 - 所以它远非完美。但应该指出的是,这两个假设对于我的具体情况确实成立。

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?

For instance I need to retrieve the java.lang.reflect.Method from a org.eclipse.jdt.core.dom.IMethodBinding.

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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花落人断肠 2024-11-16 10:17:52

鉴于该类已经存在于类路径中并且不会通过 JDT API 进行实质性更改,我自己实现了一些内容。

例如,可以使用以下代码将 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!");
}

请注意,方法 retrieveTypeClass 还解决了第二个问题。希望这对任何人都有帮助。

Given that the class already exists on the classpath and is not changes substantially via the JDT API, I implemented something myself.

For instance an IMethodBinding can be transformed to a Method with the following code:

    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文