Skip to main content

Genericity improvement / Exact parameter type at runtime

6 replies [Last post]
Joined: 2003-06-12

Add the capability for a generic class to retain the exact object param types passed at runtime.

class MyList ... {

IF you have,
MyList str = new MyList();

There should be a way for people to get inside the MyList code that the Parameter

Class cls = str.getClassParameter("T");

This is mandatory if you expect to make generic code using the genericity.

At this time you can not AFAIK.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-12

My goal was to bring generic swing data models that plug on top of regular java generic List like :

class BeanTableModel implements TableModel {
private List list;
public BeanTableModel(List list){
this.list = list;

public void addBeanProperty(String str){
Class parameterType = list.class.getClassParameter("E");
// then do some checking for a property named like given


// the rest of the implementation is a peace of cake ...


Then usage will be :

BeanTableModel view = new BeanTableModel(mylist);
// my list is a List
// then any change that will happen in the myList, the model, should be automatically reflected to view (assuming we've binded the right events ....

The problem is that the list might be empty, so there is no way for me to know at the time of adding property that the what is the meta class and if the added property is available.

I mean, the data type is already checked (to various extends), so why not bring the feature for this to be extended full to runtime ?

For instance, this can be generated as a syntetic field on the runtime if people put an annotation RuntimeGeneric.

I was planing to define generic datamodel for swing that will plug on regular collections framework (no more need to rewrite all the time swing models stuffs). With thouse generic element, the configuration will be done in the code or at runtime without any side effect.

FYI, I already got one "oldies" that works without genericity and put as manadatory for constructing the model a meta class in the constructor ;-)


Joined: 2004-03-18

This sounds great.

>FYI, I already got one "oldies" that works without
> genericity and put as manadatory for constructing the
> model a meta class in the constructor

Well you're almost there. If fact that is what you do with generics as well.

A common idiom with generics is to specify a constructor argument of type Class, so that you have the meta class info inside your object.

The advantage of generics in this case, is that you must pass the correct class, not just any class.
class BeanModelTable {
public beanModelTable(List list, Class meta) {}
List myList = new ArrayList();
BeanModelTable table = new BeanModelTable(myList,Widget.class);
If you tried to pass in something other that Widget.class, you would get a compiler error.

For sure, it would be nice not to have to repeat my self quite so much when constructing one, but the benefits of having this functionality, are still well worthwhile.

Joined: 2003-06-10

For what reason you need something like that? Maybe one can find a more elegant way.

Best regards,
Thomas Singer

Joined: 2004-03-18

Any generic class that constructs new instances of any of its type parameters, or arrays of those. With this feature List.toArray() could be overloaded with a zero argument version, just as an example.

I don't think the use cases are so common as to justify the overhead of runtime type parameter knowledge automatically in all cases, but an "on demand" (by the generic class) mechanism would be nice. However, it is syntactic sugar, whereas other language features that solve "currently not possible" things rank higher in my view.


Joined: 2004-03-18

You can coerce your library users into telling you, by having a class argument in every publicly accessible constructor
class MyList {
private Class tKind;
public MyList(Class tKind) {
this.tKind = tKind;

What would be neat would be something that told the compiler that this constructor needed the type args, without the caller having to pass them explicitly.

That way, nothing changes for most generic classes that don't need to know the runtime type, but those few cases where it is important (maybe so that new T() can be done reflectively) can achieve this without burdening the library user at the site of the invocation code.

This is not trivial, because for example the caller might be a generic class as well, and the type argument when constructing a MyList might be a Type Variable in the caller.

Another difficulty is that T might not be a raw type (like String), but itself a parameterized type (like Map ) , and so the argument should really be a Type, except Type is not generic.

Message was edited by: brucechapman

Joined: 2004-10-14

IMHO, classes are not sufficient for all solutions, T can be any (generic) type.

Syntactic suger proposal:
[code] Type t = T.type[/code]
instead of:
[code] Class cls = str.getClassParameter("T");[/code]
Access to the runtime type of a type parameter will require that the type parameters are passed into the constructor/method with each invocation of a generic constructor/method.