packageName = elements.getPackageOf(typeElement).getQualifiedName().toString(); TreePath tp = controller.getTreeUtilities().pathFor(caretPosition); while (tp != null && tp.getLeaf().getKind() != Kind.METHOD) { tp = tp.getParentPath(); Element element = controller.getTrees().getElement(tp); List<? extends AnnotationMirror> allAnnotationMirrors = elements.getAllAnnotationMirrors(element); for (Iterator<? extends AnnotationMirror> it = allAnnotationMirrors.iterator(); it.hasNext();) { typeElement = (TypeElement) annotationMirror.getAnnotationType().asElement(); if (typeElement.getQualifiedName().contentEquals(ANNOTATION)) { methodName = element.getSimpleName().toString(); break;
if (isSynthetic(info.getCompilationUnit(), method)) { return null; String name; if (jcm.name == jcm.name.table.names.init) { TreePath path = info.getTrees().getPath(info.getCompilationUnit(), jcm); if (path == null) { return null; Element em = info.getTrees().getElement(path); Element clazz; if (em == null || (clazz = em.getEnclosingElement()) == null || !clazz.getKind().isClass()) { return null; name = clazz.getSimpleName().toString(); } else { name = method.getName().toString(); return findNameSpan(name, method);
private Iterable<? extends TreePath> fullPrepareThis(TreePath tp) { //XXX: is there a faster way to do this? Collection<TreePath> result = new LinkedList<TreePath>(); Scope scope = info.getTrees().getScope(tp); TypeElement lastClass = null; while (scope != null && scope.getEnclosingClass() != null) { if (lastClass != scope.getEnclosingClass()) { ExpressionTree thisTree = info.getTreeUtilities().parseExpression("this", new SourcePositions[1]); info.getTreeUtilities().attributeTree(thisTree, scope); result.add(new TreePath(tp, thisTree)); } scope = scope.getEnclosingScope(); } return result; }
/** * Creates a new BlockTree for provided <tt>bodyText</tt>. * * @param method figures out the scope for attribution. * @param bodyText text which will be used for method body creation. * @return a new tree for <tt>bodyText</tt>. */ public BlockTree createMethodBody(MethodTree method, String bodyText) { SourcePositions[] positions = new SourcePositions[1]; final TreeUtilities treeUtils = copy.getTreeUtilities(); StatementTree body = treeUtils.parseStatement(bodyText, positions); assert Tree.Kind.BLOCK == body.getKind() : "Not a statement block!"; Scope scope = copy.getTrees().getScope(TreePath.getPath(copy.getCompilationUnit(), method)); treeUtils.attributeTree(body, scope); mapComments((BlockTree) body, bodyText, copy, handler, positions[0]); new TreePosCleaner().scan(body, null); return (BlockTree) body; }
boolean isMemberClass = false; if (selectedIdentifier != null) { Tree tree = ci.getTreeUtilities().pathFor(currentOffset).getLeaf(); if (tree.getKind() == Tree.Kind.MEMBER_SELECT) { MemberSelectTree mst = (MemberSelectTree) tree; el = ci.getTrees().getElement(ci.getTrees().getPath(ci.getCompilationUnit(), mst.getExpression())); if (el != null) { TypeMirror tm = el.asType(); if (tm.getKind().equals(TypeKind.DECLARED)) { currentElementPtr[0] = tm.toString(); TreePath currentPath = ci.getTreeUtilities().pathFor(currentOffset); Tree tree = currentPath.getLeaf(); TypeElement te; if (TreeUtilities.CLASS_TREE_KINDS.contains(tree.getKind())) { te = (TypeElement) ci.getTrees().getElement(currentPath); } else { Scope scope = ci.getTreeUtilities().scopeFor(currentOffset); te = scope.getEnclosingClass(); Scope scope = ci.getTreeUtilities().scopeFor(currentOffset); el = scope.getEnclosingMethod(); if (el != null) { currentElementPtr[0] = el.getSimpleName().toString(); if (currentElementPtr[0].equals("<init>")) { TreePath path = ci.getTreeUtilities().pathFor(currentOffset); Tree tree = path != null ? path.getLeaf() : null;
SourcePositions positions = controller.getTrees().getSourcePositions(); TreePath mainPath = treeUtilities.pathFor(offset); CompilationUnitTree unitTree = controller.getCompilationUnit(); return; Tree tree = mainPath.getLeaf(); Tree.Kind kind = tree.getKind(); for (Comment comm : treeUtilities.getComments(tree, true)) { if (comm.pos() < 0) { continue; for (Comment comm : treeUtilities.getComments(tree, false)) { if (comm.pos() < 0) { continue; tree = path.getLeaf(); kind = tree.getKind(); if (kind == Tree.Kind.IMPORT) { isValid[0] = false; TypeElement typeElement = (TypeElement)controller.getTrees().getElement(path); className[0] = ElementUtilities.getBinaryName(typeElement); path = path.getParentPath();
insideExpression(env, new TreePath(path, fa.getExpression())); TreePath parentPath = path.getParentPath(); if (type.getKind() == TypeKind.ERROR && el.getKind().isClass()) { el = controller.getElements().getPackageElement(((TypeElement) el).getQualifiedName()); kinds = EnumSet.of(CLASS, INTERFACE); if (!options.contains(Options.ALL_COMPLETION)) { exs = controller.getTreeUtilities().getUncaughtExceptions(grandParentPath.getParentPath()); if (!options.contains(Options.ALL_COMPLETION) && ((MethodTree) parent).getBody() != null) { controller.toPhase(Phase.RESOLVED); exs = controller.getTreeUtilities().getUncaughtExceptions(new TreePath(path, ((MethodTree) parent).getBody())); Trees trees = controller.getTrees(); for (ExpressionTree thr : ((MethodTree) parent).getThrows()) { break; TypeMirror tm = controller.getTreeUtilities().parseType(typeName, env.getScope().getEnclosingClass()); if (tm != null && tm.getKind() == TypeKind.DECLARED) { addMembers(env, tm, ((DeclaredType) tm).asElement(), EnumSet.of(CONSTRUCTOR), null, inImport, insideNew, false); TypeMirror tm = controller.getTreeUtilities().parseType(typeName, env.getScope().getEnclosingClass()); if (tm != null && tm.getKind() == TypeKind.DECLARED) { addMembers(env, tm, ((DeclaredType) tm).asElement(), EnumSet.of(CONSTRUCTOR), null, inImport, insideNew, false);
Tree tree = ci.getTreeUtilities().pathFor(offset).getLeaf(); if (tree.getKind() == Tree.Kind.METHOD) { Element el = ci.getTrees().getElement(ci.getTrees().getPath(ci.getCompilationUnit(), tree)); if (el != null && (el.getKind() == ElementKind.METHOD || el.getKind() == ElementKind.CONSTRUCTOR)) { currentMethodPtr[0] = el.getSimpleName().toString(); if (currentMethodPtr[0].equals("<init>")) {
final CompilationController controller = env.getController(); TreeUtilities tu = controller.getTreeUtilities(); TreePath path = tu.pathFor(offset); Tree lastTree = null; int dim = 0; while (path != null) { Tree tree = path.getLeaf(); switch (tree.getKind()) { case VARIABLE: TypeMirror type = controller.getTrees().getTypeMirror(new TreePath(path, ((VariableTree) tree).getType())); if (type == null) { return null; TreePath methodOrLambdaPath = tu.getPathElementOfKind(EnumSet.of(Tree.Kind.METHOD, Tree.Kind.LAMBDA_EXPRESSION), path); if (methodOrLambdaPath == null) { return null; if (methodOrLambdaPath.getLeaf().getKind() == Tree.Kind.METHOD) { TreePath methodPath = tu.getPathElementOfKind(Tree.Kind.METHOD, path); if (methodPath == null) { return null; final Trees trs = controller.getTrees(); final TypeElement enclClass = scope.getEnclosingClass(); final boolean isStatic = enclClass != null ? (tu.isStaticContext(scope) || (env.getPath().getLeaf().getKind() == Tree.Kind.BLOCK && ((BlockTree) env.getPath().getLeaf()).isStatic())) : false; if (SUPER_KEYWORD.equals(name) && enclClass != null) { ElementUtilities.ElementAcceptor acceptor = new ElementUtilities.ElementAcceptor() {
TreePath tp = controller.getTreeUtilities().pathFor(dot); if ( tp == null ){ return result; Element contextElement = controller.getTrees().getElement(tp ); if ( contextElement == null || contextElement.getKind() != ElementKind.ANNOTATION_TYPE ) contextElement.getSimpleName().toString(), controller.getFileObject()) );
@Override public void run(ResultIterator resultIterator) throws Exception { Result res = resultIterator.getParserResult(offset); if (res == null) { return; } CompilationController controller = CompilationController.get(res); if (controller == null || controller.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { return; } TreePath path = controller.getTreeUtilities().pathFor(offset); javax.lang.model.element.Element elem = controller.getTrees().getElement(path); ElementKind kind = elem.getKind(); if (kind == ElementKind.CLASS || kind == ElementKind.ENUM || kind == ElementKind.ANNOTATION_TYPE) { result[0] = elem.asType().toString(); } } });
@Override public Void visitMethod(MethodTree tree, EnumSet<UseTypes> d) { if (info.getTreeUtilities().isSynthetic(getCurrentPath())) { return super.visitMethod(tree, d); tl.moveToOffset(sourcePositions.getStartPosition(info.getCompilationUnit(), tree)); TreePath tp = new TreePath(getCurrentPath(), t); handlePossibleIdentifier(tp, EnumSet.of(UseTypes.CLASS_USE)); Element el = info.getTrees().getElement(getCurrentPath()); if (el != null && (el.getModifiers().contains(Modifier.ABSTRACT) || el.getModifiers().contains(Modifier.NATIVE) || !el.getModifiers().contains(Modifier.PRIVATE))) { paramsUseTypes = EnumSet.of(UseTypes.WRITE, UseTypes.READ); } else { while (tp != null && !TreeUtilities.CLASS_TREE_KINDS.contains(tp.getLeaf().getKind())) { tp = tp.getParentPath(); if (tp != null && TreeUtilities.CLASS_TREE_KINDS.contains(tp.getLeaf().getKind())) { name = ((ClassTree) tp.getLeaf()).getSimpleName().toString(); } else {
controller.toPhase(Phase.ELEMENTS_RESOLVED); final TreeUtilities tu = controller.getTreeUtilities(); final TreePath clsPath = tu.getPathElementOfKind(TreeUtilities.CLASS_TREE_KINDS, env.getPath()); if (clsPath == null) { return; final ClassTree cls = (ClassTree) clsPath.getLeaf(); final CompilationUnitTree root = env.getRoot(); final SourcePositions sourcePositions = env.getSourcePositions(); final TypeElement te = (TypeElement) trees.getElement(clsPath); if (te == null || te.getKind() == ElementKind.ANNOTATION_TYPE) { return; final Set<? extends VariableElement> uninitializedFields = tu.getUninitializedFields(clsPath); final List<ExecutableElement> constructors = ElementFilter.constructorsIn(te.getEnclosedElements()); if (currentMember != null && currentMember.getKind() == Tree.Kind.VARIABLE) { Element e = trees.getElement(new TreePath(clsPath, currentMember)); if (e.getKind().isField()) { uninitializedFields.remove((VariableElement) e); results.add(itemFactory.createInitializeAllConstructorItem(env.getController(), true, uninitializedFinalFields, dctor2generate.getKind() == CONSTRUCTOR ? (ExecutableElement) dctor2generate : null, te, anchorOffset));
int dot = strategy.getOffset(component); TreePath tp = controller.getTreeUtilities() .pathFor(dot); Element element = controller.getTrees().getElement(tp ); if ( element == null ){ StatusDisplayer.getDefault().setStatusText( subject[1] = element.getSimpleName(); subject[2] = InspectActionId.METHOD_CONTEXT; Element enclosingElement = element.getEnclosingElement(); if ( enclosingElement instanceof ExecutableElement && hasAnnotation(element, OBSERVES_ANNOTATION)) subject[1] = enclosingElement.getSimpleName(); subject[2] = InspectActionId.METHOD_CONTEXT;
TreePath path = ci.getTreeUtilities().pathFor(offset); Tree tree; do { tree = path.getLeaf(); if (!TreeUtilities.CLASS_TREE_KINDS.contains(tree.getKind())) { path = path.getParentPath(); if (path == null) { break; if (TreeUtilities.CLASS_TREE_KINDS.contains(tree.getKind())) { SourcePositions positions = ci.getTrees().getSourcePositions(); int pos = (int) positions.getStartPosition(ci.getCompilationUnit(), tree); if (pos == Diagnostic.NOPOS) { return ; Element el = ci.getTrees().getElement(ci.getTrees().getPath(ci.getCompilationUnit(), tree)); if (el != null && (el.getKind() == ElementKind.CLASS || el.getKind() == ElementKind.INTERFACE)) { currentClassPtr[0] = ElementUtilities.getBinaryName((TypeElement) el);
URL url = treePath.getCompilationUnit().getSourceFile().toUri().toURL(); file = URLMapper.findFileObject(url); if (file == null) { return new TreePathHandle(new EmptyDelegate(url, treePath.getLeaf().getKind())); int position = ((JCTree) treePath.getLeaf()).pos; if (position == (-1)) { int index = listChildren(treePath.getParentPath().getLeaf()).indexOf(treePath.getLeaf()); assert index != (-1); return new TreePathHandle(new CountingDelegate(TreePathHandle.create(treePath.getParentPath(), info), index, treePath.getLeaf().getKind())); Element correspondingElement = info.getTrees().getElement(current); Element element = null; boolean child = false; while (current != null) { if ( TreeUtilities.CLASS_TREE_KINDS.contains(current.getLeaf().getKind()) || current.getLeaf().getKind() == Kind.VARIABLE || current.getLeaf().getKind() == Kind.METHOD) { Element loc = info.getTrees().getElement(current); if (isSupported(loc)) { if (child && info.getTreeUtilities().isSynthetic(info.getCompilationUnit(), current.getLeaf())) { if (element != null && element.getKind() != ElementKind.OTHER) { elementHandle = createHandle(element);
protected List<int[]> processImpl(CompilationInfo info, Preferences node, Document doc, int caretPosition) { caretPosition = info.getSnapshot().getEmbeddedOffset(caretPosition); TokenSequence<JavaTokenId> cts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language()); CompilationUnitTree cu = info.getCompilationUnit(); TreeUtilities tu = info.getTreeUtilities(); TreePath tp = tu.pathFor(caretPosition); if (tp.getParentPath() != null && tp.getParentPath().getLeaf().getKind() == Kind.ANNOTATED_TYPE) { tp = tp.getParentPath(); if (typePath != null && typePath.getParentPath().getLeaf().getKind() == Kind.METHOD) { MethodTree decl = (MethodTree) typePath.getParentPath().getLeaf(); && isIn(cu, info.getTrees().getSourcePositions(), type, caretPosition)) { MethodExitDetector med = new MethodExitDetector(); && isIn(cu, info.getTrees().getSourcePositions(), exc, caretPosition)) { MethodExitDetector med = new MethodExitDetector(); TreePath tryPath = tu.getPathElementOfKind(Kind.TRY, typePath); if (tryPath != null) { return med.process(info, doc, ((TryTree)tryPath.getLeaf()).getBlock(), Collections.singletonList(typePath.getLeaf())); boolean isExtends = ctree.getExtendsClause() == typePath.getLeaf();
TreePath path = controller.getTreeUtilities().pathFor(exactOffset); TreePath parent = path.getParentPath(); if (parent != null) { Tree parentLeaf = parent.getLeaf(); if ( parentLeaf.getKind() == Kind.METHOD_INVOCATION){ ExpressionTree select = ((MethodInvocationTree)parentLeaf). getMethodSelect(); Scope scope = controller.getTrees().getScope(path); Element subjectClass = scope.getEnclosingClass(); Element method = controller.getTrees().getElement( new TreePath(path, select)); Element caller = controller.getTrees().getElement( new TreePath(path, ((MemberSelectTree)select).getExpression())); String methodName = method.getSimpleName().toString(); if ( FIRE.equals( methodName) && method instanceof ExecutableElement && caller instanceof VariableElement ) String variableName = caller.getSimpleName().toString(); TypeElement enclosingTypeElement = controller.getElementUtilities().
private void insideNewClass(Env env) throws IOException { TreePath path = env.getPath(); NewClassTree nc = (NewClassTree) path.getLeaf(); TokenSequence<JavaTokenId> ts = findLastNonWhitespaceToken(env, nc, env.getOffset()); if (ts != null) { controller.toPhase(Phase.RESOLVED); TypeElement tel = controller.getElements().getTypeElement("java.lang.Throwable"); //NOI18N DeclaredType base = path.getParentPath().getLeaf().getKind() == Tree.Kind.THROW && tel != null ? controller.getTypes().getDeclaredType(tel) : null; TypeElement toExclude = null; if (nc.getIdentifier().getKind() == Tree.Kind.IDENTIFIER && prefix != null) { TypeMirror tm = controller.getTreeUtilities().parseType(prefix, env.getScope().getEnclosingClass()); if (tm != null && tm.getKind() == TypeKind.DECLARED) { TypeElement te = (TypeElement) ((DeclaredType) tm).asElement(); } else if ((tm.getKind() == TypeKind.DECLARED || tm.getKind() == TypeKind.ERROR) && startsWith(env, ((DeclaredType) tm).asElement().getSimpleName().toString())) { results.add(itemFactory.createArrayItem(env.getController(), (ArrayType) smart, anchorOffset, env.getReferencesCount(), env.getController().getElements())); addTypes(env, EnumSet.of(CLASS, INTERFACE, ENUM, ANNOTATION_TYPE), base); } else { TypeMirror enclType = controller.getTrees().getTypeMirror(new TreePath(path, nc.getEnclosingExpression())); if (enclType != null && enclType.getKind() == TypeKind.DECLARED) { addMembers(env, enclType, ((DeclaredType) enclType).asElement(), EnumSet.of(CLASS, INTERFACE, ENUM, ANNOTATION_TYPE), base, false, insideNew, false);
private void insideVariable(Env env) throws IOException { int offset = env.getOffset(); TreePath path = env.getPath(); VariableTree var = (VariableTree) path.getLeaf(); SourcePositions sourcePositions = env.getSourcePositions(); CompilationUnitTree root = env.getRoot(); Tree type = var.getType(); int typePos = type.getKind() == Tree.Kind.ERRONEOUS && ((ErroneousTree) type).getErrorTrees().isEmpty() ? (int) sourcePositions.getEndPosition(root, type) : (int) sourcePositions.getStartPosition(root, type); if (offset <= typePos) { Tree parent = path.getParentPath().getLeaf(); if (parent.getKind() == Tree.Kind.CATCH) { CompilationController controller = env.getController(); if (!options.contains(Options.ALL_COMPLETION)) { TreeUtilities tu = controller.getTreeUtilities(); TreePath tryPath = tu.getPathElementOfKind(Tree.Kind.TRY, path); Set<TypeMirror> exs = tu.getUncaughtExceptions(tryPath); Elements elements = controller.getElements(); for (TypeMirror ex : exs) { if (ex.getKind() == TypeKind.DECLARED && startsWith(env, ((DeclaredType) ex).asElement().getSimpleName().toString()) && (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(((DeclaredType) ex).asElement())) && !Utilities.isExcluded(((TypeElement)((DeclaredType) ex).asElement()).getQualifiedName())) { addTypes(env, EnumSet.of(CLASS, INTERFACE, TYPE_PARAMETER), controller.getTypes().getDeclaredType(te)); } else if (parent.getKind() == Tree.Kind.TRY) { CompilationController controller = env.getController(); TypeElement te = controller.getElements().getTypeElement("java.lang.AutoCloseable"); //NOI18N