如何在Eclipse中列出类的所有可用方法?

时间:2014-10-20 20:00:00

标签: java eclipse methods

扩展Java类时,快速访问所有可用方法的实现非常有用,无论它们是在所述类中显式实现还是从其父类之一继承。

我在Eclipse中为此目的找到的最接近的工具是“类型层次结构”视图,其中启用了“显示所有继承的成员”选项。不幸的是,该选项确实显示了所有继承的成员,包括已在父类中重写的成员。这使得很难一目了然哪个方法实现是相关的,并且在接口中使用默认方法会变得更加复杂。

是否有选项,视图,插件或其他技术,只允许快速访问与特定类相关的方法实现,包括任何继承的实现

4 个答案:

答案 0 :(得分:2)

当选择了java类型时,在编辑器中键入两次Ctrl + O会弹出一个大纲上下文对话框,显示成员&&继承的成员包括默认方法。

如果某个方法被覆盖,我认为它在列表中显示得更高?通过查看实现该方法的所有类,以及列表中的顺序将执行哪一个类,您可以看到哪种方法被覆盖了?

答案 1 :(得分:1)

如果您使用的是STS eclipse,请单击显示视图->轮廓视图。

  • 这将列出类 private public 的所有成员。单击特定成员,这将带您到该特定方法定义。

  • 方法名称也可以复制

答案 2 :(得分:0)

import java.lang.reflect.*;

/**
 * Compile with this: C:\Documents and Settings\glow\My Documents\j>javac
 * DumpMethods.java
 * 
 * Run like this, and results follow C:\Documents and Settings\glow\My
 * Documents\j>java DumpMethods public void DumpMethods.foo() public int
 * DumpMethods.bar() public java.lang.String DumpMethods.baz() public static
 * void DumpMethods.main(java.lang.String[])
 */

public class DumpMethods {

    public void foo() {
    }

    public int bar() {
        return 12;
    }

    public String baz() {
        return "";
    }

    public static void main(String args[]) {
        try {
            Class c = DumpMethods.class;
            Method[] m = c.getDeclaredMethods();
            for (int i = 0; i < m.length; i++)
                System.out.println(m[i].toString());
        } catch (Throwable e) {
            System.err.println(e);
        }
    }
}

答案 3 :(得分:0)

既然您已经澄清了一些问题,我相信我有一个更新的解决方案。这会修改MethodSpy教程。

基本上,对原始 MethodSpy

