Results 1 to 20 of 22
- 06-02-2011, 01:34 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
Getting the class of a type parameter
I'm posting this in here since I'm only almost a year inside the programmer's world, so I still have some big gaps in my Java knowledge and because I'm actually unsure when something is "Advanced Java".
Basically the question is the title: How can I get the class of a type parameter?
My intention is eventually to get all the getters of a class of the type T:
Although T.class is not allowed.Java Code:Method[] methods = [B][COLOR="red"]T.class[/COLOR][/B].getMethods(); for(Method method : methods){ if(method.getName().startsWith("get") && (method.getParameterTypes().length == 0 && void.class.equals(method.getReturnType()))) { //Do stuff } }
Which solutions are there? My goal is to find the best solution but any suggestion is accepted - to help me get there.
The only thing I found was an example where they create a new instance of T so you can call getClass() on that object:
Java Generics: Instantiating Objects Of Type Parameter Without Passing Class Literal To Instance
Although I'd like to know if anyone have alternative solutions.
Thanks so much in advance!Last edited by Muskar; 06-02-2011 at 02:39 AM. Reason: Edited for clarification
- 06-02-2011, 01:52 AM #2
Let me see if I understand, you have found a solution to your problem but you say "screw that I want a different solution".
- 06-02-2011, 02:06 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
I assume that you think that the messy code I linked to is a proper solution - and judging solely from your post count I trust your opinion and will just try to build my solution around that, and see if I will run into troubles when I come into the testing phase.
Thing is, I'm really trying to create a general JTable-subclass that takes objects (like a JList takes an Object[]) instead of an Object[][] of variables - and I'd therefore prefer it to be as optimized as possible - but I'll look elsewhere if you don't want to help.
- 06-02-2011, 02:10 AM #4
No I wasn't referring to the "messy code". I was referring to your statement about what you found by doing some research.
- 06-02-2011, 02:22 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
- 06-02-2011, 02:30 AM #6
I don't understand either.
Does this solve your problem or not?The only thing I found was an example where they create a new instance of T so you can call getClass() on that object:
Java Generics: Instantiating Objects Of Type Parameter Without Passing Class Literal To Instance
- 06-02-2011, 02:42 AM #7
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
As I was trying to say, I'm trying to find the best solution, not just any solution. But perhaps this is not the forum for this kind of request?
I'm asking because I'm under the impression that it's not an optimal solution - although I'm not experienced enough with code-behind to know for sure.
- 06-02-2011, 02:46 AM #8
As long as your code works and doesn't take 6 hours to run then who cares if your solution is optimal or not. As you gain more experience as a Java programmer you might learn of better solutions. But for now why waste time trying to improve your code when it probably doesn't need it?
Last edited by Junky; 06-02-2011 at 02:47 AM. Reason: Stoopid forum software. Why does it repeat the last word on the next line?
- 06-02-2011, 12:43 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
Alright, I ran the first test and discovered that the code doesn't work. It throws a NullPointerException (at the greenline) because the following if-sentence (marked red) isn't true:
I created a simple 'Person' class with a String name and an int age (and getters and setters to both) to test this.Java Code:T t = null; ArrayList<String> resultList = new ArrayList<String>(); Type type = getClass().getGenericSuperclass();d [B][COLOR="red"]if (type instanceof ParameterizedType)[/COLOR][/B] { ParameterizedType paramType = (ParameterizedType)type; System.out.println(paramType.getClass().getName()); Class<T> tClass = (Class<T>) paramType.getActualTypeArguments()[0]; try { t = tClass.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } Method[] methods = [COLOR="lime"]t.getClass()[/COLOR].getMethods(); for(Method method : methods){ if(method.getName().startsWith("get") && (method.getParameterTypes().length == 0 && !void.class.equals(method.getReturnType()))) { resultList.add(method.getName().substring(3)); } } Object[] tmp = resultList.toArray(); columnNames = new String[tmp.length]; for (int i = 0; i < tmp.length; i++) { columnNames[i] = (String)tmp[i]; }
- I don't have a clue why it doesn't work.Last edited by Muskar; 06-03-2011 at 12:23 PM.
- 06-03-2011, 12:27 PM #10
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
I apologize for double-posting but I'm off the first page, so it's either bumping or creating a new topic.
I can add that this is most likely a Java Reflection question.
EDIT: Maybe I'd like this to be moved to Advanced Java and see if I make progress over there.Last edited by Muskar; 06-03-2011 at 12:34 PM.
- 06-03-2011, 02:24 PM #11
- 06-03-2011, 03:09 PM #12
- 06-03-2011, 04:01 PM #13
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
Thanks for the reply.
I never used ParameterizedType before and I didn't read the API until now - but I'm still not sure I understand what it is - or has to do with my goal.
All the code, that is an attempt to get the class of T, is taken from the mentioned link above.
I only had an idea of what it could be doing but now that it doesn't work I have no clue what it has to do with getting the class of T.
Basically all I understood, of what I read in the API, was that Type is implemented in Class, and that ParametizedType is extended from Type.
My goal is to be able to put any class, like the Person-class, as replacement of T and then retrieve the class from it so I can get it's getters etc.
Maybe I should mention that my current impression of T is that it represents a 'wildcard'-class - a class that isn't defined yet but will be later on. At least that's how I've used it over the past few months (just in case I'm wrong about that).Last edited by Muskar; 06-03-2011 at 04:05 PM.
- 06-03-2011, 04:30 PM #14
I don't think you can get the type of a class used this way in generics.
Do a search on the forum. I think There were attempts at this in the last year.
I think its documented in the Java Tutorial.
- 06-03-2011, 04:47 PM #15
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
- 06-03-2011, 05:22 PM #16
Read the Tutorial Generics section. Especially near the last page.
- 06-03-2011, 05:45 PM #17
java.beans.IntrospectorMy goal is to be able to put any class, like the Person-class, ... so I can get it's getters etc.
db
- 06-03-2011, 07:48 PM #18
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
I'm not sure exactly what the issue is, but you can certainly get the class of a parameterized type:
Outputs:Java Code:class Foo { public <T> void printArgClassName(T arg) { System.out.println(arg.getClass().getSimpleName()); } public static void main(String[] args) { String bar = "bar"; Float barf = 12.34f; Foo foo = new Foo(); foo.printArgClassName(bar); foo.printArgClassName(barf); } }
String
Float
- 06-03-2011, 09:00 PM #19
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
My problem is that I don't have an object of T in my class and I'm not planning on getting one. I have an ArrayList<T> though - but I'm not aware of any way to get it's parametrized type - and the list is allowed to be empty, so I can't just get an object inside it.
Otherwise that's exactly what I need.
@Norm, call me blind but I can't seem to find anything on this subject in the entire Generics Tutorial (if it this you're talking about: http://download.oracle.com/javase/tu...xtra/generics/).
@DarrylBurke, it seems like it's meant for GUI components and not classes, or am I wrong?Last edited by Muskar; 06-03-2011 at 09:16 PM.
- 06-03-2011, 09:06 PM #20
I searched my archive and found this comment by JosAH in August 2010
Do a search here on erasure. There are lots of hits.JosAH:
I understand your problem; unfortunately there is no way during runtime to find the type Double in, say,
List<Double> due to type erasure; if the list is empty (i.e. not containing at least one Double) you're
out of luck. Would it be possible to keep an 'exemplar'? (a SmallTalk thingie). The type of the exemplar
would be the type of the elements in your collection. Even a String representation of the type will do.
Similar Threads
-
Class as a Parameter.
By Soxman04 in forum New To JavaReplies: 2Last Post: 05-12-2011, 06:36 PM -
Class<T> in method parameter
By Onra in forum New To JavaReplies: 4Last Post: 03-14-2011, 12:12 AM -
doubt regarding <type parameter>
By subith86 in forum New To JavaReplies: 7Last Post: 03-01-2011, 03:35 PM -
Passing final parameter from a class
By ianyappy in forum New To JavaReplies: 8Last Post: 12-02-2010, 07:06 PM -
passing an enum type as a parameter ??!
By SCS17 in forum New To JavaReplies: 11Last Post: 07-13-2008, 01:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks