Search in sources :

Example 11 with Statement

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

the class HandleEqualsAndHashCode method createEquals.

public MethodDeclaration createEquals(EclipseNode type, Collection<EclipseNode> fields, boolean callSuper, ASTNode source, FieldAccess fieldAccess, boolean needsCanEqual, List<Annotation> onParam) {
    int pS = source.sourceStart;
    int pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    MethodDeclaration method = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult);
    setGeneratedBy(method, source);
    method.modifiers = toEclipseModifier(AccessLevel.PUBLIC);
    method.returnType = TypeReference.baseTypeReference(TypeIds.T_boolean, 0);
    method.returnType.sourceStart = pS;
    method.returnType.sourceEnd = pE;
    setGeneratedBy(method.returnType, source);
    method.annotations = new Annotation[] { makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, source) };
    method.selector = "equals".toCharArray();
    method.thrownExceptions = null;
    method.typeParameters = null;
    method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
    method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
    method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
    TypeReference objectRef = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { p, p, p });
    setGeneratedBy(objectRef, source);
    method.arguments = new Argument[] { new Argument(new char[] { 'o' }, 0, objectRef, Modifier.FINAL) };
    method.arguments[0].sourceStart = pS;
    method.arguments[0].sourceEnd = pE;
    if (!onParam.isEmpty())
        method.arguments[0].annotations = onParam.toArray(new Annotation[0]);
    setGeneratedBy(method.arguments[0], source);
    List<Statement> statements = new ArrayList<Statement>();
    /* if (o == this) return true; */
    {
        SingleNameReference oRef = new SingleNameReference(new char[] { 'o' }, p);
        setGeneratedBy(oRef, source);
        ThisReference thisRef = new ThisReference(pS, pE);
        setGeneratedBy(thisRef, source);
        EqualExpression otherEqualsThis = new EqualExpression(oRef, thisRef, OperatorIds.EQUAL_EQUAL);
        setGeneratedBy(otherEqualsThis, source);
        TrueLiteral trueLiteral = new TrueLiteral(pS, pE);
        setGeneratedBy(trueLiteral, source);
        ReturnStatement returnTrue = new ReturnStatement(trueLiteral, pS, pE);
        setGeneratedBy(returnTrue, source);
        IfStatement ifOtherEqualsThis = new IfStatement(otherEqualsThis, returnTrue, pS, pE);
        setGeneratedBy(ifOtherEqualsThis, source);
        statements.add(ifOtherEqualsThis);
    }
    /* if (!(o instanceof Outer.Inner.MyType) return false; */
    {
        SingleNameReference oRef = new SingleNameReference(new char[] { 'o' }, p);
        setGeneratedBy(oRef, source);
        TypeReference typeReference = createTypeReference(type, p, source, false);
        setGeneratedBy(typeReference, source);
        InstanceOfExpression instanceOf = new InstanceOfExpression(oRef, typeReference);
        instanceOf.sourceStart = pS;
        instanceOf.sourceEnd = pE;
        setGeneratedBy(instanceOf, source);
        Expression notInstanceOf = new UnaryExpression(instanceOf, OperatorIds.NOT);
        setGeneratedBy(notInstanceOf, source);
        FalseLiteral falseLiteral = new FalseLiteral(pS, pE);
        setGeneratedBy(falseLiteral, source);
        ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE);
        setGeneratedBy(returnFalse, source);
        IfStatement ifNotInstanceOf = new IfStatement(notInstanceOf, returnFalse, pS, pE);
        setGeneratedBy(ifNotInstanceOf, source);
        statements.add(ifNotInstanceOf);
    }
    char[] otherName = "other".toCharArray();
    /* Outer.Inner.MyType<?> other = (Outer.Inner.MyType<?>) o; */
    {
        if (!fields.isEmpty() || needsCanEqual) {
            LocalDeclaration other = new LocalDeclaration(otherName, pS, pE);
            other.modifiers |= ClassFileConstants.AccFinal;
            setGeneratedBy(other, source);
            TypeReference targetType = createTypeReference(type, p, source, true);
            setGeneratedBy(targetType, source);
            other.type = createTypeReference(type, p, source, true);
            setGeneratedBy(other.type, source);
            NameReference oRef = new SingleNameReference(new char[] { 'o' }, p);
            setGeneratedBy(oRef, source);
            other.initialization = makeCastExpression(oRef, targetType, source);
            statements.add(other);
        }
    }
    /* if (!other.canEqual((java.lang.Object) this)) return false; */
    {
        if (needsCanEqual) {
            MessageSend otherCanEqual = new MessageSend();
            otherCanEqual.sourceStart = pS;
            otherCanEqual.sourceEnd = pE;
            setGeneratedBy(otherCanEqual, source);
            otherCanEqual.receiver = new SingleNameReference(otherName, p);
            setGeneratedBy(otherCanEqual.receiver, source);
            otherCanEqual.selector = "canEqual".toCharArray();
            ThisReference thisReference = new ThisReference(pS, pE);
            setGeneratedBy(thisReference, source);
            CastExpression castThisRef = makeCastExpression(thisReference, generateQualifiedTypeRef(source, TypeConstants.JAVA_LANG_OBJECT), source);
            castThisRef.sourceStart = pS;
            castThisRef.sourceEnd = pE;
            otherCanEqual.arguments = new Expression[] { castThisRef };
            Expression notOtherCanEqual = new UnaryExpression(otherCanEqual, OperatorIds.NOT);
            setGeneratedBy(notOtherCanEqual, source);
            FalseLiteral falseLiteral = new FalseLiteral(pS, pE);
            setGeneratedBy(falseLiteral, source);
            ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE);
            setGeneratedBy(returnFalse, source);
            IfStatement ifNotCanEqual = new IfStatement(notOtherCanEqual, returnFalse, pS, pE);
            setGeneratedBy(ifNotCanEqual, source);
            statements.add(ifNotCanEqual);
        }
    }
    /* if (!super.equals(o)) return false; */
    if (callSuper) {
        MessageSend callToSuper = new MessageSend();
        callToSuper.sourceStart = pS;
        callToSuper.sourceEnd = pE;
        setGeneratedBy(callToSuper, source);
        callToSuper.receiver = new SuperReference(pS, pE);
        setGeneratedBy(callToSuper.receiver, source);
        callToSuper.selector = "equals".toCharArray();
        SingleNameReference oRef = new SingleNameReference(new char[] { 'o' }, p);
        setGeneratedBy(oRef, source);
        callToSuper.arguments = new Expression[] { oRef };
        Expression superNotEqual = new UnaryExpression(callToSuper, OperatorIds.NOT);
        setGeneratedBy(superNotEqual, source);
        FalseLiteral falseLiteral = new FalseLiteral(pS, pE);
        setGeneratedBy(falseLiteral, source);
        ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE);
        setGeneratedBy(returnFalse, source);
        IfStatement ifSuperEquals = new IfStatement(superNotEqual, returnFalse, pS, pE);
        setGeneratedBy(ifSuperEquals, source);
        statements.add(ifSuperEquals);
    }
    for (EclipseNode field : fields) {
        TypeReference fType = getFieldType(field, fieldAccess);
        char[] token = fType.getLastToken();
        Expression thisFieldAccessor = createFieldAccessor(field, fieldAccess, source);
        Expression otherFieldAccessor = createFieldAccessor(field, fieldAccess, source, otherName);
        if (fType.dimensions() == 0 && token != null) {
            if (Arrays.equals(TypeConstants.FLOAT, token)) {
                statements.add(generateCompareFloatOrDouble(thisFieldAccessor, otherFieldAccessor, "Float".toCharArray(), source));
            } else if (Arrays.equals(TypeConstants.DOUBLE, token)) {
                statements.add(generateCompareFloatOrDouble(thisFieldAccessor, otherFieldAccessor, "Double".toCharArray(), source));
            } else if (BUILT_IN_TYPES.contains(new String(token))) {
                EqualExpression fieldsNotEqual = new EqualExpression(thisFieldAccessor, otherFieldAccessor, OperatorIds.NOT_EQUAL);
                setGeneratedBy(fieldsNotEqual, source);
                FalseLiteral falseLiteral = new FalseLiteral(pS, pE);
                setGeneratedBy(falseLiteral, source);
                ReturnStatement returnStatement = new ReturnStatement(falseLiteral, pS, pE);
                setGeneratedBy(returnStatement, source);
                IfStatement ifStatement = new IfStatement(fieldsNotEqual, returnStatement, pS, pE);
                setGeneratedBy(ifStatement, source);
                statements.add(ifStatement);
            } else /* objects */
            {
                /* final java.lang.Object this$fieldName = this.fieldName; */
                /* final java.lang.Object other$fieldName = other.fieldName; */
                /* if (this$fieldName == null ? other$fieldName != null : !this$fieldName.equals(other$fieldName)) return false; */
                char[] thisDollarFieldName = ("this$" + field.getName()).toCharArray();
                char[] otherDollarFieldName = ("other$" + field.getName()).toCharArray();
                statements.add(createLocalDeclaration(source, thisDollarFieldName, generateQualifiedTypeRef(source, TypeConstants.JAVA_LANG_OBJECT), thisFieldAccessor));
                statements.add(createLocalDeclaration(source, otherDollarFieldName, generateQualifiedTypeRef(source, TypeConstants.JAVA_LANG_OBJECT), otherFieldAccessor));
                SingleNameReference this1 = new SingleNameReference(thisDollarFieldName, p);
                setGeneratedBy(this1, source);
                SingleNameReference this2 = new SingleNameReference(thisDollarFieldName, p);
                setGeneratedBy(this2, source);
                SingleNameReference other1 = new SingleNameReference(otherDollarFieldName, p);
                setGeneratedBy(other1, source);
                SingleNameReference other2 = new SingleNameReference(otherDollarFieldName, p);
                setGeneratedBy(other2, source);
                NullLiteral nullLiteral = new NullLiteral(pS, pE);
                setGeneratedBy(nullLiteral, source);
                EqualExpression fieldIsNull = new EqualExpression(this1, nullLiteral, OperatorIds.EQUAL_EQUAL);
                nullLiteral = new NullLiteral(pS, pE);
                setGeneratedBy(nullLiteral, source);
                EqualExpression otherFieldIsntNull = new EqualExpression(other1, nullLiteral, OperatorIds.NOT_EQUAL);
                MessageSend equalsCall = new MessageSend();
                equalsCall.sourceStart = pS;
                equalsCall.sourceEnd = pE;
                setGeneratedBy(equalsCall, source);
                equalsCall.receiver = this2;
                equalsCall.selector = "equals".toCharArray();
                equalsCall.arguments = new Expression[] { other2 };
                UnaryExpression fieldsNotEqual = new UnaryExpression(equalsCall, OperatorIds.NOT);
                fieldsNotEqual.sourceStart = pS;
                fieldsNotEqual.sourceEnd = pE;
                setGeneratedBy(fieldsNotEqual, source);
                ConditionalExpression fullEquals = new ConditionalExpression(fieldIsNull, otherFieldIsntNull, fieldsNotEqual);
                fullEquals.sourceStart = pS;
                fullEquals.sourceEnd = pE;
                setGeneratedBy(fullEquals, source);
                FalseLiteral falseLiteral = new FalseLiteral(pS, pE);
                setGeneratedBy(falseLiteral, source);
                ReturnStatement returnStatement = new ReturnStatement(falseLiteral, pS, pE);
                setGeneratedBy(returnStatement, source);
                IfStatement ifStatement = new IfStatement(fullEquals, returnStatement, pS, pE);
                setGeneratedBy(ifStatement, source);
                statements.add(ifStatement);
            }
        } else if (fType.dimensions() > 0 && token != null) {
            MessageSend arraysEqualCall = new MessageSend();
            arraysEqualCall.sourceStart = pS;
            arraysEqualCall.sourceEnd = pE;
            setGeneratedBy(arraysEqualCall, source);
            arraysEqualCall.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray());
            if (fType.dimensions() > 1 || !BUILT_IN_TYPES.contains(new String(token))) {
                arraysEqualCall.selector = "deepEquals".toCharArray();
            } else {
                arraysEqualCall.selector = "equals".toCharArray();
            }
            arraysEqualCall.arguments = new Expression[] { thisFieldAccessor, otherFieldAccessor };
            UnaryExpression arraysNotEqual = new UnaryExpression(arraysEqualCall, OperatorIds.NOT);
            arraysNotEqual.sourceStart = pS;
            arraysNotEqual.sourceEnd = pE;
            setGeneratedBy(arraysNotEqual, source);
            FalseLiteral falseLiteral = new FalseLiteral(pS, pE);
            setGeneratedBy(falseLiteral, source);
            ReturnStatement returnStatement = new ReturnStatement(falseLiteral, pS, pE);
            setGeneratedBy(returnStatement, source);
            IfStatement ifStatement = new IfStatement(arraysNotEqual, returnStatement, pS, pE);
            setGeneratedBy(ifStatement, source);
            statements.add(ifStatement);
        }
    }
    /* return true; */
    {
        TrueLiteral trueLiteral = new TrueLiteral(pS, pE);
        setGeneratedBy(trueLiteral, source);
        ReturnStatement returnStatement = new ReturnStatement(trueLiteral, pS, pE);
        setGeneratedBy(returnStatement, source);
        statements.add(returnStatement);
    }
    method.statements = statements.toArray(new Statement[statements.size()]);
    return method;
}
Also used : QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference) NameReference(org.eclipse.jdt.internal.compiler.ast.NameReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) ArrayList(java.util.ArrayList) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) UnaryExpression(org.eclipse.jdt.internal.compiler.ast.UnaryExpression) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) SuperReference(org.eclipse.jdt.internal.compiler.ast.SuperReference) FalseLiteral(org.eclipse.jdt.internal.compiler.ast.FalseLiteral) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) TrueLiteral(org.eclipse.jdt.internal.compiler.ast.TrueLiteral) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) BinaryExpression(org.eclipse.jdt.internal.compiler.ast.BinaryExpression) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) InstanceOfExpression(org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression) UnaryExpression(org.eclipse.jdt.internal.compiler.ast.UnaryExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) CastExpression(org.eclipse.jdt.internal.compiler.ast.CastExpression) EclipseNode(lombok.eclipse.EclipseNode) InstanceOfExpression(org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression) CastExpression(org.eclipse.jdt.internal.compiler.ast.CastExpression) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral)

