Search in sources :

Example 31 with MessageSend

use of org.eclipse.jdt.internal.compiler.ast.MessageSend in project luna-java-formatter by alostale.

the class CodeFormatterVisitor method formatCascadingMessageSends.

private void formatCascadingMessageSends(CascadingMethodInvocationFragmentBuilder builder, BlockScope scope) {
    int size = builder.size();
    MessageSend[] fragments = builder.fragments();
    Expression fragment = fragments[0].receiver;
    int startingPositionInCascade = 1;
    if (!fragment.isImplicitThis()) {
        fragment.traverse(this, scope);
    } else {
        MessageSend currentMessageSend = fragments[1];
        final int numberOfParens = (currentMessageSend.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT;
        if (numberOfParens > 0) {
            manageOpeningParenthesizedExpression(currentMessageSend, numberOfParens);
        }
        ASTNode[] arguments = currentMessageSend.arguments;
        TypeReference[] typeArguments = currentMessageSend.typeArguments;
        if (typeArguments != null) {
            this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_arguments);
            if (this.preferences.insert_space_after_opening_angle_bracket_in_type_arguments) {
                this.scribe.space();
            }
            int length = typeArguments.length;
            for (int i = 0; i < length - 1; i++) {
                typeArguments[i].traverse(this, scope);
                this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_arguments);
                if (this.preferences.insert_space_after_comma_in_type_arguments) {
                    this.scribe.space();
                }
            }
            typeArguments[length - 1].traverse(this, scope);
            if (isClosingGenericToken()) {
                this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_arguments);
            }
            if (this.preferences.insert_space_after_closing_angle_bracket_in_type_arguments) {
                this.scribe.space();
            }
        }
        // selector
        this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier);
        this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_method_invocation);
        if (arguments != null) {
            if (this.preferences.insert_space_after_opening_paren_in_method_invocation) {
                this.scribe.space();
            }
            int argumentLength = arguments.length;
            Alignment argumentsAlignment = this.scribe.createAlignment(Alignment.MESSAGE_ARGUMENTS, this.preferences.alignment_for_arguments_in_method_invocation, Alignment.R_OUTERMOST, argumentLength, this.scribe.scanner.currentPosition);
            this.scribe.enterAlignment(argumentsAlignment);
            boolean okForArguments = false;
            do {
                try {
                    for (int j = 0; j < argumentLength; j++) {
                        if (j > 0) {
                            this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments);
                            this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
                        }
                        this.scribe.alignFragment(argumentsAlignment, j);
                        if (j > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) {
                            this.scribe.space();
                        }
                        arguments[j].traverse(this, scope);
                    }
                    okForArguments = true;
                } catch (AlignmentException e) {
                    this.scribe.redoAlignment(e);
                }
            } while (!okForArguments);
            this.scribe.exitAlignment(argumentsAlignment, true);
            this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_method_invocation);
        } else {
            this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_method_invocation);
        }
        if (numberOfParens > 0) {
            manageClosingParenthesizedExpression(currentMessageSend, numberOfParens);
        }
        startingPositionInCascade = 2;
    }
    int tieBreakRule = this.preferences.wrap_outer_expressions_when_nested && size - startingPositionInCascade > 2 ? Alignment.R_OUTERMOST : Alignment.R_INNERMOST;
    Alignment cascadingMessageSendAlignment = this.scribe.createAlignment(Alignment.CASCADING_MESSAGE_SEND, this.preferences.alignment_for_selector_in_method_invocation, tieBreakRule, size, this.scribe.scanner.currentPosition);
    this.scribe.enterAlignment(cascadingMessageSendAlignment);
    boolean ok = false;
    boolean setStartingColumn = true;
    switch(this.preferences.alignment_for_arguments_in_method_invocation & Alignment.SPLIT_MASK) {
        case Alignment.M_COMPACT_FIRST_BREAK_SPLIT:
        case Alignment.M_NEXT_SHIFTED_SPLIT:
        case Alignment.M_ONE_PER_LINE_SPLIT:
            setStartingColumn = false;
            break;
    }
    do {
        if (setStartingColumn) {
            cascadingMessageSendAlignment.startingColumn = this.scribe.column;
        }
        try {
            this.scribe.alignFragment(cascadingMessageSendAlignment, 0);
            this.scribe.printNextToken(TerminalTokens.TokenNameDOT);
            for (int i = startingPositionInCascade; i < size; i++) {
                MessageSend currentMessageSend = fragments[i];
                final int numberOfParens = (currentMessageSend.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT;
                if (numberOfParens > 0) {
                    manageOpeningParenthesizedExpression(currentMessageSend, numberOfParens);
                }
                TypeReference[] typeArguments = currentMessageSend.typeArguments;
                if (typeArguments != null) {
                    this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_arguments);
                    if (this.preferences.insert_space_after_opening_angle_bracket_in_type_arguments) {
                        this.scribe.space();
                    }
                    int length = typeArguments.length;
                    for (int j = 0; j < length - 1; j++) {
                        typeArguments[j].traverse(this, scope);
                        this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_arguments);
                        if (this.preferences.insert_space_after_comma_in_type_arguments) {
                            this.scribe.space();
                        }
                    }
                    typeArguments[length - 1].traverse(this, scope);
                    if (isClosingGenericToken()) {
                        this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_arguments);
                    }
                    if (this.preferences.insert_space_after_closing_angle_bracket_in_type_arguments) {
                        this.scribe.space();
                    }
                }
                ASTNode[] arguments = currentMessageSend.arguments;
                // selector
                this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier);
                this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_method_invocation);
                if (arguments != null) {
                    if (this.preferences.insert_space_after_opening_paren_in_method_invocation) {
                        this.scribe.space();
                    }
                    int argumentLength = arguments.length;
                    int alignmentMode = this.preferences.alignment_for_arguments_in_method_invocation;
                    Alignment argumentsAlignment = this.scribe.createAlignment(Alignment.MESSAGE_ARGUMENTS, alignmentMode, Alignment.R_OUTERMOST, argumentLength, this.scribe.scanner.currentPosition);
                    this.scribe.enterAlignment(argumentsAlignment);
                    boolean okForArguments = false;
                    do {
                        switch(alignmentMode & Alignment.SPLIT_MASK) {
                            case Alignment.M_COMPACT_SPLIT:
                            case Alignment.M_NEXT_PER_LINE_SPLIT:
                                argumentsAlignment.startingColumn = this.scribe.column;
                                break;
                        }
                        try {
                            for (int j = 0; j < argumentLength; j++) {
                                if (j > 0) {
                                    this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments);
                                    this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
                                }
                                this.scribe.alignFragment(argumentsAlignment, j);
                                if (j == 0) {
                                    int fragmentIndentation = argumentsAlignment.fragmentIndentations[j];
                                    if ((argumentsAlignment.mode & Alignment.M_INDENT_ON_COLUMN) != 0 && fragmentIndentation > 0) {
                                        this.scribe.indentationLevel = fragmentIndentation;
                                    }
                                } else if (this.preferences.insert_space_after_comma_in_method_invocation_arguments) {
                                    this.scribe.space();
                                }
                                arguments[j].traverse(this, scope);
                                argumentsAlignment.startingColumn = -1;
                            }
                            okForArguments = true;
                        } catch (AlignmentException e) {
                            this.scribe.redoAlignment(e);
                        }
                    } while (!okForArguments);
                    this.scribe.exitAlignment(argumentsAlignment, true);
                    this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_method_invocation);
                } else {
                    this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_method_invocation);
                }
                if (numberOfParens > 0) {
                    manageClosingParenthesizedExpression(currentMessageSend, numberOfParens);
                }
                cascadingMessageSendAlignment.startingColumn = -1;
                if (i < size - 1) {
                    this.scribe.alignFragment(cascadingMessageSendAlignment, i);
                    this.scribe.printNextToken(TerminalTokens.TokenNameDOT);
                }
            }
            ok = true;
        } catch (AlignmentException e) {
            this.scribe.redoAlignment(e);
        }
    } while (!ok);
    this.scribe.exitAlignment(cascadingMessageSendAlignment, true);
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) Alignment(org.eclipse.jdt.luna.formatter.align.Alignment) BinaryExpression(org.eclipse.jdt.internal.compiler.ast.BinaryExpression) InstanceOfExpression(org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression) LambdaExpression(org.eclipse.jdt.internal.compiler.ast.LambdaExpression) PrefixExpression(org.eclipse.jdt.internal.compiler.ast.PrefixExpression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) OR_OR_Expression(org.eclipse.jdt.internal.compiler.ast.OR_OR_Expression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) CastExpression(org.eclipse.jdt.internal.compiler.ast.CastExpression) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) CombinedBinaryExpression(org.eclipse.jdt.internal.compiler.ast.CombinedBinaryExpression) PostfixExpression(org.eclipse.jdt.internal.compiler.ast.PostfixExpression) ArrayAllocationExpression(org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) AND_AND_Expression(org.eclipse.jdt.internal.compiler.ast.AND_AND_Expression) UnaryExpression(org.eclipse.jdt.internal.compiler.ast.UnaryExpression) AlignmentException(org.eclipse.jdt.luna.formatter.align.AlignmentException) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) IntersectionCastTypeReference(org.eclipse.jdt.internal.compiler.ast.IntersectionCastTypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) UnionTypeReference(org.eclipse.jdt.internal.compiler.ast.UnionTypeReference)

