Constant types (such as the type of the int constant '42') are represented in javac as custom Type subclasses which override the constValue() method, by returning the constant value.
There are few subtleties in the way constant types are used in javac. The most important is that constant types do not propagate through inference - that is:
<Z> Z m(Z z) { return z; }
m("Hello!");
Here, the inferred return type of 'm' is NOT a constant type - that is, inference should not preserve constant type information (same holds for type annotations), as the body of the method is not guaranteed to return the argument untouched.
For this reason, Type is equipped with a method, called 'baseType' which can be used to access the 'bare' underlying type with all constants stripped out.
It should be possible to replace constant types with metadatas on standard types. This would remove the need of creating extra custom anonymous subclasses for storing constant values - however, extra care must be taken to handle the behavior of baseType - it is likely that we will have to rewrite the method so that a new type is returned, stripped of all its original metadatas (if any).
There are few subtleties in the way constant types are used in javac. The most important is that constant types do not propagate through inference - that is:
<Z> Z m(Z z) { return z; }
m("Hello!");
Here, the inferred return type of 'm' is NOT a constant type - that is, inference should not preserve constant type information (same holds for type annotations), as the body of the method is not guaranteed to return the argument untouched.
For this reason, Type is equipped with a method, called 'baseType' which can be used to access the 'bare' underlying type with all constants stripped out.
It should be possible to replace constant types with metadatas on standard types. This would remove the need of creating extra custom anonymous subclasses for storing constant values - however, extra care must be taken to handle the behavior of baseType - it is likely that we will have to rewrite the method so that a new type is returned, stripped of all its original metadatas (if any).
- is blocked by
-
JDK-8048614 Add TypeMetadata to contain type annotations and other type information
-
- Closed
-