Search in sources :

Example 1 with FieldReference

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

the class HandleSynchronized method handle.

@Override
public void handle(AnnotationValues<Synchronized> annotation, Annotation source, EclipseNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.SYNCHRONIZED_FLAG_USAGE, "@Synchronized");
    int p1 = source.sourceStart - 1;
    int p2 = source.sourceStart - 2;
    long pos = (((long) p1) << 32) | p2;
    EclipseNode methodNode = annotationNode.up();
    if (methodNode == null || methodNode.getKind() != Kind.METHOD || !(methodNode.get() instanceof MethodDeclaration)) {
        annotationNode.addError("@Synchronized is legal only on methods.");
        return;
    }
    MethodDeclaration method = (MethodDeclaration) methodNode.get();
    if (method.isAbstract()) {
        annotationNode.addError("@Synchronized is legal only on concrete methods.");
        return;
    }
    char[] lockName = createLockField(annotation, annotationNode, method.isStatic(), true);
    if (lockName == null)
        return;
    if (method.statements == null)
        return;
    Block block = new Block(0);
    setGeneratedBy(block, source);
    block.statements = method.statements;
    // Positions for in-method generated nodes are special
    block.sourceEnd = method.bodyEnd;
    block.sourceStart = method.bodyStart;
    Expression lockVariable;
    if (method.isStatic())
        lockVariable = new QualifiedNameReference(new char[][] { methodNode.up().getName().toCharArray(), lockName }, new long[] { pos, pos }, p1, p2);
    else {
        lockVariable = new FieldReference(lockName, pos);
        ThisReference thisReference = new ThisReference(p1, p2);
        setGeneratedBy(thisReference, source);
        ((FieldReference) lockVariable).receiver = thisReference;
    }
    setGeneratedBy(lockVariable, source);
    method.statements = new Statement[] { new SynchronizedStatement(lockVariable, block, 0, 0) };
    // Positions for in-method generated nodes are special
    method.statements[0].sourceEnd = method.bodyEnd;
    method.statements[0].sourceStart = method.bodyStart;
    setGeneratedBy(method.statements[0], source);
    methodNode.rebuild();
}
Also used : FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) ArrayAllocationExpression(org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) EclipseNode(lombok.eclipse.EclipseNode) Block(org.eclipse.jdt.internal.compiler.ast.Block) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SynchronizedStatement(org.eclipse.jdt.internal.compiler.ast.SynchronizedStatement) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)

Example 2 with FieldReference

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

the class EclipseJavaUtilSingularizer method createJavaUtilSimpleCreationAndFillStatements.

