Java Code Examples for com.sun.source.tree.Tree#Kind
The following examples show how to use
com.sun.source.tree.Tree#Kind .
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: JavaInputAstVisitor.java From google-java-format with Apache License 2.0 | 6 votes |
/** Returns true if {@code atLeastM} of the expressions in the given column are the same kind. */ private static boolean expressionsAreParallel( List<List<ExpressionTree>> rows, int column, int atLeastM) { Multiset<Tree.Kind> nodeTypes = HashMultiset.create(); for (List<? extends ExpressionTree> row : rows) { if (column >= row.size()) { continue; } // Treat UnaryTree expressions as their underlying type for the comparison (so, for example // -ve and +ve numeric literals are considered the same). if (row.get(column) instanceof UnaryTree) { nodeTypes.add(((UnaryTree) row.get(column)).getExpression().getKind()); } else { nodeTypes.add(row.get(column).getKind()); } } for (Multiset.Entry<Tree.Kind> nodeType : nodeTypes.entrySet()) { if (nodeType.getCount() >= atLeastM) { return true; } } return false; }
Example 2
Source File: EditorContextSupport.java From netbeans with Apache License 2.0 | 6 votes |
private static Tree findStatementInScope(TreePath tp) { Tree tree = tp.getLeaf(); Tree.Kind kind = tree.getKind(); switch (kind) { case BLOCK: case EXPRESSION_STATEMENT: case LAMBDA_EXPRESSION: case METHOD: return tree; } tp = tp.getParentPath(); if (tp == null) { return null; } else { return findStatementInScope(tp); } }
Example 3
Source File: ReplaceBufferByString.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Object visitMemberSelect(MemberSelectTree node, Object p) { if (wc.getTrees().getElement(new TreePath(getCurrentPath(), node.getExpression())) != varElement) { return super.visitMemberSelect(node, p); } Element target = wc.getTrees().getElement(getCurrentPath()); if (target != null && target.getKind() == ElementKind.METHOD) { Tree x = getCurrentPath().getParentPath().getLeaf(); Tree.Kind k = x.getKind(); if (k == Tree.Kind.METHOD_INVOCATION) { if (node.getIdentifier().contentEquals("toString")) { // NOI18N // rewrite the node to just the variable, which is going to change the type gu.copyComments(x, node.getExpression(), true); gu.copyComments(x, node.getExpression(), false); wc.rewrite(x, node.getExpression()); } } } return super.visitMemberSelect(node, p); }
Example 4
Source File: ASTPath.java From annotation-tools with MIT License | 6 votes |
/** * Parses and constructs a new AST entry. For example, the call: * * <pre> * {@code newASTEntry(Tree.Kind.CASE, new String[] {"expression", "statement"}, new String[] {"statement"}); * </pre> * * constructs a case AST entry, where the valid child selectors are * "expression" or "statement" and the "statement" child selector requires * an argument. * * @param kind the kind of this AST entry * @param legalChildSelectors a list of the legal child selectors for this AST entry * @param argumentChildSelectors a list of the child selectors that also require an argument. * Entries here should also be in the legalChildSelectors list. * @return a new {@link ASTEntry} * @throws ParseException if an illegal argument is found */ private ASTEntry newASTEntry(Tree.Kind kind, String[] legalChildSelectors, String[] argumentChildSelectors) throws ParseException { if (gotType('.')) { getTok(); } else { throw new ParseException("expected '.', got " + st); } String s = strVal(); for (String arg : legalChildSelectors) { if (s.equals(arg)) { if (argumentChildSelectors != null && ArraysPlume.indexOf(argumentChildSelectors, arg) >= 0) { getTok(); return new ASTEntry(kind, arg, intVal()); } else { return new ASTEntry(kind, arg); } } } throw new ParseException("Invalid argument for " + kind + " (legal arguments - " + Arrays.toString(legalChildSelectors) + "): " + s); }
Example 5
Source File: IntersectionTypeLocationCriterion.java From annotation-tools with MIT License | 6 votes |
@Override public boolean isSatisfiedBy(TreePath path) { TreePath parentPath = path.getParentPath(); if (parentPath != null) { Tree parent = parentPath.getLeaf(); if (parent.getKind() == Tree.Kind.INTERSECTION_TYPE) { IntersectionTypeTree itt = (IntersectionTypeTree) parent; List<? extends Tree> bounds = itt.getBounds(); Tree leaf = path.getLeaf(); if (typeIndex < bounds.size() && leaf == bounds.get(typeIndex)) { return true; } } } Tree.Kind kind = path.getLeaf().getKind(); if (ASTPath.isTypeKind(kind) || kind == Tree.Kind.MEMBER_SELECT) { return isSatisfiedBy(path.getParentPath()); } return false; }
Example 6
Source File: ProspectiveOperation.java From netbeans with Apache License 2.0 | 5 votes |
private Tree.Kind getSuitableOperator(Tree.Kind kind) { if (Tree.Kind.AND_ASSIGNMENT == kind) { return Tree.Kind.AND; } if (Tree.Kind.OR_ASSIGNMENT == kind) { return Tree.Kind.OR; } if (Tree.Kind.PLUS_ASSIGNMENT == kind) { return Tree.Kind.PLUS; } if (Tree.Kind.MINUS_ASSIGNMENT == kind) { return Tree.Kind.MINUS; } if (Tree.Kind.DIVIDE_ASSIGNMENT == kind) { return Tree.Kind.DIVIDE; } if (Tree.Kind.MULTIPLY_ASSIGNMENT == kind) { return Tree.Kind.MULTIPLY; } if (Tree.Kind.REMAINDER_ASSIGNMENT == kind) { return Tree.Kind.REMAINDER; } if (Tree.Kind.LEFT_SHIFT_ASSIGNMENT == kind) { return Tree.Kind.LEFT_SHIFT; } if (Tree.Kind.RIGHT_SHIFT_ASSIGNMENT == kind) { return Tree.Kind.RIGHT_SHIFT; } if (Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT == kind) { return Tree.Kind.UNSIGNED_RIGHT_SHIFT; } return null; }
Example 7
Source File: UseEntityManagerCodeGenerator.java From netbeans with Apache License 2.0 | 5 votes |
public static TreePath getPathElementOfKind(Set<Tree.Kind> kinds, TreePath path) { while (path != null) { if (kinds.contains(path.getLeaf().getKind())) return path; path = path.getParentPath(); } return null; }
Example 8
Source File: GoToOppositeAction.java From netbeans with Apache License 2.0 | 5 votes |
/** */ public void run(CompilationController controller) throws IOException { controller.toPhase(Phase.RESOLVED); //cursor position needed if (cancelled) { return; } TreePath treePath = controller.getTreeUtilities() .pathFor(caretPosition); if (treePath != null) { if (cancelled) { return; } TreePath parent = treePath.getParentPath(); while (parent != null) { Tree.Kind parentKind = parent.getLeaf().getKind(); if ((TreeUtilities.CLASS_TREE_KINDS.contains(parentKind)) || (parentKind == Tree.Kind.COMPILATION_UNIT)) { break; } treePath = parent; parent = treePath.getParentPath(); } } if (treePath != null) { if (cancelled) { return; } try { element = controller.getTrees().getElement(treePath); } catch (IllegalArgumentException ex) { Logger.getLogger("global").log(Level.WARNING, null, ex); } } }
Example 9
Source File: GeneratorUtilities.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Object visitBlock(BlockTree node, Object p) { Tree parent = getCurrentPath().getParentPath().getLeaf(); Tree.Kind k = parent.getKind(); if (k == Tree.Kind.CLASS || k == Tree.Kind.INTERFACE || k == Tree.Kind.ENUM) { member = node; return super.visitBlock(node, p); } return p; }
Example 10
Source File: ASTPath.java From annotation-tools with MIT License | 5 votes |
/** * Determines if the given kind is a declaration. * * @param kind * the kind to test * @return true if the given kind is a declaration */ public static boolean isDeclaration(Tree.Kind kind) { return kind == Tree.Kind.ANNOTATION || kind == Tree.Kind.CLASS || kind == Tree.Kind.ENUM || kind == Tree.Kind.INTERFACE || kind == Tree.Kind.METHOD || kind == Tree.Kind.VARIABLE; }
Example 11
Source File: UseEntityManagerCodeGenerator.java From netbeans with Apache License 2.0 | 4 votes |
public static TreePath getPathElementOfKind(Tree.Kind kind, TreePath path) { return getPathElementOfKind(EnumSet.of(kind), path); }
Example 12
Source File: Utilities.java From netbeans with Apache License 2.0 | 4 votes |
public static TreePath getPathElementOfKind(Tree.Kind kind, TreePath path) { return getPathElementOfKind(EnumSet.of(kind), path); }
Example 13
Source File: PrefixExpression.java From j2objc with Apache License 2.0 | 4 votes |
Operator(String opString, Tree.Kind javacKind) { this.opString = opString; this.javacKind = javacKind; }
Example 14
Source File: UnusedAssignmentOrBranch.java From netbeans with Apache License 2.0 | 4 votes |
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.bugs.UnusedAssignmentOrBranch.unusedCompoundAssignment", description = "#DESC_org.netbeans.modules.java.hints.bugs.UnusedAssignmentOrBranch.unusedCompoundAssignment", category="bugs", id=UNUSED_COMPOUND_ASSIGNMENT_ID, options={Options.QUERY}, suppressWarnings="UnusedAssignment") @TriggerPatterns({ @TriggerPattern("$var |= $expr"), @TriggerPattern("$var &= $expr"), @TriggerPattern("$var += $expr"), @TriggerPattern("$var -= $expr"), @TriggerPattern("$var *= $expr"), @TriggerPattern("$var /= $expr"), @TriggerPattern("$var %= $expr"), @TriggerPattern("$var >>= $expr"), @TriggerPattern("$var <<= $expr"), @TriggerPattern("$var >>>= $expr") }) @NbBundle.Messages({ "LBL_UnusedCompoundAssignmentLabel=The target variable's value is never used", "FIX_ChangeCompoundAssignmentToOperation=Change compound assignment to operation" }) public static ErrorDescription unusedCompoundAssignment(final HintContext ctx) { final String unusedAssignmentLabel = Bundle.LBL_UnusedCompoundAssignmentLabel(); Pair<Set<Tree>, Set<Element>> computedAssignments = computeUsedAssignments(ctx); if (ctx.isCanceled() || computedAssignments == null) return null; final CompilationInfo info = ctx.getInfo(); final Set<Tree> usedAssignments = computedAssignments.first(); final Set<Element> usedVariables = computedAssignments.second(); final Element var = info.getTrees().getElement(ctx.getVariables().get("$var")); // NOI18N final TreePath valuePath = ctx.getVariables().get("$expr"); // NOI18N final Tree value = ctx.getPath().getLeaf(); final TypeMirror tm = info.getTrees().getTypeMirror(ctx.getPath()); final boolean sideEffects; final boolean booleanOp = Utilities.isValidType(tm) && tm.getKind() == TypeKind.BOOLEAN; Tree.Kind kind = value.getKind(); if (booleanOp) { sideEffects = mayHaveSideEffects(ctx, valuePath); } else { sideEffects = false; } if (var != null && LOCAL_VARIABLES.contains(var.getKind()) && !usedAssignments.contains(value) && usedVariables.contains(var)) { String replace; switch (kind) { case AND_ASSIGNMENT: if (booleanOp) { replace = sideEffects ? "$expr && $var" : "$var && $expr"; // NOI18N } else { replace = "$var & $expr"; // NOI18N } break; case OR_ASSIGNMENT: if (booleanOp) { replace = sideEffects ? "$expr || $var" : "$var || $expr"; // NOI18N } else { replace = "$var | $expr"; // NOI18N } break; case PLUS_ASSIGNMENT: replace = "$var + $expr"; // NOI18N break; case MINUS_ASSIGNMENT: replace = "$var - $expr"; // NOI18N break; case MULTIPLY_ASSIGNMENT: replace = "$var * $expr"; // NOI18N break; case DIVIDE_ASSIGNMENT: replace = "$var / $expr"; // NOI18N break; case REMAINDER_ASSIGNMENT: replace = "$var % $expr"; // NOI18N break; case LEFT_SHIFT_ASSIGNMENT: replace = "$var << $expr"; // NOI18N break; case RIGHT_SHIFT_ASSIGNMENT: replace = "$var >> $expr"; // NOI18N break; case UNSIGNED_RIGHT_SHIFT_ASSIGNMENT: replace = "$var >>> $expr"; // NOI18N break; default: return null; } return ErrorDescriptionFactory.forTree(ctx, value, unusedAssignmentLabel, JavaFixUtilities.rewriteFix(ctx, Bundle.FIX_ChangeCompoundAssignmentToOperation(), ctx.getPath(), replace) ); } return null; }
Example 15
Source File: ASTPath.java From annotation-tools with MIT License | 3 votes |
/** * Determines if the given kinds match, false otherwise. Two kinds match if * they're exactly the same or if the two kinds are both compound * assignments, unary operators, binary operators or wildcards. * <p> * This is necessary because in the JAIF file these kinds are represented by * their general types (i.e. BinaryOperator, CompoundOperator, etc.) rather * than their kind (i.e. PLUS, MINUS, PLUS_ASSIGNMENT, XOR_ASSIGNMENT, * etc.). Internally, a single kind is used to represent each general type * (i.e. PLUS is used for BinaryOperator, PLUS_ASSIGNMENT is used for * CompoundAssignment, etc.). Yet, the actual source nodes have the correct * kind. So if an AST path entry has a PLUS kind, that really means it could * be any BinaryOperator, resulting in PLUS matching any other * BinaryOperator. * * @param kind1 * the first kind to match * @param kind2 * the second kind to match * @return {@code true} if the kinds match as described above, {@code false} * otherwise. */ private static boolean kindsMatch(Tree.Kind kind1, Tree.Kind kind2) { return kind1 == kind2 || (isCompoundAssignment(kind1) && isCompoundAssignment(kind2)) || (isUnaryOperator(kind1) && isUnaryOperator(kind2)) || (isBinaryOperator(kind1) && isBinaryOperator(kind2)) || (isWildcard(kind1) && isWildcard(kind2)); }
Example 16
Source File: ASTPath.java From annotation-tools with MIT License | 2 votes |
/** * Determines if the given kind is a binary operator. * * @param kind * the kind to test * @return true if the given kind is a binary operator */ public static boolean isBinaryOperator(Tree.Kind kind) { return kind.asInterface().equals(BinaryTree.class); }
Example 17
Source File: ASTPath.java From annotation-tools with MIT License | 2 votes |
/** * Gets the tree node equivalent kind of this AST entry. For example, in * <pre> * {@code * Block.statement 3 * }</pre> * "Block" is the tree kind. * @return the tree kind */ public Tree.Kind getTreeKind() { return treeKind; }
Example 18
Source File: TreePathHandle.java From netbeans with Apache License 2.0 | 2 votes |
/** * Returns the {@link Tree.Kind} of this TreePathHandle, * it returns the kind of the {@link Tree} from which the handle * was created. * * @return {@link Tree.Kind} */ public Tree.Kind getKind() { return this.delegate.getKind(); }
Example 19
Source File: ASTPath.java From annotation-tools with MIT License | 2 votes |
/** * Determines if the given kind is a wildcard. * * @param kind * the kind to test * @return true if the given kind is a wildcard */ public static boolean isWildcard(Tree.Kind kind) { return kind.asInterface().equals(WildcardTree.class); }
Example 20
Source File: TreePathHandle.java From netbeans with Apache License 2.0 | votes |
public Tree.Kind getKind();