use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class StaticQualifiedUsingExpression method matchMemberSelect.
@Override
public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) {
if (!staticAccessedFromInstanceMatcher.matches(tree, state)) {
return Description.NO_MATCH;
}
// Is the static member being accessed a method or a variable?
Symbol staticMemberSym = ASTHelpers.getSymbol(tree);
if (staticMemberSym == null) {
return Description.NO_MATCH;
}
boolean isMethod = staticMemberSym instanceof MethodSymbol;
// Is the static member defined in this class?
Symbol ownerSym = staticMemberSym.owner;
Symbol whereAccessedSym = ASTHelpers.getSymbol(ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), ClassTree.class));
if (!(ownerSym instanceof ClassSymbol && whereAccessedSym instanceof ClassSymbol)) {
return Description.NO_MATCH;
}
boolean staticMemberDefinedHere = whereAccessedSym.equals(ownerSym);
SuggestedFix.Builder fix = SuggestedFix.builder();
String replacement;
if (staticMemberDefinedHere && isMethod) {
// If the static member is defined in the enclosing class and the member is a method, then
// just use the bare method name. Don't do this for fields, because they may share a simple
// name with a local in the same scope.
// TODO(eaftan): If we had access to name resolution info, we could do this in all applicable
// cases. Investigate Scope.Entry for this.
replacement = tree.getIdentifier().toString();
} else {
// Replace the operand of the field access expression with the simple name of the class.
replacement = ownerSym.getSimpleName() + "." + tree.getIdentifier();
// Don't import implicitly imported packages (java.lang.* and current package).
// TODO(cushon): move this logic into addImport?
Symbol packageSym = ownerSym.packge();
if (!packageSym.toString().equals("java.lang") && !packageSym.equals(whereAccessedSym.packge())) {
fix.addImport(ownerSym.toString());
}
}
fix.replace(tree, replacement);
// Compute strings to interpolate into diagnostic message.
String memberName = staticMemberSym.getSimpleName().toString();
String methodOrVariable = isMethod ? "method" : "variable";
String customDiagnosticMessage = String.format(MESSAGE_TEMPLATE, methodOrVariable, memberName, replacement);
return buildDescription(tree).setMessage(customDiagnosticMessage).addFix(fix.build()).build();
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class DoubleCheckedLocking method handleLocal.
/**
* Report a diagnostic for an instance of DCL on a local variable. A match is only reported
* if a non-volatile field is written to the variable after acquiring the lock and before
* the second null-check on the local.
*
* <p>e.g.
*
* <pre>
* {@code
* if ($X == null) {
* synchronized (...) {
* $X = myNonVolatileField;
* if ($X == null) {
* ...
* }
* ...
* }
* }
* }
* </pre>
*/
private Description handleLocal(DCLInfo info, VisitorState state) {
JCExpressionStatement expr = getChild(info.synchTree().getBlock(), JCExpressionStatement.class);
if (expr.getStartPosition() > ((JCTree) info.innerIf()).getStartPosition()) {
return Description.NO_MATCH;
}
if (!(expr.getExpression() instanceof JCAssign)) {
return Description.NO_MATCH;
}
JCAssign assign = (JCAssign) expr.getExpression();
if (!Objects.equals(ASTHelpers.getSymbol(assign.getVariable()), info.sym())) {
return Description.NO_MATCH;
}
Symbol sym = ASTHelpers.getSymbol(assign.getExpression());
if (!(sym instanceof VarSymbol)) {
return Description.NO_MATCH;
}
VarSymbol fvar = (VarSymbol) sym;
if (fvar.getKind() != ElementKind.FIELD) {
return Description.NO_MATCH;
}
return handleField(info.outerIf(), fvar, state);
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class GuardedByChecker method isRWLock.
/**
* Returns true if the lock expression corresponds to a
* {@code java.util.concurrent.locks.ReadWriteLock}.
*/
private static boolean isRWLock(GuardedByExpression guard, VisitorState state) {
Type guardType = guard.type();
if (guardType == null) {
return false;
}
Symbol rwLockSymbol = state.getSymbolFromString(JUC_READ_WRITE_LOCK);
if (rwLockSymbol == null) {
return false;
}
return state.getTypes().isSubtype(guardType, rwLockSymbol.type);
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class GuardedBySymbolResolver method resolveType.
/**
* Resolves a simple name as a type. Considers super classes, lexically enclosing classes, and
* then arbitrary types available in the current environment.
*/
private Symbol resolveType(String name, SearchSuperTypes searchSuperTypes) {
Symbol type = null;
if (searchSuperTypes == SearchSuperTypes.YES) {
type = getSuperType(enclosingClass, name);
}
if (enclosingClass.getSimpleName().contentEquals(name)) {
type = enclosingClass;
}
if (type == null) {
type = getLexicallyEnclosing(enclosingClass, name);
}
if (type == null) {
type = attribIdent(name);
}
checkGuardedBy(!(type instanceof Symbol.PackageSymbol), "All we could find for '%s' was a package symbol.", name);
return type;
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class GuardedBySymbolResolver method resolveIdentifier.
@Override
public Symbol resolveIdentifier(IdentifierTree node) {
String name = node.getName().toString();
if (name.equals("this")) {
return enclosingClass;
}
// isn't legal java.
if (name.equals("itself")) {
Symbol sym = ASTHelpers.getSymbol(decl);
if (sym == null) {
throw new IllegalGuardedBy(decl.getClass().toString());
}
return sym;
}
Symbol.VarSymbol field = getField(enclosingClass, name);
if (field != null) {
return field;
}
Symbol type = resolveType(name, SearchSuperTypes.YES);
if (type != null) {
return type;
}
throw new IllegalGuardedBy(name);
}
Aggregations