Search in sources :

Example 16 with FieldDeclaration

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

the class HandleSetter method generateSetterForType.

public boolean generateSetterForType(EclipseNode typeNode, EclipseNode pos, AccessLevel level, boolean checkForTypeLevelSetter) {
    if (checkForTypeLevelSetter) {
        if (hasAnnotation(Setter.class, typeNode)) {
            // The annotation will make it happen, so we can skip it.
            return true;
        }
    }
    TypeDeclaration typeDecl = null;
    if (typeNode.get() instanceof TypeDeclaration)
        typeDecl = (TypeDeclaration) typeNode.get();
    int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
    boolean notAClass = (modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation | ClassFileConstants.AccEnum)) != 0;
    if (typeDecl == null || notAClass) {
        pos.addError("@Setter is only supported on a class or a field.");
        return false;
    }
    for (EclipseNode field : typeNode.down()) {
        if (field.getKind() != Kind.FIELD)
            continue;
        FieldDeclaration fieldDecl = (FieldDeclaration) field.get();
        if (!filterField(fieldDecl))
            continue;
        // Skip final fields.
        if ((fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0)
            continue;
        generateSetterForField(field, pos, level);
    }
    return true;
}
Also used : EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 17 with FieldDeclaration

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

the class HandleSetter method createSetterForField.

public void createSetterForField(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("@Setter is only supported on a class or a field.");
        return;
    }
    FieldDeclaration field = (FieldDeclaration) fieldNode.get();
    TypeReference fieldType = copyType(field.type, source);
    boolean isBoolean = isBoolean(fieldType);
    String setterName = toSetterName(fieldNode, isBoolean);
    boolean shouldReturnThis = shouldReturnThis(fieldNode);
    if (setterName == null) {
        fieldNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
        return;
    }
    int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic);
    for (String altName : toAllSetterNames(fieldNode, isBoolean)) {
        switch(methodExists(altName, fieldNode, false, 1)) {
            case EXISTS_BY_LOMBOK:
                return;
            case EXISTS_BY_USER:
                if (whineIfExists) {
                    String altNameExpl = "";
                    if (!altName.equals(setterName))
                        altNameExpl = String.format(" (%s)", altName);
                    fieldNode.addWarning(String.format("Not generating %s(): A method with that name already exists%s", setterName, altNameExpl));
                }
                return;
            default:
            case NOT_EXISTS:
        }
    }
    MethodDeclaration method = createSetter((TypeDeclaration) fieldNode.up().get(), false, fieldNode, setterName, null, shouldReturnThis, modifier, sourceNode, onMethod, onParam);
    injectMethod(fieldNode.up(), method);
}
Also used : MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 18 with FieldDeclaration

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

the class EclipseHandlerUtil method createListOfNonExistentFields.

/**
 * Given a list of field names and a node referring to a type, finds each name in the list that does not match a field within the type.
 */
public static List<Integer> createListOfNonExistentFields(List<String> list, EclipseNode type, boolean excludeStandard, boolean excludeTransient) {
    boolean[] matched = new boolean[list.size()];
    for (EclipseNode child : type.down()) {
        if (list.isEmpty())
            break;
        if (child.getKind() != Kind.FIELD)
            continue;
        if (excludeStandard) {
            if ((((FieldDeclaration) child.get()).modifiers & ClassFileConstants.AccStatic) != 0)
                continue;
            if (child.getName().startsWith("$"))
                continue;
        }
        if (excludeTransient && (((FieldDeclaration) child.get()).modifiers & ClassFileConstants.AccTransient) != 0)
            continue;
        int idx = list.indexOf(child.getName());
        if (idx > -1)
            matched[idx] = true;
    }
    List<Integer> problematic = new ArrayList<Integer>();
    for (int i = 0; i < list.size(); i++) {
        if (!matched[i])
            problematic.add(i);
    }
    return problematic;
}
Also used : ArrayList(java.util.ArrayList) EclipseNode(lombok.eclipse.EclipseNode) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 19 with FieldDeclaration

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

the class HandleBuilder method makeSimpleSetterMethodForBuilder.

private void makeSimpleSetterMethodForBuilder(EclipseNode builderType, boolean deprecate, EclipseNode fieldNode, char[] nameOfSetFlag, EclipseNode sourceNode, boolean fluent, boolean chain) {
    TypeDeclaration td = (TypeDeclaration) builderType.get();
    AbstractMethodDeclaration[] existing = td.methods;
    if (existing == null)
        existing = EMPTY;
    int len = existing.length;
    FieldDeclaration fd = (FieldDeclaration) fieldNode.get();
    char[] name = fd.name;
    for (int i = 0; i < len; i++) {
        if (!(existing[i] instanceof MethodDeclaration))
            continue;
        char[] existingName = existing[i].selector;
        if (Arrays.equals(name, existingName) && !isTolerate(fieldNode, existing[i]))
            return;
    }
    String setterName = fluent ? fieldNode.getName() : HandlerUtil.buildAccessorName("set", fieldNode.getName());
    MethodDeclaration setter = HandleSetter.createSetter(td, deprecate, fieldNode, setterName, nameOfSetFlag, chain, ClassFileConstants.AccPublic, sourceNode, Collections.<Annotation>emptyList(), Collections.<Annotation>emptyList());
    injectMethod(builderType, setter);
}
Also used : MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) 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 20 with FieldDeclaration

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

the class HandleBuilder method generateDefaultProvider.

public MethodDeclaration generateDefaultProvider(char[] methodName, TypeParameter[] typeParameters, EclipseNode fieldNode, ASTNode source) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    MethodDeclaration out = new MethodDeclaration(((CompilationUnitDeclaration) fieldNode.top().get()).compilationResult);
    out.typeParameters = copyTypeParams(typeParameters, source);
    out.selector = methodName;
    out.modifiers = ClassFileConstants.AccPrivate | ClassFileConstants.AccStatic;
    out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    FieldDeclaration fd = (FieldDeclaration) fieldNode.get();
    out.returnType = copyType(fd.type, source);
    out.statements = new Statement[] { new ReturnStatement(fd.initialization, pS, pE) };
    fd.initialization = null;
    out.traverse(new SetGeneratedByVisitor(source), ((TypeDeclaration) fieldNode.up().get()).scope);
    return out;
}
Also used : MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) 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