Example 32 with MessageSend

use of org.eclipse.jdt.internal.compiler.ast.MessageSend in project bazel-jdt-java-toolchain by salesforce.

the class ConstraintExpressionFormula method reduce.

@Override
public Object reduce(InferenceContext18 inferenceContext) throws InferenceFailureException {
    if (this.relation == POTENTIALLY_COMPATIBLE) {
        return this.left.isPotentiallyCompatibleWith(this.right, inferenceContext.scope) ? TRUE : FALSE;
    }
    // JLS 18.2.1
    if (this.right.isProperType(true)) {
        if (this.left.isCompatibleWith(this.right, inferenceContext.scope) || this.left.isBoxingCompatibleWith(this.right, inferenceContext.scope)) {
            if (this.left.resolvedType != null && this.left.resolvedType.needsUncheckedConversion(this.right)) {
                inferenceContext.usesUncheckedConversion = true;
            }
            return TRUE;
        }
        return FALSE;
    }
    if (!canBePolyExpression(this.left)) {
        TypeBinding exprType = this.left.resolvedType;
        if (exprType == null || !exprType.isValidBinding()) {
            if (this.left instanceof MessageSend && ((MessageSend) this.left).actualReceiverType instanceof InferenceVariable)
                // nothing valuable to infer from this
                return null;
            return FALSE;
        }
        return ConstraintTypeFormula.create(exprType, this.right, COMPATIBLE, this.isSoft);
    } else {
        // - parenthesized expression : these are transparent in our AST
        if (this.left instanceof Invocation) {
            Invocation invocation = (Invocation) this.left;
            MethodBinding previousMethod = invocation.binding();
            if (// can happen, e.g., if inside a copied lambda with ignored errors
            previousMethod == null)
                // -> proceed with no new constraints
                return null;
            MethodBinding method = previousMethod;
            // ignore previous (inner) inference result and do a fresh start:
            // avoid original(), since we only want to discard one level of instantiation
            // (method type variables - not class type variables)!
            method = previousMethod.shallowOriginal();
            SuspendedInferenceRecord prevInvocation = inferenceContext.enterPolyInvocation(invocation, invocation.arguments());
            // Invocation Applicability Inference: 18.5.1 & Invocation Type Inference: 18.5.2
            InferenceContext18 innerCtx = null;
            try {
                Expression[] arguments = invocation.arguments();
                TypeBinding[] argumentTypes = arguments == null ? Binding.NO_PARAMETERS : new TypeBinding[arguments.length];
                for (int i = 0; i < argumentTypes.length; i++) argumentTypes[i] = arguments[i].resolvedType;
                if (previousMethod instanceof ParameterizedGenericMethodBinding) {
                    // find the previous inner inference context to see what inference kind this invocation needs:
                    innerCtx = invocation.getInferenceContext((ParameterizedGenericMethodBinding) previousMethod);
                    if (innerCtx == null) {
                        /* No inference context -> the method was likely manufactured by Scope.findExactMethod -> assume it wasn't really poly after all.
							   -> proceed as for non-poly expressions.
							*/
                        TypeBinding exprType = this.left.resolvedType;
                        if (exprType == null || !exprType.isValidBinding())
                            return FALSE;
                        return ConstraintTypeFormula.create(exprType, this.right, COMPATIBLE, this.isSoft);
                    }
                    if (innerCtx.stepCompleted >= InferenceContext18.APPLICABILITY_INFERRED) {
                        inferenceContext.integrateInnerInferenceB2(innerCtx);
                    } else {
                        // should not reach here.
                        return FALSE;
                    }
                // b2 has been lifted, inferring poly invocation type amounts to lifting b3.
                } else {
                    inferenceContext.inferenceKind = inferenceContext.getInferenceKind(previousMethod, argumentTypes);
                    boolean isDiamond = method.isConstructor() && this.left.isPolyExpression(method);
                    inferInvocationApplicability(inferenceContext, method, argumentTypes, isDiamond, inferenceContext.inferenceKind);
                // b2 has been lifted, inferring poly invocation type amounts to lifting b3.
                }
                if (!inferenceContext.computeB3(invocation, this.right, method))
                    return FALSE;
                // already incorporated
                return null;
            } finally {
                inferenceContext.resumeSuspendedInference(prevInvocation, innerCtx);
            }
        } else if (this.left instanceof ConditionalExpression) {
            ConditionalExpression conditional = (ConditionalExpression) this.left;
            return new ConstraintFormula[] { new ConstraintExpressionFormula(conditional.valueIfTrue, this.right, this.relation, this.isSoft), new ConstraintExpressionFormula(conditional.valueIfFalse, this.right, this.relation, this.isSoft) };
        } else if (this.left instanceof SwitchExpression) {
            SwitchExpression se = (SwitchExpression) this.left;
            ConstraintFormula[] cfs = new ConstraintFormula[se.resultExpressions.size()];
            int i = 0;
            for (Expression re : se.resultExpressions) {
                cfs[i++] = new ConstraintExpressionFormula(re, this.right, this.relation, this.isSoft);
            }
            return cfs;
        } else if (this.left instanceof LambdaExpression) {
            LambdaExpression lambda = (LambdaExpression) this.left;
            BlockScope scope = lambda.enclosingScope;
            if (this.right instanceof InferenceVariable)
                // assume inner inference will handle the fine print
                return TRUE;
            if (!this.right.isFunctionalInterface(scope))
                return FALSE;
            ReferenceBinding t = (ReferenceBinding) this.right;
            ParameterizedTypeBinding withWildCards = InferenceContext18.parameterizedWithWildcard(t);
            if (withWildCards != null) {
                t = findGroundTargetType(inferenceContext, scope, lambda, withWildCards);
            }
            if (t == null)
                return FALSE;
            MethodBinding functionType = t.getSingleAbstractMethod(scope, true);
            if (functionType == null)
                return FALSE;
            TypeBinding[] parameters = functionType.parameters;
            if (parameters.length != lambda.arguments().length)
                return FALSE;
            if (lambda.argumentsTypeElided())
                for (int i = 0; i < parameters.length; i++) if (!parameters[i].isProperType(true))
                    return FALSE;
            lambda = lambda.resolveExpressionExpecting(t, inferenceContext.scope, inferenceContext);
            if (lambda == null)
                // not strictly unreduceable, but proceeding with TRUE would likely produce secondary errors
                return FALSE;
            if (functionType.returnType == TypeBinding.VOID) {
                if (!lambda.isVoidCompatible())
                    return FALSE;
            } else {
                if (!lambda.isValueCompatible())
                    return FALSE;
            }
            List<ConstraintFormula> result = new ArrayList<>();
            if (!lambda.argumentsTypeElided()) {
                Argument[] arguments = lambda.arguments();
                for (int i = 0; i < parameters.length; i++) result.add(ConstraintTypeFormula.create(parameters[i], arguments[i].type.resolvedType, SAME));
                // in addition, ⟨T' <: T⟩:
                if (lambda.resolvedType != null)
                    result.add(ConstraintTypeFormula.create(lambda.resolvedType, this.right, SUBTYPE));
            }
            if (functionType.returnType != TypeBinding.VOID) {
                TypeBinding r = functionType.returnType;
                Expression[] exprs = lambda.resultExpressions();
                for (int i = 0, length = exprs == null ? 0 : exprs.length; i < length; i++) {
                    Expression expr = exprs[i];
                    if (r.isProperType(true) && expr.resolvedType != null) {
                        TypeBinding exprType = expr.resolvedType;
                        // "not compatible in an assignment context with R"?
                        if (!(expr.isConstantValueOfTypeAssignableToType(exprType, r) || exprType.isCompatibleWith(r) || expr.isBoxingCompatible(exprType, r, expr, scope)))
                            return FALSE;
                    } else {
                        result.add(new ConstraintExpressionFormula(expr, r, COMPATIBLE, this.isSoft));
                    }
                }
            }
            if (result.size() == 0)
                return TRUE;
            return result.toArray(new ConstraintFormula[result.size()]);
        } else if (this.left instanceof ReferenceExpression) {
            return reduceReferenceExpressionCompatibility((ReferenceExpression) this.left, inferenceContext);
        }
    }
    return FALSE;
}
Also used : Invocation(org.eclipse.jdt.internal.compiler.ast.Invocation) SwitchExpression(org.eclipse.jdt.internal.compiler.ast.SwitchExpression) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) ArrayList(java.util.ArrayList) List(java.util.List) SuspendedInferenceRecord(org.eclipse.jdt.internal.compiler.lookup.InferenceContext18.SuspendedInferenceRecord) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) LambdaExpression(org.eclipse.jdt.internal.compiler.ast.LambdaExpression) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) SwitchExpression(org.eclipse.jdt.internal.compiler.ast.SwitchExpression) ReferenceExpression(org.eclipse.jdt.internal.compiler.ast.ReferenceExpression) ReferenceExpression(org.eclipse.jdt.internal.compiler.ast.ReferenceExpression) LambdaExpression(org.eclipse.jdt.internal.compiler.ast.LambdaExpression)

