private void captureJavadoc(TypeElement elem) { List<String> imports = new ArrayList<>(); List<? extends ImportTree> importLines = Trees.instance(env).getPath(elem).getCompilationUnit().getImports(); for (ImportTree importLine : importLines) { imports.add(importLine.getQualifiedIdentifier().toString());
private TypeMirror getType(Tree t) { TreePath tp = verifier.getTreeUtils().getPath(verifier.getCompilationUnit(), t); return verifier.getTreeUtils().getTypeMirror(tp); }
@Nullable private VariableTree findDeclaration(VisitorState state, Symbol parameter) { JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context); TreePath declPath = Trees.instance(javacEnv).getPath(parameter); if (declPath != null && declPath.getCompilationUnit() == state.getPath().getCompilationUnit() && (declPath.getLeaf() instanceof VariableTree)) { return (VariableTree) declPath.getLeaf(); } return null; } }
@Nullable private VariableTree findDeclaration(VisitorState state, Symbol field) { JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context); TreePath fieldDeclPath = Trees.instance(javacEnv).getPath(field); // Skip fields declared in other compilation units since we can't make a fix for them here. if (fieldDeclPath != null && fieldDeclPath.getCompilationUnit() == state.getPath().getCompilationUnit() && (fieldDeclPath.getLeaf() instanceof VariableTree)) { return (VariableTree) fieldDeclPath.getLeaf(); } return null; }
private Element getElement(Tree t) { TreePath tp = verifier.getTreeUtils().getPath(verifier.getCompilationUnit(), t); Element e = verifier.getTreeUtils().getElement(tp); if (e == null) { // hack to make JDK 7 symbol resolution working if (t instanceof MethodInvocationTree) { JCTree.JCExpression jce = ((JCTree.JCMethodInvocation)t).meth; if (jce instanceof IdentifierTree) { e = ((JCTree.JCIdent)jce).sym; } else if (jce instanceof MemberSelectTree) { e = ((JCTree.JCFieldAccess)jce).sym; } } else if (t instanceof JCTree.JCIdent) { e = ((JCTree.JCIdent)t).sym; } else if (t instanceof JCTree.JCNewClass) { e = ((JCTree.JCIdent)((JCTree.JCNewClass)t).clazz).sym; } else if (t instanceof JCTree.JCThrow) { e = ((JCTree.JCNewClass)((JCTree.JCThrow)t).expr).type.tsym; } } return e; }
TreePath fieldDeclPath = Trees.instance(javacEnv).getPath(accessed.symbol);
private boolean checkSampling(MethodTree node) { TreePath mPath = verifier.getTreeUtils().getPath(verifier.getCompilationUnit(), node); ExecutableElement ee = (ExecutableElement)verifier.getTreeUtils().getElement(mPath); Sampled s = ee.getAnnotation(Sampled.class); OnMethod om = ee.getAnnotation(OnMethod.class); if (s != null && om != null) { Kind k = om.location().value(); switch (k) { case ENTRY: case RETURN: case ERROR: case CALL: { return true; } default: { // noop } } reportError("sampler.invalid.location", node); return false; } return true; }
@Override public Boolean visitReturn(ReturnTree node, Void v) { if (node.getExpression() != null) { TreePath tp = verifier.getTreeUtils().getPath(verifier.getCompilationUnit(), node); while (tp != null) { tp = tp.getParentPath(); Tree leaf = tp.getLeaf(); if (leaf.getKind() == Tree.Kind.METHOD) { if (isAnnotated((MethodTree)leaf)) { return reportError("return.type.should.be.void", node); } else { return super.visitReturn(node, v); } } } } return super.visitReturn(node, v); }
@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 Location getClassLocation(TypeElement typeElement) { Trees trees = Trees.instance(env); return getLocation(typeElement.getQualifiedName().toString(), trees.getPath(typeElement)); }
public TreePath resolve(CompilationInfo compilationInfo) throws IllegalArgumentException { Element e = resolveElement(compilationInfo); if (e == null) { return null; } return compilationInfo.getTrees().getPath(e); }
public PhaseOneResult process(CompilationUnitTree root, UnderlyingAST underlyingAST) { // TODO: Isn't this costly? Is there no cache we can reuse? TreePath bodyPath = trees.getPath(root, underlyingAST.getCode()); assert bodyPath != null; return process(bodyPath, underlyingAST); }
public Env<AttrContext> getEnv( final Element e, final Trees trees ) { return enter.getTopLevelEnv( (JCTree.JCCompilationUnit) trees.getPath( e ). getCompilationUnit() ); }
static MethodSignature create( CompilationUnitTree compilationUnitTree, MethodTree tree, Trees trees) { ImmutableList.Builder<Equivalence.Wrapper<TypeMirror>> parameterTypes = ImmutableList.builder(); for (VariableTree parameter : tree.getParameters()) { parameterTypes.add( MoreTypes.equivalence() .wrap(trees.getTypeMirror(trees.getPath(compilationUnitTree, parameter)))); } return new AutoValue_TreeDiffer_MethodSignature( tree.getName().toString(), parameterTypes.build()); } }
static MethodSignature create( CompilationUnitTree compilationUnitTree, MethodTree tree, Trees trees) { ImmutableList.Builder<Equivalence.Wrapper<TypeMirror>> parameterTypes = ImmutableList.builder(); for (VariableTree parameter : tree.getParameters()) { parameterTypes.add( MoreTypes.equivalence() .wrap(trees.getTypeMirror(trees.getPath(compilationUnitTree, parameter)))); } return new AutoValue_TreeDiffer_MethodSignature( tree.getName().toString(), parameterTypes.build()); } }
static boolean isViewClass(ClassTree classT, CompilationController controller) { Trees trees = controller.getTrees(); TreePath classTPath = trees.getPath(controller.getCompilationUnit(), classT); TypeElement classEl = (TypeElement) trees.getElement(classTPath); TypeElement appEl = controller.getElements().getTypeElement("org.jdesktop.application.View"); // NOI18N Types types = controller.getTypes(); TypeMirror tm1 = types.erasure(classEl.asType()); TypeMirror tm2 = types.erasure(appEl.asType()); return types.isSubtype(tm1, tm2); }
static HashMap<String, String> getImports(ProcessingEnvironment processingEnv, Data.MethodData methodData) { Trees trees = Trees.instance(processingEnv); TreePath tp = trees.getPath(methodData._method); TreePath lastTp = tp; while (tp != null) { lastTp = tp; tp = tp.getParentPath(); } ImportReader ir = new ImportReader(); ir.scan(lastTp, trees); return ir.imports; }
private boolean canSkip(TreePath path) { if (cinfo.getTreeUtilities().isSynthetic(path)) { return false; } Element el = cinfo.getTrees().getElement(path); if (el != null && elementAndNames.containsKey(el)) { TreePath declPath = cinfo.getTrees().getPath(el); if(declPath.getLeaf().equals(path.getLeaf())) { return true; } } return false; }
@Nullable private VariableTree findDeclaration(VisitorState state, Symbol field) { JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context); TreePath fieldDeclPath = Trees.instance(javacEnv).getPath(field); // Skip fields declared in other compilation units since we can't make a fix for them here. if (fieldDeclPath != null && fieldDeclPath.getCompilationUnit() == state.getPath().getCompilationUnit() && (fieldDeclPath.getLeaf() instanceof VariableTree)) { return (VariableTree) fieldDeclPath.getLeaf(); } return null; }
@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); }); }); }