protected List<Statement> createJavaUtilSimpleCreationAndFillStatements(SingularData data, EclipseNode builderType, boolean mapMode, boolean defineVar, boolean addInitialCapacityArg, boolean nullGuard, String targetType) {
    char[] varName = mapMode ? (new String(data.getPluralName()) + "$key").toCharArray() : data.getPluralName();
    Statement createStat;
    {
        // pluralName = new java.util.TargetType(initialCap);
        Expression[] constructorArgs = null;
        if (addInitialCapacityArg) {
            // this.varName.size() < MAX_POWER_OF_2 ? 1 + this.varName.size() + (this.varName.size() - 3) / 3 : Integer.MAX_VALUE;
            // lessThanCutOff = this.varName.size() < MAX_POWER_OF_2
            Expression lessThanCutoff = new BinaryExpression(getSize(builderType, varName, nullGuard), makeIntLiteral("0x40000000".toCharArray(), null), OperatorIds.LESS);
            FieldReference integerMaxValue = new FieldReference("MAX_VALUE".toCharArray(), 0L);
            integerMaxValue.receiver = new QualifiedNameReference(TypeConstants.JAVA_LANG_INTEGER, NULL_POSS, 0, 0);
            Expression sizeFormulaLeft = new BinaryExpression(makeIntLiteral(new char[] { '1' }, null), getSize(builderType, varName, nullGuard), OperatorIds.PLUS);
            Expression sizeFormulaRightLeft = new BinaryExpression(getSize(builderType, varName, nullGuard), makeIntLiteral(new char[] { '3' }, null), OperatorIds.MINUS);
            Expression sizeFormulaRight = new BinaryExpression(sizeFormulaRightLeft, makeIntLiteral(new char[] { '3' }, null), OperatorIds.DIVIDE);
            Expression sizeFormula = new BinaryExpression(sizeFormulaLeft, sizeFormulaRight, OperatorIds.PLUS);
            Expression cond = new ConditionalExpression(lessThanCutoff, sizeFormula, integerMaxValue);
            constructorArgs = new Expression[] { cond };
        }
        TypeReference targetTypeRef = new QualifiedTypeReference(new char[][] { TypeConstants.JAVA, TypeConstants.UTIL, targetType.toCharArray() }, NULL_POSS);
        targetTypeRef = addTypeArgs(mapMode ? 2 : 1, false, builderType, targetTypeRef, data.getTypeArgs());
        AllocationExpression constructorCall = new AllocationExpression();
        constructorCall.type = targetTypeRef;
        constructorCall.arguments = constructorArgs;
        if (defineVar) {
            TypeReference localShadowerType = new QualifiedTypeReference(fromQualifiedName(data.getTargetFqn()), NULL_POSS);
            localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType, data.getTypeArgs());
            LocalDeclaration localShadowerDecl = new LocalDeclaration(data.getPluralName(), 0, 0);
            localShadowerDecl.type = localShadowerType;
            localShadowerDecl.initialization = constructorCall;
            createStat = localShadowerDecl;
        } else {
            createStat = new Assignment(new SingleNameReference(data.getPluralName(), 0L), constructorCall, 0);
        }
    }
    Statement fillStat;
    {
        if (mapMode) {
            // for (int $i = 0; $i < this.pluralname$key.size(); i++) pluralname.put(this.pluralname$key.get($i), this.pluralname$value.get($i));
            char[] iVar = new char[] { '$', 'i' };
            MessageSend pluralnameDotPut = new MessageSend();
            pluralnameDotPut.selector = new char[] { 'p', 'u', 't' };
            pluralnameDotPut.receiver = new SingleNameReference(data.getPluralName(), 0L);
            FieldReference thisDotKey = new FieldReference(varName, 0L);
            thisDotKey.receiver = new ThisReference(0, 0);
            FieldReference thisDotValue = new FieldReference((new String(data.getPluralName()) + "$value").toCharArray(), 0L);
            thisDotValue.receiver = new ThisReference(0, 0);
            MessageSend keyArg = new MessageSend();
            keyArg.receiver = thisDotKey;
            keyArg.arguments = new Expression[] { new SingleNameReference(iVar, 0L) };
            keyArg.selector = new char[] { 'g', 'e', 't' };
            MessageSend valueArg = new MessageSend();
            valueArg.receiver = thisDotValue;
            valueArg.arguments = new Expression[] { new SingleNameReference(iVar, 0L) };
            valueArg.selector = new char[] { 'g', 'e', 't' };
            pluralnameDotPut.arguments = new Expression[] { keyArg, valueArg };
            LocalDeclaration forInit = new LocalDeclaration(iVar, 0, 0);
            forInit.type = TypeReference.baseTypeReference(TypeIds.T_int, 0);
            forInit.initialization = makeIntLiteral(new char[] { '0' }, null);
            Expression checkExpr = new BinaryExpression(new SingleNameReference(iVar, 0L), getSize(builderType, varName, nullGuard), OperatorIds.LESS);
            Expression incrementExpr = new PostfixExpression(new SingleNameReference(iVar, 0L), IntLiteral.One, OperatorIds.PLUS, 0);
            fillStat = new ForStatement(new Statement[] { forInit }, checkExpr, new Statement[] { incrementExpr }, pluralnameDotPut, true, 0, 0);
        } else {
            // pluralname.addAll(this.pluralname);
            MessageSend pluralnameDotAddAll = new MessageSend();
            pluralnameDotAddAll.selector = new char[] { 'a', 'd', 'd', 'A', 'l', 'l' };
            pluralnameDotAddAll.receiver = new SingleNameReference(data.getPluralName(), 0L);
            FieldReference thisDotPluralname = new FieldReference(varName, 0L);
            thisDotPluralname.receiver = new ThisReference(0, 0);
            pluralnameDotAddAll.arguments = new Expression[] { thisDotPluralname };
            fillStat = pluralnameDotAddAll;
        }
        if (nullGuard) {
            FieldReference thisDotField = new FieldReference(varName, 0L);
            thisDotField.receiver = new ThisReference(0, 0);
            Expression cond = new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL);
            fillStat = new IfStatement(cond, fillStat, 0, 0);
        }
    }
    Statement unmodifiableStat;
    {
        // pluralname = Collections.unmodifiableInterfaceType(pluralname);
        Expression arg = new SingleNameReference(data.getPluralName(), 0L);
        MessageSend invoke = new MessageSend();
        invoke.arguments = new Expression[] { arg };
        invoke.selector = ("unmodifiable" + data.getTargetSimpleType()).toCharArray();
        invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
        unmodifiableStat = new Assignment(new SingleNameReference(data.getPluralName(), 0L), invoke, 0);
    }
    return Arrays.asList(createStat, fillStat, unmodifiableStat);
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) BreakStatement(org.eclipse.jdt.internal.compiler.ast.BreakStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) ForStatement(org.eclipse.jdt.internal.compiler.ast.ForStatement) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) BinaryExpression(org.eclipse.jdt.internal.compiler.ast.BinaryExpression) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) PostfixExpression(org.eclipse.jdt.internal.compiler.ast.PostfixExpression) BinaryExpression(org.eclipse.jdt.internal.compiler.ast.BinaryExpression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) PostfixExpression(org.eclipse.jdt.internal.compiler.ast.PostfixExpression) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ForStatement(org.eclipse.jdt.internal.compiler.ast.ForStatement) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)

