use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project checker-framework by typetools.
the class ElementUtils method getAllSupertypes.
/**
* Get all the supertypes of a given type, including the type itself. The result includes both
* superclasses and implemented interfaces.
*
* @param type a type
* @param env the processing environment
* @return list including the type and all its supertypes, with a guarantee that direct supertypes
* (i.e. those that appear in extends or implements clauses) appear before indirect supertypes
*/
public static List<TypeElement> getAllSupertypes(TypeElement type, ProcessingEnvironment env) {
Context ctx = ((JavacProcessingEnvironment) env).getContext();
com.sun.tools.javac.code.Types javacTypes = com.sun.tools.javac.code.Types.instance(ctx);
return CollectionsPlume.<Type, TypeElement>mapList(t -> (TypeElement) t.tsym, javacTypes.closure(((Symbol) type).type));
}
use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project checker-framework by typetools.
the class DefaultReflectionResolver method getMethodSymbolsfor.
/**
* Get set of MethodSymbols based on class name, method name, and parameter length.
*
* @param className the class that contains the method
* @param methodName the method's name
* @param paramLength the number of parameters
* @param env the environment
* @return the (potentially empty) set of corresponding method Symbol(s)
*/
private List<Symbol> getMethodSymbolsfor(String className, String methodName, int paramLength, Env<AttrContext> env) {
Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
Resolve resolve = Resolve.instance(context);
Names names = Names.instance(context);
Symbol sym = getSymbol(className, env, names, resolve);
if (!sym.exists()) {
debugReflection("Unable to resolve class: " + className);
return Collections.emptyList();
}
// The common case is probably that `result` is a singleton at method exit.
List<Symbol> result = new ArrayList<>();
ClassSymbol classSym = (ClassSymbol) sym;
while (classSym != null) {
for (Symbol s : classSym.getEnclosedElements()) {
// check all member methods
if (s.getKind() == ElementKind.METHOD) {
// Check for method name and number of arguments
if (names.fromString(methodName) == s.name && ((MethodSymbol) s).getParameters().size() == paramLength) {
result.add(s);
}
}
}
if (!result.isEmpty()) {
break;
}
Type t = classSym.getSuperclass();
if (!t.hasTag(TypeTag.CLASS) || t.isErroneous()) {
break;
}
classSym = (ClassSymbol) t.tsym;
}
if (result.isEmpty()) {
debugReflection("Unable to resolve method: " + className + "@" + methodName);
}
return result;
}
use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project checker-framework by typetools.
the class AnnotatedTypeFactory method getCheckerNames.
/**
* Returns the names of the annotation processors that are being run.
*
* @return the names of the annotation processors that are being run
*/
// ClassLoader.getResources returns an Enumeration
@SuppressWarnings("JdkObsolete")
public String[] getCheckerNames() {
com.sun.tools.javac.util.Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
String processorArg = Options.instance(context).get("-processor");
if (processorArg != null) {
return processorArg.split(",");
}
try {
String filename = "META-INF/services/javax.annotation.processing.Processor";
List<String> lines = new ArrayList<>();
Enumeration<URL> urls = getClass().getClassLoader().getResources(filename);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
lines.addAll(in.lines().collect(Collectors.toList()));
}
String[] result = lines.toArray(new String[0]);
return result;
} catch (IOException e) {
throw new BugInCF(e);
}
}
use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project checker-framework by typetools.
the class BoundsInitializer method createAndSetLowerBound.
/**
* Creates the lower bound type for {@code typeVar} and sets it. If the type variable does not
* have a lower bound, then a null type is created.
*
* @param typeVar type variable
* @return the newly created lower bound
*/
private static AnnotatedTypeMirror createAndSetLowerBound(AnnotatedTypeVariable typeVar) {
TypeMirror lb = typeVar.getUnderlyingType().getLowerBound();
if (lb == null) {
// Use bottom type to ensure there is a lower bound.
Context context = ((JavacProcessingEnvironment) typeVar.atypeFactory.processingEnv).getContext();
Symtab syms = Symtab.instance(context);
lb = syms.botType;
}
AnnotatedTypeMirror lowerBound = AnnotatedTypeMirror.createType(lb, typeVar.atypeFactory, false);
typeVar.setLowerBound(lowerBound);
return lowerBound;
}
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);
}
Aggregations