Example 12 with Statement

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

the class HandleSetter method createSetter.

static MethodDeclaration createSetter(TypeDeclaration parent, boolean deprecate, EclipseNode fieldNode, String name, char[] booleanFieldToSet, boolean shouldReturnThis, int modifier, EclipseNode sourceNode, List<Annotation> onMethod, List<Annotation> onParam) {
    FieldDeclaration field = (FieldDeclaration) fieldNode.get();
    ASTNode source = sourceNode.get();
    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) || deprecate) {
        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 (booleanFieldToSet != null) {
        statements.add(new Assignment(new SingleNameReference(booleanFieldToSet, p), new TrueLiteral(pS, pE), pE));
    }
    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;
}
Also used : NameReference(org.eclipse.jdt.internal.compiler.ast.NameReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) 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) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) TrueLiteral(org.eclipse.jdt.internal.compiler.ast.TrueLiteral) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement)

Example 13 with Statement

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

the class HandleSneakyThrows method handleMethod.

// private boolean handleField(Node annotation, FieldDeclaration field, List<DeclaredException> exceptions) {
// if (field.initialization == null) {
// annotation.addError("@SneakyThrows can only be used on fields with an initialization statement.");
// return true;
// }
// 
// Expression expression = field.initialization;
// Statement[] content = new Statement[] {new Assignment(
// new SingleNameReference(field.name, 0), expression, 0)};
// field.initialization = null;
// 
// for (DeclaredException exception : exceptions) {
// content = new Statement[] { buildTryCatchBlock(content, exception) };
// }
// 
// Block block = new Block(0);
// block.statements = content;
// 
// Node typeNode = annotation.up().up();
// 
// Initializer initializer = new Initializer(block, field.modifiers & Modifier.STATIC);
// initializer.sourceStart = expression.sourceStart;
// initializer.sourceEnd = expression.sourceEnd;
// initializer.declarationSourceStart = expression.sourceStart;
// initializer.declarationSourceEnd = expression.sourceEnd;
// injectField(typeNode, initializer);
// 
// typeNode.rebuild();
// 
// return true;
// }
public void handleMethod(EclipseNode annotation, AbstractMethodDeclaration method, List<DeclaredException> exceptions) {
    if (method.isAbstract()) {
        annotation.addError("@SneakyThrows can only be used on concrete methods.");
        return;
    }
    if (method.statements == null || method.statements.length == 0) {
        boolean hasConstructorCall = false;
        if (method instanceof ConstructorDeclaration) {
            ExplicitConstructorCall constructorCall = ((ConstructorDeclaration) method).constructorCall;
            hasConstructorCall = constructorCall != null && !constructorCall.isImplicitSuper() && !constructorCall.isImplicitThis();
        }
        if (hasConstructorCall) {
            annotation.addWarning("Calls to sibling / super constructors are always excluded from @SneakyThrows; @SneakyThrows has been ignored because there is no other code in this constructor.");
        } else {
            annotation.addWarning("This method or constructor is empty; @SneakyThrows has been ignored.");
        }
        return;
    }
    Statement[] contents = method.statements;
    for (DeclaredException exception : exceptions) {
        contents = new Statement[] { buildTryCatchBlock(contents, exception, exception.node, method) };
    }
    method.statements = contents;
    annotation.up().rebuild();
}
Also used : ExplicitConstructorCall(org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) ThrowStatement(org.eclipse.jdt.internal.compiler.ast.ThrowStatement) TryStatement(org.eclipse.jdt.internal.compiler.ast.TryStatement)

