Example usage for org.eclipse.jdt.internal.compiler.ast SynchronizedStatement SynchronizedStatement

List of usage examples for org.eclipse.jdt.internal.compiler.ast SynchronizedStatement SynchronizedStatement

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast SynchronizedStatement SynchronizedStatement.

Prototype

public SynchronizedStatement(Expression expression, Block statement, int s, int e) 

Source Link

Usage

From source file:lombok.eclipse.handlers.ast.EclipseASTMaker.java

License:Open Source License

@Override
public ASTNode visitSynchronized(final lombok.ast.Synchronized node, final Void p) {
    final Block block = new Block(0);
    setGeneratedByAndCopyPos(block, source, posHintOf(node));
    block.statements = toArray(build(node.getStatements()), new Statement[0]);
    final SynchronizedStatement synchronizedStatemenet = new SynchronizedStatement(
            build(node.getLock(), Expression.class), block, 0, 0);
    setGeneratedByAndCopyPos(synchronizedStatemenet, source, posHintOf(node));
    return synchronizedStatemenet;
}

From source file:lombok.eclipse.handlers.HandleGetter.java

License:Open Source License

public Statement[] createLazyGetterBody(ASTNode source, EclipseNode fieldNode) {
    /*/*  w  w  w  . jav a 2  s.  c om*/
    java.lang.Object value = this.fieldName.get();
    if (value == null) {
       synchronized (this.fieldName) {
    value = this.fieldName.get();
    if (value == null) {
       final RawValueType actualValue = INITIALIZER_EXPRESSION;
       [IF PRIMITIVE]
       value = actualValue;
       [ELSE]
       value = actualValue == null ? this.fieldName : actualValue;
       [END IF]
       this.fieldName.set(value);
    }
       }
    }
    [IF PRIMITIVE]
    return (BoxedValueType) value;
    [ELSE]
    return (BoxedValueType) (value == this.fieldName ? null : value);
    [END IF]
    */

    FieldDeclaration field = (FieldDeclaration) fieldNode.get();
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;

    TypeReference rawComponentType = copyType(field.type, source);
    TypeReference boxedComponentType = null;
    boolean isPrimitive = false;
    if (field.type instanceof SingleTypeReference && !(field.type instanceof ArrayTypeReference)) {
        char[][] newType = TYPE_MAP.get(new String(((SingleTypeReference) field.type).token));
        if (newType != null) {
            boxedComponentType = new QualifiedTypeReference(newType, poss(source, 3));
            isPrimitive = true;
        }
    }
    if (boxedComponentType == null)
        boxedComponentType = copyType(field.type, source);
    boxedComponentType.sourceStart = pS;
    boxedComponentType.sourceEnd = boxedComponentType.statementEnd = pE;

    Statement[] statements = new Statement[3];

    /* java.lang.Object value = this.fieldName.get(); */ {
        LocalDeclaration valueDecl = new LocalDeclaration(valueName, pS, pE);
        valueDecl.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(source, 3));
        valueDecl.type.sourceStart = pS;
        valueDecl.type.sourceEnd = valueDecl.type.statementEnd = pE;

        MessageSend getter = new MessageSend();
        getter.sourceStart = pS;
        getter.statementEnd = getter.sourceEnd = pE;
        getter.selector = new char[] { 'g', 'e', 't' };
        getter.receiver = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);

        valueDecl.initialization = getter;
        statements[0] = valueDecl;
    }

    /*
    if (value == null) {
       synchronized (this.fieldName) {
    value = this.fieldName.get();
    if (value == null) { 
       final ValueType actualValue = INITIALIZER_EXPRESSION;
       [IF PRIMITIVE]
       value = actualValue;
       [ELSE]
       value = actualValue == null ? this.fieldName : actualValue;
       [END IF]
       this.fieldName.set(value);
    }
       }
    }
     */ {
        EqualExpression cond = new EqualExpression(new SingleNameReference(valueName, p),
                new NullLiteral(pS, pE), BinaryExpression.EQUAL_EQUAL);
        Block then = new Block(0);
        Expression lock = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
        Block inner = new Block(0);
        inner.statements = new Statement[2];
        /* value = this.fieldName.get(); */ {
            MessageSend getter = new MessageSend();
            getter.sourceStart = pS;
            getter.sourceEnd = getter.statementEnd = pE;
            getter.selector = new char[] { 'g', 'e', 't' };
            getter.receiver = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
            Assignment assign = new Assignment(new SingleNameReference(valueName, p), getter, pE);
            assign.sourceStart = pS;
            assign.statementEnd = assign.sourceEnd = pE;
            inner.statements[0] = assign;
        }
        /* if (value == null) */ {
            EqualExpression innerCond = new EqualExpression(new SingleNameReference(valueName, p),
                    new NullLiteral(pS, pE), BinaryExpression.EQUAL_EQUAL);
            innerCond.sourceStart = pS;
            innerCond.sourceEnd = innerCond.statementEnd = pE;
            Block innerThen = new Block(0);
            innerThen.statements = new Statement[3];
            /* final ValueType actualValue = INITIALIZER_EXPRESSION */ {
                LocalDeclaration actualValueDecl = new LocalDeclaration(actualValueName, pS, pE);
                actualValueDecl.type = rawComponentType;
                actualValueDecl.type.sourceStart = pS;
                actualValueDecl.type.sourceEnd = actualValueDecl.type.statementEnd = pE;
                actualValueDecl.initialization = field.initialization;
                actualValueDecl.modifiers = ClassFileConstants.AccFinal;
                innerThen.statements[0] = actualValueDecl;
            }
            /* [IF PRIMITIVE] value = actualValue; */ {
                if (isPrimitive) {
                    Assignment innerAssign = new Assignment(new SingleNameReference(valueName, p),
                            new SingleNameReference(actualValueName, p), pE);
                    innerAssign.sourceStart = pS;
                    innerAssign.statementEnd = innerAssign.sourceEnd = pE;
                    innerThen.statements[1] = innerAssign;
                }
            }
            /* [ELSE] value = actualValue == null ? this.fieldName : actualValue; */ {
                if (!isPrimitive) {
                    EqualExpression avIsNull = new EqualExpression(new SingleNameReference(actualValueName, p),
                            new NullLiteral(pS, pE), BinaryExpression.EQUAL_EQUAL);
                    avIsNull.sourceStart = pS;
                    avIsNull.sourceEnd = avIsNull.statementEnd = pE;
                    Expression fieldRef = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
                    ConditionalExpression ternary = new ConditionalExpression(avIsNull, fieldRef,
                            new SingleNameReference(actualValueName, p));
                    ternary.sourceStart = pS;
                    ternary.sourceEnd = ternary.statementEnd = pE;
                    Assignment innerAssign = new Assignment(new SingleNameReference(valueName, p), ternary, pE);
                    innerAssign.sourceStart = pS;
                    innerAssign.statementEnd = innerAssign.sourceEnd = pE;
                    innerThen.statements[1] = innerAssign;
                }
            }

            /* this.fieldName.set(value); */ {
                MessageSend setter = new MessageSend();
                setter.sourceStart = pS;
                setter.sourceEnd = setter.statementEnd = pE;
                setter.receiver = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
                setter.selector = new char[] { 's', 'e', 't' };
                setter.arguments = new Expression[] { new SingleNameReference(valueName, p) };
                innerThen.statements[2] = setter;
            }

            IfStatement innerIf = new IfStatement(innerCond, innerThen, pS, pE);
            inner.statements[1] = innerIf;
        }

        SynchronizedStatement sync = new SynchronizedStatement(lock, inner, pS, pE);
        then.statements = new Statement[] { sync };

        IfStatement ifStatement = new IfStatement(cond, then, pS, pE);
        statements[1] = ifStatement;
    }

    /* [IF PRIMITIVE] return (BoxedValueType)value; */ {
        if (isPrimitive) {
            CastExpression cast = makeCastExpression(new SingleNameReference(valueName, p), boxedComponentType,
                    source);
            statements[2] = new ReturnStatement(cast, pS, pE);
        }
    }
    /* [ELSE] return (BoxedValueType)(value == this.fieldName ? null : value); */ {
        if (!isPrimitive) {
            EqualExpression vIsThisFieldName = new EqualExpression(new SingleNameReference(valueName, p),
                    createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source),
                    BinaryExpression.EQUAL_EQUAL);
            vIsThisFieldName.sourceStart = pS;
            vIsThisFieldName.sourceEnd = vIsThisFieldName.statementEnd = pE;
            ConditionalExpression ternary = new ConditionalExpression(vIsThisFieldName, new NullLiteral(pS, pE),
                    new SingleNameReference(valueName, p));
            ternary.sourceStart = pS;
            ternary.sourceEnd = ternary.statementEnd = pE;
            ternary.bits |= PARENTHESIZED;
            CastExpression cast = makeCastExpression(ternary, boxedComponentType, source);
            statements[2] = new ReturnStatement(cast, pS, pE);
        }
    }

    // update the field type and init last

    /*    private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> fieldName = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>(); */ {
        TypeReference innerType = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(source, 3));
        TypeReference[][] typeParams = new TypeReference[5][];
        typeParams[4] = new TypeReference[] { innerType };
        TypeReference type = new ParameterizedQualifiedTypeReference(AR, typeParams, 0, poss(source, 5));

        // Some magic here
        type.sourceStart = -1;
        type.sourceEnd = -2;

        field.type = type;
        AllocationExpression init = new AllocationExpression();
        // Some magic here
        init.sourceStart = field.initialization.sourceStart;
        init.sourceEnd = init.statementEnd = field.initialization.sourceEnd;
        init.type = copyType(type, source);
        field.initialization = init;
    }
    return statements;
}