进行了一些更改
  1. 方法名称(即Java.util.ArrayList.toString())仅用方法(即toString())替换。我们可以将它与contains一起使用来查看是否有任何方法,即toString只找到一次,最后定义它。
  2. 我们遍历所有超类并检查是否添加了方法。值得注意的是,出现了一些原生方法,即hashCode()
  3. 这是整个班级。修改在注释中注明。可能有一种更好的方法可以解决所有问题,也许使用地图,但目前,它似乎与粗略的一瞥一起使用ArrayList。

    public class MethodSpyWithInheritance {
        private static final String fmt = "     %24s: %s%n";
    
        // for the morbidly curious
        <E extends RuntimeException> void genericThrow() throws E {
        }
    
        public static void main(String... args) {
            try {
                ArrayList<String> output = new ArrayList<String>();
                Class<?> c = Class.forName(args[0]);
                while (c != null) {
                    output.add("Methods in " + c.getCanonicalName()); //Add the class name to array
                    Method[] allMethods = c.getDeclaredMethods(); //get all the methods for this
                    for (Method m : allMethods) {
                        String method = "";
                        method += String.format("     %s%n", m.toGenericString())
                                .replace(c.getCanonicalName() + ".", "");//remove the canonical name from the name of the method
    
                        method += String.format(fmt, "ReturnType",
                                m.getReturnType());
                        method += String.format(fmt, "GenericReturnType",
                                m.getGenericReturnType());
    
                        Class<?>[] pType = m.getParameterTypes();
                        Type[] gpType = m.getGenericParameterTypes();
                        for (int i = 0; i < pType.length; i++) {
                            method += String.format(fmt, "ParameterType", pType[i]);
                            method += String.format(fmt, "GenericParameterType",
                                    gpType[i]);
                        }
    
                        Class<?>[] xType = m.getExceptionTypes();
                        Type[] gxType = m.getGenericExceptionTypes();
                        for (int i = 0; i < xType.length; i++) {
                            method += String.format(fmt, "ExceptionType", xType[i]);
                            method += String.format(fmt, "GenericExceptionType",
                                    gxType[i]);
                        }
    
                        if (!output.contains(method)) { //check to see if a method already exists, i.e. overridden
                            output.add(method); //not overridden, we can add
                        }
                    }
                    c = c.getSuperclass(); //get superclass and continue
                }
                for (String s : output) {
                    System.out.println(s); //get output
                }
    
                // production code should handle these exceptions more gracefully
            } catch (ClassNotFoundException x) {
                x.printStackTrace();
            }
        }
    }
    

    Java.util.ArrayList

    的示例
    Methods in java.util.ArrayList
         public boolean add(E)
                       ReturnType: boolean
                GenericReturnType: boolean
                    ParameterType: class java.lang.Object
             GenericParameterType: E
    
         public void add(int,E)
                       ReturnType: void
                GenericReturnType: void
                    ParameterType: int
             GenericParameterType: int
                    ParameterType: class java.lang.Object
             GenericParameterType: E
    
         public E get(int)
                       ReturnType: class java.lang.Object
                GenericReturnType: E
                    ParameterType: int
             GenericParameterType: int
    
         public java.lang.Object clone()
                       ReturnType: class java.lang.Object
                GenericReturnType: class java.lang.Object
    
         public int indexOf(java.lang.Object)
                       ReturnType: int
                GenericReturnType: int
                    ParameterType: class java.lang.Object
             GenericParameterType: class java.lang.Object
    
         public void clear()
                       ReturnType: void
                GenericReturnType: void
    
         public boolean contains(java.lang.Object)
                       ReturnType: boolean
                GenericReturnType: boolean
                    ParameterType: class java.lang.Object
             GenericParameterType: class java.lang.Object
    
         public boolean isEmpty()
                       ReturnType: boolean
                GenericReturnType: boolean
    
         public int lastIndexOf(java.lang.Object)
                       ReturnType: int
                GenericReturnType: int
                    ParameterType: class java.lang.Object
             GenericParameterType: class java.lang.Object
    
         public boolean addAll(int,java.util.Collection<? extends E>)
                       ReturnType: boolean
                GenericReturnType: boolean
                    ParameterType: int
             GenericParameterType: int
                    ParameterType: interface java.util.Collection
             GenericParameterType: java.util.Collection<? extends E>
    
         public boolean addAll(java.util.Collection<? extends E>)
                       ReturnType: boolean
                GenericReturnType: boolean
                    ParameterType: interface java.util.Collection
             GenericParameterType: java.util.Collection<? extends E>
    
         public int size()
                       ReturnType: int
                GenericReturnType: int
    
         public <T> T[] toArray(T[])
                       ReturnType: class [Ljava.lang.Object;
                GenericReturnType: T[]
                    ParameterType: class [Ljava.lang.Object;
             GenericParameterType: T[]
    
         public java.lang.Object[] toArray()
                       ReturnType: class [Ljava.lang.Object;
                GenericReturnType: class [Ljava.lang.Object;
    
         public boolean remove(java.lang.Object)
                       ReturnType: boolean
                GenericReturnType: boolean
                    ParameterType: class java.lang.Object
             GenericParameterType: class java.lang.Object
    
         public E remove(int)
                       ReturnType: class java.lang.Object
                GenericReturnType: E
                    ParameterType: int
             GenericParameterType: int
    
         private void writeObject(java.io.ObjectOutputStream) throws java.io.IOException
                       ReturnType: void
                GenericReturnType: void
                    ParameterType: class java.io.ObjectOutputStream
             GenericParameterType: class java.io.ObjectOutputStream
                    ExceptionType: class java.io.IOException
             GenericExceptionType: class java.io.IOException
    
         private void readObject(java.io.ObjectInputStream) throws java.io.IOException,java.lang.ClassNotFoundException
                       ReturnType: void
                GenericReturnType: void
                    ParameterType: class java.io.ObjectInputStream
             GenericParameterType: class java.io.ObjectInputStream
                    ExceptionType: class java.io.IOException
             GenericExceptionType: class java.io.IOException
                    ExceptionType: class java.lang.ClassNotFoundException
             GenericExceptionType: class java.lang.ClassNotFoundException
    
         public E set(int,E)
                       ReturnType: class java.lang.Object
                GenericReturnType: E
                    ParameterType: int
             GenericParameterType: int
                    ParameterType: class java.lang.Object
             GenericParameterType: E
    
         public void ensureCapacity(int)
                       ReturnType: void
                GenericReturnType: void
                    ParameterType: int
             GenericParameterType: int
    
         public void trimToSize()
                       ReturnType: void
                GenericReturnType: void
    
         protected void removeRange(int,int)
                       ReturnType: void
                GenericReturnType: void
                    ParameterType: int
             GenericParameterType: int
                    ParameterType: int
             GenericParameterType: int
    
         private void RangeCheck(int)
                       ReturnType: void
                GenericReturnType: void
                    ParameterType: int
             GenericParameterType: int
    
         private void fastRemove(int)
                       ReturnType: void
                GenericReturnType: void
                    ParameterType: int
             GenericParameterType: int
    
    Methods in java.util.AbstractList
         public abstract E get(int)
                       ReturnType: class java.lang.Object
                GenericReturnType: E
                    ParameterType: int
             GenericParameterType: int
    
         public boolean equals(java.lang.Object)
                       ReturnType: boolean
                GenericReturnType: boolean
                    ParameterType: class java.lang.Object
             GenericParameterType: class java.lang.Object
    
         public int hashCode()
                       ReturnType: int
                GenericReturnType: int
    
         public java.util.Iterator<E> iterator()
                       ReturnType: interface java.util.Iterator
                GenericReturnType: java.util.Iterator<E>
    
         public java.util.ListIterator<E> listIterator(int)
                       ReturnType: interface java.util.ListIterator
                GenericReturnType: java.util.ListIterator<E>
                    ParameterType: int
             GenericParameterType: int
    
         public java.util.ListIterator<E> listIterator()
                       ReturnType: interface java.util.ListIterator
                GenericReturnType: java.util.ListIterator<E>
    
         public java.util.List<E> subList(int,int)
                       ReturnType: interface java.util.List
                GenericReturnType: java.util.List<E>
                    ParameterType: int
             GenericParameterType: int
                    ParameterType: int
             GenericParameterType: int
    
    Methods in java.util.AbstractCollection
         public java.lang.String toString()
                       ReturnType: class java.lang.String
                GenericReturnType: class java.lang.String
    
         public abstract java.util.Iterator<E> iterator()
                       ReturnType: interface java.util.Iterator
                GenericReturnType: java.util.Iterator<E>
    
         public abstract int size()
                       ReturnType: int
                GenericReturnType: int
    
         public boolean containsAll(java.util.Collection<?>)
                       ReturnType: boolean
                GenericReturnType: boolean
                    ParameterType: interface java.util.Collection
             GenericParameterType: java.util.Collection<?>
    
         public boolean removeAll(java.util.Collection<?>)
                       ReturnType: boolean
                GenericReturnType: boolean
                    ParameterType: interface java.util.Collection
             GenericParameterType: java.util.Collection<?>
    
         public boolean retainAll(java.util.Collection<?>)
                       ReturnType: boolean
                GenericReturnType: boolean
                    ParameterType: interface java.util.Collection
             GenericParameterType: java.util.Collection<?>
    
         private static <T> T[] finishToArray(T[],java.util.Iterator<?>)
                       ReturnType: class [Ljava.lang.Object;
                GenericReturnType: T[]
                    ParameterType: class [Ljava.lang.Object;
             GenericParameterType: T[]
                    ParameterType: interface java.util.Iterator
             GenericParameterType: java.util.Iterator<?>
    
    Methods in java.lang.Object
         protected void finalize() throws java.lang.Throwable
                       ReturnType: void
                GenericReturnType: void
                    ExceptionType: class java.lang.Throwable
             GenericExceptionType: class java.lang.Throwable
    
         public final native void wait(long) throws java.lang.InterruptedException
                       ReturnType: void
                GenericReturnType: void
                    ParameterType: long
             GenericParameterType: long
                    ExceptionType: class java.lang.InterruptedException
             GenericExceptionType: class java.lang.InterruptedException
    
         public final void wait() throws java.lang.InterruptedException
                       ReturnType: void
                GenericReturnType: void
                    ExceptionType: class java.lang.InterruptedException
             GenericExceptionType: class java.lang.InterruptedException
    
         public final void wait(long,int) throws java.lang.InterruptedException
                       ReturnType: void
                GenericReturnType: void
                    ParameterType: long
             GenericParameterType: long
                    ParameterType: int
             GenericParameterType: int
                    ExceptionType: class java.lang.InterruptedException
             GenericExceptionType: class java.lang.InterruptedException
    
         public native int hashCode()
                       ReturnType: int
                GenericReturnType: int
    
         public final native java.lang.Class<?> getClass()
                       ReturnType: class java.lang.Class
                GenericReturnType: java.lang.Class<?>
    
         protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException
                       ReturnType: class java.lang.Object
                GenericReturnType: class java.lang.Object
                    ExceptionType: class java.lang.CloneNotSupportedException
             GenericExceptionType: class java.lang.CloneNotSupportedException
    
         private static native void registerNatives()
                       ReturnType: void
                GenericReturnType: void
    
         public final native void notify()
                       ReturnType: void
                GenericReturnType: void
    
         public final native void notifyAll()
                       ReturnType: void
                GenericReturnType: void