How to list all available class methods in Eclipse?

When extending the Java class, it can be very useful to have quick access to the implementation of all available methods, regardless of whether they are explicitly implemented in the specified class or inherited from one of its parent classes.

The closest tool I could find in Eclipse for this purpose is the hierarchy type view with the Show all inherited members option turned on. Unfortunately, this parameter does show all inherited elements, including those that have already been overridden in the parent class. This makes it difficult to see at a glance which implementation of the method is relevant, and everything becomes even more complicated using the default methods in the interfaces.

Is there an option, view, plugin, or other method that will allow you to quickly access only the implementations of the method that belong to a particular class, including any inherited implementations?

+4
source share
3 answers

By typing Ctrl + O twice in the editor, when the java type is selected, a context context dialog box pops up showing the && & inherited members, including the default methods.

If the method is overridden, I think it appears higher in the list? So, you can see which methods are overridden in this way, seeing all the classes implementing this method, but also which one will be executed in order in the list?

+1
source
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);
        }
    }
}
0

, , , . MethodSpy.

, MethodSpy

  • (.. Java.util.ArrayList.toString()) ( toString()). contains, , - , toString , .
  • , . , , , .. hashCode()

. . , , , Maps, 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
0

All Articles