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

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

Introduction

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

Prototype

public Assignment(Expression lhs, Expression expression, int sourceEnd) 

Source Link

Usage

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

License:Open Source License

@Override
public ASTNode visitAssignment(final lombok.ast.Assignment node, final Void p) {
    final Assignment assignment = new Assignment(build(node.getLeft(), Expression.class),
            build(node.getRight(), Expression.class), 0);
    setGeneratedByAndCopyPos(assignment, source, posHintOf(node));
    return assignment;
}

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

License:Open Source License

private MethodDeclaration generateCleanMethod(List<BuilderFieldData> builderFields, EclipseNode builderType,
        ASTNode source) {/*from  ww w  . ja va  2  s.c o  m*/
    List<Statement> statements = new ArrayList<Statement>();

    for (BuilderFieldData bfd : builderFields) {
        if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) {
            bfd.singularData.getSingularizer().appendCleaningCode(bfd.singularData, builderType, statements);
        }
    }

    FieldReference thisUnclean = new FieldReference(CLEAN_FIELD_NAME, 0);
    thisUnclean.receiver = new ThisReference(0, 0);
    statements.add(new Assignment(thisUnclean, new FalseLiteral(0, 0), 0));
    MethodDeclaration decl = new MethodDeclaration(
            ((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    decl.selector = CLEAN_METHOD_NAME;
    decl.modifiers = ClassFileConstants.AccPrivate;
    decl.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    decl.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
    decl.statements = statements.toArray(new Statement[0]);
    decl.traverse(new SetGeneratedByVisitor(source), (ClassScope) null);
    return decl;
}

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

License:Open Source License

public MethodDeclaration generateBuildMethod(String name, char[] staticName, TypeReference returnType,
        List<BuilderFieldData> builderFields, EclipseNode type, TypeReference[] thrownExceptions,
        boolean addCleaning, ASTNode source) {
    MethodDeclaration out = new MethodDeclaration(
            ((CompilationUnitDeclaration) type.top().get()).compilationResult);
    out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    List<Statement> statements = new ArrayList<Statement>();

    if (addCleaning) {
        FieldReference thisUnclean = new FieldReference(CLEAN_FIELD_NAME, 0);
        thisUnclean.receiver = new ThisReference(0, 0);
        Expression notClean = new UnaryExpression(thisUnclean, OperatorIds.NOT);
        MessageSend invokeClean = new MessageSend();
        invokeClean.selector = CLEAN_METHOD_NAME;
        statements.add(new IfStatement(notClean, invokeClean, 0, 0));
    }/* www .ja v  a2s  .co  m*/

    for (BuilderFieldData bfd : builderFields) {
        if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) {
            bfd.singularData.getSingularizer().appendBuildCode(bfd.singularData, type, statements, bfd.name);
        }
    }

    List<Expression> args = new ArrayList<Expression>();
    for (BuilderFieldData bfd : builderFields) {
        args.add(new SingleNameReference(bfd.name, 0L));
    }

    if (addCleaning) {
        FieldReference thisUnclean = new FieldReference(CLEAN_FIELD_NAME, 0);
        thisUnclean.receiver = new ThisReference(0, 0);
        statements.add(new Assignment(thisUnclean, new TrueLiteral(0, 0), 0));
    }

    out.modifiers = ClassFileConstants.AccPublic;
    out.selector = name.toCharArray();
    out.thrownExceptions = copyTypes(thrownExceptions);
    out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    out.returnType = returnType;

    if (staticName == null) {
        AllocationExpression allocationStatement = new AllocationExpression();
        allocationStatement.type = copyType(out.returnType);
        allocationStatement.arguments = args.isEmpty() ? null : args.toArray(new Expression[args.size()]);
        statements.add(new ReturnStatement(allocationStatement, 0, 0));
    } else {
        MessageSend invoke = new MessageSend();
        invoke.selector = staticName;
        invoke.receiver = new SingleNameReference(type.up().getName().toCharArray(), 0);
        TypeParameter[] tps = ((TypeDeclaration) type.get()).typeParameters;
        if (tps != null) {
            TypeReference[] trs = new TypeReference[tps.length];
            for (int i = 0; i < trs.length; i++) {
                trs[i] = new SingleTypeReference(tps[i].name, 0);
            }
            invoke.typeArguments = trs;
        }
        invoke.arguments = args.isEmpty() ? null : args.toArray(new Expression[args.size()]);
        if (returnType instanceof SingleTypeReference
                && Arrays.equals(TypeConstants.VOID, ((SingleTypeReference) returnType).token)) {
            statements.add(invoke);
        } else {
            statements.add(new ReturnStatement(invoke, 0, 0));
        }
    }
    out.statements = statements.isEmpty() ? null : statements.toArray(new Statement[statements.size()]);
    out.traverse(new SetGeneratedByVisitor(source), (ClassScope) null);
    return out;
}

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

License:Open Source License

public static ConstructorDeclaration createConstructor(AccessLevel level, EclipseNode type,
        Collection<EclipseNode> fields, boolean allToDefault, Boolean suppressConstructorProperties,
        EclipseNode sourceNode, List<Annotation> onConstructor) {

    ASTNode source = sourceNode.get();/*w ww .jav  a  2  s .  com*/
    TypeDeclaration typeDeclaration = ((TypeDeclaration) type.get());
    long p = (long) source.sourceStart << 32 | source.sourceEnd;

    boolean isEnum = (((TypeDeclaration) type.get()).modifiers & ClassFileConstants.AccEnum) != 0;

    if (isEnum)
        level = AccessLevel.PRIVATE;

    if (suppressConstructorProperties == null) {
        if (fields.isEmpty()) {
            suppressConstructorProperties = false;
        } else {
            suppressConstructorProperties = Boolean.TRUE.equals(type.getAst()
                    .readConfiguration(ConfigurationKeys.ANY_CONSTRUCTOR_SUPPRESS_CONSTRUCTOR_PROPERTIES));
        }
    }

    ConstructorDeclaration constructor = new ConstructorDeclaration(
            ((CompilationUnitDeclaration) type.top().get()).compilationResult);

    constructor.modifiers = toEclipseModifier(level);
    constructor.selector = typeDeclaration.name;
    constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper);
    constructor.constructorCall.sourceStart = source.sourceStart;
    constructor.constructorCall.sourceEnd = source.sourceEnd;
    constructor.thrownExceptions = null;
    constructor.typeParameters = null;
    constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart;
    constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd;
    constructor.arguments = null;

    List<Argument> params = new ArrayList<Argument>();
    List<Statement> assigns = new ArrayList<Statement>();
    List<Statement> nullChecks = new ArrayList<Statement>();

    for (EclipseNode fieldNode : fields) {
        FieldDeclaration field = (FieldDeclaration) fieldNode.get();
        char[] rawName = field.name;
        char[] fieldName = removePrefixFromField(fieldNode);
        FieldReference thisX = new FieldReference(rawName, p);
        int s = (int) (p >> 32);
        int e = (int) p;
        thisX.receiver = new ThisReference(s, e);

        Expression assignmentExpr = allToDefault ? getDefaultExpr(field.type, s, e)
                : new SingleNameReference(fieldName, p);

        Assignment assignment = new Assignment(thisX, assignmentExpr, (int) p);
        assignment.sourceStart = (int) (p >> 32);
        assignment.sourceEnd = assignment.statementEnd = (int) (p >> 32);
        assigns.add(assignment);
        if (!allToDefault) {
            long fieldPos = (((long) field.sourceStart) << 32) | field.sourceEnd;
            Argument parameter = new Argument(fieldName, fieldPos, copyType(field.type, source),
                    Modifier.FINAL);
            Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN);
            Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN);
            if (nonNulls.length != 0) {
                Statement nullCheck = generateNullCheck(field, sourceNode);
                if (nullCheck != null)
                    nullChecks.add(nullCheck);
            }
            parameter.annotations = copyAnnotations(source, nonNulls, nullables);
            params.add(parameter);
        }
    }

    nullChecks.addAll(assigns);
    constructor.statements = nullChecks.isEmpty() ? null : nullChecks.toArray(new Statement[nullChecks.size()]);
    constructor.arguments = params.isEmpty() ? null : params.toArray(new Argument[params.size()]);

    /* Generate annotations that must  be put on the generated method, and attach them. */ {
        Annotation[] constructorProperties = null;
        if (!allToDefault && !suppressConstructorProperties && level != AccessLevel.PRIVATE
                && level != AccessLevel.PACKAGE && !isLocalType(type)) {
            constructorProperties = createConstructorProperties(source, fields);
        }

        constructor.annotations = copyAnnotations(source, onConstructor.toArray(new Annotation[0]),
                constructorProperties);
    }

    constructor.traverse(new SetGeneratedByVisitor(source), typeDeclaration.scope);
    return constructor;
}

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

