@Override public void onMatchReturn(NullAway analysis, ReturnTree tree, VisitorState state) { // Figure out the enclosing method node TreePath enclosingMethodOrLambda = NullabilityUtil.findEnclosingMethodOrLambdaOrInitializer(state.getPath()); if (enclosingMethodOrLambda == null) { throw new RuntimeException("no enclosing method, lambda or initializer!"); } if (!(enclosingMethodOrLambda.getLeaf() instanceof MethodTree || enclosingMethodOrLambda.getLeaf() instanceof LambdaExpressionTree)) { throw new RuntimeException( "return statement outside of a method or lambda! (e.g. in an initializer block)"); } Tree leaf = enclosingMethodOrLambda.getLeaf(); if (filterMethodOrLambdaSet.contains(leaf)) { returnToEnclosingMethodOrLambda.put(tree, leaf); // We need to manually trigger the dataflow analysis to run on the filter method, // this ensures onDataflowVisitReturn(...) gets called for all return statements in this // method before // onDataflowInitialStore(...) is called for all successor methods in the observable chain. // Caching should prevent us from re-analyzing any given method. AccessPathNullnessAnalysis nullnessAnalysis = analysis.getNullnessAnalysis(state); nullnessAnalysis.forceRunOnMethod(new TreePath(state.getPath(), leaf), state.context); } }
path = new TreePath(taskEvent.getCompilationUnit()); verify(seen.add(path.getLeaf()), "Duplicate FLOW event for: %s", taskEvent.getTypeElement()); Context subContext = new SubContext(context); subContext.put(ErrorProneOptions.class, errorProneOptions); return; if (path.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) { transformer.get().apply(new TreePath(compilation), subContext, countingDescriptionListener);
private String getPackageName(CompilationInfo cinfo) { Element e = cinfo.getTrees().getElement(new TreePath(cinfo.getCompilationUnit())); if (e != null && e.getKind() == ElementKind.PACKAGE) { return ((PackageElement) e).getQualifiedName().toString(); } return null; }
while (it.hasNext()) { Tree tr = it.next(); if (tr.getKind() == Tree.Kind.VARIABLE) { Trees trees = myInfo.getTrees(); TreePath path = new TreePath(getCurrentPath(), tr); Element el = trees.getElement(path); String sname = el.getSimpleName().toString(); if (sname.equals(myMember)) { myFieldHandle = TreePathHandle.create(path, myInfo); } else if (tr.getKind() == Tree.Kind.METHOD) { Trees trees = myInfo.getTrees(); TreePath path = new TreePath(getCurrentPath(), tr); Element el = trees.getElement(path); String sname = el.getSimpleName().toString(); if (sname.equals(myMethod)) {
public void run(WorkingCopy workingCopy) throws Exception { workingCopy.toPhase(Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); gu = GenerationUtils.newInstance(workingCopy); for (Tree typeDecl : cut.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) { Element e = workingCopy.getTrees().getElement(new TreePath(new TreePath(workingCopy.getCompilationUnit()), typeDecl)); if (e != null && e.getKind().isClass()) { TypeElement te = (TypeElement) e; ClassTree ct = (ClassTree) typeDecl; workingCopy.rewrite(ct, generateInterfaces(workingCopy, te, ct, gu)); } } } } };
public static <T extends Tree> T importFQNs(WorkingCopy copy, T tree) { if (tree == null) return null; TranslateIdentifier ti = new TranslateIdentifier(copy); //XXX: the TreePath constructed below below depends on javac internals (that elements are attributes of a tree, not a tree path): ti.scan(tree.getKind() == Kind.COMPILATION_UNIT ? new TreePath((CompilationUnitTree) tree) : new TreePath(new TreePath(copy.getCompilationUnit()), tree), null); return (T) copy.getTreeUtilities().translate(tree, ti.translateMap); }
@Override public Void visitAssignment(AssignmentTree node, Boolean p) { Element el = trees.getElement(new TreePath(getCurrentPath(), node.getVariable())); fields.remove(el); return null; } @Override
private static ExpressionTree findImplementIdentifier (Trees trees, TreePath classTreePath, String fullyQualifiedName) { ClassTree clazz = (ClassTree) classTreePath.getLeaf (); for (Tree tree : clazz.getImplementsClause ()) { Element element = trees.getElement (new TreePath (classTreePath, tree)); if (equalsElementWithFQN (element, fullyQualifiedName)) return (ExpressionTree) tree; } return null; }
private void insideUnionType(Env env) throws IOException { TreePath path = env.getPath(); UnionTypeTree dtt = (UnionTypeTree) path.getLeaf(); CompilationController controller = env.getController(); TokenSequence<JavaTokenId> last = findLastNonWhitespaceToken(env, dtt, env.getOffset()); Types types = controller.getTypes(); for (Tree t : dtt.getTypeAlternatives()) { TypeMirror tm = trees.getTypeMirror(new TreePath(path, t)); if (tm != null && tm.getKind() != TypeKind.ERROR) { for (Iterator<TypeMirror> it = exs.iterator(); it.hasNext();) {
public Void visitThrow(ThrowTree node, Set<TypeMirror> p) { super.visitThrow(node, p); TypeMirror tm = info.getTrees().getTypeMirror(new TreePath(getCurrentPath(), node.getExpression())); if (tm != null) { if (tm.getKind() == TypeKind.DECLARED) p.add(tm); else if (tm.getKind() == TypeKind.UNION) p.addAll(((UnionType)tm).getAlternatives()); } return null; }
int j = 0; for (Tree t : argTypes) { types[j++] = controller.getTrees().getTypeMirror(new TreePath(path, t)); path = new TreePath(path, nct.getIdentifier()); final Trees trees = controller.getTrees(); final TypeMirror type = trees.getTypeMirror(path); final Element el = trees.getElement(path); final Scope scope = env.getScope(); final boolean isAnonymous = nct.getClassBody() != null || (el != null && (el.getKind().isInterface() || el.getModifiers().contains(ABSTRACT)));
@Override public void run(CompilationInfo p) throws Exception { final CompilationUnitTree compilationUnitTree = p.getCompilationUnit(); final TreePath rootPath = new TreePath(compilationUnitTree); mappedElementExtractor = new MappedElementExtractor(p.getFileObject(), compilationUnitTree, p.getTrees(), rootPath); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { targetModel.refresh(compilationUnitTree.accept(mappedElementExtractor, null)); table.setModel(targetModel); } }); } }
@Override public Void visitClass(ClassTree t, Void v) { if (variableElement == null) { // try to find the component's field variable in the class List<? extends Tree> members = (List<? extends Tree>) t.getMembers(); Iterator<? extends Tree> it = members.iterator(); while(it.hasNext()){ Tree tr = it.next(); if (tr.getKind() == Tree.Kind.VARIABLE) { Trees trees = info.getTrees(); TreePath path = new TreePath(getCurrentPath(), tr); Element el = trees.getElement(path); if (el != null) { // Issue 185420 String sname = el.getSimpleName().toString(); if(sname.equals(this.member)){ this.handle = TreePathHandle.create(path, info); variableElement = el; if (findUsages) { usagesPositions = new ArrayList<Integer>(); } } } } } } if (findUsages) { super.visitClass(t, v); } return null; }
public TreePath resolve(CompilationInfo compilationInfo) throws IllegalArgumentException { TreePath p = parent.resolve(compilationInfo); if (p == null) return null; List<Tree> children = listChildren(p.getLeaf()); if (index < children.size()) { Tree t = children.get(index); if (t.getKind() == kind) { return new TreePath(p, t); } } return null; }
final UnderlyingAST ast; final ProcessingEnvironment env = key.environment(); if (codePath.getLeaf() instanceof LambdaExpressionTree) { LambdaExpressionTree lambdaExpressionTree = (LambdaExpressionTree) codePath.getLeaf(); ast = new UnderlyingAST.CFGLambda(lambdaExpressionTree); bodyPath = new TreePath(codePath, lambdaExpressionTree.getBody()); } else if (codePath.getLeaf() instanceof MethodTree) { MethodTree method = (MethodTree) codePath.getLeaf(); ClassTree enclClass = ASTHelpers.findEnclosingNode(codePath, ClassTree.class); "trying to compute CFG for method " + method + ", which has no body"); bodyPath = new TreePath(codePath, body); } else {
private Element getImportedElement(CompilationUnitTree cut, ImportTree imp) { Trees trees = copy.getTrees(); Tree qualIdent = imp.getQualifiedIdentifier(); if (qualIdent.getKind() != Tree.Kind.MEMBER_SELECT) { Element element = trees.getElement(TreePath.getPath(cut, qualIdent)); if (element == null) { String fqn = qualIdent.toString(); Element element = trees.getElement(TreePath.getPath(cut, ((MemberSelectTree)qualIdent).getExpression())); if (element == null) element = getElementByFQN(((MemberSelectTree)qualIdent).getExpression().toString()); Element parent = trees.getElement(TreePath.getPath(cut, ((MemberSelectTree)qualIdent).getExpression())); if (parent == null) parent = getElementByFQN(((MemberSelectTree)qualIdent).getExpression().toString()); if (parent != null && (parent.getKind().isClass() || parent.getKind().isInterface())) { Scope s = trees.getScope(new TreePath(cut)); for (Element e : parent.getEnclosedElements()) { if (name == e.getSimpleName() && e.getModifiers().contains(Modifier.STATIC) && trees.isAccessible(s, e, (DeclaredType)parent.asType())) found = new TreePath(new TreePath(new TreePath(cut), imp), qualIdent); Element element = trees.getElement(found);
@Override public Void visitTypeCast(TypeCastTree tree, EnumSet<UseTypes> d) { Tree expr = tree.getExpression(); if (expr.getKind() == Kind.IDENTIFIER) { handlePossibleIdentifier(new TreePath(getCurrentPath(), expr), EnumSet.of(UseTypes.READ)); } Tree cast = tree.getType(); if (cast.getKind() == Kind.IDENTIFIER) { handlePossibleIdentifier(new TreePath(getCurrentPath(), cast), EnumSet.of(UseTypes.READ)); } super.visitTypeCast(tree, d); return null; }
@Override public Boolean visitAssignment(AssignmentTree tree, Element e) { return e == trees.getElement(new TreePath(getCurrentPath(), tree.getVariable())) ? Boolean.TRUE : super.visitAssignment(tree, e); }
@Override public Boolean visitMethodInvocation(MethodInvocationTree tree, Stack<Tree> d) { Element el = info.getTrees().getElement(new TreePath(getCurrentPath(), tree.getMethodSelect())); if (el == null) { System.err.println("Warning: decl == null"); System.err.println("tree=" + tree); } if (el != null && el.getKind() == ElementKind.METHOD) { for (TypeMirror m : ((ExecutableElement) el).getThrownTypes()) { addToExceptionsMap(m, tree); } } super.visitMethodInvocation(tree, d); return null; }
private void insideBinaryTree(Env env) throws IOException { int offset = env.getOffset(); TreePath path = env.getPath(); BinaryTree bi = (BinaryTree) path.getLeaf(); SourcePositions sourcePositions = env.getSourcePositions(); CompilationUnitTree root = env.getRoot(); controller.toPhase(Phase.RESOLVED); TypeMirror tm = last.token().id() == JavaTokenId.AMP ? controller.getTrees().getTypeMirror(new TreePath(path, bi.getLeftOperand())) : null; if (tm != null && tm.getKind() == TypeKind.DECLARED) { env.addToExcludes(((DeclaredType) tm).asElement());