1

I need a way to get a list of all methods of a java class which could be called from a java file. I know that I can use Class.getMethods, but that does not work in some situations like I need it:

public interface IField<T> {
    public void setValue(T value);
}

public class Field implements IField<String> {
    private String value;

    @Override
    public void setValue(String value) {
        this.value = value;
    }
}

Calling Field.class.getMethods() contains to setValue methods, but only the one with the String parameter is allowed to be called from a java source file.

Just remove the one with the "less specific" parameter is also not a solution becasue there might be situations like this:

public abstract class AbstractBean {
    private Object value;

    public void setValue(Object value) {
        this.value = value;
    }
}

public class Field extends AbstractBean {
    private Integer value;

    public void setValue(Integer value) {
        this.value = value;
    };
}

where both setValue methods are ok to be called.

3
  • you may aswell want to loop through the superclasses of Field. Commented Apr 19, 2016 at 12:00
  • 1
    This can't work because of Type Erasure. Commented Apr 19, 2016 at 12:00
  • This should give you some pointers: stackoverflow.com/questions/14841952/… Commented Apr 19, 2016 at 12:53

1 Answer 1

1

After some more research I found this article on type erasure. So I have to filter out the bridge methods via Method.isBridge()

Sign up to request clarification or add additional context in comments.

Comments

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.