Search in sources :

Example 26 with FieldDeclaration

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

the class HandleFieldDefaults method setFieldDefaultsForField.

public void setFieldDefaultsForField(EclipseNode fieldNode, ASTNode pos, AccessLevel level, boolean makeFinal) {
    FieldDeclaration field = (FieldDeclaration) fieldNode.get();
    if (level != null && level != AccessLevel.NONE) {
        if ((field.modifiers & (ClassFileConstants.AccPublic | ClassFileConstants.AccPrivate | ClassFileConstants.AccProtected)) == 0) {
            if (!hasAnnotation(PackagePrivate.class, fieldNode)) {
                if ((field.modifiers & ClassFileConstants.AccStatic) == 0) {
                    field.modifiers |= EclipseHandlerUtil.toEclipseModifier(level);
                }
            }
        }
    }
    if (makeFinal && (field.modifiers & ClassFileConstants.AccFinal) == 0) {
        if (!hasAnnotation(NonFinal.class, fieldNode)) {
            if ((field.modifiers & ClassFileConstants.AccStatic) == 0) {
                field.modifiers |= ClassFileConstants.AccFinal;
            }
        }
    }
    fieldNode.rebuild();
}
Also used : PackagePrivate(lombok.experimental.PackagePrivate) NonFinal(lombok.experimental.NonFinal) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 27 with FieldDeclaration

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

the class HandleGetter method createGetter.

public MethodDeclaration createGetter(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, ASTNode source, boolean lazy, List<Annotation> onMethod) {
    FieldDeclaration field = (FieldDeclaration) fieldNode.get();
    // Remember the type; lazy will change it;
    TypeReference returnType = copyType(((FieldDeclaration) fieldNode.get()).type, source);
    Statement[] statements;
    if (lazy) {
        statements = createLazyGetterBody(source, fieldNode);
    } else {
        statements = createSimpleGetterBody(source, fieldNode);
    }
    MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
    method.modifiers = modifier;
    method.returnType = returnType;
    method.annotations = null;
    method.arguments = null;
    method.selector = name.toCharArray();
    method.binding = null;
    method.thrownExceptions = null;
    method.typeParameters = null;
    method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
    method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
    method.statements = statements;
    EclipseHandlerUtil.registerCreatedLazyGetter((FieldDeclaration) fieldNode.get(), method.selector, returnType);
    /* Generate annotations that must  be put on the generated method, and attach them. */
    {
        Annotation[] deprecated = null;
        if (isFieldDeprecated(fieldNode)) {
            deprecated = new Annotation[] { generateDeprecatedAnnotation(source) };
        }
        method.annotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN), findDelegatesAndMarkAsHandled(fieldNode), deprecated);
    }
    method.traverse(new SetGeneratedByVisitor(source), parent.scope);
    return method;
}
Also used : ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) SynchronizedStatement(org.eclipse.jdt.internal.compiler.ast.SynchronizedStatement) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation)

Example 28 with FieldDeclaration

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

the class HandleGetter method createGetterForField.

public void createGetterForField(AccessLevel level, EclipseNode fieldNode, EclipseNode errorNode, ASTNode source, boolean whineIfExists, boolean lazy, List<Annotation> onMethod) {
    if (fieldNode.getKind() != Kind.FIELD) {
        errorNode.addError("@Getter is only supported on a class or a field.");
        return;
    }
    FieldDeclaration field = (FieldDeclaration) fieldNode.get();
    if (lazy) {
        if ((field.modifiers & ClassFileConstants.AccPrivate) == 0 || (field.modifiers & ClassFileConstants.AccFinal) == 0) {
            errorNode.addError("'lazy' requires the field to be private and final.");
            return;
        }
        if ((field.modifiers & ClassFileConstants.AccTransient) != 0) {
            errorNode.addError("'lazy' is not supported on transient fields.");
            return;
        }
        if (field.initialization == null) {
            errorNode.addError("'lazy' requires field initialization.");
            return;
        }
    }
    TypeReference fieldType = copyType(field.type, source);
    boolean isBoolean = isBoolean(fieldType);
    String getterName = toGetterName(fieldNode, isBoolean);
    if (getterName == null) {
        errorNode.addWarning("Not generating getter for this field: It does not fit your @Accessors prefix list.");
        return;
    }
    int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic);
    for (String altName : toAllGetterNames(fieldNode, isBoolean)) {
        switch(methodExists(altName, fieldNode, false, 0)) {
            case EXISTS_BY_LOMBOK:
                return;
            case EXISTS_BY_USER:
                if (whineIfExists) {
                    String altNameExpl = "";
                    if (!altName.equals(getterName))
                        altNameExpl = String.format(" (%s)", altName);
                    errorNode.addWarning(String.format("Not generating %s(): A method with that name already exists%s", getterName, altNameExpl));
                }
                return;
            default:
            case NOT_EXISTS:
        }
    }
    MethodDeclaration method = createGetter((TypeDeclaration) fieldNode.up().get(), fieldNode, getterName, modifier, source, lazy, onMethod);
    injectMethod(fieldNode.up(), method);
}
Also used : MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 29 with FieldDeclaration

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

the class EclipseGuavaSingularizer method generateFields.

@Override
public List<EclipseNode> generateFields(SingularData data, EclipseNode builderType) {
    String simpleTypeName = getSimpleTargetTypeName(data);
    char[][] tokenizedName = makeGuavaTypeName(simpleTypeName, true);
    TypeReference type = new QualifiedTypeReference(tokenizedName, NULL_POSS);
    type = addTypeArgs(getTypeArgumentsCount(), false, builderType, type, data.getTypeArgs());
    FieldDeclaration buildField = new FieldDeclaration(data.getPluralName(), 0, -1);
    buildField.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    buildField.modifiers = ClassFileConstants.AccPrivate;
    buildField.declarationSourceEnd = -1;
    buildField.type = type;
    data.setGeneratedByRecursive(buildField);
    return Collections.singletonList(injectFieldAndMarkGenerated(builderType, buildField));
}
Also used : QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 30 with FieldDeclaration

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

the class EclipseJavaUtilListSetSingularizer method generateFields.

@Override
public List<EclipseNode> generateFields(SingularData data, EclipseNode builderType) {
    if (useGuavaInstead(builderType)) {
        return guavaListSetSingularizer.generateFields(data, builderType);
    }
    TypeReference type = new QualifiedTypeReference(JAVA_UTIL_ARRAYLIST, NULL_POSS);
    type = addTypeArgs(1, false, builderType, type, data.getTypeArgs());
    FieldDeclaration buildField = new FieldDeclaration(data.getPluralName(), 0, -1);
    buildField.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    buildField.modifiers = ClassFileConstants.AccPrivate;
    buildField.declarationSourceEnd = -1;
    buildField.type = type;
    data.setGeneratedByRecursive(buildField);
    return Collections.singletonList(injectFieldAndMarkGenerated(builderType, buildField));
}
Also used : QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Aggregations

FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)40 EclipseNode (lombok.eclipse.EclipseNode)21 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)17 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)17 ArrayList (java.util.ArrayList)15 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)11 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)10 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)10 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)10 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)7 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)7 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)7 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)7 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)6 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)6 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)6 ArrayTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference)4 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)4 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)4 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)4