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

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

Introduction

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

Prototype

public IfStatement(Expression condition, Statement thenStatement, Statement elseStatement, int sourceStart,
            int sourceEnd) 

Source Link

Usage

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

License:Open Source License

/**
 * if (VersionableUtils.isAllowedForThisVersion(V1,V2,V3...)) {
 *  this.xxx = xxx;/*from  w ww  .  j ava2 s .  c om*/
 * } 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;
}

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

License:Open Source License

/**
 * Generates a constructor that has a builder as the only parameter.
 * The values from the builder are used to initialize the fields of new instances.
 *
 * @param typeNode/*  www  .  ja v a 2s. c  om*/
 *            the type (with the {@code @Builder} annotation) for which a
 *            constructor should be generated.
 * @param typeParams
 * @param builderFields a list of fields in the builder which should be assigned to new instances.
 * @param source the annotation (used for setting source code locations for the generated code).
 * @param callBuilderBasedSuperConstructor
 *            If {@code true}, the constructor will explicitly call a super
 *            constructor with the builder as argument. Requires
 *            {@code builderClassAsParameter != null}.
 */
private void generateBuilderBasedConstructor(EclipseNode typeNode, TypeParameter[] typeParams,
        List<BuilderFieldData> builderFields, EclipseNode sourceNode, String builderClassName,
        boolean callBuilderBasedSuperConstructor) {

    ASTNode source = sourceNode.get();

    TypeDeclaration typeDeclaration = ((TypeDeclaration) typeNode.get());
    long p = (long) source.sourceStart << 32 | source.sourceEnd;

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

    constructor.modifiers = toEclipseModifier(AccessLevel.PROTECTED);
    constructor.selector = typeDeclaration.name;
    if (callBuilderBasedSuperConstructor) {
        constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.Super);
        constructor.constructorCall.arguments = new Expression[] {
                new SingleNameReference(BUILDER_VARIABLE_NAME, p) };
    } else {
        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;

    TypeReference[] wildcards = new TypeReference[] { new Wildcard(Wildcard.UNBOUND),
            new Wildcard(Wildcard.UNBOUND) };
    TypeReference builderType = new ParameterizedSingleTypeReference(builderClassName.toCharArray(),
            mergeToTypeReferences(typeParams, wildcards), 0, p);
    constructor.arguments = new Argument[] {
            new Argument(BUILDER_VARIABLE_NAME, p, builderType, Modifier.FINAL) };

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

    for (BuilderFieldData fieldNode : builderFields) {
        char[] fieldName = removePrefixFromField(fieldNode.originalFieldNode);
        FieldReference fieldInThis = new FieldReference(fieldNode.rawName, p);
        int s = (int) (p >> 32);
        int e = (int) p;
        fieldInThis.receiver = new ThisReference(s, e);

        Expression assignmentExpr;
        if (fieldNode.singularData != null && fieldNode.singularData.getSingularizer() != null) {
            fieldNode.singularData.getSingularizer().appendBuildCode(fieldNode.singularData, typeNode,
                    statements, fieldNode.name, BUILDER_VARIABLE_NAME_STRING);
            assignmentExpr = new SingleNameReference(fieldNode.name, p);
        } else {
            char[][] variableInBuilder = new char[][] { BUILDER_VARIABLE_NAME, fieldName };
            long[] positions = new long[] { p, p };
            assignmentExpr = new QualifiedNameReference(variableInBuilder, positions, s, e);
        }
        Statement assignment = new Assignment(fieldInThis, assignmentExpr, (int) p);

        // In case of @Builder.Default, set the value to the default if it was NOT set in the builder.
        if (fieldNode.nameOfSetFlag != null) {
            char[][] setVariableInBuilder = new char[][] { BUILDER_VARIABLE_NAME, fieldNode.nameOfSetFlag };
            long[] positions = new long[] { p, p };
            QualifiedNameReference setVariableInBuilderRef = new QualifiedNameReference(setVariableInBuilder,
                    positions, s, e);

            MessageSend defaultMethodCall = new MessageSend();
            defaultMethodCall.sourceStart = source.sourceStart;
            defaultMethodCall.sourceEnd = source.sourceEnd;
            defaultMethodCall.receiver = new SingleNameReference(((TypeDeclaration) typeNode.get()).name, 0L);
            defaultMethodCall.selector = fieldNode.nameOfDefaultProvider;
            defaultMethodCall.typeArguments = typeParameterNames(
                    ((TypeDeclaration) typeNode.get()).typeParameters);

            Statement defaultAssignment = new Assignment(fieldInThis, defaultMethodCall, (int) p);
            IfStatement ifBlockForDefault = new IfStatement(setVariableInBuilderRef, assignment,
                    defaultAssignment, s, e);
            statements.add(ifBlockForDefault);
        } else {
            statements.add(assignment);
        }

        if (hasNonNullAnnotations(fieldNode.originalFieldNode)) {
            Statement nullCheck = generateNullCheck((FieldDeclaration) fieldNode.originalFieldNode.get(),
                    sourceNode);
            if (nullCheck != null)
                statements.add(nullCheck);
        }
    }

    constructor.statements = statements.isEmpty() ? null : statements.toArray(new Statement[0]);

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

    injectMethod(typeNode, constructor);
}

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

License:Open Source License

protected void consumeStatementIfWithElse() {
    // IfThenElseStatement ::=  'if' '(' Expression ')' StatementNoShortIf 'else' Statement
    // IfThenElseStatementNoShortIf ::=  'if' '(' Expression ')' StatementNoShortIf 'else' StatementNoShortIf

    this.expressionLengthPtr--;

    // optimized {..., Then, Else } ==> {..., If }
    this.astLengthPtr--;

    //optimize the push/pop
    this.astStack[--this.astPtr] = new IfStatement(this.expressionStack[this.expressionPtr--],
            (Statement) this.astStack[this.astPtr], (Statement) this.astStack[this.astPtr + 1],
            this.intStack[this.intPtr--], this.endStatementPosition);
}

From source file:org.nabucco.framework.mda.model.java.ast.produce.JavaAstModelProducer.java

License:Open Source License

/**
 * Creates an if-statement with else.//  w w w  .  j  a  va2 s. c  o m
 * 
 * @param condition
 *            the condition
 * @param thenStatement
 *            the then block
 * @param elseStatement
 *            the else statement
 * 
 * @return the created if-statement
 * 
 * @throws JavaModelException
 */
public IfStatement createIfStatement(Expression condition, Statement thenStatement, Statement elseStatement)
        throws JavaModelException {
    return new IfStatement(condition, thenStatement, elseStatement, 0, 0);
}