//********************************************************************* // // Copyright (C) 2005 // Carnegie Learning Inc. // // All Rights Reserved. // //This program is the subject of trade secrets and intellectual //property rights owned by Carnegie Learning. // //This legend must continue to appear in the source code despite //modifications or enhancements by any party. // //********************************************************************* /* $Id$ $Name$ */ package cl.sdk.common.testcode; import java.util.Collections; import java.util.List; public class TestGenerics { /**Subclasses are parameterized by their own type*/ private static abstract class SelfType>{ public abstract T getThis(); } /**Supertype inherits directly from the parameterized SelfType*/ private static class SuperType extends SelfType{ @Override public SuperType getThis(){ return this; } } /**Subtype inherits indirectly from the parameterized SelfType*/ private static class SubType extends SuperType{} /**Creates a list containing a single SelfType*/ public static > List makeSingletonList(T t){ return Collections.singletonList(t); } /** * Creates a list containing a single SelfType, allowing the list's * element-type to be a supertype of the type of its single element */ public static ,S extends T> List makeSingletonList2(S s){ return Collections.singletonList((T)s); } public static void main(String[] args){ /*making lists of super types works fine ...*/ makeSingletonList(new SuperType()); List lsup = makeSingletonList(new SuperType()); /*but we can't make a list of sub types; seems weird ...*/ List lsub = makeSingletonList(new SubType()); //ERROR /*can't even call it w/o assigning the return value:*/ makeSingletonList(new SubType()); //ERROR /*so instead, we should be able to make lists of super type containing sub type elements*/ makeSingletonList2(new SubType()); //ERROR /*even if we assign the return value:*/ lsup = makeSingletonList2(new SubType()); // ERROR (eclipse is okay with this though) /*this still doesn't work either:*/ lsub = makeSingletonList2(new SubType()); // ERROR /*we can make lists of super type this way though*/ makeSingletonList2(new SuperType()); // (eclipse doesn't like this though) /*also ok if we assign the return value*/ lsup = makeSingletonList2(new SuperType()); } } interface FooInterface{} enum FooEnum implements FooInterface{FOO} class FooParam

& FooInterface>{} class FooTest{ public static void main(String[] args){ FooParam unparam = null; method(unparam); } public static

& FooInterface> void method(FooParam

fooparam){ } }