Java Code Examples for com.sun.source.tree.Tree.Kind#ASSIGNMENT

The following examples show how to use com.sun.source.tree.Tree.Kind#ASSIGNMENT . 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: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static ExpressionTree findValue(AnnotationTree m, String name) {
    for (ExpressionTree et : m.getArguments()) {
        if (et.getKind() == Kind.ASSIGNMENT) {
            AssignmentTree at = (AssignmentTree) et;
            String varName = ((IdentifierTree) at.getVariable()).getName().toString();

            if (varName.equals(name)) {
                return at.getExpression();
            }
        }

        if (et instanceof LiteralTree/*XXX*/ && "value".equals(name)) {
            return et;
        }
    }

    return null;
}
 
Example 2
Source File: AddParameterOrLocalFix.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void resolveResourceVariable(final WorkingCopy wc, TreePath tp, TreeMaker make, TypeMirror proposedType) {
    final String name = ((IdentifierTree) tp.getLeaf()).getName().toString();

    final Element el = wc.getTrees().getElement(tp);
    if (el == null) {
        return;
    }

    if (tp.getParentPath().getLeaf().getKind() != Kind.ASSIGNMENT) {
        //?
        return ;
    }

    AssignmentTree at = (AssignmentTree) tp.getParentPath().getLeaf();
    VariableTree vt = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, make.Type(proposedType), at.getExpression());

    wc.rewrite(at, vt);
}
 
Example 3
Source File: XPFlagCleaner.java    From piranha with Apache License 2.0 5 votes vote down vote up
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {

  for (String name : handledAnnotations) {
    AnnotationTree at =
        ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), name);

    if (at != null) {
      for (ExpressionTree et : at.getArguments()) {
        if (et.getKind() == Kind.ASSIGNMENT) {
          AssignmentTree assn = (AssignmentTree) et;
          if (ASTHelpers.getSymbol(assn.getExpression())
              .getQualifiedName()
              .toString()
              .endsWith(xpFlagName)) {
            Description.Builder builder = buildDescription(tree);
            SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
            if (isTreated) {
              fixBuilder.delete(at);
              decrementAllSymbolUsages(at, state, fixBuilder);
            } else {
              fixBuilder.delete(tree);
              decrementAllSymbolUsages(tree, state, fixBuilder);
            }
            builder.addFix(fixBuilder.build());
            return builder.build();
          }
        }
      }
    }
  }

  return Description.NO_MATCH;
}
 
Example 4
Source File: CopyFinder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean visitAnnotation(AnnotationTree node, TreePath p) {
    if (p == null)
        return super.visitAnnotation(node, p);

    AnnotationTree t = (AnnotationTree) p.getLeaf();
    
    List<? extends ExpressionTree> arguments = t.getArguments();
    
    if (arguments.size() == 1) {
        ExpressionTree arg = arguments.get(0);
        
        if (arg.getKind() == Kind.ASSIGNMENT) {
            AssignmentTree at = (AssignmentTree) arg;
            
            if (   at.getVariable().getKind() == Kind.IDENTIFIER
                && isMultistatementWildcardTree(at.getExpression())
                && ((IdentifierTree) at.getVariable()).getName().contentEquals("value")) {
                arguments = Collections.singletonList(at.getExpression());
            }
        }
    }
    
    if (!checkLists(node.getArguments(), arguments, p))
        return false;

    return scan(node.getAnnotationType(), t.getAnnotationType(), p);
}
 
Example 5
Source File: IteratorToFor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@TriggerPattern(value="for (int $index = 0; $index < $arr.length; $index++) $statement;", constraints=@ConstraintVariableType(variable="$arr", type="Object[]"))
public static ErrorDescription forIndexedArray(final HintContext ctx) {
    AccessAndVarVisitor v = new AccessAndVarVisitor(ctx) {
        @Override public Void visitArrayAccess(ArrayAccessTree node, Void p) {
        TreePath path = getCurrentPath();
            if (MatcherUtilities.matches(ctx, path, "$arr[$index]")) { // NOI18N
                if (path.getParentPath() != null) {
                    if (   path.getParentPath().getLeaf().getKind() == Kind.ASSIGNMENT
                        && ((AssignmentTree) path.getParentPath().getLeaf()).getVariable() == node) {
                        unsuitable();
                    }
                    if (CompoundAssignmentTree.class.isAssignableFrom(path.getParentPath().getLeaf().getKind().asInterface())
                        && ((CompoundAssignmentTree) path.getParentPath().getLeaf()).getVariable() == node) {
                        unsuitable();
                    }
                }
                toReplace.add(path);
                return null;
            }
            return super.visitArrayAccess(node, p);
        }
    };
    v.scan(ctx.getVariables().get("$statement"), null); // NOI18N
    if (v.unsuitable) return null;
    
    return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_IteratorToForArray(), 
            new ReplaceIndexedForEachLoop(ctx.getInfo(), ctx.getPath(), ctx.getVariables().get("$arr"), 
            v.toReplace, v.definedVariables).toEditorFix());
}
 
Example 6
Source File: AddParameterOrLocalFix.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** 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 7
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Name getLeftTreeName(StatementTree statement) {
    if (statement.getKind() != Kind.EXPRESSION_STATEMENT) {
        return null;
    }
    JCTree.JCExpressionStatement jceTree = (JCTree.JCExpressionStatement) statement;
    if (jceTree.expr.getKind() != Kind.ASSIGNMENT) {
        return null;
    }
    JCTree.JCAssign assignTree = (JCTree.JCAssign) jceTree.expr;
    return ((JCTree.JCIdent) assignTree.lhs).name;
}
 
