I want to create a Program that takes another Java program as input and should return number of methods in the class and also number of attributes.
Asked
Active
Viewed 251 times
3 Answers
5
You'll need to use reflection:
http://java.sun.com/developer/technicalArticles/ALT/Reflection/
edit: Question already asked here: Is it possible to use Java Reflection to print out attributes of the parent class?

Community
- 1
- 1

mowwwalker
- 16,634
- 25
- 104
- 157
0
Sample Code:
public class Test {
public static void main(String[] args) {
System.out.println(Test.class.getMethods().length);
System.out.println(Test.class.getFields().length);
}
}

zsxwing
- 20,270
- 4
- 37
- 59
-
5Best not to answer `homework` questions with the actual code, but rather (as Walkerneo did) with a pointer to how to proceed to build one's own solution. – T.J. Crowder Mar 28 '12 at 07:48
0
This sounds like you are asking us a homework question. Look at the Reflection API, in package java.lang.reflect
and the Class class. Then when you have some code you want us to help with, post again.

Jon
- 3,510
- 6
- 27
- 32