Теперь, когда вы немного уточнили свой вопрос, я считаю, что у меня есть обновленное решение. Это изменяет учебник 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
Uh, 'getDeclaredMethods' не будет возвращать унаследовали методы IIRC - вам нужно перебирать все родительские классы и любые реализованные интерфейсы. – thkala