use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project error-prone by google.
the class FieldMissingNullable method findDeclaration.
@Nullable
private VariableTree findDeclaration(VisitorState state, Symbol field) {
JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context);
TreePath fieldDeclPath = Trees.instance(javacEnv).getPath(field);
// Skip fields declared in other compilation units since we can't make a fix for them here.
if (fieldDeclPath != null && fieldDeclPath.getCompilationUnit() == state.getPath().getCompilationUnit() && (fieldDeclPath.getLeaf() instanceof VariableTree)) {
return (VariableTree) fieldDeclPath.getLeaf();
}
return null;
}
use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project error-prone by google.
the class ParameterNotNullable method findDeclaration.
@Nullable
private VariableTree findDeclaration(VisitorState state, Symbol parameter) {
JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context);
TreePath declPath = Trees.instance(javacEnv).getPath(parameter);
if (declPath != null && declPath.getCompilationUnit() == state.getPath().getCompilationUnit() && (declPath.getLeaf() instanceof VariableTree)) {
return (VariableTree) declPath.getLeaf();
}
return null;
}
use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project bazel by bazelbuild.
the class InternalUtils method greatestLowerBound.
/**
* Returns the greatest lower bound of two {@link TypeMirror}s.
*
* @param processingEnv The {@link ProcessingEnvironment} to use.
* @param tm1 A {@link TypeMirror}.
* @param tm2 A {@link TypeMirror}.
* @return The greatest lower bound of {@code tm1} and {@code tm2}.
*/
public static TypeMirror greatestLowerBound(ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) {
Type t1 = (Type) tm1;
Type t2 = (Type) tm2;
JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
Types types = Types.instance(javacEnv.getContext());
if (types.isSameType(t1, t2)) {
// Special case if the two types are equal.
return t1;
}
// Handle the 'null' type manually.
if (t1.getKind() == TypeKind.NULL) {
return t1;
}
if (t2.getKind() == TypeKind.NULL) {
return t2;
}
// Special case for primitives.
if (TypesUtils.isPrimitive(t1) || TypesUtils.isPrimitive(t2)) {
if (types.isAssignable(t1, t2)) {
return t1;
} else if (types.isAssignable(t2, t1)) {
return t2;
} else {
// instead.
return processingEnv.getTypeUtils().getNoType(TypeKind.NONE);
}
}
if (t1.getKind() == TypeKind.WILDCARD) {
return t2;
}
if (t2.getKind() == TypeKind.WILDCARD) {
return t1;
}
return types.glb(t1, t2);
}
use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project bazel by bazelbuild.
the class TypesUtils method wildUpperBound.
// Version of com.sun.tools.javac.code.Types.wildUpperBound(Type)
// that works with both jdk8 (called upperBound there) and jdk8u.
// TODO: contrast to upperBound.
public static Type wildUpperBound(ProcessingEnvironment env, TypeMirror tm) {
Type t = (Type) tm;
if (t.hasTag(TypeTag.WILDCARD)) {
Context context = ((JavacProcessingEnvironment) env).getContext();
Type.WildcardType w = (Type.WildcardType) TypeAnnotationUtils.unannotatedType(t);
if (w.isSuperBound()) {
Symtab syms = Symtab.instance(context);
return w.bound == null ? syms.objectType : w.bound.bound;
} else {
return wildUpperBound(env, w.type);
}
} else {
return TypeAnnotationUtils.unannotatedType(t);
}
}
use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project error-prone by google.
the class RestrictedApiChecker method anyAnnotation.
private static Matcher<Tree> anyAnnotation(List<? extends TypeMirror> mirrors, VisitorState state) {
JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context);
ArrayList<Matcher<Tree>> matchers = new ArrayList<>(mirrors.size());
for (TypeMirror mirror : mirrors) {
TypeElement typeElem = (TypeElement) javacEnv.getTypeUtils().asElement(mirror);
String name = mirror.toString();
if (typeElem != null) {
// Get the binary name if possible ($ to separate nested members). See b/36160747
name = javacEnv.getElementUtils().getBinaryName(typeElem).toString();
}
matchers.add(Matchers.hasAnnotation(name));
}
return Matchers.anyOf(matchers);
}
Aggregations