Search in sources :

Example 6 with Annotation

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

the class HandleSetter method createSetter.

static MethodDeclaration createSetter(TypeDeclaration parent, EclipseNode fieldNode, String name, boolean shouldReturnThis, int modifier, EclipseNode sourceNode, List<Annotation> onMethod, List<Annotation> onParam) {
    FieldDeclaration field = (FieldDeclaration) fieldNode.get();
    ASTNode source = sourceNode.get();
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
    method.modifiers = modifier;
    if (shouldReturnThis) {
        method.returnType = cloneSelfType(fieldNode, source);
    }
    if (method.returnType == null) {
        method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
        method.returnType.sourceStart = pS;
        method.returnType.sourceEnd = pE;
        shouldReturnThis = false;
    }
    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), Modifier.FINAL);
    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;
    Expression fieldRef = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
    NameReference fieldNameRef = new SingleNameReference(field.name, p);
    Assignment assignment = new Assignment(fieldRef, fieldNameRef, (int) p);
    assignment.sourceStart = pS;
    assignment.sourceEnd = assignment.statementEnd = pE;
    method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
    method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
    Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN);
    Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN);
    List<Statement> statements = new ArrayList<Statement>(5);
    if (nonNulls.length == 0) {
        statements.add(assignment);
    } else {
        Statement nullCheck = generateNullCheck(field, sourceNode);
        if (nullCheck != null)
            statements.add(nullCheck);
        statements.add(assignment);
    }
    if (shouldReturnThis) {
        ThisReference thisRef = new ThisReference(pS, pE);
        ReturnStatement returnThis = new ReturnStatement(thisRef, pS, pE);
        statements.add(returnThis);
    }
    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 : NameReference(org.eclipse.jdt.internal.compiler.ast.NameReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) 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) ArrayList(java.util.ArrayList) 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) Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement)

Example 7 with Annotation

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

the class HandleFieldDefaults method visitType.

@Override
public void visitType(EclipseNode typeNode, TypeDeclaration type) {
    AnnotationValues<FieldDefaults> fieldDefaults = null;
    EclipseNode source = typeNode;
    boolean levelIsExplicit = false;
    boolean makeFinalIsExplicit = false;
    FieldDefaults fd = null;
    for (EclipseNode jn : typeNode.down()) {
        if (jn.getKind() != Kind.ANNOTATION)
            continue;
        Annotation ann = (Annotation) jn.get();
        TypeReference typeTree = ann.type;
        if (typeTree == null)
            continue;
        if (typeTree instanceof SingleTypeReference) {
            char[] t = ((SingleTypeReference) typeTree).token;
            if (!Arrays.equals(t, FIELD_DEFAULTS))
                continue;
        } else if (typeTree instanceof QualifiedTypeReference) {
            char[][] t = ((QualifiedTypeReference) typeTree).tokens;
            if (!Eclipse.nameEquals(t, "lombok.experimental.FieldDefaults"))
                continue;
        } else {
            continue;
        }
        if (!typeMatches(FieldDefaults.class, jn, typeTree))
            continue;
        source = jn;
        fieldDefaults = createAnnotation(FieldDefaults.class, jn);
        levelIsExplicit = fieldDefaults.isExplicit("level");
        makeFinalIsExplicit = fieldDefaults.isExplicit("makeFinal");
        handleExperimentalFlagUsage(jn, ConfigurationKeys.FIELD_DEFAULTS_FLAG_USAGE, "@FieldDefaults");
        fd = fieldDefaults.getInstance();
        if (!levelIsExplicit && !makeFinalIsExplicit) {
            jn.addError("This does nothing; provide either level or makeFinal or both.");
        }
        if (levelIsExplicit && fd.level() == AccessLevel.NONE) {
            jn.addError("AccessLevel.NONE doesn't mean anything here. Pick another value.");
            levelIsExplicit = false;
        }
        break;
    }
    if (fd == null && (type.modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0)
        return;
    boolean defaultToPrivate = levelIsExplicit ? false : Boolean.TRUE.equals(typeNode.getAst().readConfiguration(ConfigurationKeys.FIELD_DEFAULTS_PRIVATE_EVERYWHERE));
    boolean defaultToFinal = makeFinalIsExplicit ? false : Boolean.TRUE.equals(typeNode.getAst().readConfiguration(ConfigurationKeys.FIELD_DEFAULTS_FINAL_EVERYWHERE));
    if (!defaultToPrivate && !defaultToFinal && fieldDefaults == null)
        return;
    AccessLevel fdAccessLevel = (fieldDefaults != null && levelIsExplicit) ? fd.level() : defaultToPrivate ? AccessLevel.PRIVATE : null;
    boolean fdToFinal = (fieldDefaults != null && makeFinalIsExplicit) ? fd.makeFinal() : defaultToFinal;
    generateFieldDefaultsForType(typeNode, source, fdAccessLevel, fdToFinal, false);
}
Also used : SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) EclipseNode(lombok.eclipse.EclipseNode) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) AccessLevel(lombok.AccessLevel) FieldDefaults(lombok.experimental.FieldDefaults)

