Search in sources :

Example 6 with AllocationExpression

use of org.eclipse.jdt.internal.compiler.ast.AllocationExpression 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 7 with AllocationExpression

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

the class HandleBuilder method generateToBuilderMethod.

private MethodDeclaration generateToBuilderMethod(String methodName, String builderClassName, EclipseNode type, TypeParameter[] typeParams, List<BuilderFieldData> builderFields, boolean fluent, ASTNode source) {
    // return new ThingieBuilder<A, B>().setA(this.a).setB(this.b);
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    MethodDeclaration out = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult);
    out.selector = methodName.toCharArray();
    out.modifiers = ClassFileConstants.AccPublic;
    out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    out.returnType = namePlusTypeParamsToTypeReference(builderClassName.toCharArray(), typeParams, p);
    AllocationExpression invoke = new AllocationExpression();
    invoke.type = namePlusTypeParamsToTypeReference(builderClassName.toCharArray(), typeParams, p);
    Expression receiver = invoke;
    for (BuilderFieldData bfd : builderFields) {
        char[] setterName = fluent ? bfd.name : HandlerUtil.buildAccessorName("set", new String(bfd.name)).toCharArray();
        MessageSend ms = new MessageSend();
        if (bfd.obtainVia == null || !bfd.obtainVia.field().isEmpty()) {
            char[] fieldName = bfd.obtainVia == null ? bfd.rawName : bfd.obtainVia.field().toCharArray();
            FieldReference fr = new FieldReference(fieldName, 0);
            fr.receiver = new ThisReference(0, 0);
            ms.arguments = new Expression[] { fr };
        } else {
            String obtainName = bfd.obtainVia.method();
            boolean obtainIsStatic = bfd.obtainVia.isStatic();
            MessageSend obtainExpr = new MessageSend();
            obtainExpr.receiver = obtainIsStatic ? new SingleNameReference(type.getName().toCharArray(), 0) : new ThisReference(0, 0);
            obtainExpr.selector = obtainName.toCharArray();
            if (obtainIsStatic)
                obtainExpr.arguments = new Expression[] { new ThisReference(0, 0) };
            ms.arguments = new Expression[] { obtainExpr };
        }
        ms.receiver = receiver;
        ms.selector = setterName;
        receiver = ms;
    }
    out.statements = new Statement[] { new ReturnStatement(receiver, pS, pE) };
    out.traverse(new SetGeneratedByVisitor(source), ((TypeDeclaration) type.get()).scope);
    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) QualifiedThisReference(org.eclipse.jdt.internal.compiler.ast.QualifiedThisReference) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) 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) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement)

Example 8 with AllocationExpression

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

the class HandleBuilder method generateBuilderMethod.

public MethodDeclaration generateBuilderMethod(boolean isStatic, String builderMethodName, String builderClassName, EclipseNode type, TypeParameter[] typeParams, ASTNode source) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    MethodDeclaration out = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult);
    out.selector = builderMethodName.toCharArray();
    out.modifiers = ClassFileConstants.AccPublic;
    if (isStatic)
        out.modifiers |= ClassFileConstants.AccStatic;
    out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    out.returnType = namePlusTypeParamsToTypeReference(builderClassName.toCharArray(), typeParams, p);
    out.typeParameters = copyTypeParams(typeParams, source);
    AllocationExpression invoke = new AllocationExpression();
    invoke.type = namePlusTypeParamsToTypeReference(builderClassName.toCharArray(), typeParams, p);
    out.statements = new Statement[] { new ReturnStatement(invoke, pS, pE) };
    out.traverse(new SetGeneratedByVisitor(source), ((TypeDeclaration) type.get()).scope);
    return out;
}
Also used : AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement)

Example 9 with AllocationExpression

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

the class HandleHelper method handle.

