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

The following examples show how to use com.sun.source.tree.Tree.Kind#PREFIX_DECREMENT . 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: 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 2
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.incrementDecrementUsed", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.incrementDecrementUsed", category = "assignment_issues", enabled = false, suppressWarnings = "ValueOfIncrementOrDecrementUsed", options=Options.QUERY) //NOI18N
@TriggerTreeKind({Kind.PREFIX_INCREMENT, Kind.PREFIX_DECREMENT, Kind.POSTFIX_INCREMENT, Kind.POSTFIX_DECREMENT})
public static ErrorDescription incrementDecrementUsed(HintContext context) {
    final TreePath path = context.getPath();
    if (path.getParentPath().getLeaf().getKind() != Kind.EXPRESSION_STATEMENT) {
        final Kind kind = path.getLeaf().getKind();
        return ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class,
                kind == Kind.PREFIX_INCREMENT || kind == Kind.POSTFIX_INCREMENT
                ? "MSG_IncrementUsedAsExpression" : "MSG_DecrementUsedAsExpression", path.getLeaf())); //NOI18N
    }
    return null;
}
 
Example 3
Source File: EncapsulateFieldRefactoringPlugin.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Tree visitUnary(UnaryTree node, Element field) {
    ExpressionTree t = node.getExpression();
    Kind kind = node.getKind();
    boolean isArrayOrImmutable = kind != Kind.POSTFIX_DECREMENT
            && kind != Kind.POSTFIX_INCREMENT
            && kind != Kind.PREFIX_DECREMENT
            && kind != Kind.PREFIX_INCREMENT;
    while (t.getKind() == Tree.Kind.ARRAY_ACCESS) {
        isArrayOrImmutable = true;
        t = ((ArrayAccessTree) t).getExpression();
    }
    Element el = workingCopy.getTrees().getElement(new TreePath(getCurrentPath(), t));
    EncapsulateDesc desc = el == null ? null : fields.get(el);
    if (desc != null && desc.useAccessors
            && desc.refactoring.getGetterName() != null
            && (isArrayOrImmutable || checkAssignmentInsideExpression())
            && !isInConstructorOfFieldClass(getCurrentPath(), desc.field)
            && !isInGetterSetter(getCurrentPath(), desc.currentGetter, desc.currentSetter)) {
        // check (++field + 3)
        ExpressionTree invkgetter = createGetterInvokation(t, desc.refactoring.getGetterName());
        if (isArrayOrImmutable) {
            rewrite(t, invkgetter);
        } else if (desc.refactoring.getSetterName() != null) {
            ExpressionTree setter = createMemberSelection(node.getExpression(), desc.refactoring.getSetterName());

            Tree.Kind operator = kind == Tree.Kind.POSTFIX_INCREMENT || kind == Tree.Kind.PREFIX_INCREMENT
                    ? Tree.Kind.PLUS
                    : Tree.Kind.MINUS;

            // resolve types
            Trees trees = workingCopy.getTrees();
            ExpressionTree expTree = node.getExpression();
            TreePath varPath = trees.getPath(workingCopy.getCompilationUnit(), expTree);
            TypeMirror varType = trees.getTypeMirror(varPath);
            TypeMirror expType = workingCopy.getTypes().getPrimitiveType(TypeKind.INT);
            ExpressionTree newExpTree = make.Binary(operator, invkgetter, make.Literal(1));
            if (!workingCopy.getTypes().isSubtype(expType, varType)) {
                newExpTree = make.TypeCast(make.Type(varType), make.Parenthesized(newExpTree));
            }

            MethodInvocationTree invksetter = make.MethodInvocation(
                    Collections.<ExpressionTree>emptyList(),
                    setter,
                    Collections.singletonList(newExpTree));
            rewrite(node, invksetter);
        }
    }
    return null;
}