Example 33 with MessageSend

use of org.eclipse.jdt.internal.compiler.ast.MessageSend in project lombok by rzwitserloot.

the class EclipseJavaUtilListSetSingularizer method generateClearMethod.

private void generateClearMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    FieldReference thisDotField2 = new FieldReference(data.getPluralName(), 0L);
    thisDotField2.receiver = new ThisReference(0, 0);
    md.selector = HandlerUtil.buildAccessorName("clear", new String(data.getPluralName())).toCharArray();
    MessageSend clearMsg = new MessageSend();
    clearMsg.receiver = thisDotField2;
    clearMsg.selector = "clear".toCharArray();
    Statement clearStatement = new IfStatement(new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL), clearMsg, 0, 0);
    md.statements = returnStatement != null ? new Statement[] { clearStatement, returnStatement } : new Statement[] { clearStatement };
    md.returnType = returnType;
    injectMethod(builderType, md);
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral)

Example 34 with MessageSend

use of org.eclipse.jdt.internal.compiler.ast.MessageSend in project lombok by rzwitserloot.

the class EclipseJavaUtilListSingularizer method appendBuildCode.

@Override
public void appendBuildCode(SingularData data, EclipseNode builderType, List<Statement> statements, char[] targetVariableName) {
    if (useGuavaInstead(builderType)) {
        guavaListSetSingularizer.appendBuildCode(data, builderType, statements, targetVariableName);
        return;
    }
    List<Statement> switchContents = new ArrayList<Statement>();
    /* case 0: (empty) break; */
    {
        switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '0' }, null), 0, 0));
        MessageSend invoke = new MessageSend();
        invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
        invoke.selector = "emptyList".toCharArray();
        switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0));
        switchContents.add(new BreakStatement(null, 0, 0));
    }
    /* case 1: (singleton) break; */
    {
        switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '1' }, null), 0, 0));
        FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
        thisDotField.receiver = new ThisReference(0, 0);
        MessageSend thisDotFieldGet0 = new MessageSend();
        thisDotFieldGet0.receiver = thisDotField;
        thisDotFieldGet0.selector = new char[] { 'g', 'e', 't' };
        thisDotFieldGet0.arguments = new Expression[] { makeIntLiteral(new char[] { '0' }, null) };
        Expression[] args = new Expression[] { thisDotFieldGet0 };
        MessageSend invoke = new MessageSend();
        invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
        invoke.selector = "singletonList".toCharArray();
        invoke.arguments = args;
        switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0));
        switchContents.add(new BreakStatement(null, 0, 0));
    }
    /* default: Create by passing builder field to constructor. */
    {
        switchContents.add(new CaseStatement(null, 0, 0));
        Expression argToUnmodifiable;
        /* new j.u.ArrayList<Generics>(this.pluralName); */
        {
            FieldReference thisDotPluralName = new FieldReference(data.getPluralName(), 0L);
            thisDotPluralName.receiver = new ThisReference(0, 0);
            TypeReference targetTypeExpr = new QualifiedTypeReference(JAVA_UTIL_ARRAYLIST, NULL_POSS);
            targetTypeExpr = addTypeArgs(1, false, builderType, targetTypeExpr, data.getTypeArgs());
            AllocationExpression constructorCall = new AllocationExpression();
            constructorCall.type = targetTypeExpr;
            constructorCall.arguments = new Expression[] { thisDotPluralName };
            argToUnmodifiable = constructorCall;
        }
        /* pluralname = Collections.unmodifiableList(-newlist-); */
        {
            MessageSend unmodInvoke = new MessageSend();
            unmodInvoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
            unmodInvoke.selector = "unmodifiableList".toCharArray();
            unmodInvoke.arguments = new Expression[] { argToUnmodifiable };
            switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), unmodInvoke, 0));
        }
    }
    SwitchStatement switchStat = new SwitchStatement();
    switchStat.statements = switchContents.toArray(new Statement[switchContents.size()]);
    switchStat.expression = getSize(builderType, data.getPluralName(), true);
    TypeReference localShadowerType = new QualifiedTypeReference(Eclipse.fromQualifiedName(data.getTargetFqn()), NULL_POSS);
    localShadowerType = addTypeArgs(1, false, builderType, localShadowerType, data.getTypeArgs());
    LocalDeclaration varDefStat = new LocalDeclaration(data.getPluralName(), 0, 0);
    varDefStat.type = localShadowerType;
    statements.add(varDefStat);
    statements.add(switchStat);
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) BreakStatement(org.eclipse.jdt.internal.compiler.ast.BreakStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) BreakStatement(org.eclipse.jdt.internal.compiler.ast.BreakStatement) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)

