use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.
the class LiteralClassName method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!CLASS_NAME.matches(tree, state)) {
return NO_MATCH;
}
String className = constValue(getOnlyElement(tree.getArguments()), String.class);
if (className == null) {
return NO_MATCH;
}
if (className.startsWith("[")) {
// TODO(cushon): consider handling arrays
return NO_MATCH;
}
Type type = state.getTypeFromString(className);
if (type == null) {
return NO_MATCH;
}
ClassSymbol owner = getSymbol(state.findEnclosing(ClassTree.class));
Enter enter = Enter.instance(state.context);
if (!Resolve.instance(state.context).isAccessible(enter.getEnv(owner), type.tsym)) {
return NO_MATCH;
}
SuggestedFix.Builder fix = SuggestedFix.builder();
String replaceWith = String.format("%s.class", qualifyType(state, fix, state.getTypes().erasure(type)));
if (state.getPath().getParentPath().getLeaf().getKind() == Tree.Kind.EXPRESSION_STATEMENT) {
fix.addStaticImport("java.util.Objects.requireNonNull");
replaceWith = String.format("requireNonNull(%s)", replaceWith);
}
fix.replace(tree, replaceWith);
return describeMatch(tree, fix.build());
}
use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.
the class HeldLockAnalyzer method handleMonitorGuards.
private static HeldLockSet handleMonitorGuards(VisitorState state, HeldLockSet locks) {
JCNewClass newClassTree = ASTHelpers.findEnclosingNode(state.getPath(), JCNewClass.class);
if (newClassTree == null) {
return locks;
}
Symbol clazzSym = ASTHelpers.getSymbol(newClassTree.clazz);
if (!(clazzSym instanceof ClassSymbol)) {
return locks;
}
if (!((ClassSymbol) clazzSym).fullname.contentEquals(MONITOR_GUARD_CLASS)) {
return locks;
}
Optional<GuardedByExpression> lockExpression = GuardedByBinder.bindExpression(Iterables.getOnlyElement(newClassTree.getArguments()), state);
if (!lockExpression.isPresent()) {
return locks;
}
return locks.plus(lockExpression.get());
}
use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.
the class ImmutableAnnotationChecker method matchClass.
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
ClassSymbol symbol = getSymbol(tree);
if (symbol == null || symbol.isAnnotationType() || !WellKnownMutability.isAnnotation(state, symbol.type)) {
return NO_MATCH;
}
if (ASTHelpers.hasAnnotation(symbol, Immutable.class, state)) {
AnnotationTree annotation = ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Immutable");
if (annotation != null) {
state.reportMatch(buildDescription(annotation).setMessage(ANNOTATED_ANNOTATION_MESSAGE).addFix(SuggestedFix.delete(annotation)).build());
} else {
state.reportMatch(buildDescription(tree).setMessage(ANNOTATED_ANNOTATION_MESSAGE).build());
}
}
Violation info = new ImmutableAnalysis(this, state, "annotations should be immutable, and cannot have non-final fields", "annotations should be immutable").checkForImmutability(Optional.of(tree), ImmutableSet.of(), getType(tree));
if (!info.isPresent()) {
return NO_MATCH;
}
String message = "annotations should be immutable: " + info.message();
return buildDescription(tree).setMessage(message).build();
}
use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.
the class ImmutableChecker method checkSubtype.
// Strong behavioural subtyping
/** Check for classes without {@code @Immutable} that have immutable supertypes. */
private Description checkSubtype(ClassTree tree, VisitorState state) {
ClassSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null) {
return Description.NO_MATCH;
}
Type superType = immutableSupertype(sym, state);
if (superType == null) {
return Description.NO_MATCH;
}
String message = String.format("Class extends @Immutable type %s, but is not annotated as immutable", superType);
Fix fix = SuggestedFix.builder().prefixWith(tree, "@Immutable ").addImport(Immutable.class.getName()).build();
return buildDescription(tree).setMessage(message).addFix(fix).build();
}
use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.
the class UTemplater method visitIdentifier.
@Override
public UExpression visitIdentifier(IdentifierTree tree, Void v) {
Symbol sym = ASTHelpers.getSymbol(tree);
if (sym instanceof ClassSymbol) {
return UClassIdent.create((ClassSymbol) sym);
} else if (sym != null && sym.isStatic()) {
return staticMember(sym);
} else if (freeVariables.containsKey(tree.getName().toString())) {
VarSymbol symbol = freeVariables.get(tree.getName().toString());
checkState(symbol == sym);
UExpression ident = UFreeIdent.create(tree.getName());
Matches matches = ASTHelpers.getAnnotation(symbol, Matches.class);
if (matches != null) {
ident = UMatches.create(getValue(matches), true, ident);
}
NotMatches notMatches = ASTHelpers.getAnnotation(symbol, NotMatches.class);
if (notMatches != null) {
ident = UMatches.create(getValue(notMatches), false, ident);
}
OfKind hasKind = ASTHelpers.getAnnotation(symbol, OfKind.class);
if (hasKind != null) {
EnumSet<Kind> allowed = EnumSet.copyOf(Arrays.asList(hasKind.value()));
ident = UOfKind.create(ident, ImmutableSet.copyOf(allowed));
}
// @Repeated annotations need to be checked last.
Repeated repeated = ASTHelpers.getAnnotation(symbol, Repeated.class);
if (repeated != null) {
ident = URepeated.create(tree.getName(), ident);
}
return ident;
}
if (sym == null) {
return UTypeVarIdent.create(tree.getName());
}
switch(sym.getKind()) {
case TYPE_PARAMETER:
return UTypeVarIdent.create(tree.getName());
default:
return ULocalVarIdent.create(tree.getName());
}
}
Aggregations