0

i use the following code to get the methods declared in a class. but the code returns the unnecessary values as well the declared methods too.

following is my code :

EDIT :

Class cls;
List classNames = hexgenClassUtils.findMyTypes("com.hexgen.*");
            Iterator<Class> it = classNames.iterator();
            while(it.hasNext())
            {

                Class obj = it.next(); 
                System.out.println("Methods available in : "+obj.getName());
                System.out.println("===================================");
                cls = Class.forName(obj.getName());
                Method[] method = cls.getDeclaredMethods();
                int i=1;
    Method[] method = cls.getDeclaredMethods();
     int i=1;
     for (Method method2 : method) {
    System.out.println(+i+":"+method2.getName());
    }
}

I have also tried with getMethods()

following is my output :

1:ajc$get$validator
2:ajc$set$validator
3:ajc$get$requestToEventTranslator
4:ajc$set$requestToEventTranslator
5:ajc$interMethodDispatch2$com_hexgen_api_facade_HexgenWebAPIValidation$validate
6:handleValidationException

after this only i get the methods i have declared in class i give. What are the above values and how to avoid them.?.

Best Regards

4
  • this seems to be a proxy object. if you don't want to include those methods, you might want check other properties of the methods. or you use reflection to check for implemented interfaces? how do you get the class? Commented Apr 22, 2013 at 10:04
  • Include the class cls please. Commented Apr 22, 2013 at 10:05
  • Please have a look at my edited question, thought it is enough to serve with few points, Sorry for misleading. Commented Apr 22, 2013 at 10:08
  • 1
    There is no meaning to down vote, ask the person for more clarification if you think you can help. Apruv commented and left the place and even deleted his post. Laughable. Commented Apr 22, 2013 at 10:12

1 Answer 1

1

Try this:

Method[] method = cls.getClass().getDeclaredMethods();

instead of

Method[] method = cls.getDeclaredMethods();

See this example:

import java.lang.reflect.Method;

public class Example {
    private void one() {
    }

    private void two() {
    }

    private void three() {
    }

    public static void main(String[] args) {
        Example program = new Example();
        Class progClass = program.getClass();

        // Get all the methods associated with this class.
        Method[] methods = progClass.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println("Method " + (i + 1) + " :"
                    + methods[i].toString());
        }
    }
}

Output:

Method 1 :public static void Example.main(java.lang.String[])
Method 2 :private void Example.one()
Method 3 :private void Example.two()
Method 4 :private void Example.three()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the Answer Achintya but now it gives so many things which are not even methods and so on like 1:newInstance0 2:forName 3:forName 4:forName0 my actual methods are missing :(
@Anto See my example above . May be it will be helpful.
same answer i get if i use it separtly but in the program i don't get the same, the Class i investigate is spring class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.