Search in sources :

Example 31 with ThisReference

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

the class EclipseJavaUtilListSetSingularizer method generateSingularMethod.

void generateSingularMethod(CheckerFrameworkVersion cfv, boolean deprecate, TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent, AccessLevel access) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = toEclipseModifier(access);
    List<Statement> statements = new ArrayList<Statement>();
    statements.add(createConstructBuilderVarIfNeeded(data, builderType, false));
    FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    MessageSend thisDotFieldDotAdd = new MessageSend();
    thisDotFieldDotAdd.arguments = new Expression[] { new SingleNameReference(data.getSingularName(), 0L) };
    thisDotFieldDotAdd.receiver = thisDotField;
    thisDotFieldDotAdd.selector = "add".toCharArray();
    statements.add(thisDotFieldDotAdd);
    if (returnStatement != null)
        statements.add(returnStatement);
    md.statements = statements.toArray(new Statement[0]);
    TypeReference paramType = cloneParamType(0, data.getTypeArgs(), builderType);
    Annotation[] typeUseAnns = getTypeUseAnnotations(paramType);
    removeTypeUseAnnotations(paramType);
    Argument param = new Argument(data.getSingularName(), 0, paramType, ClassFileConstants.AccFinal);
    param.annotations = typeUseAnns;
    md.arguments = new Argument[] { param };
    md.returnType = returnType;
    addCheckerFrameworkReturnsReceiver(md.returnType, data.getSource(), cfv);
    char[] prefixedSingularName = data.getSetterPrefix().length == 0 ? data.getSingularName() : HandlerUtil.buildAccessorName(builderType, new String(data.getSetterPrefix()), new String(data.getSingularName())).toCharArray();
    md.selector = fluent ? prefixedSingularName : HandlerUtil.buildAccessorName(builderType, "add", new String(data.getSingularName())).toCharArray();
    Annotation[] selfReturnAnnotations = generateSelfReturnAnnotations(deprecate, data.getSource());
    Annotation[] copyToSetterAnnotations = copyAnnotations(md, findCopyableToBuilderSingularSetterAnnotations(data.getAnnotation().up()));
    md.annotations = concat(selfReturnAnnotations, copyToSetterAnnotations, Annotation.class);
    if (returnStatement != null)
        createRelevantNonNullAnnotation(builderType, md);
    data.setGeneratedByRecursive(md);
    HandleNonNull.INSTANCE.fix(injectMethod(builderType, md));
}
Also used : FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)

Example 32 with ThisReference

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

the class HandleSetter method createSetter.

static MethodDeclaration createSetter(TypeDeclaration parent, boolean deprecate, EclipseNode fieldNode, String name, char[] paramName, char[] booleanFieldToSet, boolean shouldReturnThis, int modifier, EclipseNode sourceNode, List<Annotation> onMethod, List<Annotation> onParam) {
    ASTNode source = sourceNode.get();
    int pS = source.sourceStart, pE = source.sourceEnd;
    TypeReference returnType = null;
    ReturnStatement returnThis = null;
    if (shouldReturnThis) {
        returnType = cloneSelfType(fieldNode, source);
        addCheckerFrameworkReturnsReceiver(returnType, source, getCheckerFrameworkVersion(sourceNode));
        ThisReference thisRef = new ThisReference(pS, pE);
        returnThis = new ReturnStatement(thisRef, pS, pE);
    }
    MethodDeclaration d = createSetter(parent, deprecate, fieldNode, name, paramName, booleanFieldToSet, returnType, returnThis, modifier, sourceNode, onMethod, onParam);
    return d;
}
Also used : MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference)

Example 33 with ThisReference

use of org.eclipse.jdt.internal.compiler.ast.ThisReference 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 ThisReference

use of org.eclipse.jdt.internal.compiler.ast.ThisReference 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 ThisReference

use of org.eclipse.jdt.internal.compiler.ast.ThisReference 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

ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)56 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)44 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)43 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)38 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)38 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)37 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)37 ArrayList (java.util.ArrayList)31 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)31 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)27 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)26 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)26 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)22 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)21 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)20 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)16 NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)16 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)15 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)15 EclipseNode (lombok.eclipse.EclipseNode)13