License:Open Source License

public Expression createResultCalculation(ASTNode source, Expression ex) {
    /* result = result * PRIME + (ex); */
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    SingleNameReference resultRef = new SingleNameReference(RESULT, p);
    setGeneratedBy(resultRef, source);// w ww  .  ja  v a  2s  .  c o m
    SingleNameReference primeRef = new SingleNameReference(PRIME, p);
    setGeneratedBy(primeRef, source);
    BinaryExpression multiplyByPrime = new BinaryExpression(resultRef, primeRef, OperatorIds.MULTIPLY);
    multiplyByPrime.sourceStart = pS;
    multiplyByPrime.sourceEnd = pE;
    setGeneratedBy(multiplyByPrime, source);
    BinaryExpression addItem = new BinaryExpression(multiplyByPrime, ex, OperatorIds.PLUS);
    addItem.sourceStart = pS;
    addItem.sourceEnd = pE;
    setGeneratedBy(addItem, source);
    resultRef = new SingleNameReference(RESULT, p);
    setGeneratedBy(resultRef, source);
    Assignment assignment = new Assignment(resultRef, addItem, pE);
    assignment.sourceStart = pS;
    assignment.sourceEnd = assignment.statementEnd = pE;
    setGeneratedBy(assignment, source);
    return assignment;
}

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

