简体   繁体   中英

how to perform typecastng at runtime in java?

i am trying to make a client for services exposed through REST.I have multiple number of classes extending a single class.

Now when i send the request and get a response,every time i need to type cast the response for specific class.i am trying to automate this process,can this be achieved at run time?

I am thinking of using generics and reflection but unable to move forward.what i exactly want to achieve is by just mentioning a unique string or say request,i must be able to get exact same response without type casting it with that particular response class.

By using generics i succeeded in reducing some code for typecasting,still i am not satisfied as i want to completely achieve it at run time.

RequestClass request=(RequestClass)getRequest(some attributes);
output=(Responseclass)response.getResult();

Here every time i need to mention the request and response classes,i don't want to do this.

can i do something where i can map the request and response classes to a key or a string and based on it the code will fetch the request and response class and perform the operation according to it(not sure about it).

please guide me in doing this,or any other way i can do the above mentioned thing. Thanks in advance.

Consider using one of the many Java REST libraries. Our client uses the Jersey API to handle requests and responses to our Python based RESTful server.

Jersey uses a class called ClientResponse that stores the generic response data. you can use the getEntity method to return the response as a specific type.

Here is an excerpt of my code, which only deals with strings, but you can see how it could be extended:

    ClientResponse response; //a Jersey class
    String responseText;
    WebResource odbc = resourceCollection.path("ODBC"); //another Jersey class
    try {
        //we send a POST and get a response stored as generic
        response = odbc.type(media)
                .accept(MediaType.APPLICATION_XML,MediaType.TEXT_PLAIN)
                .post(ClientResponse.class, formdata);
        //we pull out the response entity as a string
        responseText = response.getEntity(String.class);
    } catch (UniformInterfaceException e) {
        write("<UniformInterfaceException>\n");
        write("  Response type was not expected\n");
        write("</UniformInterfaceException>\n");
        return;         
    } catch (ClientHandlerException e) {
        write("<Error>\n");
        write("  Unable to connect or connection refused\n");
        write("</Error>\n");
        return;
public class DynamicCasting{
    DynamicCasting e1=new DynamicCasting();
    private Object obj=new Object();
    DynamicCasting.doSomething(obj);
    public static DynamicClass doSomething(DynamicClass dynClassObject){
        return dynClassObject;
    }
}

The obj will be type casted to DynamicClass in this example.

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