The following code snippet works fine in my eclipse development environment (1.6.0_06), but the build system running 1.6.0_06 throws exception:
MyClass:343: incompatible types
found : java.util.List<C>
required: java.util.List<B>
entries = createListFromSmartCopy(myAList, new B(), true);
----
Types:
A is an interface
B is an interface of A
C is an implementation of B
List<A> aList = new ArrayList<A>();
aList.add(new A());
List<B> return = createListFromSmartCopy(aList, new C(), true);
/**
* <p>Creates a copy of a list where the source list could be an ancestor
* type of the returned list. It also uses a reference object to actually
* construct the objects that populate the return list.</p>
*
* @param <T> - The ancestor type of the source list
* @param <R> - The derived type of the destination list
* @param <S> - The more derived type of the prototype object used to
* construct the list of R's
* @param sourceList - The source list
* @param referenceObject - The object used to construct the return list
* @param deepCopy - Deep copy serializable objects instead of just copying
* the reference
* @return a list of R's (as defined by the caller) of entries with the
* object constructed as a copy of referenceObject with the properties of the
* sourceList copyied in after construction
*/
public static <T extends Serializable, R extends T, S extends R> List<R> createListFromSmartCopy(
List<T> sourceList, S referenceObject, boolean deepCopy)
{
List<R> retr = new ArrayList<R>();
for(int i = 0; i < sourceList.size(); i++)
{
retr.add(copyOf(referenceObject));
copyInto(sourceList.get(i), retr.get(i), deepCopy);
}
return retr;
}
Any thoughts on either:
1. How does this pass the compiler validation inside eclipse, even through 'R' has not been defined? I believe that the code is doing some sort of return type inference to return the exactly correct type of list as referred by the return result or else it is simply ignoring the invariant capture of R all together and silently dropping the error. The funny thing is that the code does work just fine in practice in my development system without an issue.
or
2. Why if the code is valid does the independent build system disallow this generic return type 'inference' to occur? Are there compiler flags I can use to withhold this special condition?