From source file:lombok.eclipse.handlers.HandleSynchronized.java

License:Open Source License

@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;/*from  w ww.j  a v  a  2s  .com*/
    }

    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();
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumeStatementSynchronized() {
    // SynchronizedStatement ::= OnlySynchronized '(' Expression ')' Block
    //optimize the push/pop

    if (this.astLengthStack[this.astLengthPtr] == 0) {
        this.astLengthStack[this.astLengthPtr] = 1;
        this.expressionLengthPtr--;
        this.astStack[++this.astPtr] = new SynchronizedStatement(this.expressionStack[this.expressionPtr--],
                null, this.intStack[this.intPtr--], this.endStatementPosition);
    } else {// w  w w . java  2 s .c  om
        this.expressionLengthPtr--;
        this.astStack[this.astPtr] = new SynchronizedStatement(this.expressionStack[this.expressionPtr--],
                (Block) this.astStack[this.astPtr], this.intStack[this.intPtr--], this.endStatementPosition);
    }
    resetModifiers();
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.AstGenerator.java

License:Open Source License

public SynchronizedStatement synchronizedStatement(Expression expr, Statement[] stmts) {
    return new SynchronizedStatement(expr, block(stmts), this.sourceStart, this.sourceEnd);
}

From source file:org.nabucco.framework.mda.template.java.extract.statement.JavaAstStatementExtractorVisitor.java

License:Open Source License

@Override
public boolean visit(SynchronizedStatement synchronizedStatement, BlockScope scope) {

    Block block = copy(synchronizedStatement.block, scope);
    Expression expression = copy(synchronizedStatement.expression, scope);

    SynchronizedStatement synchronizedCopy = new SynchronizedStatement(expression, block,
            synchronizedStatement.sourceStart, synchronizedStatement.sourceEnd);

    this.statement = synchronizedCopy;

    return false;
}