Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiate generic type

Tags:

java

I'm trying to instantiate a new object by Class clazz like this:

Code:

public class EntityType {
    private Map json = null;
    public EntityType(Map json){
        this.json = json;
    }
    public <T> T getAs(Class<T> clazz){
        final Class<T> clz = clazz;
        T obj = null;
        try {
            if (checkMap(json)){
                obj = clz.newInstance();
            }
        } catch (Exception e){
            if (e instanceof InstantiationException){
                e.printStackTrace();
            } else if (e instanceof IllegalAccessException){
                e.printStackTrace();
            } else {
                e.printStackTrace();
            }
        }
        return obj;
    }
}

This is how I use the code:

EntityType type = new EntityType(map); // map contains values 
User user = type.getAs(User.class);

java.lang.InstantiationException is thrown for the above code.

What could be wrong in my code?

Update:

    @Entity(name = "user")
    public class User { 
        @Id 
        private String id;
        @Column
        private String name;
        @Column
        private Long age;
        public User() {}
       // Setters and getter 
   }

Full stack trace:

java.lang.InstantiationException: org.goo.AnnotationTest$User
    at java.lang.Class.newInstance0(Class.java:357)
    at java.lang.Class.newInstance(Class.java:325)
    at org.goodb.client.Goo$EntityType.getAs(Goo.java:90)
    at org.goo.AnnotationTest.doTest(AnnotationTest.java:97)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

2 Answers

You probably don't have default constructor in your user's class

like image 137
nDijax Avatar answered Aug 04 '26 18:08

nDijax


The exception stack trace says

java.lang.InstantiationException: org.goo.AnnotationTest$User

which suggests that User is a nested or inner class rather than a top-level one. Static nested classes behave like top-level ones but for non-static inner classes, every instance is associated with a particular instance of its containing class and the compiler adds an extra parameter to the front of the parameter list for every constructor to pass in the appropriate containing instance. What in the source code looks like

public User() {}

actually corresponds in the bytecode to something like

public User(AnnotationTest containingInstance) { this.container = containingInstance; }

(not with those parameter/field names but you get the idea). So your User class does not in fact have a no-argument constructor.

If you declared the User class as a top-level class or marked it static then it would work as you expect it to.

like image 44
Ian Roberts Avatar answered Aug 04 '26 17:08

Ian Roberts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!