Troubleshooting
Problem
Attempts to compile with Javac option from the JDK bin directory within IBM Rational Software Architect v7.5.x result in "no unique maximal instance exists for type variable ..." output.
Symptom
Using the default compilation mechanism of Rational Software Architect v7.5.x works as expected, but compiling with Javac
option fails.
Steps to reproduce:
- Use the following source code
public class GenericsTest2 {
public static void main(String[] args) throws Exception {
Integer i = readObject(args[0]);
System.out.println(i);
}
public static <T> T readObject(String file) throws Exception {
return readObject(new ObjectInputStream(new
fileInputStream(file)));
// closing the stream in finally removed to get a small example
}
@SuppressWarnings("unchecked")
public static <T> T readObject(ObjectInputStream stream) throws Exception {
return (T)stream.readObject();
}
}
- Compile using command line option
C:\Program Files\IBM\SDP\jdk\bin\javac.exe GenericsTest2.java
Expected result:
Compiled
GenericsTest2.class
fileActual result:
The following exception is produced:
type parameters of T cannot be determined; no unique maximal instance exists for type variable T with upper bounds T, java.lang.Object return readObject(new ObjectInputStream(new FileInputStream(file)));
Cause
This issue has been identified as a product defect and has been logged under APAR PM19633.
Resolving The Problem
WORKAROUND
Change the above code sample as shown below:
...
public static <T> T readObject(String file) throws Exception {
return (T) readObject(new ObjectInputStream(new
fileInputStream(file)));
...
The explicit cast to type T will enable compilation of the code with the following warning message:
GenericsTest2.java:12: warning: [unchecked] unchecked cast
found : java.lang.Object
required: T
return (T) readObject(new ObjectInputStream(new FileInputStream(file)));
1 warning
Was this topic helpful?
Document Information
More support for:
Rational Software Architect Standard Edition
Software version:
7.5, 7.5.1, 7.5.2, 7.5.3, 7.5.4, 7.5.5, 7.5.5.1
Operating system(s):
Windows
Document number:
141653
Modified date:
16 June 2018
UID
swg21443094