use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class GuardedBySymbolResolver method resolveTypeLiteral.
@Override
public Symbol resolveTypeLiteral(ExpressionTree expr) {
checkGuardedBy(expr instanceof IdentifierTree, "bad type literal: %s", expr);
IdentifierTree ident = (IdentifierTree) expr;
Symbol type = resolveType(ident.getName().toString(), SearchSuperTypes.YES);
if (type instanceof Symbol.ClassSymbol) {
return type;
}
return null;
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class ImmutableAnalysis method areFieldsImmutable.
/**
* Check a single class' fields for immutability.
*
* @param immutableTyParams the in-scope immutable type parameters
* @param classType the type to check the fields of
*/
Violation areFieldsImmutable(Optional<ClassTree> tree, ImmutableSet<String> immutableTyParams, ClassType classType) {
ClassSymbol classSym = (ClassSymbol) classType.tsym;
if (classSym.members() == null) {
return Violation.absent();
}
Filter<Symbol> instanceFieldFilter = new Filter<Symbol>() {
@Override
public boolean accepts(Symbol symbol) {
return symbol.getKind() == ElementKind.FIELD && !symbol.isStatic();
}
};
Map<Symbol, Tree> declarations = new HashMap<>();
if (tree.isPresent()) {
for (Tree member : tree.get().getMembers()) {
Symbol sym = ASTHelpers.getSymbol(member);
if (sym != null) {
declarations.put(sym, member);
}
}
}
// javac gives us members in reverse declaration order
// handling them in declaration order leads to marginally better diagnostics
List<Symbol> members = ImmutableList.copyOf(classSym.members().getSymbols(instanceFieldFilter)).reverse();
for (Symbol member : members) {
Optional<Tree> memberTree = Optional.fromNullable(declarations.get(member));
Violation info = isFieldImmutable(memberTree, immutableTyParams, classSym, classType, (VarSymbol) member);
if (info.isPresent()) {
return info;
}
}
return Violation.absent();
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class WildcardImport method qualifiedNameFix.
/**
* Add an import for {@code owner}, and qualify all on demand imported references to members of
* owner by owner's simple name.
*/
private static void qualifiedNameFix(final SuggestedFix.Builder fix, final Symbol owner, VisitorState state) {
fix.addImport(owner.getQualifiedName().toString());
final JCCompilationUnit unit = (JCCompilationUnit) state.getPath().getCompilationUnit();
new TreePathScanner<Void, Void>() {
@Override
public Void visitIdentifier(IdentifierTree tree, Void unused) {
Symbol sym = ASTHelpers.getSymbol(tree);
if (sym == null) {
return null;
}
Tree parent = getCurrentPath().getParentPath().getLeaf();
if (parent.getKind() == Tree.Kind.CASE && ((CaseTree) parent).getExpression().equals(tree) && sym.owner.getKind() == ElementKind.ENUM) {
// switch cases can refer to enum constants by simple name without importing them
return null;
}
if (sym.owner.equals(owner) && unit.starImportScope.includes(sym)) {
fix.prefixWith(tree, owner.getSimpleName() + ".");
}
return null;
}
}.scan(unit, null);
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class WrongParameterPackage method matchMethod.
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
MethodSymbol method = ASTHelpers.getSymbol(tree);
if (method == null) {
return Description.NO_MATCH;
}
ClassSymbol classSym = method.enclClass();
if (classSym == null) {
return Description.NO_MATCH;
}
TypeSymbol superClass = classSym.getSuperclass().tsym;
if (superClass == null) {
return Description.NO_MATCH;
}
for (Symbol s : superClass.members().getSymbols()) {
if (s.name.contentEquals(method.name) && s.getKind() == ElementKind.METHOD) {
MethodSymbol supermethod = (MethodSymbol) s;
// if this method actually overrides the supermethod, then it's correct and not a match.
if (method.overrides(supermethod, superClass, state.getTypes(), true)) {
return Description.NO_MATCH;
}
// if this doesn't have the right number of parameters, look at other ones.
if (supermethod.params().size() != method.params().size()) {
continue;
}
for (int x = 0; x < method.params().size(); x++) {
Type methodParamType = method.params().get(x).type;
Type supermethodParamType = supermethod.params().get(x).type;
if (methodParamType.tsym.name.contentEquals(supermethodParamType.tsym.name) && !state.getTypes().isSameType(methodParamType, supermethodParamType)) {
this.supermethod = supermethod;
return describe(tree, state);
}
}
}
}
return Description.NO_MATCH;
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class ASTHelpersTest method testHasDirectAnnotationWithSimpleName.
@Test
public void testHasDirectAnnotationWithSimpleName() {
writeFile(//
"A.java", "public class A {", " @Deprecated public void doIt() {}", "}");
TestScanner scanner = new TestScanner() {
@Override
public Void visitMethod(MethodTree tree, VisitorState state) {
if (tree.getName().contentEquals("doIt")) {
setAssertionsComplete();
Symbol sym = ASTHelpers.getSymbol(tree);
assertThat(ASTHelpers.hasDirectAnnotationWithSimpleName(sym, "Deprecated")).isTrue();
assertThat(ASTHelpers.hasDirectAnnotationWithSimpleName(sym, "Nullable")).isFalse();
}
return super.visitMethod(tree, state);
}
};
tests.add(scanner);
assertCompiles(scanner);
}
Aggregations