Example 35 with MessageSend

use of org.eclipse.jdt.internal.compiler.ast.MessageSend in project lombok by rzwitserloot.

the class EclipseJavaUtilMapSingularizer method generateClearMethod.

private void generateClearMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    String pN = new String(data.getPluralName());
    char[] keyFieldName = (pN + "$key").toCharArray();
    char[] valueFieldName = (pN + "$value").toCharArray();
    FieldReference thisDotField = new FieldReference(keyFieldName, 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    FieldReference thisDotField2 = new FieldReference(keyFieldName, 0L);
    thisDotField2.receiver = new ThisReference(0, 0);
    FieldReference thisDotField3 = new FieldReference(valueFieldName, 0L);
    thisDotField3.receiver = new ThisReference(0, 0);
    md.selector = HandlerUtil.buildAccessorName("clear", new String(data.getPluralName())).toCharArray();
    MessageSend clearMsg1 = new MessageSend();
    clearMsg1.receiver = thisDotField2;
    clearMsg1.selector = "clear".toCharArray();
    MessageSend clearMsg2 = new MessageSend();
    clearMsg2.receiver = thisDotField3;
    clearMsg2.selector = "clear".toCharArray();
    Block clearMsgs = new Block(2);
    clearMsgs.statements = new Statement[] { clearMsg1, clearMsg2 };
    Statement clearStatement = new IfStatement(new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL), clearMsgs, 0, 0);
    md.statements = returnStatement != null ? new Statement[] { clearStatement, returnStatement } : new Statement[] { clearStatement };
    md.returnType = returnType;
    injectMethod(builderType, md);
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ForeachStatement(org.eclipse.jdt.internal.compiler.ast.ForeachStatement) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) Block(org.eclipse.jdt.internal.compiler.ast.Block) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral)

Aggregations

MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)67 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)43 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)41 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)39 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)36 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)36 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)36 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)35 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)34 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)32 ArrayList (java.util.ArrayList)30 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)30 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)29 NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)22 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)21 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)21 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)20 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)18 ConditionalExpression (org.eclipse.jdt.internal.compiler.ast.ConditionalExpression)17 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)17