Example 8
Source File: AssignmentIssues.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToMethodParam", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToMethodParam", category = "assignment_issues", enabled = false, suppressWarnings = "AssignmentToMethodParameter", options=Options.QUERY) //NOI18N
@TriggerTreeKind({Kind.ASSIGNMENT, Kind.AND_ASSIGNMENT, Kind.DIVIDE_ASSIGNMENT,
    Kind.LEFT_SHIFT_ASSIGNMENT, Kind.MINUS_ASSIGNMENT, Kind.MULTIPLY_ASSIGNMENT,
    Kind.OR_ASSIGNMENT, Kind.PLUS_ASSIGNMENT, Kind.REMAINDER_ASSIGNMENT, Kind.RIGHT_SHIFT_ASSIGNMENT,
    Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT, Kind.XOR_ASSIGNMENT, Kind.PREFIX_INCREMENT,
    Kind.PREFIX_DECREMENT, Kind.POSTFIX_INCREMENT, Kind.POSTFIX_DECREMENT})
public static ErrorDescription assignmentToMethodParam(HintContext context) {
    final TreePath path = context.getPath();
    Element element = null;
    switch (path.getLeaf().getKind()) {
        case ASSIGNMENT:
            element = context.getInfo().getTrees().getElement(TreePath.getPath(path, ((AssignmentTree) path.getLeaf()).getVariable()));
            break;
        case PREFIX_INCREMENT:
        case PREFIX_DECREMENT:
        case POSTFIX_INCREMENT:
        case POSTFIX_DECREMENT:
            element = context.getInfo().getTrees().getElement(TreePath.getPath(path, ((UnaryTree) path.getLeaf()).getExpression()));
            break;
        default:
            element = context.getInfo().getTrees().getElement(TreePath.getPath(path, ((CompoundAssignmentTree) path.getLeaf()).getVariable()));
    }
    if (element != null && element.getKind() == ElementKind.PARAMETER) {
        return ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class, "MSG_AssignmentToMethodParam", element.getSimpleName())); //NOI18N
    }
    return null;
}
 
Example 9
Source File: AssignmentIssues.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.AssignmentIssues.nestedAssignment", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.nestedAssignment", category = "assignment_issues", enabled = false, suppressWarnings = "NestedAssignment", options=Options.QUERY) //NOI18N
@TriggerTreeKind({Kind.ASSIGNMENT, Kind.AND_ASSIGNMENT, Kind.DIVIDE_ASSIGNMENT,
    Kind.LEFT_SHIFT_ASSIGNMENT, Kind.MINUS_ASSIGNMENT, Kind.MULTIPLY_ASSIGNMENT,
    Kind.OR_ASSIGNMENT, Kind.PLUS_ASSIGNMENT, Kind.REMAINDER_ASSIGNMENT, Kind.RIGHT_SHIFT_ASSIGNMENT,
    Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT, Kind.XOR_ASSIGNMENT})
public static ErrorDescription nestedAssignment(HintContext context) {
    final TreePath path = context.getPath();
    final Kind parentKind = path.getParentPath().getLeaf().getKind();
    if (parentKind != Kind.EXPRESSION_STATEMENT && parentKind != Kind.ANNOTATION) {
        return ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class, "MSG_NestedAssignment", path.getLeaf())); //NOI18N
    }
    return null;
}
 
Example 10
Source File: IntroduceHint.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static TreePath validateSelection(CompilationInfo ci, int start, int end, Set<TypeKind> ignoredTypes) {
    int[] span = TreeUtils.ignoreWhitespaces(ci, Math.min(start, end), Math.max(start, end));

    start = span[0];
    end   = span[1];
    
    TreePath tp = ci.getTreeUtilities().pathFor((start + end) / 2 + 1);

    for ( ; tp != null; tp = tp.getParentPath()) {
        Tree leaf = tp.getLeaf();

        if (   !ExpressionTree.class.isAssignableFrom(leaf.getKind().asInterface())
            && (leaf.getKind() != Kind.VARIABLE || ((VariableTree) leaf).getInitializer() == null))
           continue;

        long treeStart = ci.getTrees().getSourcePositions().getStartPosition(ci.getCompilationUnit(), leaf);
        long treeEnd   = ci.getTrees().getSourcePositions().getEndPosition(ci.getCompilationUnit(), leaf);

        if (treeStart != start || treeEnd != end) {
            continue;
        }

        TypeMirror type = ci.getTrees().getTypeMirror(tp);

        if (type != null && type.getKind() == TypeKind.ERROR) {
            type = ci.getTrees().getOriginalType((ErrorType) type);
        }

        if (type == null || ignoredTypes.contains(type.getKind()))
            continue;

        if(tp.getLeaf().getKind() == Kind.ASSIGNMENT)
            continue;

        if (tp.getLeaf().getKind() == Kind.ANNOTATION)
            continue;

        if (!TreeUtils.isInsideClass(tp))
            return null;

        TreePath candidate = tp;

        tp = tp.getParentPath();

        while (tp != null) {
            switch (tp.getLeaf().getKind()) {
                case VARIABLE:
                    VariableTree vt = (VariableTree) tp.getLeaf();
                    if (vt.getInitializer() == leaf) {
                        return candidate;
                    } else {
                        return null;
                    }
                case NEW_CLASS:
                    NewClassTree nct = (NewClassTree) tp.getLeaf();
                    
                    if (nct.getIdentifier().equals(candidate.getLeaf())) { //avoid disabling hint ie inside of anonymous class higher in treepath
                        for (Tree p : nct.getArguments()) {
                            if (p == leaf) {
                                return candidate;
                            }
                        }

                        return null;
                    }
            }

            leaf = tp.getLeaf();
            tp = tp.getParentPath();
        }

        return candidate;
    }

    return null;
}