use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class SuggestedFixes method qualifyType.
/**
* Returns a human-friendly name of the given {@link Symbol.TypeSymbol} for use in fixes.
*
* <ul>
* <li>If the type is already imported, its simple name is used.
* <li>If an enclosing type is imported, that enclosing type is used as a qualified.
* <li>Otherwise the outermost enclosing type is imported and used as a qualifier.
* </ul>
*/
public static String qualifyType(TreeMaker make, SuggestedFix.Builder fix, Symbol.TypeSymbol sym) {
// let javac figure out whether the type is already imported
JCTree.JCExpression qual = make.QualIdent(sym);
if (!selectsPackage(qual)) {
return qual.toString();
}
Deque<String> names = new ArrayDeque<>();
Symbol curr = sym;
while (true) {
names.addFirst(curr.getSimpleName().toString());
if (curr.owner == null || curr.owner.getKind() == ElementKind.PACKAGE) {
break;
}
curr = curr.owner;
}
fix.addImport(curr.toString());
return Joiner.on('.').join(names);
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class SuggestedFixes method qualifyDocReference.
/**
* Fully qualifies a javadoc reference, e.g. for replacing {@code {@link List}} with
* {@code {@link java.util.List}}
*
* @param fix the fix builder to add to
* @param docPath the path to a {@link DCTree.DCReference} element
*/
public static void qualifyDocReference(SuggestedFix.Builder fix, DocTreePath docPath, VisitorState state) {
DocTree leaf = docPath.getLeaf();
checkArgument(leaf.getKind() == DocTree.Kind.REFERENCE, "expected a path to a reference, got %s instead", leaf.getKind());
DCTree.DCReference reference = (DCTree.DCReference) leaf;
Symbol sym = (Symbol) JavacTrees.instance(state.context).getElement(docPath);
if (sym == null) {
return;
}
String refString = reference.toString();
String qualifiedName;
int idx = refString.indexOf('#');
if (idx >= 0) {
qualifiedName = sym.owner.getQualifiedName() + refString.substring(idx, refString.length());
} else {
qualifiedName = sym.getQualifiedName().toString();
}
replaceDocTree(fix, docPath, qualifiedName);
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class ConstructorMatcherImpl method getConstructor.
private static MethodSymbol getConstructor(ExpressionTree tree) {
switch(tree.getKind()) {
case NEW_CLASS:
case METHOD_INVOCATION:
break;
default:
return null;
}
Symbol sym = ASTHelpers.getSymbol(tree);
if (!(sym instanceof MethodSymbol)) {
return null;
}
MethodSymbol method = (MethodSymbol) sym;
if (!method.isConstructor()) {
return null;
}
return method;
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class Scanner method updateSuppressions.
/**
* Updates current suppression state with information for the given {@code tree}. Returns
* the previous suppression state so that it can be restored when going up the tree.
*/
private SuppressionHelper.SuppressionInfo updateSuppressions(Tree tree, VisitorState state) {
SuppressionHelper.SuppressionInfo prevSuppressionInfo = new SuppressionHelper.SuppressionInfo(suppressions, customSuppressions, inGeneratedCode);
initSuppressionHelper(state);
Symbol sym = ASTHelpers.getSymbol(tree);
if (sym != null) {
SuppressionHelper.SuppressionInfo newSuppressions = suppressionHelper.extendSuppressionSets(sym, state.getSymtab().suppressWarningsType, suppressions, customSuppressions, inGeneratedCode, state);
if (newSuppressions.suppressWarningsStrings != null) {
suppressions = newSuppressions.suppressWarningsStrings;
}
if (newSuppressions.customSuppressions != null) {
customSuppressions = newSuppressions.customSuppressions;
}
inGeneratedCode = newSuppressions.inGeneratedCode;
}
return prevSuppressionInfo;
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class ASTHelpers method findMatchingMethods.
/**
* Finds all methods in any superclass of {@code startClass} with a certain {@code name} that
* match the given {@code predicate}.
*
* @return The (possibly empty) set of methods in any superclass that match {@code predicate} and
* have the given {@code name}.
*/
public static Set<MethodSymbol> findMatchingMethods(Name name, final Predicate<MethodSymbol> predicate, Type startClass, Types types) {
Filter<Symbol> matchesMethodPredicate = sym -> sym instanceof MethodSymbol && predicate.apply((MethodSymbol) sym);
Set<MethodSymbol> matchingMethods = new HashSet<>();
// Iterate over all classes and interfaces that startClass inherits from.
for (Type superClass : types.closure(startClass)) {
// Iterate over all the methods declared in superClass.
TypeSymbol superClassSymbol = superClass.tsym;
Scope superClassSymbols = superClassSymbol.members();
if (superClassSymbols != null) {
// Can be null if superClass is a type variable
for (Symbol symbol : superClassSymbols.getSymbolsByName(name, matchesMethodPredicate, NON_RECURSIVE)) {
// By definition of the filter, we know that the symbol is a MethodSymbol.
matchingMethods.add((MethodSymbol) symbol);
}
}
}
return matchingMethods;
}
Aggregations