Search in sources :

Example 11 with MethodDeclaration

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

the class HandleUtilityClass method changeModifiersAndGenerateConstructor.

private void changeModifiersAndGenerateConstructor(EclipseNode typeNode, EclipseNode annotationNode) {
    TypeDeclaration classDecl = (TypeDeclaration) typeNode.get();
    boolean makeConstructor = true;
    classDecl.modifiers |= ClassFileConstants.AccFinal;
    boolean markStatic = true;
    boolean requiresClInit = false;
    boolean alreadyHasClinit = false;
    if (typeNode.up().getKind() == Kind.COMPILATION_UNIT)
        markStatic = false;
    if (markStatic && typeNode.up().getKind() == Kind.TYPE) {
        TypeDeclaration typeDecl = (TypeDeclaration) typeNode.up().get();
        if ((typeDecl.modifiers & ClassFileConstants.AccInterface) != 0)
            markStatic = false;
    }
    if (markStatic)
        classDecl.modifiers |= ClassFileConstants.AccStatic;
    for (EclipseNode element : typeNode.down()) {
        if (element.getKind() == Kind.FIELD) {
            FieldDeclaration fieldDecl = (FieldDeclaration) element.get();
            if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) == 0) {
                requiresClInit = true;
                fieldDecl.modifiers |= ClassFileConstants.AccStatic;
            }
        } else if (element.getKind() == Kind.METHOD) {
            AbstractMethodDeclaration amd = (AbstractMethodDeclaration) element.get();
            if (amd instanceof ConstructorDeclaration) {
                ConstructorDeclaration constrDecl = (ConstructorDeclaration) element.get();
                if (getGeneratedBy(constrDecl) == null && (constrDecl.bits & ASTNode.IsDefaultConstructor) == 0) {
                    element.addError("@UtilityClasses cannot have declared constructors.");
                    makeConstructor = false;
                    continue;
                }
            } else if (amd instanceof MethodDeclaration) {
                amd.modifiers |= ClassFileConstants.AccStatic;
            } else if (amd instanceof Clinit) {
                alreadyHasClinit = true;
            }
        } else if (element.getKind() == Kind.TYPE) {
            ((TypeDeclaration) element.get()).modifiers |= ClassFileConstants.AccStatic;
        }
    }
    if (makeConstructor)
        createPrivateDefaultConstructor(typeNode, annotationNode);
    if (requiresClInit && !alreadyHasClinit)
        classDecl.addClinit();
}
Also used : Clinit(org.eclipse.jdt.internal.compiler.ast.Clinit) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 12 with MethodDeclaration

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

the class HandleWither method createWitherForField.

public void createWitherForField(AccessLevel level, EclipseNode fieldNode, EclipseNode sourceNode, boolean whineIfExists, List<Annotation> onMethod, List<Annotation> onParam) {
    ASTNode source = sourceNode.get();
    if (fieldNode.getKind() != Kind.FIELD) {
        sourceNode.addError("@Wither is only supported on a class or a field.");
        return;
    }
    EclipseNode typeNode = fieldNode.up();
    boolean makeAbstract = typeNode != null && typeNode.getKind() == Kind.TYPE && (((TypeDeclaration) typeNode.get()).modifiers & ClassFileConstants.AccAbstract) != 0;
    FieldDeclaration field = (FieldDeclaration) fieldNode.get();
    TypeReference fieldType = copyType(field.type, source);
    boolean isBoolean = isBoolean(fieldType);
    String witherName = toWitherName(fieldNode, isBoolean);
    if (witherName == null) {
        fieldNode.addWarning("Not generating wither for this field: It does not fit your @Accessors prefix list.");
        return;
    }
    if ((field.modifiers & ClassFileConstants.AccStatic) != 0) {
        fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for static fields.");
        return;
    }
    if ((field.modifiers & ClassFileConstants.AccFinal) != 0 && field.initialization != null) {
        fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for final, initialized fields.");
        return;
    }
    if (field.name != null && field.name.length > 0 && field.name[0] == '$') {
        fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for fields starting with $.");
        return;
    }
    for (String altName : toAllWitherNames(fieldNode, isBoolean)) {
        switch(methodExists(altName, fieldNode, false, 1)) {
            case EXISTS_BY_LOMBOK:
                return;
            case EXISTS_BY_USER:
                if (whineIfExists) {
                    String altNameExpl = "";
                    if (!altName.equals(witherName))
                        altNameExpl = String.format(" (%s)", altName);
                    fieldNode.addWarning(String.format("Not generating %s(): A method with that name already exists%s", witherName, altNameExpl));
                }
                return;
            default:
            case NOT_EXISTS:
        }
    }
    int modifier = toEclipseModifier(level);
    MethodDeclaration method = createWither((TypeDeclaration) fieldNode.up().get(), fieldNode, witherName, modifier, sourceNode, onMethod, onParam, makeAbstract);
    injectMethod(fieldNode.up(), method);
}
Also used : MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) EclipseNode(lombok.eclipse.EclipseNode) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 13 with MethodDeclaration

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

