0

How to pass parameter for method reciveClassTest(Class clazz)

  • When fasterxml.jackson.annotation converter for one type for example MyType, can't convert because it's not the type
  • jackson properties all null

Below is an example of classes that well represent the scenario where withType not has a type Paper

public class TestPassClassType {

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
    TestPassClassType call=new TestPassClassType();
    
    Paper paper=new Paper();
    System.out.println(paper);
    
    MyType<Paper> type=new MyType<Paper>();
    
    System.out.println(type);
    
    // How to pass  call.reciveClassTest(MyType<Paper>.class)
    call.reciveClassTest(MyType.class);

    }

    public <T> void reciveClassTest(Class<T> clazz) throws InstantiationException, IllegalAccessException {
    
    T t=clazz.newInstance();
    MyType<Paper> withType=(MyType<Paper>) t;
    
    //withType not has a type Paper
    System.out.println(withType);
    }
}

class MyType<T> {
    
    public MyType() {}

    T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

class Paper implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
Marcelo Ferreira
  • 428
  • 1
  • 5
  • 20

0 Answers0