use of com.sun.source.tree.TypeParameterTree in project error-prone by google.
the class TypeParameterShadowing method renameTypeVariable.
private static SuggestedFix renameTypeVariable(Tree sourceTree, List<? extends TypeParameterTree> typeParameters, Name typeVariable, String typeVarReplacement, VisitorState state) {
TypeParameterTree matchingTypeParam = typeParameters.stream().filter(t -> t.getName().contentEquals(typeVariable)).collect(MoreCollectors.onlyElement());
Symbol typeVariableSymbol = ASTHelpers.getSymbol(matchingTypeParam);
// replace only the type parameter name (and not any upper bounds)
String name = matchingTypeParam.getName().toString();
int pos = ((JCTree) matchingTypeParam).getStartPosition();
SuggestedFix.Builder fixBuilder = SuggestedFix.builder().replace(pos, pos + name.length(), typeVarReplacement);
((JCTree) sourceTree).accept(new TreeScanner() {
@Override
public void visitIdent(JCTree.JCIdent tree) {
Symbol identSym = ASTHelpers.getSymbol(tree);
if (Objects.equal(identSym, typeVariableSymbol)) {
// }
if (Objects.equal(state.getSourceForNode(tree), name)) {
fixBuilder.replace(tree, typeVarReplacement);
}
}
}
});
return fixBuilder.build();
}
use of com.sun.source.tree.TypeParameterTree 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, "@Immutable classes cannot have non-final fields", "@Immutable class has mutable field");
if (tree.getSimpleName().length() == 0) {
// TODO(cushon): once Java 8 happens, require @Immutable on anonymous classes
return handleAnonymousClass(tree, state, analysis);
}
ImmutableAnnotationInfo annotation = getImmutableAnnotation(tree);
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.KNOWN_IMMUTABLE.containsValue(annotation)) {
return Description.NO_MATCH;
}
// Check that the types in containerOf actually exist
Set<String> typarams = new HashSet<>();
for (TypeParameterTree typaram : tree.getTypeParameters()) {
typarams.add(typaram.getName().toString());
}
SetView<String> difference = Sets.difference(annotation.containerOf(), typarams);
if (!difference.isEmpty()) {
String message = String.format("could not find type(s) referenced by containerOf: %s", Joiner.on("', '").join(difference));
return buildDescription(tree).setMessage(message).build();
}
// Main path for @Immutable-annotated types:
//
// Check that the fields (including inherited fields) are immutable, and
// validate the type hierarchy superclass.
Violation info = analysis.checkForImmutability(Optional.of(tree), immutableTypeParametersInScope(ASTHelpers.getSymbol(tree)), ASTHelpers.getType(tree));
if (!info.isPresent()) {
return Description.NO_MATCH;
}
String message = "type annotated with @Immutable could not be proven immutable: " + info.message();
return buildDescription(tree).setMessage(message).build();
}
use of com.sun.source.tree.TypeParameterTree 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(tree, typeParameters, v.name, replacementTypeVarName(v.name, typeVarsInScope), state)).forEach(fixBuilder::merge);
descriptionBuilder.addFix(fixBuilder.build());
return descriptionBuilder.build();
}
Aggregations