列出其他项目类方法

时间:2023-01-14 11:59:50

I have problem accessing class methods. My task is to make eclipse plug-in to get selected file's (from another project), name of class and list out class methods. I figured out how to get class name, but i don't know how to access that class as an class (Class <?>). Here's what I've got

我在访问类方法时遇到问题。我的任务是使eclipse插件获取所选文件(来自另一个项目),类名和列出类方法。我想出了如何获取类名,但我不知道如何作为类(Class <?>)访问该类。这就是我所拥有的

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    String className = "";
    Object firstElement;

    ISelectionService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
    IStructuredSelection structured = (IStructuredSelection) service.getSelection();

    /* Getting first element of selected structure (path and workspace)*/
    firstElement = structured.getFirstElement();
    IResource resource = (IResource) Platform.getAdapterManager().getAdapter(firstElement, IResource.class);

    /* Extracting class name from selected object */
    className = resource.getName();
}

Now that I've got class name extracted I'm trying to get that class methods. This is a method that I'm using for getting class methods

现在我已经提取了类名,我正在尝试获取该类方法。这是我用来获取类方法的方法

public static Collection<Method> getMethods(Class<?> clazz) {
    Collection<Method> found = new ArrayList<Method>();
    while (clazz != null) {
        for (Method m1 : clazz.getDeclaredMethods()) {
            boolean overridden = false;
            for (Method m2 : found) {
                if (m2.getName().equals(m1.getName())
                        && Arrays.deepEquals(m1.getParameterTypes(), m2.getParameterTypes())) {
                    overridden = true;
                    break;
                }
            }
            if (!overridden)
                found.add(m1);
        }
        clazz = clazz.getSuperclass();
    }
    return found;
}

Main problem: Don't know how to access this class as an class. It's accessing it as an compilation unit. Because of this when Collection getMethods(Class clazz) is called i don't get any method list. This is how I tryed calling it

主要问题:不知道如何作为一个类访问这个类。它作为编译单元访问它。因此,当调用Collection getMethods(Class clazz)时,我没有得到任何方法列表。这就是我尝试调用它的方式

    Class<?> clazz1 = firstElement.getClass();
    Collection<Method> found1 = new ArrayList<Method>();
    found1 = getMethods(clazz1);

Return value is []. Any ideas what to do?

返回值为[]。有什么想法怎么办?

Sorry for bad language and long explanation, I'm rookie with java, just started.

对不起语言不好和解释很长,我是java的新手,刚开始。

EDIT: Forgot to add, this is what I get in "firstElement" variable after calling structured.getFirstElement()

编辑:忘了添加,这是我在调用structured.getFirstElement()后得到的“firstElement”变量

[Working copy] Test.java [in org.eclipse.testing [in src [in TestingProject]]]
package org.eclipse.testing
class Test
int myInt
Test()
int someInt()
static String someString()

So as I see it, firstElement has structure, containing class and methods, but I don't know how to parse them, and extract list of methods.

所以我看到它,firstElement有结构,包含类和方法,但我不知道如何解析它们,并提取方法列表。

1 个解决方案

#1


0  

You need to write a Methodvisitor and "visit" all methods in a CompilationUnit, something like this:

您需要编写一个Methodvisitor并“访问”CompilationUnit中的所有方法,如下所示:

import org.eclipse.jdt.core.dom.*;

    ICompilationUnit unit = ...;
    MethodVisitor visitor = createMethodVisitor(unit);

    public class MethodVisitor extends ASTVisitor {

        List<MethodDeclaration> methods = new ArrayList<MethodDeclaration>();

        @Override
        public boolean visit(MethodDeclaration node) {

            node.getName();
            return super.visit(node);
        }

        public List<MethodDeclaration> getMethods() {
            return methods;
        }
    } `

#1


0  

You need to write a Methodvisitor and "visit" all methods in a CompilationUnit, something like this:

您需要编写一个Methodvisitor并“访问”CompilationUnit中的所有方法,如下所示:

import org.eclipse.jdt.core.dom.*;

    ICompilationUnit unit = ...;
    MethodVisitor visitor = createMethodVisitor(unit);

    public class MethodVisitor extends ASTVisitor {

        List<MethodDeclaration> methods = new ArrayList<MethodDeclaration>();

        @Override
        public boolean visit(MethodDeclaration node) {

            node.getName();
            return super.visit(node);
        }

        public List<MethodDeclaration> getMethods() {
            return methods;
        }
    } `