Take the 2-minute tour ×

I'm building a custom component for Talend Open Studio to access a source of data from an application (JIRA) that has a wrapper to get all its data. It exposes an API full of very volatile getters for all its data structure. Since Talend Open Studio supports primitive type only, i usually need to chain several call to get a primitive data (ie. Project.getProjectDetail().getOwner().getName())

But these API often vary and i don't want to change my code every time. So i decided to use an enumeration of methods: when a new field is exposed, i need only to add an element to the enumeration. Something like that:

(i cut most elements to be concise)

public enum JiraProjectField {

    KEY(new String[]{"getKey"}),
    COMPONENTS_NUM(new String[]{"getComponents", "size"}),

    private Method[] m;
    private ArrayList<Class<?>> r;

    private JiraProjectField(String[] methods) {

        this.r = new ArrayList<Class<?>>(methods.length);

        Class<?> initClass = Project.class;
            try {
                m[i] = initClass.getMethod(methods[0], (Class<?>)null);
                initClass = m[i].getReturnType();
            } catch (SecurityException e) {
                throw new ExceptionInInitializerError(e.toString());
            } catch (NoSuchMethodException e) {
                throw new ExceptionInInitializerError(e.toString());

But I always get a NoSuchMethodException, even if i'm sure the method with that name exists. Is there something i don't know?

share|improve this question
It's not clear what enums have to do this. Are you saying this code works outside an enum? It would really help if you'd give a short but complete example of this failing. –  Jon Skeet Jan 15 '13 at 23:24
What is the signature of the method? –  assylias Jan 15 '13 at 23:24
Sure it's a public method in a public class? –  Tom Hawtin - tackline Jan 15 '13 at 23:26
"Is there something i don't know?" Thomas Edison was heard to say "We don't know a millionth of 1% about anything.". So I expect the answer to that is. Yes. –  Andrew Thompson Jan 15 '13 at 23:26
I don't know even an estimate of how much I don't. ;) –  Peter Lawrey Jan 15 '13 at 23:45
show 3 more comments

1 Answer

up vote 3 down vote accepted

I am not sure why you are not using varargs more but you can try

m[i] = initClass.getMethod(methods[i]);
share|improve this answer
Ah yes, it getMethod(methods[0], new Class<?>[] { null }); which actually looks like unspecified behaviour. –  Tom Hawtin - tackline Jan 16 '13 at 0:03
+1, it was much more easier than i supposed. The problem was with that cast Class<?>; remove the cast resolved the issue. However I'm wondering if i could find a better design (an array of method instead of an array of string? a varargs prototype? nothing like that?) –  Gabriele B Jan 16 '13 at 8:16
You will be able to in Java 8 as it has first class method objects. i.e. you can refer to a method and access it without explicit use of reflection. –  Peter Lawrey Jan 16 '13 at 13:26
add comment

Your Answer


