-
Bug
-
Resolution: Won't Fix
-
P3
-
6, 7
-
generic
-
generic
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2187689 | 7 | Jonathan Gibbons | P2 | Closed | Fixed | b132 |
Please see the following code sample.
If parametrized class below
class MyClass<T> { }
is given to annotation processor that checks spec assertion:
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class MyProcessor extends AbstractProcessor {
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Set<? extends Element> rootElements = roundEnv.getRootElements();
if (rootElements.size() > 0) {
performCheck(rootElements);
}
return true;
}
private void performCheck(Set<? extends Element> rootElements) {
TypeElement typeElement = ElementFilter.typesIn(rootElements).iterator().next();
TypeParameterElement typeParameterElement = typeElement.getTypeParameters().get(0);
// Element getEnclosingElement():
// If this is a type parameter, {@code null} is returned.
Element enclosingElement = typeParameterElement.getEnclosingElement();
assert enclosingElement == null :
"As spec for getEnclosingElement() says, enclosing element must be null for type parameter";
}
}
We get:
java.lang.AssertionError: As spec for getEnclosingElement() says, enclosing element must be null for type parameter
Please don't forget to give -ea option to VM to enable asserts.
If parametrized class below
class MyClass<T> { }
is given to annotation processor that checks spec assertion:
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class MyProcessor extends AbstractProcessor {
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Set<? extends Element> rootElements = roundEnv.getRootElements();
if (rootElements.size() > 0) {
performCheck(rootElements);
}
return true;
}
private void performCheck(Set<? extends Element> rootElements) {
TypeElement typeElement = ElementFilter.typesIn(rootElements).iterator().next();
TypeParameterElement typeParameterElement = typeElement.getTypeParameters().get(0);
// Element getEnclosingElement():
// If this is a type parameter, {@code null} is returned.
Element enclosingElement = typeParameterElement.getEnclosingElement();
assert enclosingElement == null :
"As spec for getEnclosingElement() says, enclosing element must be null for type parameter";
}
}
We get:
java.lang.AssertionError: As spec for getEnclosingElement() says, enclosing element must be null for type parameter
Please don't forget to give -ea option to VM to enable asserts.
- backported by
-
JDK-2187689 javax.lang.model.element.Element.getEnclosingElement() doesn't return null for type parameter
-
- Closed
-
- relates to
-
JDK-7026868 Apply specification changes for 6505047
-
- Closed
-