• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Dynamic Generic Type

 
author
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to create a generic map with type specified at runtime and its being a pain. The following does not compile:



I've tried replacing the Type reference with Class, but no luck. The compiler does not recognize type in <Integer,type>.

Any suggestions? If there isn't a synatical way to do this, is there a way I can manually do this using reflection?
 
Marshal
Posts: 27287
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generics are almost completely a compile-time phenomenon. So probably your best bet is to create a Map<Integer, Object>, or perhaps a subclass of that. But you aren't going to get any of the type-safety benefits of generics that way. And I don't think that reflection helps because you can't create a Map<Integer, Widget> via reflection, only a Map.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final Map<Integer,Type> map = new HashMap<Integer,Type>();

What about that? Type as with a CAPITAL T
 
Scott Selikoff
author
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I have a specific type in mind... actually let me give you an example of what I'm trying to do.

I would like to build an array of n-diminsional class for hashmaps from 1 to n where n is some number, so if n was 4 then:



I'm trying to do this in a loop such that, build c[i], then construct c[i+1] out of c[i] using the line:


But the second value of type will not compile no matter what I put in there. For example I can put a defined class like Double in it but not a class object that represents Double.
[ September 14, 2006: Message edited by: Scott Selikoff ]
 
Scott Selikoff
author
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, I can always just use "Object" and it works but no type checking... maybe the solution here is to write a utility method that generates code and copy and paste the code into my application? I'd really prefer another solution without such a restriction on predetermining size.
 
Paul Clapham
Marshal
Posts: 27287
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Selikoff:
But the second value of type will not compile no matter what I put in there. For example I can put a defined class like Double in it but not a class object that represents Double.

Right. Like I said, generics are a compile-time thing.

What you have there, at least to my eyes, is like a tree (except that the nodes are accessed by keys). You have some constraints on each of the trees, in terms of the depths of the leaf nodes, but they're still those key-based trees. So maybe you could recast your data into tree structures and access them recursively, rather than trying to declare them recursively at compile time.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A friend of mine -- we used to call him "The Alien Super-Genius" -- dealt with a similar kind of problem in C++, once. Multi-multi dimensional arrays with complex types, lots of templating, etc. It was a high-performance distributed computing situation as well -- i.e., an MPI on a massively parallel machine.

Turned out that it was simply not possible to write C++ code to handle all possible dimensionalities, much as (I believe) it's not possible here. The way he solved this was to write a program to write the program -- based on the inputs, program one would emit code custom-designed for the the appropriate N-space, then compile and execute it to solve the actual problem.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Those last three are all class java.util.HashMap, period. You could examine them with reflection to find that that class has two type parameters, K and V (as seen in the API). But at runtime there is absolutely no record of the values those type parameters had while the code was being compiled. At runtime, they've been forgotten. Well, there may be some traces of the compile-time types elsewhere in the bytecode, e.g. casts have probably been inserted at various places. But the Class objects have no memory whatsoever of the compile-time values of the type parameters.
 
Scott Selikoff
author
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting... so at runtime there's no saving of the depth information of generics? I guess there's no point using anything other than HashMap or Object for all references (except the first one).
[ September 14, 2006: Message edited by: Scott Selikoff ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. Unless you want to make a Map decorator that retains such knowledge at runtime, with some sort of Type composite to represent the nested generic types possible. I once made a set of collection decorators to remember the allowed Class of the elements at runtime, so as to ensure that even if the collection was used by code that used raw types and thus erased the compile-time generics checking, at runtime the collection would still throw appropriate ClassCastExceptions if an attempt was made to insert the wrong type of element. E.g.

This creates a decorator around the HashMap which remembers at runtime that the keys must be instances of String, and the values must be instances of Integer. It only remembers a Class for each type parameter though - if you want something that understands nested generic types, you'll probably have to write that yourself. If you're interested I can send you the files I have for the classes I wrote. Could well be more trouble than it's worth for your problem, unless you anticipate that there's a high chance of these instances being misused, and you really want togive detailed error messages about what's wrong with the way they're being used.
[ September 14, 2006: Message edited by: Jim Yingst ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Selikoff:
Interesting... so at runtime there's no saving of the depth information of generics?



Yes. Read about Erasure at http://today.java.net/pub/a/today/2003/12/02/explorations.html
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic