use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.
the class FindIdentifiers method inStaticContext.
/** Returns true iff the leaf node of the {@code path} occurs in a JLS 8.3.1 static context. */
private static boolean inStaticContext(TreePath path) {
Tree prev = path.getLeaf();
path = path.getParentPath();
ClassSymbol enclosingClass = ASTHelpers.getSymbol(ASTHelpers.findEnclosingNode(path, ClassTree.class));
ClassSymbol directSuperClass = (ClassSymbol) enclosingClass.getSuperclass().tsym;
for (Tree tree : path) {
switch(tree.getKind()) {
case METHOD:
return ASTHelpers.getSymbol(tree).isStatic();
case // static initializer
BLOCK:
if (((BlockTree) tree).isStatic()) {
return true;
}
break;
case // variable initializer of static variable
VARIABLE:
VariableTree variableTree = (VariableTree) tree;
VarSymbol variableSym = ASTHelpers.getSymbol(variableTree);
if (variableSym.getKind() == ElementKind.FIELD) {
return Objects.equals(variableTree.getInitializer(), prev) && variableSym.isStatic();
}
break;
case // JLS 8.8.7.1 explicit constructor invocation
METHOD_INVOCATION:
MethodSymbol methodSym = ASTHelpers.getSymbol((MethodInvocationTree) tree);
if (methodSym == null) {
// visibility)
return true;
}
if (methodSym.isConstructor() && (Objects.equals(methodSym.owner, enclosingClass) || Objects.equals(methodSym.owner, directSuperClass))) {
return true;
}
break;
default:
break;
}
prev = tree;
}
return false;
}
use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.
the class FindIdentifiers method findUnusedIdentifiers.
/**
* Finds all variable declarations which are unused at this point in the AST (i.e. they might be
* used further on).
*/
public static ImmutableSet<VarSymbol> findUnusedIdentifiers(VisitorState state) {
ImmutableSet.Builder<VarSymbol> definedVariables = ImmutableSet.builder();
ImmutableSet.Builder<Symbol> usedSymbols = ImmutableSet.builder();
Tree prev = state.getPath().getLeaf();
for (Tree curr : state.getPath().getParentPath()) {
createFindIdentifiersScanner(usedSymbols, prev).scan(curr, null);
switch(curr.getKind()) {
case BLOCK:
// If we see a block then walk over each statement to see if it defines a variable
for (StatementTree statement : ((BlockTree) curr).getStatements()) {
if (statement.equals(prev)) {
// declared/used before us in the tree
break;
}
addIfVariable(statement, definedVariables);
}
break;
case FOR_LOOP:
ForLoopTree forLoop = (ForLoopTree) curr;
forLoop.getInitializer().stream().forEach(t -> addIfVariable(t, definedVariables));
break;
case ENHANCED_FOR_LOOP:
EnhancedForLoopTree enhancedFor = (EnhancedForLoopTree) curr;
addIfVariable(enhancedFor.getVariable(), definedVariables);
break;
default:
break;
}
prev = curr;
}
return ImmutableSet.copyOf(Sets.difference(definedVariables.build(), usedSymbols.build()));
}
use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.
the class Matchers method selectedIsInstance.
/**
* Returns true if the expression is a member access on an instance, rather than a static type.
* Supports member method invocations and field accesses.
*/
public static Matcher<ExpressionTree> selectedIsInstance() {
return new Matcher<ExpressionTree>() {
@Override
public boolean matches(ExpressionTree expr, VisitorState state) {
if (!(expr instanceof JCFieldAccess)) {
// TODO(cushon): throw IllegalArgumentException?
return false;
}
JCExpression selected = ((JCFieldAccess) expr).getExpression();
if (selected instanceof JCNewClass) {
return true;
}
Symbol sym = ASTHelpers.getSymbol(selected);
return sym instanceof VarSymbol;
}
};
}
use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.
the class TryFailThrowable method hasInitialStringParameter.
private static boolean hasInitialStringParameter(MethodSymbol sym, VisitorState state) {
Types types = state.getTypes();
List<VarSymbol> parameters = sym.getParameters();
return !parameters.isEmpty() && types.isSameType(parameters.get(0).type, state.getSymtab().stringType);
}
use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.
the class TypeParameterUnusedInFormals method matchMethod.
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
MethodSymbol methodSymbol = ASTHelpers.getSymbol(tree);
if (methodSymbol == null) {
return Description.NO_MATCH;
}
// Only match methods where the return type is just a type parameter.
// e.g. the following is OK: <T> List<T> newArrayList();
TypeVar retType;
switch(methodSymbol.getReturnType().getKind()) {
case TYPEVAR:
retType = (TypeVar) methodSymbol.getReturnType();
break;
default:
return Description.NO_MATCH;
}
if (!methodSymbol.equals(retType.tsym.owner)) {
return Description.NO_MATCH;
}
// e.g.: <T extends Enum<T>> T unsafeEnumDeserializer();
if (retType.bound != null && TypeParameterFinder.visit(retType.bound).contains(retType.tsym)) {
return Description.NO_MATCH;
}
// e.g.: <T> T noop(T t);
for (VarSymbol formalParam : methodSymbol.getParameters()) {
if (TypeParameterFinder.visit(formalParam.type).contains(retType.tsym)) {
return Description.NO_MATCH;
}
}
return describeMatch(tree);
}
Aggregations