1

I want to list all the instance methods of Vector class in my Java program. I was wondering if there is any equivalent method to Ruby's instance_methods in Java.

Please let me know if there is any other way to do so.

wero
  • 32,544
  • 3
  • 59
  • 84
itsh
  • 1,063
  • 3
  • 14
  • 28

2 Answers2

4

To get all public, non-static methods of a class, including methods inherited from base classes, do this:

for (Method method : Vector.class.getMethods())
    if ((method.getModifiers() & Modifier.STATIC) == 0)
        System.out.println(method);

Output (Java 1.8.0_65)

public synchronized boolean java.util.Vector.add(java.lang.Object)
public void java.util.Vector.add(int,java.lang.Object)
public synchronized java.lang.Object java.util.Vector.remove(int)
public boolean java.util.Vector.remove(java.lang.Object)
public synchronized java.lang.Object java.util.Vector.get(int)
public synchronized boolean java.util.Vector.equals(java.lang.Object)
public synchronized java.lang.String java.util.Vector.toString()
public synchronized int java.util.Vector.hashCode()
public synchronized java.lang.Object java.util.Vector.clone()
public synchronized int java.util.Vector.indexOf(java.lang.Object,int)
public int java.util.Vector.indexOf(java.lang.Object)
public void java.util.Vector.clear()
public boolean java.util.Vector.contains(java.lang.Object)
public synchronized boolean java.util.Vector.isEmpty()
public synchronized java.util.Iterator java.util.Vector.iterator()
public synchronized int java.util.Vector.lastIndexOf(java.lang.Object,int)
public synchronized int java.util.Vector.lastIndexOf(java.lang.Object)
public synchronized void java.util.Vector.replaceAll(java.util.function.UnaryOperator)
public synchronized int java.util.Vector.size()
public synchronized java.util.List java.util.Vector.subList(int,int)
public synchronized java.lang.Object[] java.util.Vector.toArray()
public synchronized java.lang.Object[] java.util.Vector.toArray(java.lang.Object[])
public java.util.Spliterator java.util.Vector.spliterator()
public synchronized boolean java.util.Vector.addAll(int,java.util.Collection)
public synchronized boolean java.util.Vector.addAll(java.util.Collection)
public synchronized void java.util.Vector.addElement(java.lang.Object)
public synchronized java.lang.Object java.util.Vector.elementAt(int)
public java.util.Enumeration java.util.Vector.elements()
public synchronized void java.util.Vector.forEach(java.util.function.Consumer)
public synchronized java.lang.Object java.util.Vector.set(int,java.lang.Object)
public synchronized int java.util.Vector.capacity()
public synchronized void java.util.Vector.ensureCapacity(int)
public synchronized void java.util.Vector.trimToSize()
public synchronized void java.util.Vector.copyInto(java.lang.Object[])
public synchronized boolean java.util.Vector.containsAll(java.util.Collection)
public synchronized java.lang.Object java.util.Vector.firstElement()
public synchronized void java.util.Vector.insertElementAt(java.lang.Object,int)
public synchronized java.lang.Object java.util.Vector.lastElement()
public synchronized java.util.ListIterator java.util.Vector.listIterator()
public synchronized java.util.ListIterator java.util.Vector.listIterator(int)
public synchronized boolean java.util.Vector.removeAll(java.util.Collection)
public synchronized void java.util.Vector.removeAllElements()
public synchronized boolean java.util.Vector.removeElement(java.lang.Object)
public synchronized void java.util.Vector.removeElementAt(int)
public synchronized boolean java.util.Vector.removeIf(java.util.function.Predicate)
public synchronized boolean java.util.Vector.retainAll(java.util.Collection)
public synchronized void java.util.Vector.setElementAt(java.lang.Object,int)
public synchronized void java.util.Vector.setSize(int)
public synchronized void java.util.Vector.sort(java.util.Comparator)
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public default java.util.stream.Stream java.util.Collection.stream()
public default java.util.stream.Stream java.util.Collection.parallelStream()
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Just asking out of curiosity, would this capture methods made in an anonymous type? – SGM1 Jan 12 '16 at 22:43
  • @SGM1 An anonymous class is a real class, it just doesn't have a name. Technically, it does, but the auto-generated name is not for you to use. So yes, you can call `myAnonymousObj.getClass().getMethods()`. – Andreas Jan 12 '16 at 22:48
  • There is a static helper method in the Modifier class to check for flags. Call ```!Modifier.isStatic(method.getModifiers())``` – superjugy Jun 02 '19 at 21:51
3

If I understand you correctly you want to get a list of all methods from a class?

Class getMethodsFromClass = ob.getClass();
for (Method method : getMethodsFromClass.getDeclaredMethods()) {
  if (method.getAnnotation(PostConstruct.class) != null) {
    System.out.println(method.getName());
  }
}
Parsa
  • 3,054
  • 3
  • 19
  • 35
  • you are welcome! feel free to accept this answer if you are happy with the solution. – Parsa Jan 12 '16 at 22:35
  • 1
    *FYI:* This returns private and protected methods too, and doesn't return methods inherited from base classes. And like `getMethods()`, it returns static methods, so they need to be filtered out to fit OP's stated goal of "instance methods". – Andreas Jan 12 '16 at 22:37
  • ^ yep, this is important in some special case applications of this method – Parsa Jan 12 '16 at 22:39
  • @Andreas Just easily do `method.getAnnotation(PostConstruct.class) != null && !method.getName().contains(" static ");` for the if condition. :) – SGM1 Jan 12 '16 at 22:46
  • @SGM1 Don't exclude `static` using string comparison. See [my answer](http://stackoverflow.com/a/34755049/5221149) for correct way to check `static`. – Andreas Jan 12 '16 at 22:50
  • @Andreas I noticed, I just wanted to introduce a simpler thought. – SGM1 Jan 12 '16 at 22:53
  • @SGM1 It's a bad thought (performance, compatibility), besides it's wrong, since the method name doesn't have spaces, and doesn't have modifiers. Perhaps you meant `toString()`, but don't. Just don't. – Andreas Jan 12 '16 at 22:55
  • @Adreas You're right, I did mean `!method.toString().contains(" static ");`. I really do wonder how much string comparisons and addition method calls add to performance when already dealing with reflection. – SGM1 Jan 12 '16 at 22:59