Java5, generics, and "just not quite there"

So an attendee comes up to me at one of the past NFJS shows, with this challenge:

The implementation does not know what parametrized Iterable class will be used. The Iterable class will need to know what class it contains. Interfaces are passed to the factory and it calls a lookup to identify (or create) the implementing class. Can this be done without causing a compile warning?
// usage:
        Seq<Item> items = factory.createBean(null, Seq.class, Item.class);

// interface: public abstract <T> T getBean(String localName, Class<T> javaClass, Type… typeArguments);

// impl: public <T> T createBean(String localName, Class<T> javaClass, Type… typeArguments) { Resource resource = createResource(localName); Collection<STRING> rdfTypes = findRdfTypes(javaClass); for (String rdfType : rdfTypes) { addStatement(resource, RDF.TYPE, createResource(rdfType)); } T bean = rdfBeanFactory.createBean(this, resource, rdfTypes, javaClass); if (typeArguments != null && bean instanceof RdfParameterizedBean) ((RdfParameterizedBean)bean).setActualTypeArguments(typeArguments); return bean; }

– Some ideas I have tried.

import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.HashSet; import java.util.Set;

public class test {

public static class plain {
    public static Collection getSomethingOf(Class type, Class contentType) throws Exception {
        Collection result = (Collection) type.newInstance(); // cast
        result.addAll(Arrays.asList(contentType.newInstance())); // warning
        return result;
    }

    public static Set&lt;Date> getSetOfDate() throws Exception {
        return (Set&lt;Date>) getSomethingOf(HashSet.class, Date.class); // warning
    }
}

public static class fixed {
    public static &lt;C> Collection&lt;C> getSomethingOf(Class&lt;? extends Collection> type, Class&lt;C> contentType) throws Exception {
        Collection&lt;C> result = type.newInstance(); // warning
        result.addAll(Arrays.asList(contentType.newInstance()));
        return result;
    }

    public static Set&lt;Date> getSetOfDate() throws Exception {
        return (Set&lt;Date>) getSomethingOf(HashSet.class, Date.class); // cast
    }
}

public static class external {
    public static &lt;C, Collection&lt;C extends T>&gt; T getSomethingOf2(Class&lt;T> type, Class&lt;C> contentType) throws Exception {
        T result = type.newInstance();
        result.addAll(Arrays.asList(contentType.newInstance()));
        return result;
    }

    public static Set&lt;Date> getSetOfDate2() throws Exception {
        Class&lt;HashSet&lt;Date>&gt; type = (Class&lt;HahSet<Date>&gt;) HashSet.class; // warning
        return getSomethingOf2(type, Date.class);
    }
}

public static class internal {
    public static &lt;C, T extends Collection, R extends Collection&lt;C>> R getSomethingOf(Class&lt;T> type, Class&lt;C> contentType)
                    throws Exception {
        R result = (R) type.newInstance(); // warning
                    result.addAll(Arrays.asList(contentType.newInstance()));
        return result;
    }

    public static Set&lt;Date> getSetOfDate() throws Exception {
        return getSomethingOf(HashSet.class, Date.class);
    }
}

The goal here, I think, is to be able to construct instances of T without compiler warnings or errors (or old-style casts). Needless to say, neither Venkat nor I could manage to cruft up something that could work, and so I thought to throw this out to the blogosphere to see what others could come up with.

If I’m feeling bored one day I’ll try coding it in C#, just to (hopefully) exemplify the differences in the generics model between the two.

UPDATE: Hopefully I got the formatting right this time; have I mentioned how much I hate the fact that Java, C# and C++ all use the left-pointy-bracket/right-pointy-bracket syntax when posting code snippets like this...?