@Override
public void handle(AnnotationValues<Helper> annotation, Annotation ast, EclipseNode annotationNode) {
    handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.HELPER_FLAG_USAGE, "@Helper");
    EclipseNode annotatedType = annotationNode.up();
    EclipseNode containingBlock = annotatedType == null ? null : annotatedType.directUp();
    Statement[] origStatements = getStatementsFromAstNode(containingBlock == null ? null : containingBlock.get());
    if (annotatedType == null || annotatedType.getKind() != Kind.TYPE || origStatements == null) {
        annotationNode.addError("@Helper is legal only on method-local classes.");
        return;
    }
    TypeDeclaration annotatedType_ = (TypeDeclaration) annotatedType.get();
    int indexOfType = -1;
    for (int i = 0; i < origStatements.length; i++) {
        if (origStatements[i] == annotatedType_) {
            indexOfType = i;
            break;
        }
    }
    final List<String> knownMethodNames = new ArrayList<String>();
    for (AbstractMethodDeclaration methodOfHelper : annotatedType_.methods) {
        if (!(methodOfHelper instanceof MethodDeclaration))
            continue;
        char[] name = methodOfHelper.selector;
        if (name != null && name.length > 0 && name[0] != '<')
            knownMethodNames.add(new String(name));
    }
    Collections.sort(knownMethodNames);
    final String[] knownMethodNames_ = knownMethodNames.toArray(new String[knownMethodNames.size()]);
    final char[] helperName = new char[annotatedType_.name.length + 1];
    final boolean[] helperUsed = new boolean[1];
    helperName[0] = '$';
    System.arraycopy(annotatedType_.name, 0, helperName, 1, helperName.length - 1);
    ASTVisitor visitor = new ASTVisitor() {

        @Override
        public boolean visit(MessageSend messageSend, BlockScope scope) {
            if (messageSend.receiver instanceof ThisReference) {
                if ((((ThisReference) messageSend.receiver).bits & ASTNode.IsImplicitThis) == 0)
                    return true;
            } else if (messageSend.receiver != null)
                return true;
            char[] name = messageSend.selector;
            if (name == null || name.length == 0 || name[0] == '<')
                return true;
            String n = new String(name);
            if (Arrays.binarySearch(knownMethodNames_, n) < 0)
                return true;
            messageSend.receiver = new SingleNameReference(helperName, messageSend.nameSourcePosition);
            helperUsed[0] = true;
            return true;
        }
    };
    for (int i = indexOfType + 1; i < origStatements.length; i++) {
        origStatements[i].traverse(visitor, null);
    }
    if (!helperUsed[0]) {
        annotationNode.addWarning("No methods of this helper class are ever used.");
        return;
    }
    Statement[] newStatements = new Statement[origStatements.length + 1];
    System.arraycopy(origStatements, 0, newStatements, 0, indexOfType + 1);
    System.arraycopy(origStatements, indexOfType + 1, newStatements, indexOfType + 2, origStatements.length - indexOfType - 1);
    LocalDeclaration decl = new LocalDeclaration(helperName, 0, 0);
    decl.modifiers |= ClassFileConstants.AccFinal;
    AllocationExpression alloc = new AllocationExpression();
    alloc.type = new SingleTypeReference(annotatedType_.name, 0L);
    decl.initialization = alloc;
    decl.type = new SingleTypeReference(annotatedType_.name, 0L);
    SetGeneratedByVisitor sgbvVisitor = new SetGeneratedByVisitor(annotationNode.get());
    decl.traverse(sgbvVisitor, null);
    newStatements[indexOfType + 1] = decl;
    setStatementsOfAstNode(containingBlock.get(), newStatements);
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) ASTVisitor(org.eclipse.jdt.internal.compiler.ASTVisitor) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) BlockScope(org.eclipse.jdt.internal.compiler.lookup.BlockScope) EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Example 10 with AllocationExpression

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

the class HandleUtilityClass method createPrivateDefaultConstructor.

private void createPrivateDefaultConstructor(EclipseNode typeNode, EclipseNode sourceNode) {
    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 = ClassFileConstants.AccPrivate;
    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;
    AllocationExpression exception = new AllocationExpression();
    setGeneratedBy(exception, source);
    long[] ps = new long[JAVA_LANG_UNSUPPORTED_OPERATION_EXCEPTION.length];
    Arrays.fill(ps, p);
    exception.type = new QualifiedTypeReference(JAVA_LANG_UNSUPPORTED_OPERATION_EXCEPTION, ps);
    setGeneratedBy(exception.type, source);
    exception.arguments = new Expression[] { new StringLiteral(UNSUPPORTED_MESSAGE, source.sourceStart, source.sourceEnd, 0) };
    setGeneratedBy(exception.arguments[0], source);
    ThrowStatement throwStatement = new ThrowStatement(exception, source.sourceStart, source.sourceEnd);
    setGeneratedBy(throwStatement, source);
    constructor.statements = new Statement[] { throwStatement };
    injectMethod(typeNode, constructor);
}
Also used : ExplicitConstructorCall(org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall) StringLiteral(org.eclipse.jdt.internal.compiler.ast.StringLiteral) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) ThrowStatement(org.eclipse.jdt.internal.compiler.ast.ThrowStatement) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Aggregations

AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)12 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)9 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)8 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)7 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)7 ConditionalExpression (org.eclipse.jdt.internal.compiler.ast.ConditionalExpression)6 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)6 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)6 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)6 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)6 ArrayList (java.util.ArrayList)5 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)5 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)5 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)5 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)5 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)4 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)4 NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)4 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)4 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)4