File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Generic Black hole? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generic Black hole?" Watch "Generic Black hole?" New topic
Author

Generic Black hole?

H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 391
Basically, I implemented MessageBodyReader within the context of using restful web service.
But I think my issue is more toward understanding Java related than restful feature.
Generics is black hole for me in general :-D

How do we test for the genericity of java.util.List<org.concepts.domain.Computer>


}
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

H Paul wrote:
How do we test for the genericity of java.util.List<org.concepts.domain.Computer>


It depends on what you mean by that. Taking an educated guess at what you might mean, I would say we try to add 1) Computer, 2) subclass of Computer, and 3) non-Computer to the list, and then we verify that 1 and 2 compile and 3 gives a compile error. We could further retrieve from the list and verify that declaring the result as Computer without casting does not cause a compiler error.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

H Paul wrote:How do we test for the genericity of java.util.List<org.concepts.domain.Computer>

You don't. Or at least, you shouldn't.

You're in the realm of Object-Orientation here. If you plan on checking types and then dispatching code based on that, then you're either:
(a) Doing something wrong, or
(b) Into the realm of execution-time binding, which Java was never intended to solve.

Why don't you back up and tell us what you want to do; not how you want to do it?

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 391
Basically

1. Client (jax-rs) send a GET to the server
2. The server send back a JSON string in the form of an JSON Array.
3. Client (jax-rs) need to convert this JSON string into a Java List that I need.


4. Client (jax-rs) call my custom code to convert JSON string into a Java List.


5. The Client (jax-rs) ask my custom code isReadable to produce a Java List? (return true/false)
If my isReadable OK (true), then the Client will call readFrom to convert the JSON string into a Java List.

The issue here is at isReadable (how do I test for genericity of List<Computer>


what is this line mean?

genericType - the type of object to be produced. E.g. if the message body is to be converted into a method parameter, this will be the formal type of the method parameter as returned by Method.getGenericParameterTypes.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 391
After google

A generic type is a type with formal type parameters.
A parameterized type is an instantiation of a generic type with actual type arguments.


and re-read:
Ascertain if the MessageBodyReader can produce an instance of a particular type.
The type parameter gives the class of the object that should be produced,
the genericType parameter gives the java.lang.reflect.Type of the object that should be produced.
E.g. if the object to be produced is List, the type parameter will be java.util.List and
the genericType parameter will be java.lang.reflect.ParameterizedType.


I put debug statement to see what values are:






Now how do I verbalize the WHAT I want to understand.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 391


Question:
Is 1. sufficient as a test? OR Is 2. more defensive?

1.
return type==List.class;

2.
return type==List.class && genericType instanceof ParameterizedType;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Black hole?
 
Similar Threads
bounded wild card - confusion
Generic - Type Erasure removes information (at compile time) required at runtime. How does it work?
Error while parsing simple plain text fields in multipart form at Restlet 1.1
Resteasy and Spring - howto register javax.xml.bind.ValidationEventHandler
returning a generic list from REST