use of com.sun.tools.javac.code.Symbol.TypeVariableSymbol in project error-prone by google.
the class Inliner method inlineAsVar.
public TypeVar inlineAsVar(UTypeVar var) throws CouldNotResolveImportException {
/*
* In order to handle recursively bounded type variables without a stack overflow,
* we first cache a type var with no bounds, then we inline the bounds.
*/
TypeVar typeVar = typeVarCache.get(var.getName());
if (typeVar != null) {
return typeVar;
}
Name name = asName(var.getName());
TypeSymbol sym = new TypeVariableSymbol(0, name, null, symtab().noSymbol);
typeVar = new TypeVar(sym, null, null);
sym.type = typeVar;
typeVarCache.put(var.getName(), typeVar);
// Any recursive uses of var will point to the same TypeVar object generated above.
typeVar.bound = var.getUpperBound().inline(this);
typeVar.lower = var.getLowerBound().inline(this);
return typeVar;
}
use of com.sun.tools.javac.code.Symbol.TypeVariableSymbol in project error-prone by google.
the class TypeParameterShadowing method findDuplicatesOf.
private Description findDuplicatesOf(Tree tree, List<? extends TypeParameterTree> typeParameters, VisitorState state) {
Symbol symbol = ASTHelpers.getSymbol(tree);
if (symbol == null) {
return Description.NO_MATCH;
}
List<TypeVariableSymbol> enclosingTypeSymbols = typeVariablesEnclosing(symbol);
if (enclosingTypeSymbols.isEmpty()) {
return Description.NO_MATCH;
}
List<TypeVariableSymbol> conflictingTypeSymbols = new ArrayList<>();
typeParameters.forEach(param -> enclosingTypeSymbols.stream().filter(tvs -> tvs.name.contentEquals(param.getName())).findFirst().ifPresent(conflictingTypeSymbols::add));
if (conflictingTypeSymbols.isEmpty()) {
return Description.NO_MATCH;
}
Description.Builder descriptionBuilder = buildDescription(tree);
String message = "Found aliased type parameters: " + conflictingTypeSymbols.stream().map(tvs -> tvs.name + " declared in " + tvs.owner.getSimpleName()).collect(Collectors.joining("\n"));
descriptionBuilder.setMessage(message);
Set<String> typeVarsInScope = Streams.concat(enclosingTypeSymbols.stream(), symbol.getTypeParameters().stream()).map(v -> v.name.toString()).collect(toImmutableSet());
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
conflictingTypeSymbols.stream().map(v -> renameTypeVariable(typeParameterInList(typeParameters, v), tree, replacementTypeVarName(v.name, typeVarsInScope), state)).forEach(fixBuilder::merge);
descriptionBuilder.addFix(fixBuilder.build());
return descriptionBuilder.build();
}
use of com.sun.tools.javac.code.Symbol.TypeVariableSymbol in project error-prone by google.
the class ImmutableChecker method matchClass.
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
ImmutableAnalysis analysis = new ImmutableAnalysis(this, state, wellKnownMutability);
if (tree.getSimpleName().length() == 0) {
// TODO(cushon): once Java 8 happens, require @Immutable on anonymous classes
return handleAnonymousClass(tree, state, analysis);
}
AnnotationInfo annotation = analysis.getImmutableAnnotation(tree, state);
if (annotation == null) {
// report an error if it extends/implements any @Immutable-annotated types.
return checkSubtype(tree, state);
}
// of the annotation are "trusted".
if (wellKnownMutability.getKnownImmutableClasses().containsValue(annotation)) {
return NO_MATCH;
}
// Check that the types in containerOf actually exist
Map<String, TypeVariableSymbol> typarams = new HashMap<>();
for (TypeParameterTree typaram : tree.getTypeParameters()) {
typarams.put(typaram.getName().toString(), (TypeVariableSymbol) ASTHelpers.getSymbol(typaram));
}
SetView<String> difference = Sets.difference(annotation.containerOf(), typarams.keySet());
if (!difference.isEmpty()) {
return buildDescription(tree).setMessage(String.format("could not find type(s) referenced by containerOf: %s", Joiner.on("', '").join(difference))).build();
}
ImmutableSet<String> immutableAndContainer = typarams.entrySet().stream().filter(e -> annotation.containerOf().contains(e.getKey()) && analysis.isImmutableTypeParameter(e.getValue())).map(Entry::getKey).collect(toImmutableSet());
if (!immutableAndContainer.isEmpty()) {
return buildDescription(tree).setMessage(String.format("using both @ImmutableTypeParameter and containerOf is redundant: %s", Joiner.on("', '").join(immutableAndContainer))).build();
}
// Main path for @Immutable-annotated types:
//
// Check that the fields (including inherited fields) are immutable, and
// validate the type hierarchy superclass.
ClassSymbol sym = ASTHelpers.getSymbol(tree);
Violation info = analysis.checkForImmutability(Optional.of(tree), immutableTypeParametersInScope(ASTHelpers.getSymbol(tree), state, analysis), ASTHelpers.getType(tree), (Tree matched, Violation violation) -> describeClass(matched, sym, annotation, violation));
if (!info.isPresent()) {
return NO_MATCH;
}
return describeClass(tree, sym, annotation, info).build();
}
Aggregations