License:Open Source License

public Statement[] createLazyGetterBody(ASTNode source, EclipseNode fieldNode) {
    /*//w w  w .j  a v a2 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.HandleRelations.java

License:Open Source License

private static MethodDeclaration createSetReferencedObjectMethod(FieldDeclaration fieldDecl, ASTNode source,
        String relatedFieldName, boolean isOneToOne, boolean isUnique, TypeReference baseType,
        TypeReference referenceType, CompilationUnitDeclaration compilationUnitDeclaration) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;

    TypeReference refType = fieldDecl.type;

    if (isUnique) {
        refType = new ParameterizedQualifiedTypeReference(Eclipse.fromQualifiedName(List.class.getName()),
                new TypeReference[][] { null, null, new TypeReference[] { refType } }, 0,
                new long[] { p, p, p });
        setGeneratedBy(refType, source);
        refType.sourceStart = pS;//from   w  w w . j a  v a  2  s  .  c o m
        refType.sourceEnd = pE;
    }

    MethodDeclaration setReferencedObject = new MethodDeclaration(compilationUnitDeclaration.compilationResult);
    setGeneratedBy(setReferencedObject, source);
    setReferencedObject.modifiers = ClassFileConstants.AccPublic;
    setReferencedObject.annotations = new Annotation[] {
            makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, source) };
    setReferencedObject.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
    setReferencedObject.returnType.sourceStart = pS;
    setReferencedObject.returnType.sourceEnd = pE;
    setReferencedObject.arguments = new Argument[] {
            new Argument("item".toCharArray(), 0, baseType, ClassFileConstants.AccFinal),
            new Argument("related".toCharArray(), 0, refType, ClassFileConstants.AccFinal), };
    setGeneratedBy(setReferencedObject.arguments[0], source);
    setGeneratedBy(setReferencedObject.arguments[1], source);
    setReferencedObject.arguments[0].sourceStart = pS;
    setReferencedObject.arguments[0].sourceEnd = pE;
    setReferencedObject.arguments[1].sourceStart = pS;
    setReferencedObject.arguments[1].sourceEnd = pE;
    setReferencedObject.selector = "setReferencedObject".toCharArray();
    setReferencedObject.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    setReferencedObject.bodyStart = setReferencedObject.declarationSourceStart = setReferencedObject.sourceStart = source.sourceStart;
    setReferencedObject.bodyEnd = setReferencedObject.declarationSourceEnd = setReferencedObject.sourceEnd = source.sourceEnd;

    List<Statement> statements = new ArrayList<Statement>();

    SingleNameReference fieldNameRef = new SingleNameReference("related".toCharArray(), p);
    setGeneratedBy(fieldNameRef, source);
    Expression fieldRef = createFieldAccessor(new String(fieldDecl.name), source, "item".toCharArray());
    setGeneratedBy(fieldRef, source);
    fieldRef.sourceStart = pS;
    fieldRef.sourceEnd = pE;

    if (isUnique) {
        MessageSend get = new MessageSend();
        setGeneratedBy(get, source);
        get.sourceStart = pS;
        get.sourceEnd = get.statementEnd = pE;
        get.receiver = new ThisReference(pS, pE);
        get.selector = "firstOrDefault".toCharArray();
        get.arguments = new Expression[] { fieldNameRef };

        Assignment assignment = new Assignment(fieldRef, get, pE);
        setGeneratedBy(assignment, source);
        assignment.sourceStart = pS;
        assignment.sourceEnd = assignment.statementEnd = pE;

        statements.add(assignment);
    } else {
        Assignment assignment = new Assignment(fieldRef, fieldNameRef, pE);
        assignment.sourceStart = pS;
        assignment.sourceEnd = assignment.statementEnd = pE;
        setGeneratedBy(assignment, source);
        statements.add(assignment);
    }

    setReferencedObject.statements = statements.toArray(new Statement[statements.size()]);

    return setReferencedObject;
}

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

License:Open Source License

static MethodDeclaration createSetter(TypeDeclaration parent, EclipseNode fieldNode, String name,
        boolean shouldReturnThis, int modifier, EclipseNode sourceNode, List<Annotation> onMethod,
        List<Annotation> onParam) {
    FieldDeclaration field = (FieldDeclaration) fieldNode.get();
    ASTNode source = sourceNode.get();/*from www .ja  v  a  2s .c o m*/
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
    method.modifiers = modifier;
    if (shouldReturnThis) {
        method.returnType = cloneSelfType(fieldNode, source);
    }

    if (method.returnType == null) {
        method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
        method.returnType.sourceStart = pS;
        method.returnType.sourceEnd = pE;
        shouldReturnThis = false;
    }
    Annotation[] deprecated = null;
    if (isFieldDeprecated(fieldNode)) {
        deprecated = new Annotation[] { generateDeprecatedAnnotation(source) };
    }
    method.annotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), deprecated);
    Argument param = new Argument(field.name, p, copyType(field.type, source), Modifier.FINAL);
    param.sourceStart = pS;
    param.sourceEnd = pE;
    method.arguments = new Argument[] { param };
    method.selector = name.toCharArray();
    method.binding = null;
    method.thrownExceptions = null;
    method.typeParameters = null;
    method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    Expression fieldRef = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
    NameReference fieldNameRef = new SingleNameReference(field.name, p);
    Assignment assignment = new Assignment(fieldRef, fieldNameRef, (int) p);
    assignment.sourceStart = pS;
    assignment.sourceEnd = assignment.statementEnd = pE;
    method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
    method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;

    Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN);
    Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN);
    List<Statement> statements = new ArrayList<Statement>(5);
    if (nonNulls.length == 0) {
        statements.add(assignment);
    } else {
        Statement nullCheck = generateNullCheck(field, sourceNode);
        if (nullCheck != null)
            statements.add(nullCheck);
        statements.add(assignment);
    }

    if (shouldReturnThis) {
        ThisReference thisRef = new ThisReference(pS, pE);
        ReturnStatement returnThis = new ReturnStatement(thisRef, pS, pE);
        statements.add(returnThis);
    }
    method.statements = statements.toArray(new Statement[0]);
    param.annotations = copyAnnotations(source, nonNulls, nullables, onParam.toArray(new Annotation[0]));

    method.traverse(new SetGeneratedByVisitor(source), parent.scope);
    return method;
}

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

