private Id elementToId(Element element, Class<? extends Annotation> annotation, int value) { JCTree tree = (JCTree) trees.getTree(element, getMirror(element, annotation)); if (tree != null) { // tree can be null if the references are compiled types and not source rScanner.reset(); tree.accept(rScanner); if (!rScanner.resourceIds.isEmpty()) { return rScanner.resourceIds.values().iterator().next(); } } return new Id(value); }
private Map<Integer, Id> elementToIds(Element element, Class<? extends Annotation> annotation, int[] values) { Map<Integer, Id> resourceIds = new LinkedHashMap<>(); JCTree tree = (JCTree) trees.getTree(element, getMirror(element, annotation)); if (tree != null) { // tree can be null if the references are compiled types and not source rScanner.reset(); tree.accept(rScanner); resourceIds = rScanner.resourceIds; } // Every value looked up should have an Id for (int value : values) { resourceIds.putIfAbsent(value, new Id(value)); } return resourceIds; }
@Override public Void visitExecutable(ExecutableElement e, Void p) { Tree tree = t.getTree(e); if (tree != null && tree.getKind() == Kind.METHOD) { JCMethodDecl jcTree = (JCMethodDecl) tree; toClean.add(jcTree.defaultValue); } return super.visitExecutable(e, p); } }.scan(resolved, null);
private void addGuaranteedNonNullFromInvokes( VisitorState state, Trees trees, Set<Element> safeInitMethods, AccessPathNullnessAnalysis nullnessAnalysis, Set<Element> guaranteedNonNull) { for (Element invoked : safeInitMethods) { Tree invokedTree = trees.getTree(invoked); guaranteedNonNull.addAll( nullnessAnalysis.getNonnullFieldsOfReceiverAtExit( new TreePath(state.getPath(), invokedTree), state.context)); } }
List<ResourceValue> getResourcesInAnnotation(Element element, Class annotationClass, String resourceType, List<Integer> resourceValues) { List<ResourceValue> resources = new ArrayList<>(resourceValues.size()); JCTree tree = (JCTree) trees.getTree(element, getAnnotationMirror(element, annotationClass)); // tree can be null if the references are compiled types and not source if (tree != null) { // Collects details about the layout resource used for the annotation parameter scanner.clearResults(); scanner.setCurrentAnnotationDetails(element, annotationClass, resourceType); tree.accept(scanner); resources.addAll(scanner.getResults()); } // Resource values may not have been picked up by the scanner if they are hardcoded. // In that case we just use the hardcoded value without an R class if (resources.size() != resourceValues.size()) { for (int layoutValue : resourceValues) { if (!isLayoutValueInResources(resources, layoutValue)) { resources.add(new ResourceValue(layoutValue)); } } } return resources; }
private Symbol.MethodSymbol getSymbolOfSuperConstructor( Symbol.MethodSymbol anonClassConstructorSymbol, VisitorState state) { // get the statements in the body of the anonymous class constructor List<? extends StatementTree> statements = getTreesInstance(state).getTree(anonClassConstructorSymbol).getBody().getStatements(); // there should be exactly one statement, which is an invocation of the super constructor if (statements.size() == 1) { StatementTree stmt = statements.get(0); if (stmt instanceof ExpressionStatementTree) { ExpressionTree expression = ((ExpressionStatementTree) stmt).getExpression(); if (expression instanceof MethodInvocationTree) { return ASTHelpers.getSymbol((MethodInvocationTree) expression); } } } throw new IllegalStateException("unexpected anonymous class constructor body " + statements); }
private void reportInitializerError( Symbol.MethodSymbol methodSymbol, String message, VisitorState state) { if (symbolHasSuppressInitalizationWarningsAnnotation(methodSymbol)) { return; } Tree methodTree = getTreesInstance(state).getTree(methodSymbol); state.reportMatch( createErrorDescription(MessageTypes.METHOD_NO_INIT, methodTree, message, methodTree)); }
@Nullable private ClassTree findEnclosingLocalOrAnonymousClass(ClassTree classTree) { Symbol.ClassSymbol symbol = ASTHelpers.getSymbol(classTree); // we need this while loop since we can have a NestingKind.NESTED class (i.e., a nested // class declared at the top-level within its enclosing class) nested (possibly deeply) // within a NestingKind.ANONYMOUS or NestingKind.LOCAL class while (symbol.getNestingKind().isNested()) { if (symbol.getNestingKind().equals(NestingKind.ANONYMOUS) || symbol.getNestingKind().equals(NestingKind.LOCAL)) { return Trees.instance(JavacProcessingEnvironment.instance(context)).getTree(symbol); } else { // symbol.owner is the enclosing element, which could be a class or a method. // if it's a class, the enclClass() method will (surprisingly) return the class itself, // so this works symbol = symbol.owner.enclClass(); } } return null; }
private void reportInitErrorOnField(Symbol symbol, VisitorState state) { if (symbolHasSuppressInitalizationWarningsAnnotation(symbol)) { return; } Tree tree = getTreesInstance(state).getTree(symbol); if (symbol.isStatic()) { state.reportMatch( createErrorDescription( MessageTypes.FIELD_NO_INIT, tree, "@NonNull static field " + symbol + " not initialized", tree)); } else { state.reportMatch( createErrorDescription( MessageTypes.FIELD_NO_INIT, tree, "@NonNull field " + symbol + " not initialized", tree)); } }
errorTree = memberReferenceTree; } else { errorTree = getTreesInstance(state).getTree(paramSymbol);
memberReferenceTree != null ? memberReferenceTree : getTreesInstance(state).getTree(overridingMethod); return createErrorDescription( MessageTypes.WRONG_OVERRIDE_RETURN, errorTree, message, state.getPath());
@Override public Set<Name> getOwnMethodInvocations(ExecutableElement method) { try { return ImmutableSet.copyOf(trees .getTree(method) .accept(OWN_METHOD_INVOCATIONS_FETCHER, null) .names); } catch (RuntimeException e) { // Fail gracefully return ImmutableSet.of(); } }
@Override public void visitAllOwnMethodInvocations( ExecutableElement method, OwnMethodInvocationVisitor visitor) { trees.getTree(method).accept(OWN_METHOD_INVOCATIONS_VISITOR, (tree, methodName) -> { visitor.visitInvocation(methodName, (kind, msg) -> { CompilationUnitTree compilationUnit = trees.getPath(method).getCompilationUnit(); trees.printMessage(kind, msg, tree, compilationUnit); }); }); }
private Id elementToId(Element element, Class<? extends Annotation> annotation, int value) { JCTree tree = (JCTree) trees.getTree(element, getMirror(element, annotation)); if (tree != null) { // tree can be null if the references are compiled types and not source rScanner.reset(); tree.accept(rScanner); if (!rScanner.resourceIds.isEmpty()) { return rScanner.resourceIds.values().iterator().next(); } } return new Id(value); }
public Object run(WorkingCopy wc) { TreeMaker make = wc.getTreeMaker(); ExecutableElement elem = execElementHandle.resolve(wc);//javaClass.getMethod(wc, "_init", new Class[]{}); MethodInvocationTree methInvkTree = TreeMakerUtils.createMethodInvocation(wc, methodName, pNames); if(noreturn) { addMethodInvocationStatement(wc, wc.getTrees().getTree(elem), methInvkTree); }else { addReturnStatement(wc, wc.getTrees().getTree(elem), methInvkTree); } return null; } }, javaClass.getFileObject());
public Object run(WorkingCopy wc) { ExecutableElement execElement = methodElementHandle.resolve(wc); TypeElement typeElement = typeElementHandle.resolve(wc); ClassTree ctree = wc.getTrees().getTree(typeElement); ClassTree newctree = ctree; newctree = wc.getTreeMaker().removeClassMember(ctree, wc.getTrees().getTree(execElement)); wc.rewrite(ctree, newctree); return null; } }, fObj);
public Object run(WorkingCopy wc) { ExecutableElement execElement = execElementHandle.resolve(wc); MethodTree oldTree = wc.getTrees().getTree(execElement); MethodTree newTree = TreeMakerUtils.updateMethod(wc, method, oldTree); wc.rewrite(oldTree, newTree); return null; } }, javaClass.getFileObject());
@Override public void visitAllOwnMethodInvocations( ExecutableElement method, OwnMethodInvocationVisitor visitor) { trees.getTree(method).accept(OWN_METHOD_INVOCATIONS_VISITOR, (tree, methodName) -> { visitor.visitInvocation(methodName, (kind, msg) -> { CompilationUnitTree compilationUnit = trees.getPath(method).getCompilationUnit(); trees.printMessage(kind, msg, tree, compilationUnit); }); }); }
public Object run(WorkingCopy wc) { ExecutableElement execElement = execElementHandle.resolve(wc); BlockTree block = wc.getTrees().getTree(execElement).getBody(); TreePath treePath = TreeUtils.getTreePath(wc, block); new Refactor.ReturnStatementLiteralRenamer(wc, oldStr, newStr).scan(treePath, null); return null; } }, javaClass.getFileObject());
void addCleanupStatements(WorkingCopy wc, List<Bean> beans) { ExecutableElement elem = execElementHandle.resolve(wc); BlockTree oldBlockTree = wc.getTrees().getTree(elem).getBody(); BlockTree newBlockTree = oldBlockTree; for (Bean bean : beans) { if (bean.getCleanupMethod() != null) { newBlockTree = addStatement(wc, newBlockTree, bean.getName(), bean.getCleanupMethod(), null); } } wc.rewrite(oldBlockTree, newBlockTree); }