Example 3 with FieldReference

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

the class EclipseGuavaSingularizer method createConstructBuilderVarIfNeeded.

protected Statement createConstructBuilderVarIfNeeded(SingularData data, EclipseNode builderType) {
    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);
    Expression cond = new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL);
    MessageSend createBuilderInvoke = new MessageSend();
    char[][] tokenizedName = makeGuavaTypeName(getSimpleTargetTypeName(data), false);
    createBuilderInvoke.receiver = new QualifiedNameReference(tokenizedName, NULL_POSS, 0, 0);
    createBuilderInvoke.selector = getBuilderMethodName(data);
    return new IfStatement(cond, new Assignment(thisDotField2, createBuilderInvoke, 0), 0, 0);
}
Also used : Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)

Example 4 with FieldReference

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

the class EclipseJavaUtilListSetSingularizer method generatePluralMethod.

void generatePluralMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    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 thisDotFieldDotAddAll = new MessageSend();
    thisDotFieldDotAddAll.arguments = new Expression[] { new SingleNameReference(data.getPluralName(), 0L) };
    thisDotFieldDotAddAll.receiver = thisDotField;
    thisDotFieldDotAddAll.selector = "addAll".toCharArray();
    statements.add(thisDotFieldDotAddAll);
    if (returnStatement != null)
        statements.add(returnStatement);
    md.statements = statements.toArray(new Statement[statements.size()]);
    TypeReference paramType = new QualifiedTypeReference(TypeConstants.JAVA_UTIL_COLLECTION, NULL_POSS);
    paramType = addTypeArgs(1, true, builderType, paramType, data.getTypeArgs());
    Argument param = new Argument(data.getPluralName(), 0, paramType, 0);
    md.arguments = new Argument[] { param };
    md.returnType = returnType;
    md.selector = fluent ? data.getPluralName() : HandlerUtil.buildAccessorName("addAll", new String(data.getPluralName())).toCharArray();
    data.setGeneratedByRecursive(md);
    injectMethod(builderType, md);
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) 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) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayList(java.util.ArrayList) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference)

Example 5 with FieldReference

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

the class EclipseJavaUtilListSetSingularizer method generateSingularMethod.

void generateSingularMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    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[statements.size()]);
    TypeReference paramType = cloneParamType(0, data.getTypeArgs(), builderType);
    Argument param = new Argument(data.getSingularName(), 0, paramType, 0);
    md.arguments = new Argument[] { param };
    md.returnType = returnType;
    md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName("add", new String(data.getSingularName())).toCharArray();
    data.setGeneratedByRecursive(md);
    injectMethod(builderType, md);
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) 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) ArrayList(java.util.ArrayList) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference)

Aggregations

FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)22 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)21 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)16 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)16 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)15 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)13 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)13 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)13 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)12 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)12 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)12 ArrayList (java.util.ArrayList)11 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)9 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)9 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)8 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)7 NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)7 QualifiedNameReference (org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)6 ConditionalExpression (org.eclipse.jdt.internal.compiler.ast.ConditionalExpression)5 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)5