License:Open Source License

static MethodDeclaration createSetter(TypeDeclaration parent, EclipseNode fieldNode, String name,
        boolean shouldReturnThis, int modifier, EclipseNode sourceNode, List<Annotation> onMethod,
        List<Annotation> onParam, SetterVersionable ann, DataVersionable annData) {

    MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
    method.modifiers = modifier;//  www.j a  va 2 s . co  m

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

    if (shouldReturnThis) {
        method.returnType = cloneSelfType(fieldNode, source);
    }

    if (method.returnType == null) {
        method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
        method.returnType.sourceStart = pS;
        method.returnType.sourceEnd = pE;
        shouldReturnThis = false;
    }
    Annotation[] deprecated = null;
    if (isFieldDeprecated(fieldNode)) {
        deprecated = new Annotation[] { generateDeprecatedAnnotation(source) };
    }
    method.annotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), deprecated);
    Argument param = new Argument(field.name, p, copyType(field.type, source), Modifier.FINAL);
    param.sourceStart = pS;
    param.sourceEnd = pE;
    method.arguments = new Argument[] { param };
    method.selector = name.toCharArray();
    method.binding = null;
    method.thrownExceptions = null;
    method.typeParameters = null;
    method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    Expression fieldRef = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
    NameReference fieldNameRef = new SingleNameReference(field.name, p);
    Assignment assignment = new Assignment(fieldRef, fieldNameRef, (int) p);
    assignment.sourceStart = pS;
    assignment.sourceEnd = assignment.statementEnd = pE;
    method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
    method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;

    Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN);
    Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN);
    List<Statement> statements = new ArrayList<Statement>(5);

    if (nonNulls.length != 0) {
        Statement nullCheck = generateNullCheck(field, sourceNode);
        if (nullCheck != null)
            statements.add(nullCheck);
    }

    IfStatement ifVersionable = generateVersionableIf(p, sourceNode, fieldNode, assignment, ann, annData);

    if (ifVersionable != null) {
        statements.add(ifVersionable);
    }

    if (shouldReturnThis) {
        ThisReference thisRef = new ThisReference(pS, pE);
        ReturnStatement returnThis = new ReturnStatement(thisRef, pS, pE);
        statements.add(returnThis);
    }
    method.statements = statements.toArray(new Statement[0]);
    param.annotations = copyAnnotations(source, nonNulls, nullables, onParam.toArray(new Annotation[0]));

    method.traverse(new SetGeneratedByVisitor(source), parent.scope);

    return method;
}

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

