简体   繁体   中英

How to obtain the runtime type from an object in java

I have this code:

public void doSomething(Integer param){
.
.
.
  JAXBElement<Integer> jaxb = new JAXBElement<Integer>
                                    (new QName("uri","local"),Integer.class, param);
.
.
.
}

And I want it to be more flexible. I need the parameter to be of type Object, but I don't know how to change this line:

JAXBElement< what_do_I_put_here > jaxb = new JAXBElement< what_do_I_put_here >(new QName("uri","local"),Integer.class, param);

I want it to use the runtime class of the type stored in param

public void doSomething(Object param){
.
.
.
  JAXBElement<??????> jaxb = new JAXBElement<??????>
                                    (new QName("uri","local"),?????.class, param);
.
.
.
}

Not entirely sure (don't have access to an IDE at the moment) but isn't is something like

public <T extends Object> void doSomething(T param) {
    .
    .
    JAXBElement<T> jaxb = new JAXBElement<T>(new QName("uri", "local"), (Class<T>) param.getClass(), param);
    .
    .
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM