【问题标题】:How to list all available methods of a class in Eclipse?如何在 Eclipse 中列出类的所有可用方法?
【发布时间】:2014-12-15 22:05:50
【问题描述】:

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

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

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

【问题讨论】:

    标签: java eclipse methods


    【解决方案1】:

    如果您使用的是 STS eclipse,请单击 Show View -> Outline 视图。

    • 这将列出类 privatepublic 的所有成员。单击特定成员,这将带您进入该特定方法定义。

    • 方法名也可以复制

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 这似乎仍然显示了父类中的所有方法,包括那些已被覆盖的方法...
      • 如果我正在寻找一种特定的方法(尽管有更快的方法),这可能会有所帮助,但当我正在寻找需要被覆盖的方法时则不然。我必须记住我是否已经在列表中看到了更高的特定方法签名,这通常并不容易......
      • @thkala 如果您只想查看应该/可以覆盖的方法 source->override/implement methods 有什么问题?
      • 嗯,您的意思是为所有内容生成虚拟覆盖,检查覆盖的方法(“Open Super implementation”),然后删除我不需要的所有内容?这是我为新的源代码所做的,但它仍然很尴尬。如果您正在重新设计或调试现有代码,这也不是很实用......
      • 我明白你在做什么。如果您禁用“按类型分组方法”选项,您确实会获得所有可覆盖方法的列表 - 但没有提及源类型,也没有简单的方法跳转到实现......
      【解决方案3】:
      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);
              }
          }
      }
      

      【讨论】:

      • 呃,getDeclaredMethods 不会返回继承的方法 IIRC - 你需要遍历所有父类和任何实现的接口。
      【解决方案4】:

      现在您已经稍微澄清了您的问题,我相信我有一个更新的解决方案。这修改了MethodSpy 教程。

      基本上对原来的MethodSpy

      有一些改动
      1. 方法名称(即 Java.util.ArrayList.toString())仅替换为方法(即 toString())。我们可以将它与 contains 一起使用来查看是否有任何方法,即 toString 只找到一次,它是最后定义的。
      2. 我们遍历所有超类并检查是否添加了方法。值得注意的是,出现了一些本机方法,即hashCode()

      这是整个班级。修改在 cmets 中注明。可能有更好的方法来获取所有内容,也许使用 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
      

      【讨论】:

      • 嗯,我想过自己写这样的东西,但是在处理 Java 8 默认接口方法时我失去了兴趣,这不是微不足道的......
      • @thkala 我不知道这是否意味着这就是你要找的东西。 Java 8 会打破这一点吗?
      • 据我所知,这段代码完全没有默认接口方法。 Java 8 允许在接口中实现方法,然后遵循一个奇怪的 most-specific-when-not-implemented 规则来确定应用哪个实现......
      • @thkala 我只是对默认方法感到震惊。可怕。可能有办法得到它,只需要阅读更多的反射。
      • 这将涉及对所有已实现接口的某种递归,但 BFS 或 DFS 似乎都不适合。令人沮丧的是 Eclipse 已经有了执行此操作的代码 - 我只是没有发现它在哪里(如果?)暴露给 UI...
      猜你喜欢
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      相关资源
      最近更新 更多