com.sun.source.tree.IdentifierTree Java Examples
The following examples show how to use
com.sun.source.tree.IdentifierTree.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IntroduceConstantFix.java From netbeans with Apache License 2.0 | 6 votes |
static boolean checkConstantExpression(final CompilationInfo info, TreePath path) { InstanceRefFinder finder = new InstanceRefFinder(info, path) { @Override public Object visitIdentifier(IdentifierTree node, Object p) { Element el = info.getTrees().getElement(getCurrentPath()); if (el == null || el.asType() == null || el.asType().getKind() == TypeKind.ERROR) { return null; } if (el.getKind() == ElementKind.LOCAL_VARIABLE || el.getKind() == ElementKind.PARAMETER) { throw new StopProcessing(); } else if (el.getKind() == ElementKind.FIELD) { if (!el.getModifiers().contains(Modifier.FINAL)) { throw new StopProcessing(); } } return super.visitIdentifier(node, p); } }; try { finder.process(); return !(finder.containsInstanceReferences() || finder.containsLocalReferences() || finder.containsReferencesToSuper()); } catch (StopProcessing e) { return false; } }
Example #2
Source File: ImportClass.java From netbeans with Apache License 2.0 | 6 votes |
@Override protected boolean perform() { TreePath replacePath = replacePathHandle.resolve(copy); if (replacePath == null) { Logger.getAnonymousLogger().warning(String.format("Attempt to change import for FQN: %s, but the import cannot be resolved in the current context", fqn)); return false; } Element el = toImport.resolve(copy); if (el == null) { return false; } CharSequence elFQN = copy.getElementUtilities().getElementName(el, true); IdentifierTree id = copy.getTreeMaker().Identifier(elFQN); copy.rewrite(replacePath.getLeaf(), id); for (TreePathHandle tph : additionalLocations) { replacePath = tph.resolve(copy); if (replacePath == null) { continue; } copy.rewrite(replacePath.getLeaf(), id); } return false; }
Example #3
Source File: FinalizeDoesNotCallSuper.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Void visitMethodInvocation(MethodInvocationTree node, Void p) { if (!node.getArguments().isEmpty()) { return null; } final ExpressionTree et = node.getMethodSelect(); if (et.getKind() != Tree.Kind.MEMBER_SELECT) { return null; } final MemberSelectTree mst = (MemberSelectTree) et; if (!FINALIZE.contentEquals(mst.getIdentifier())) { return null; } if (mst.getExpression().getKind() != Tree.Kind.IDENTIFIER) { return null; } if (!SUPER.contentEquals(((IdentifierTree)mst.getExpression()).getName())) { return null; } found = true; return null; }
Example #4
Source File: CopyFinder.java From netbeans with Apache License 2.0 | 6 votes |
public Boolean visitIdentifier(IdentifierTree node, TreePath p) { if (p == null) return super.visitIdentifier(node, p); switch (verifyElements(getCurrentPath(), p)) { case MATCH_CHECK_DEEPER: if (node.getKind() == p.getLeaf().getKind()) { return true; } return deepVerifyIdentifier2MemberSelect(getCurrentPath(), p); case MATCH: return true; default: case NO_MATCH: case NO_MATCH_CONTINUE: return false; } }
Example #5
Source File: JavaInputAstVisitor.java From javaide with GNU General Public License v3.0 | 6 votes |
private void dotExpressionUpToArgs(ExpressionTree expression, Optional<BreakTag> tyargTag) { expression = getArrayBase(expression); switch (expression.getKind()) { case MEMBER_SELECT: MemberSelectTree fieldAccess = (MemberSelectTree) expression; visit(fieldAccess.getIdentifier()); break; case METHOD_INVOCATION: MethodInvocationTree methodInvocation = (MethodInvocationTree) expression; if (!methodInvocation.getTypeArguments().isEmpty()) { builder.open(plusFour); addTypeArguments(methodInvocation.getTypeArguments(), ZERO); // TODO(jdd): Should indent the name -4. builder.breakOp(Doc.FillMode.UNIFIED, "", ZERO, tyargTag); builder.close(); } visit(getMethodName(methodInvocation)); break; case IDENTIFIER: visit(((IdentifierTree) expression).getName()); break; default: scan(expression, null); break; } }
Example #6
Source File: JavaInputAstVisitor.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
private void dotExpressionUpToArgs(ExpressionTree expression, Optional<BreakTag> tyargTag) { expression = getArrayBase(expression); switch (expression.getKind()) { case MEMBER_SELECT: MemberSelectTree fieldAccess = (MemberSelectTree) expression; visit(fieldAccess.getIdentifier()); break; case METHOD_INVOCATION: MethodInvocationTree methodInvocation = (MethodInvocationTree) expression; if (!methodInvocation.getTypeArguments().isEmpty()) { builder.open(plusFour); addTypeArguments(methodInvocation.getTypeArguments(), ZERO); // TODO(jdd): Should indent the name -4. builder.breakOp(Doc.FillMode.UNIFIED, "", ZERO, tyargTag); builder.close(); } visit(getMethodName(methodInvocation)); break; case IDENTIFIER: visit(((IdentifierTree) expression).getName()); break; default: scan(expression, null); break; } }
Example #7
Source File: SemanticHighlighterBase.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Void visitIdentifier(IdentifierTree tree, Void p) { if (info.getTreeUtilities().isSynthetic(getCurrentPath())) return null; tl.moveToOffset(sourcePositions.getStartPosition(info.getCompilationUnit(), tree)); if (memberSelectBypass != (-1)) { tl.moveToOffset(memberSelectBypass); memberSelectBypass = -1; } tl.identifierHere(tree, tree2Tokens); handlePossibleIdentifier(getCurrentPath(), false); addParameterInlineHint(tree); super.visitIdentifier(tree, null); return null; }
Example #8
Source File: JavaInputAstVisitor.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
/** * Helper method for import declarations, names, and qualified names. */ private void visitName(Tree node) { Deque<Name> stack = new ArrayDeque<>(); for (; node instanceof MemberSelectTree; node = ((MemberSelectTree) node).getExpression()) { stack.addFirst(((MemberSelectTree) node).getIdentifier()); } stack.addFirst(((IdentifierTree) node).getName()); boolean first = true; for (Name name : stack) { if (!first) { token("."); } token(name.toString()); first = false; } }
Example #9
Source File: PreconditionsChecker.java From netbeans with Apache License 2.0 | 6 votes |
private void checkIfRefactorableMutation(TreePath currentTreePath, IdentifierTree that) { Tree parent = currentTreePath.getParentPath().getLeaf(); TreePath parentOfParentPath = currentTreePath.getParentPath().getParentPath(); Tree parentOfParent = parentOfParentPath.getLeaf(); if ((isStatementPreOrPostfix(parent, parentOfParent) || isLeftHandSideOfCompoundAssignement(parent, that)) && isPureMutator(parentOfParentPath)) { if (this.hasOneNEFReducer) { this.hasNonEffectivelyFinalVars = true; } else { this.hasOneNEFReducer = true; this.reducerStatement = currentTreePath.getParentPath().getParentPath().getLeaf(); this.mutatedVariable = that; } } else { this.hasNonEffectivelyFinalVars = true; } }
Example #10
Source File: WatchPanel.java From netbeans with Apache License 2.0 | 6 votes |
@Override public TypeMirror getTypeMirror(TreePath arg0) { Tree tree = arg0.getLeaf(); if (tree.getKind() == Tree.Kind.IDENTIFIER) { Map<String, ObjectVariable> map = null; try { // [TODO] add JPDADebuggerImpl.getAllLabels() to API Method method = debugger.getClass().getMethod("getAllLabels"); // NOI18N map = (Map<String, ObjectVariable>) method.invoke(debugger); } catch (Exception ex) { Exceptions.printStackTrace(ex); } if (map != null) { String name = ((IdentifierTree)tree).getName().toString(); ObjectVariable var = map.get(name); if (var != null) { Elements elements = controller.getElements(); TypeElement typeElem = elements.getTypeElement(var.getClassType().getName()); if (typeElem != null) { return typeElem.asType(); } } } } return trees.getTypeMirror(arg0); }
Example #11
Source File: WorkingCopy.java From netbeans with Apache License 2.0 | 6 votes |
private void addSyntheticTrees(DiffContext diffContext, Tree node) { if (node == null) return ; if (((JCTree) node).pos == (-1)) { diffContext.syntheticTrees.add(node); return ; } if (node.getKind() == Kind.EXPRESSION_STATEMENT) { ExpressionTree est = ((ExpressionStatementTree) node).getExpression(); if (est.getKind() == Kind.METHOD_INVOCATION) { ExpressionTree select = ((MethodInvocationTree) est).getMethodSelect(); if (select.getKind() == Kind.IDENTIFIER && ((IdentifierTree) select).getName().contentEquals("super")) { if (getTreeUtilities().isSynthetic(diffContext.origUnit, node)) { diffContext.syntheticTrees.add(node); } } } } }
Example #12
Source File: ProspectiveOperation.java From netbeans with Apache License 2.0 | 6 votes |
private void beautifyAssignement(Tree currentTree, Set<Name> needed) { AssignmentTree assigned = (AssignmentTree) ((ExpressionStatementTree) currentTree).getExpression(); ExpressionTree variable = assigned.getVariable(); if (variable.getKind() == Tree.Kind.IDENTIFIER) { IdentifierTree id = (IdentifierTree) variable; if (needed.contains(id.getName())) { this.correspondingTree = treeMaker.ExpressionStatement(assigned.getExpression()); } else { this.correspondingTree = this.addReturn(castToStatementTree(currentTree), getOneFromSet(needed)); } } else { this.correspondingTree = this.addReturn(castToStatementTree(currentTree), getOneFromSet(needed)); } }
Example #13
Source File: TreeUtilitiesTest.java From netbeans with Apache License 2.0 | 6 votes |
public void testForEachLoop() throws Exception { prepareTest("Test", "package test; public class Test { public Test(java.util.List<String> ll) { for (String s : ll.subList(0, ll.size()) ) { } } }"); TreePath tp1 = info.getTreeUtilities().pathFor(122 - 30); assertEquals(Kind.IDENTIFIER, tp1.getLeaf().getKind()); assertEquals("ll", ((IdentifierTree) tp1.getLeaf()).getName().toString()); TreePath tp2 = info.getTreeUtilities().pathFor(127 - 30); assertEquals(Kind.MEMBER_SELECT, tp2.getLeaf().getKind()); assertEquals("subList", ((MemberSelectTree) tp2.getLeaf()).getIdentifier().toString()); TreePath tp3 = info.getTreeUtilities().pathFor(140 - 30); assertEquals(Kind.MEMBER_SELECT, tp3.getLeaf().getKind()); assertEquals("size", ((MemberSelectTree) tp3.getLeaf()).getIdentifier().toString()); TreePath tp4 = info.getTreeUtilities().pathFor(146 - 30); assertEquals(Kind.METHOD_INVOCATION, tp4.getLeaf().getKind()); assertEquals("subList", ((MemberSelectTree) ((MethodInvocationTree) tp4.getLeaf()).getMethodSelect()).getIdentifier().toString()); }
Example #14
Source File: NullAway.java From NullAway with MIT License | 6 votes |
@Override public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) { if (!matchWithinClass) { return Description.NO_MATCH; } Symbol symbol = ASTHelpers.getSymbol(tree); // some checks for cases where we know it is not // a null dereference if (symbol == null || symbol.getSimpleName().toString().equals("class") || symbol.isEnum()) { return Description.NO_MATCH; } Description badDeref = matchDereference(tree.getExpression(), tree, state); if (!badDeref.equals(Description.NO_MATCH)) { return badDeref; } // if we're accessing a field of this, make sure we're not reading the field before init if (tree.getExpression() instanceof IdentifierTree && ((IdentifierTree) tree.getExpression()).getName().toString().equals("this")) { return checkForReadBeforeInit(tree, state); } return Description.NO_MATCH; }
Example #15
Source File: Utilities.java From netbeans with Apache License 2.0 | 6 votes |
private static Token<JavaTokenId> findTokenWithText(CompilationInfo info, String text, int start, int end) { TokenHierarchy<?> th = info.getTokenHierarchy(); TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language()).subSequence(start, end); while (ts.moveNext()) { Token<JavaTokenId> t = ts.token(); if (t.id() == JavaTokenId.IDENTIFIER) { boolean nameMatches; if (!(nameMatches = text.equals(info.getTreeUtilities().decodeIdentifier(t.text()).toString()))) { ExpressionTree expr = info.getTreeUtilities().parseExpression(t.text().toString(), new SourcePositions[1]); nameMatches = expr.getKind() == Kind.IDENTIFIER && text.contentEquals(((IdentifierTree) expr).getName()); } if (nameMatches) { return t; } } } return null; }
Example #16
Source File: ConvertAnonymousToInner.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Boolean visitIdentifier(IdentifierTree node, Void p) { Element el = info.getTrees().getElement(getCurrentPath()); if (el != null && (el.getKind().isField() || el.getKind() == ElementKind.METHOD) && !el.getModifiers().contains(Modifier.STATIC)) { return true; } return super.visitIdentifier(node, p); }
Example #17
Source File: NullAway.java From NullAway with MIT License | 5 votes |
@Override public Description matchIdentifier(IdentifierTree tree, VisitorState state) { if (!matchWithinClass) { return Description.NO_MATCH; } return checkForReadBeforeInit(tree, state); }
Example #18
Source File: IteratorToFor.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Void visitIdentifier(IdentifierTree node, Void p) { if (MatcherUtilities.matches(ctx, getCurrentPath(), "$index")) { // NOI18N unsuitable(); return null; } return super.visitIdentifier(node, p); }
Example #19
Source File: ConvertToLambdaConverter.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Tree visitIdentifier(IdentifierTree identifierTree, Trees trees) { //rename shadowed variable TreePath currentPath = getCurrentPath(); CharSequence newName = originalToNewName.get(trees.getElement(currentPath)); if (newName != null) { IdentifierTree newTree = copy.getTreeMaker().Identifier(newName); copy.rewrite(identifierTree, newTree); } return super.visitIdentifier(identifierTree, trees); }
Example #20
Source File: SuspiciousNamesCombination.java From netbeans with Apache License 2.0 | 5 votes |
static String getName(ExpressionTree et) { if (et == null) return null; switch (et.getKind()) { case IDENTIFIER: return ((IdentifierTree) et).getName().toString(); case METHOD_INVOCATION: return getName(((MethodInvocationTree) et).getMethodSelect()); case MEMBER_SELECT: return ((MemberSelectTree) et).getIdentifier().toString(); default: return null; } }
Example #21
Source File: TreeNode.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Void visitIdentifier(IdentifierTree tree, List<Node> d) { List<Node> below = new ArrayList<Node>(); addCorrespondingElement(below); addCorrespondingType(below); addCorrespondingComments(below); super.visitIdentifier(tree, below); d.add(new TreeNode(info, getCurrentPath(), below)); return null; }
Example #22
Source File: BreadCrumbsNodeImpl.java From netbeans with Apache License 2.0 | 5 votes |
private static String simpleName(Tree t) { switch (t.getKind()) { case PARAMETERIZED_TYPE: return simpleName(((ParameterizedTypeTree) t).getType()); case IDENTIFIER: return ((IdentifierTree) t).getName().toString(); case MEMBER_SELECT: return ((MemberSelectTree) t).getIdentifier().toString(); default: return "";//XXX } }
Example #23
Source File: Utilities.java From netbeans with Apache License 2.0 | 5 votes |
private static boolean isSynthetic(CompilationUnitTree cut, Tree leaf) throws NullPointerException { JCTree tree = (JCTree) leaf; if (tree.pos == (-1)) return true; if (leaf.getKind() == Kind.METHOD) { //check for synthetic constructor: return (((JCMethodDecl)leaf).mods.flags & Flags.GENERATEDCONSTR) != 0L; } //check for synthetic superconstructor call: if (cut != null && leaf.getKind() == Kind.EXPRESSION_STATEMENT) { ExpressionStatementTree est = (ExpressionStatementTree) leaf; if (est.getExpression().getKind() == Kind.METHOD_INVOCATION) { MethodInvocationTree mit = (MethodInvocationTree) est.getExpression(); if (mit.getMethodSelect().getKind() == Kind.IDENTIFIER) { IdentifierTree it = (IdentifierTree) mit.getMethodSelect(); if ("super".equals(it.getName().toString())) { return ((JCCompilationUnit) cut).endPositions.getEndPos(tree) == (-1); } } } } return false; }
Example #24
Source File: InstanceRefFinder.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Object visitIdentifier(IdentifierTree node, Object p) { Element el = ci.getTrees().getElement(getCurrentPath()); if (el == null || el.asType() == null || el.asType().getKind() == TypeKind.ERROR) { return null; } switch (el.getKind()) { case LOCAL_VARIABLE: addLocalClassVariable(el); addUsedMember(el, enclosingType); break; case TYPE_PARAMETER: addInstanceOfTypeParameter(el); break; case FIELD: case METHOD: addInstanceForMemberOf(el); break; case CLASS: case ENUM: case INTERFACE: if (node.getName().contentEquals("this") || node.getName().contentEquals("super")) { addInstanceForType(enclosingType); } break; case EXCEPTION_PARAMETER: case RESOURCE_VARIABLE: addLocalClassVariable(el); // fall through case PARAMETER: addInstanceOfParameterOwner(el); break; case PACKAGE: break; default: addUsedMember(el, enclosingType); } return super.visitIdentifier(node, p); }
Example #25
Source File: AddParameterOrLocalFix.java From netbeans with Apache License 2.0 | 5 votes |
/** In case statement is an Assignment, replace it with variable declaration */ private boolean initExpression(StatementTree statement, TreeMaker make, final String name, TypeMirror proposedType, final WorkingCopy wc, TreePath tp) { ExpressionTree exp = ((ExpressionStatementTree) statement).getExpression(); if (exp.getKind() == Kind.ASSIGNMENT) { AssignmentTree at = (AssignmentTree) exp; if (at.getVariable().getKind() == Kind.IDENTIFIER && ((IdentifierTree) at.getVariable()).getName().contentEquals(name)) { //replace the expression statement with a variable declaration: VariableTree vt = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, make.Type(proposedType), at.getExpression()); vt = Utilities.copyComments(wc, statement, vt); wc.rewrite(statement, vt); return true; } } return false; }
Example #26
Source File: LoggerGenerator.java From netbeans with Apache License 2.0 | 5 votes |
private static VariableTree createLoggerField(TreeMaker make, ClassTree cls, CharSequence name, Set<Modifier> mods) { ModifiersTree modifiers = make.Modifiers(mods, Collections.<AnnotationTree>emptyList()); final List<ExpressionTree> none = Collections.<ExpressionTree>emptyList(); IdentifierTree className = make.Identifier(cls.getSimpleName()); MemberSelectTree classType = make.MemberSelect(className, "class"); // NOI18N MemberSelectTree getName = make.MemberSelect(classType, "getName"); // NOI18N MethodInvocationTree initClass = make.MethodInvocation(none, getName, none); final ExpressionTree logger = make.QualIdent(Logger.class.getName()); MemberSelectTree getLogger = make.MemberSelect(logger, "getLogger"); // NOI18N MethodInvocationTree initField = make.MethodInvocation(none, getLogger, Collections.nCopies(1, initClass)); return make.Variable(modifiers, name, logger, initField); // NOI18N }
Example #27
Source File: ConvertAnonymousToInner.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Void visitIdentifier(IdentifierTree node, Set<VariableElement> p) { Element el = info.getTrees().getElement(getCurrentPath()); TreePath elPath = el != null ? info.getTrees().getPath(el) : null; if (el != null && elPath != null && VARIABLES.contains(el.getKind()) && !isParent(newClassToConvert, elPath)) { p.add((VariableElement) el); } return super.visitIdentifier(node, p); }
Example #28
Source File: ClassImplementsTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testRenameInImpl() throws Exception { testFile = new File(getWorkDir(), "Test.java"); TestUtilities.copyStringToFile(testFile, "package hierbas.del.litoral;\n\n" + "import java.util.*;\n\n" + "public class Test<E> extends Object implements List {\n" + " public void taragui() {\n" + " }\n" + "}\n" ); String golden = "package hierbas.del.litoral;\n\n" + "import java.util.*;\n\n" + "public class Test<E> extends Object implements Seznam {\n" + " public void taragui() {\n" + " }\n" + "}\n"; JavaSource src = getJavaSource(testFile); Task task = new Task<WorkingCopy>() { public void run(WorkingCopy workingCopy) throws IOException { workingCopy.toPhase(Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); TreeMaker make = workingCopy.getTreeMaker(); for (Tree typeDecl : cut.getTypeDecls()) { // should check kind, here we can be sure! ClassTree clazz = (ClassTree) typeDecl; IdentifierTree ident = (IdentifierTree) clazz.getImplementsClause().get(0); workingCopy.rewrite(ident, make.setLabel(ident, "Seznam")); } } }; src.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); //System.err.println(res); assertEquals(golden, res); }
Example #29
Source File: JavaInputAstVisitor.java From javaide with GNU General Public License v3.0 | 5 votes |
private boolean fillFirstArgument( ExpressionTree e, List<ExpressionTree> items, Indent indent) { // is there a trailing dereference? if (items.size() < 2) { return false; } // don't special-case calls nested inside expressions if (e.getKind() != METHOD_INVOCATION) { return false; } MethodInvocationTree methodInvocation = (MethodInvocationTree) e; Name name = getMethodName(methodInvocation); if (!(methodInvocation.getMethodSelect() instanceof IdentifierTree) || name.length() > 4 || !methodInvocation.getTypeArguments().isEmpty() || methodInvocation.getArguments().size() != 1) { return false; } builder.open(ZERO); builder.open(indent); visit(name); token("("); ExpressionTree arg = getOnlyElement(methodInvocation.getArguments()); scan(arg, null); builder.close(); token(")"); builder.close(); return true; }
Example #30
Source File: PreconditionsChecker.java From netbeans with Apache License 2.0 | 5 votes |
private boolean isLocalVariable(IdentifierTree id, Trees trees) { Element el = trees.getElement(TreePath.getPath(treePath, id)); if (el != null) { return el.getKind() == ElementKind.LOCAL_VARIABLE || el.getKind() == ElementKind.PARAMETER; } return false; }