License:Open Source License

/**
 * if (VersionableUtils.isAllowedForThisVersion(V1,V2,V3...)) {
 *  this.xxx = xxx;/*from   w ww .  ja va  2  s . c  o m*/
 * } else {
 *  this.xxx = null;
 * }
 * @param ann 
 * @param treeMaker
 * @param field
 * @param source
 * @param assign
 * @return
 */
private static IfStatement generateVersionableIf(long position, EclipseNode sourceNode, EclipseNode fieldNode,
        Assignment assignment, SetterVersionable ann, DataVersionable annData) {

    Version versions[] = null;
    if (ann != null) {
        versions = ann.versions();
    } else if (annData != null) {
        versions = annData.versions();
    }

    if (versions != null) {

        // VersionableUtils.isAllowedForThisVersion()
        MessageSend versionableUtilIsAllowedCall = new MessageSend();
        versionableUtilIsAllowedCall.sourceStart = sourceNode.get().sourceStart;
        versionableUtilIsAllowedCall.sourceEnd = sourceNode.get().sourceEnd;
        setGeneratedBy(versionableUtilIsAllowedCall, sourceNode.get());
        versionableUtilIsAllowedCall.receiver = generateQualifiedNameRef(sourceNode.get(),
                "lombok".toCharArray(), VersionableUtils.class.getSimpleName().toCharArray());
        versionableUtilIsAllowedCall.selector = "isAllowedForThisVersion".toCharArray();

        Expression[] expr = new Expression[versions.length];
        for (int i = 0; i < versions.length; i++) {
            expr[i] = HandleSetterVersionable.generateQualifiedNameRef(sourceNode.get(), "lombok".toCharArray(),
                    Version.class.getSimpleName().toCharArray(), versions[i].name().toCharArray());
        }
        versionableUtilIsAllowedCall.arguments = expr;

        // this.xxx = null
        Expression fieldRef = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, sourceNode.get());
        NullLiteral nullLiteral = new NullLiteral(sourceNode.get().sourceStart, sourceNode.get().sourceEnd);
        Assignment assignmentNull = new Assignment(fieldRef, nullLiteral, (int) position);

        // this.xxx = xxx
        FieldDeclaration field = (FieldDeclaration) fieldNode.get();
        NameReference fieldNameRef = new SingleNameReference(field.name, position);
        Assignment assignmentThis = new Assignment(fieldRef, fieldNameRef, (int) position);

        return new IfStatement(versionableUtilIsAllowedCall, assignmentThis, assignmentNull,
                sourceNode.get().sourceStart, sourceNode.get().sourceEnd);
    }
    return null;
}