framework.src.org.checkerframework.qualframework.base.TypeAnnotatorAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of checker Show documentation
Show all versions of checker Show documentation
The Checker Framework enhances Java’s type system to
make it more powerful and useful. This lets software developers
detect and prevent errors in their Java programs.
The Checker Framework includes compiler plug-ins ("checkers")
that find bugs or verify their absence. It also permits you to
write your own compiler plug-ins.
package org.checkerframework.qualframework.base;
import org.checkerframework.framework.type.AnnotatedTypeMirror;
import org.checkerframework.framework.type.typeannotator.ImplicitsTypeAnnotator;
import org.checkerframework.qualframework.qual.QualifierKey;
import org.checkerframework.qualframework.util.ExtendedTypeMirror;
import org.checkerframework.qualframework.util.WrappedAnnotatedTypeMirror;
/**
* Adapter for {@link TypeAnnotator}, extending
* {@link org.checkerframework.framework.type.typeannotator.TypeAnnotator org.checkerframework.framework.type.ImplicitsTypeAnnotator}.
*/
class TypeAnnotatorAdapter extends ImplicitsTypeAnnotator {
private final TypeAnnotator underlying;
private final TypeMirrorConverter converter;
public TypeAnnotatorAdapter(TypeAnnotator underlying,
TypeMirrorConverter converter,
QualifiedTypeFactoryAdapter factoryAdapter) {
super(factoryAdapter);
this.underlying = underlying;
this.converter = converter;
}
/**
* Return the qualifier indicated by the @QualifierKey
annotation on an
* {@link ExtendedTypeMirror}, or null
if there is no such
* annotation. The default {@link TypeAnnotator} implementation uses this
* method to avoid re-processing a type more than once, which will likely
* produce wrong results (since {@link TypeMirrorConverter} removes the
* existing annotations when it adds the @QualifierKey
).
*/
public Q getExistingQualifier(ExtendedTypeMirror type) {
if (type instanceof WrappedAnnotatedTypeMirror) {
AnnotatedTypeMirror atm = ((WrappedAnnotatedTypeMirror)type).unwrap();
if (atm.hasAnnotation(QualifierKey.class)) {
return converter.getQualifier(atm);
}
}
return null;
}
@Override
protected Void scan(AnnotatedTypeMirror atm, Void p) {
// Produce a qualified version of the ATM.
WrappedAnnotatedTypeMirror watm = WrappedAnnotatedTypeMirror.wrap(atm);
QualifiedTypeMirror qtm = underlying.visit(watm, null);
// Update the input ATM with the new qualifiers.
converter.applyQualifiers(qtm, atm);
return null;
}
}