5

I have a class that looks like this:

public class UploadBean {


    protected UploadBean(Map<String,?> map){ 
        //do nothing.
    }
}

To use reflection and create a object by invoking the corresponding constructor, I wrote code as follows:

Class<?> parTypes[] = new Class<?>[1];
parTypes[0] = Map.class;
Constructor ct = format.getMappingBean().getConstructor(parTypes);
Object[] argList  = new Object[1];
argList[0] = map;
Object retObj = ct.newInstance(argList);

This code fails at runtime with "No Such Method Exception". Now, how do I set the param type correctly?! such that the generic map argument in the constructor is identified?

1
  • Format is an implementation of this: public interface FormatFactory { public Map<String, Class> getFormat(); public Class<? extends UploadBean> getMappingBean(); } Commented Jun 2, 2009 at 14:44

3 Answers 3

7

The constructor is protected - if you make it public or use getDeclaredConstructor instead of getConstructor it should work.

(You'll need to use setAccessible if you're trying to call this from somewhere you wouldn't normally have access.)

EDIT: Here's a test to show it working okay:

import java.lang.reflect.*;
import java.util.*;

public class UploadBean {

    // "throws Exception" just for simplicity. Not nice normally!
    public static void main(String[] args) throws Exception {
        Class<?> parTypes[] = new Class<?>[1];
        parTypes[0] = Map.class;
        Constructor ct = UploadBean.class.getDeclaredConstructor(parTypes);
        Object[] argList  = new Object[1];
        argList[0] = null;
        Object retObj = ct.newInstance(argList);
    }

    protected UploadBean(Map<String,?> map){ 
        //do nothing.
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Or use getDeclaredConstructor(), as I just found out (but too late!).
doesn't work throws this exception again: java.lang.NoSuchMethodException: test.fileupload.XYZUploadBean.<init>(java.util.Map)
Would getDeclaredConstructor() then require setAccessible(true)? In my tests it doesn't, but I didn't try separating main() from the UploadBean.
@Jay: Please provide a short but complete program then. If you're using the right class and it's got the right constructor, it should be okay. It works in my tests.
In particular, does XYZUploadBean have the right constructor? You've shown UploadBean, but not XYZUploadBean...
1

The generic information is not available at runtime, it's just for static analysis, so do it as if the generics didn't exist.

1 Comment

Yeah, the error really has nothing to do with generics; this is really just a reflection question. (But it's nice to clarify this point.)
0

I believe you need to call

ct.setAccessible(true)

The setAccessible method allows you to override access methods.

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.