Example 14 with Statement

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

the class HandleBuilder method generateBuildMethod.

public MethodDeclaration generateBuildMethod(EclipseNode tdParent, boolean isStatic, 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));
    }
    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) {
        if (bfd.nameOfSetFlag != null) {
            MessageSend inv = new MessageSend();
            inv.sourceStart = source.sourceStart;
            inv.sourceEnd = source.sourceEnd;
            inv.receiver = new SingleNameReference(((TypeDeclaration) tdParent.get()).name, 0L);
            inv.selector = bfd.nameOfDefaultProvider;
            inv.typeArguments = typeParameterNames(((TypeDeclaration) type.get()).typeParameters);
            args.add(new ConditionalExpression(new SingleNameReference(bfd.nameOfSetFlag, 0L), new SingleNameReference(bfd.name, 0L), inv));
        } else {
            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;
        if (isStatic)
            invoke.receiver = new SingleNameReference(type.up().getName().toCharArray(), 0);
        else
            invoke.receiver = new QualifiedThisReference(new SingleTypeReference(type.up().getName().toCharArray(), 0), 0, 0);
        invoke.typeArguments = typeParameterNames(((TypeDeclaration) type.get()).typeParameters);
        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;
}
Also used : FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) ArrayList(java.util.ArrayList) QualifiedThisReference(org.eclipse.jdt.internal.compiler.ast.QualifiedThisReference) UnaryExpression(org.eclipse.jdt.internal.compiler.ast.UnaryExpression) QualifiedThisReference(org.eclipse.jdt.internal.compiler.ast.QualifiedThisReference) 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) TrueLiteral(org.eclipse.jdt.internal.compiler.ast.TrueLiteral) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) UnaryExpression(org.eclipse.jdt.internal.compiler.ast.UnaryExpression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 15 with Statement

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

the class HandleCleanup method handle.

public void handle(AnnotationValues<Cleanup> annotation, Annotation ast, EclipseNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.CLEANUP_FLAG_USAGE, "@Cleanup");
    String cleanupName = annotation.getInstance().value();
    if (cleanupName.length() == 0) {
        annotationNode.addError("cleanupName cannot be the empty string.");
        return;
    }
    if (annotationNode.up().getKind() != Kind.LOCAL) {
        annotationNode.addError("@Cleanup is legal only on local variable declarations.");
        return;
    }
    LocalDeclaration decl = (LocalDeclaration) annotationNode.up().get();
    if (decl.initialization == null) {
        annotationNode.addError("@Cleanup variable declarations need to be initialized.");
        return;
    }
    EclipseNode ancestor = annotationNode.up().directUp();
    ASTNode blockNode = ancestor.get();
    final boolean isSwitch;
    final Statement[] statements;
    if (blockNode instanceof AbstractMethodDeclaration) {
        isSwitch = false;
        statements = ((AbstractMethodDeclaration) blockNode).statements;
    } else if (blockNode instanceof Block) {
        isSwitch = false;
        statements = ((Block) blockNode).statements;
    } else if (blockNode instanceof SwitchStatement) {
        isSwitch = true;
        statements = ((SwitchStatement) blockNode).statements;
    } else {
        annotationNode.addError("@Cleanup is legal only on a local variable declaration inside a block.");
        return;
    }
    if (statements == null) {
        annotationNode.addError("LOMBOK BUG: Parent block does not contain any statements.");
        return;
    }
    int start = 0;
    for (; start < statements.length; start++) {
        if (statements[start] == decl)
            break;
    }
    if (start == statements.length) {
        annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent.");
        return;
    }
    // We start with try{} *AFTER* the var declaration.
    start++;
    int end;
    if (isSwitch) {
        end = start + 1;
        for (; end < statements.length; end++) {
            if (statements[end] instanceof CaseStatement) {
                break;
            }
        }
    } else
        end = statements.length;
    // At this point:
    // start-1 = Local Declaration marked with @Cleanup
    // start = first instruction that needs to be wrapped into a try block
    // end = last instruction of the scope -OR- last instruction before the next case label in switch statements.
    // hence:
    // [start, end) = statements for the try block.
    Statement[] tryBlock = new Statement[end - start];
    System.arraycopy(statements, start, tryBlock, 0, end - start);
    // Remove the stuff we just dumped into the tryBlock, and then leave room for the try node.
    // Remove room for every statement moved into try block...
    int newStatementsLength = statements.length - (end - start);
    // But add room for the TryStatement node itself.
    newStatementsLength += 1;
    Statement[] newStatements = new Statement[newStatementsLength];
    // copy all statements before the try block verbatim.
    System.arraycopy(statements, 0, newStatements, 0, start);
    // For switch statements.
    System.arraycopy(statements, end, newStatements, start + 1, statements.length - end);
    doAssignmentCheck(annotationNode, tryBlock, decl.name);
    TryStatement tryStatement = new TryStatement();
    setGeneratedBy(tryStatement, ast);
    tryStatement.tryBlock = new Block(0);
    tryStatement.tryBlock.statements = tryBlock;
    setGeneratedBy(tryStatement.tryBlock, ast);
    // Positions for in-method generated nodes are special
    int ss = decl.declarationSourceEnd + 1;
    int se = ss;
    if (tryBlock.length > 0) {
        // +1 for the closing semicolon. Yes, there could be spaces. Bummer.
        se = tryBlock[tryBlock.length - 1].sourceEnd + 1;
        tryStatement.sourceStart = ss;
        tryStatement.sourceEnd = se;
        tryStatement.tryBlock.sourceStart = ss;
        tryStatement.tryBlock.sourceEnd = se;
    }
    newStatements[start] = tryStatement;
    Statement[] finallyBlock = new Statement[1];
    MessageSend unsafeClose = new MessageSend();
    setGeneratedBy(unsafeClose, ast);
    unsafeClose.sourceStart = ast.sourceStart;
    unsafeClose.sourceEnd = ast.sourceEnd;
    SingleNameReference receiver = new SingleNameReference(decl.name, 0);
    setGeneratedBy(receiver, ast);
    unsafeClose.receiver = receiver;
    long nameSourcePosition = (long) ast.sourceStart << 32 | ast.sourceEnd;
    if (ast.memberValuePairs() != null)
        for (MemberValuePair pair : ast.memberValuePairs()) {
            if (pair.name != null && new String(pair.name).equals("value")) {
                nameSourcePosition = (long) pair.value.sourceStart << 32 | pair.value.sourceEnd;
                break;
            }
        }
    unsafeClose.nameSourcePosition = nameSourcePosition;
    unsafeClose.selector = cleanupName.toCharArray();
    int pS = ast.sourceStart, pE = ast.sourceEnd;
    long p = (long) pS << 32 | pE;
    SingleNameReference varName = new SingleNameReference(decl.name, p);
    setGeneratedBy(varName, ast);
    NullLiteral nullLiteral = new NullLiteral(pS, pE);
    setGeneratedBy(nullLiteral, ast);
    MessageSend preventNullAnalysis = preventNullAnalysis(ast, varName);
    EqualExpression equalExpression = new EqualExpression(preventNullAnalysis, nullLiteral, OperatorIds.NOT_EQUAL);
    equalExpression.sourceStart = pS;
    equalExpression.sourceEnd = pE;
    setGeneratedBy(equalExpression, ast);
    Block closeBlock = new Block(0);
    closeBlock.statements = new Statement[1];
    closeBlock.statements[0] = unsafeClose;
    setGeneratedBy(closeBlock, ast);
    IfStatement ifStatement = new IfStatement(equalExpression, closeBlock, 0, 0);
    setGeneratedBy(ifStatement, ast);
    finallyBlock[0] = ifStatement;
    tryStatement.finallyBlock = new Block(0);
    // Positions for in-method generated nodes are special
    if (!isSwitch) {
        tryStatement.finallyBlock.sourceStart = blockNode.sourceEnd;
        tryStatement.finallyBlock.sourceEnd = blockNode.sourceEnd;
    }
    setGeneratedBy(tryStatement.finallyBlock, ast);
    tryStatement.finallyBlock.statements = finallyBlock;
    tryStatement.catchArguments = null;
    tryStatement.catchBlocks = null;
    if (blockNode instanceof AbstractMethodDeclaration) {
        ((AbstractMethodDeclaration) blockNode).statements = newStatements;
    } else if (blockNode instanceof Block) {
        ((Block) blockNode).statements = newStatements;
    } else if (blockNode instanceof SwitchStatement) {
        ((SwitchStatement) blockNode).statements = newStatements;
    }
    ancestor.rebuild();
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) TryStatement(org.eclipse.jdt.internal.compiler.ast.TryStatement) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) MemberValuePair(org.eclipse.jdt.internal.compiler.ast.MemberValuePair) TryStatement(org.eclipse.jdt.internal.compiler.ast.TryStatement) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) EclipseNode(lombok.eclipse.EclipseNode) Block(org.eclipse.jdt.internal.compiler.ast.Block) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral)

Aggregations

Statement (org.eclipse.jdt.internal.compiler.ast.Statement)38 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)27 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)27 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)27 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)23 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)21 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)21 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)19 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)19 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)18 ArrayList (java.util.ArrayList)17 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)15 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)15 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)14 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)14 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)12 NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)12 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)10 ConditionalExpression (org.eclipse.jdt.internal.compiler.ast.ConditionalExpression)9 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)9