Example 8 with Annotation

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

the class HandleGetter method findDelegatesAndMarkAsHandled.

public static Annotation[] findDelegatesAndMarkAsHandled(EclipseNode fieldNode) {
    List<Annotation> delegates = new ArrayList<Annotation>();
    for (EclipseNode child : fieldNode.down()) {
        if (annotationTypeMatches(Delegate.class, child)) {
            Annotation delegate = (Annotation) child.get();
            PatchDelegate.markHandled(delegate);
            delegates.add(delegate);
        }
    }
    return delegates.toArray(EMPTY_ANNOTATIONS_ARRAY);
}
Also used : ArrayList(java.util.ArrayList) EclipseNode(lombok.eclipse.EclipseNode) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation)

Example 9 with Annotation

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

the class HandleWither method handle.

@Override
public void handle(AnnotationValues<Wither> annotation, Annotation ast, EclipseNode annotationNode) {
    handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.WITHER_FLAG_USAGE, "@Wither");
    EclipseNode node = annotationNode.up();
    AccessLevel level = annotation.getInstance().value();
    if (level == AccessLevel.NONE || node == null)
        return;
    List<Annotation> onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@Wither(onMethod", annotationNode);
    List<Annotation> onParam = unboxAndRemoveAnnotationParameter(ast, "onParam", "@Wither(onParam", annotationNode);
    switch(node.getKind()) {
        case FIELD:
            createWitherForFields(level, annotationNode.upFromAnnotationToFields(), annotationNode, true, onMethod, onParam);
            break;
        case TYPE:
            if (!onMethod.isEmpty()) {
                annotationNode.addError("'onMethod' is not supported for @Wither on a type.");
            }
            if (!onParam.isEmpty()) {
                annotationNode.addError("'onParam' is not supported for @Wither on a type.");
            }
            generateWitherForType(node, annotationNode, level, false);
            break;
    }
}
Also used : EclipseNode(lombok.eclipse.EclipseNode) AccessLevel(lombok.AccessLevel) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation)

Example 10 with Annotation

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

Aggregations

Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)24 EclipseNode (lombok.eclipse.EclipseNode)12 ArrayList (java.util.ArrayList)11 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)8 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)6 SingleMemberAnnotation (org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation)6 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)5 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)5 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)5 List (java.util.List)4 AccessLevel (lombok.AccessLevel)4 MarkerAnnotation (org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation)4 MemberValuePair (org.eclipse.jdt.internal.compiler.ast.MemberValuePair)4 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)4 NormalAnnotation (org.eclipse.jdt.internal.compiler.ast.NormalAnnotation)4 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)4 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)4 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)3 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)3 CastExpression (org.eclipse.jdt.internal.compiler.ast.CastExpression)3