the class HandleWither method createWither.

public MethodDeclaration createWither(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, EclipseNode sourceNode, List<Annotation> onMethod, List<Annotation> onParam, boolean makeAbstract) {
    ASTNode source = sourceNode.get();
    if (name == null)
        return null;
    FieldDeclaration field = (FieldDeclaration) fieldNode.get();
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
    if (makeAbstract)
        modifier = modifier | ClassFileConstants.AccAbstract | ExtraCompilerModifiers.AccSemicolonBody;
    method.modifiers = modifier;
    method.returnType = cloneSelfType(fieldNode, source);
    if (method.returnType == null)
        return null;
    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), ClassFileConstants.AccFinal);
    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;
    Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN);
    Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN);
    if (!makeAbstract) {
        List<Expression> args = new ArrayList<Expression>();
        for (EclipseNode child : fieldNode.up().down()) {
            if (child.getKind() != Kind.FIELD)
                continue;
            FieldDeclaration childDecl = (FieldDeclaration) child.get();
            // Skip fields that start with $
            if (childDecl.name != null && childDecl.name.length > 0 && childDecl.name[0] == '$')
                continue;
            long fieldFlags = childDecl.modifiers;
            // Skip static fields.
            if ((fieldFlags & ClassFileConstants.AccStatic) != 0)
                continue;
            // Skip initialized final fields.
            if (((fieldFlags & ClassFileConstants.AccFinal) != 0) && childDecl.initialization != null)
                continue;
            if (child.get() == fieldNode.get()) {
                args.add(new SingleNameReference(field.name, p));
            } else {
                args.add(createFieldAccessor(child, FieldAccess.ALWAYS_FIELD, source));
            }
        }
        AllocationExpression constructorCall = new AllocationExpression();
        constructorCall.arguments = args.toArray(new Expression[0]);
        constructorCall.type = cloneSelfType(fieldNode, source);
        Expression identityCheck = new EqualExpression(createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source), new SingleNameReference(field.name, p), OperatorIds.EQUAL_EQUAL);
        ThisReference thisRef = new ThisReference(pS, pE);
        Expression conditional = new ConditionalExpression(identityCheck, thisRef, constructorCall);
        Statement returnStatement = new ReturnStatement(conditional, pS, pE);
        method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
        method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
        List<Statement> statements = new ArrayList<Statement>(5);
        if (nonNulls.length > 0) {
            Statement nullCheck = generateNullCheck(field, sourceNode);
            if (nullCheck != null)
                statements.add(nullCheck);
        }
        statements.add(returnStatement);
        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 : 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) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) ArrayList(java.util.ArrayList) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) 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) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) EclipseNode(lombok.eclipse.EclipseNode)

Example 14 with MethodDeclaration

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

the class EclipseGuavaSingularizer method generateClearMethod.

void generateClearMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    Assignment a = new Assignment(thisDotField, new NullLiteral(0, 0), 0);
    md.selector = HandlerUtil.buildAccessorName("clear", new String(data.getPluralName())).toCharArray();
    md.statements = returnStatement != null ? new Statement[] { a, returnStatement } : new Statement[] { a };
    md.returnType = returnType;
    injectMethod(builderType, md);
}
Also used : Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral)

Example 15 with MethodDeclaration

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

the class HandleBuilder method generateCleanMethod.

private MethodDeclaration generateCleanMethod(List<BuilderFieldData> builderFields, EclipseNode builderType, ASTNode source) {
    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;
}
Also used : Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) ArrayList(java.util.ArrayList) QualifiedThisReference(org.eclipse.jdt.internal.compiler.ast.QualifiedThisReference) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) FalseLiteral(org.eclipse.jdt.internal.compiler.ast.FalseLiteral)

Aggregations

MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)40 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)22 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)19 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)19 ArrayList (java.util.ArrayList)18 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)17 EclipseNode (lombok.eclipse.EclipseNode)16 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)16 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)15 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)15 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)15 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)15 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)14 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)14 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)13 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)12 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)11 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)10 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)8 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)6