use of com.sun.tools.javac.code.Symbol.ClassSymbol in project j2objc by google.
the class JavacEnvironment method resolve.
@Override
public Element resolve(String name) {
Name className = javacNames.fromString(name);
// Check first if compiler already created or loaded the class.
ClassSymbol symbol = symbolTable.classes.get(className);
if (symbol == null) {
// Not available, read it from a class file.
// Note: the enterName(Name) method moved from ClassReader to
// Symtab in Java 9. Reflection is used to support both locations.
symbol = enterClassJavac(className);
if (symbol != null) {
symbolTable.classes.put(className, symbol);
} else {
symbol = enterClassJavac9(className);
// The symbolTable is already updated in Java 9.
}
}
return symbol;
}
use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.
the class VisitorState method getTypeFromStringInternal.
private Type getTypeFromStringInternal(String typeStr) {
validateTypeStr(typeStr);
if (isPrimitiveType(typeStr)) {
return getPrimitiveType(typeStr);
}
Name typeName = getName(typeStr);
try {
ClassSymbol typeSymbol = getSymtab().classes.get(typeName);
if (typeSymbol == null) {
JavaCompiler compiler = JavaCompiler.instance(context);
Symbol sym = compiler.resolveIdent(typeStr);
if (!(sym instanceof ClassSymbol)) {
return null;
}
typeSymbol = (ClassSymbol) sym;
}
Type type = typeSymbol.asType();
// Throws CompletionFailure if the source/class file for this type is not available.
// This is hacky but the best way I can think of to handle this case.
type.complete();
if (type.isErroneous()) {
return null;
}
return type;
} catch (CompletionFailure failure) {
return null;
}
}
use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.
the class IncompatibleArgumentType method resolveTypeFromClass.
// class Foo<X> { void something(@CW("X") Object x); }
// new Foo<String>().something(123);
@Nullable
private RequiredType resolveTypeFromClass(Type calledType, ClassSymbol clazzSymbol, String typeArgName, VisitorState state) {
// Try on the class
int tyargIndex = findTypeArgInList(clazzSymbol, typeArgName);
if (tyargIndex != -1) {
return RequiredType.create(extractTypeArgAsMemberOfSupertype(calledType, clazzSymbol, tyargIndex, state.getTypes()));
}
while (clazzSymbol.isInner()) {
// class Foo<T> {
// class Bar {
// void something(@CW("T") Object o));
// }
// }
// new Foo<String>().new Bar().something(123); // should fail, 123 needs to match String
ClassSymbol encloser = clazzSymbol.owner.enclClass();
calledType = calledType.getEnclosingType();
tyargIndex = findTypeArgInList(encloser, typeArgName);
if (tyargIndex != -1) {
if (calledType.getTypeArguments().isEmpty()) {
// bar.something(123); // Nope (this call would be unchecked if arg was T)
return null;
}
return RequiredType.create(calledType.getTypeArguments().get(tyargIndex));
}
clazzSymbol = encloser;
}
return null;
}
use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.
the class CheckReturnValue method checkEnclosingClasses.
private static Optional<Boolean> checkEnclosingClasses(MethodSymbol method, VisitorState state) {
Symbol enclosingClass = enclosingClass(method);
while (enclosingClass instanceof ClassSymbol) {
Optional<Boolean> result = shouldCheckReturnValue(enclosingClass, state);
if (result.isPresent()) {
return result;
}
enclosingClass = enclosingClass.owner;
}
return Optional.absent();
}
use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.
the class FunctionalInterfaceClash method matchClass.
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
ClassSymbol origin = getSymbol(tree);
Types types = state.getTypes();
// collect declared and inherited methods whose signature contains a functional interface
Multimap<String, MethodSymbol> methods = HashMultimap.create();
for (Symbol sym : types.membersClosure(getType(tree), /*skipInterface=*/
false).getSymbols()) {
if (!(sym instanceof MethodSymbol)) {
continue;
}
if (isBugCheckerSuppressed((MethodSymbol) sym)) {
continue;
}
MethodSymbol msym = (MethodSymbol) sym;
if (msym.getParameters().stream().noneMatch(p -> maybeFunctionalInterface(p.type, types))) {
continue;
}
if (msym.isConstructor() && !msym.owner.equals(origin)) {
continue;
}
methods.put(functionalInterfaceSignature(state, msym), msym);
}
// (don't report clashes between inherited members)
for (Tree member : tree.getMembers()) {
if (!(member instanceof MethodTree)) {
continue;
}
MethodSymbol msym = getSymbol((MethodTree) member);
if (msym.getParameters().stream().noneMatch(p -> maybeFunctionalInterface(p.type, types))) {
continue;
}
Collection<MethodSymbol> clash = new ArrayList<>(methods.removeAll(functionalInterfaceSignature(state, msym)));
clash.remove(msym);
// ignore inherited methods that are overridden in the original class
clash.removeIf(m -> msym.overrides(m, origin, types, false));
if (!clash.isEmpty()) {
String message = "When passing lambda arguments to this function, callers will need a cast to" + " disambiguate with: " + clash.stream().map(m -> Signatures.prettyMethodSignature(origin, m)).collect(joining("\n "));
state.reportMatch(buildDescription(member).setMessage(message).build());
}
}
return NO_MATCH;
}
Aggregations