Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Black hole?

 
H Paul
Ranch Hand
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10427
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
H Paul
Ranch Hand
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic