Search in sources :

Example 1 with QualifiedThisReference

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

the class HandleBuilder method generateBuildMethod.

public MethodDeclaration generateBuildMethod(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) {
        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);
        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;
}
Also used : TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) ArrayList(java.util.ArrayList) QualifiedThisReference(org.eclipse.jdt.internal.compiler.ast.QualifiedThisReference) UnaryExpression(org.eclipse.jdt.internal.compiler.ast.UnaryExpression) 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) 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) 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) QualifiedThisReference(org.eclipse.jdt.internal.compiler.ast.QualifiedThisReference) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) 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) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Aggregations

ArrayList (java.util.ArrayList)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)1 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)1 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)1 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)1 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)1 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)1 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)1 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)1 ParameterizedSingleTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference)1 QualifiedThisReference (org.eclipse.jdt.internal.compiler.ast.QualifiedThisReference)1 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)1 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)1 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)1 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)1 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)1 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)1 TrueLiteral (org.eclipse.jdt.internal.compiler.ast.TrueLiteral)1 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1