Search in sources :

Example 1 with Getter

use of lombok.Getter in project lombok by rzwitserloot.

the class JavacHandlerUtil method findGetter.

private static GetterMethod findGetter(JavacNode field) {
    JCVariableDecl decl = (JCVariableDecl) field.get();
    JavacNode typeNode = field.up();
    for (String potentialGetterName : toAllGetterNames(field)) {
        for (JavacNode potentialGetter : typeNode.down()) {
            if (potentialGetter.getKind() != Kind.METHOD)
                continue;
            JCMethodDecl method = (JCMethodDecl) potentialGetter.get();
            if (!method.name.toString().equalsIgnoreCase(potentialGetterName))
                continue;
            /**
             * static getX() methods don't count.
             */
            if ((method.mods.flags & Flags.STATIC) != 0)
                continue;
            /**
             * Nor do getters with a non-empty parameter list.
             */
            if (method.params != null && method.params.size() > 0)
                continue;
            return new GetterMethod(method.name, method.restype);
        }
    }
    // Check if the field has a @Getter annotation.
    boolean hasGetterAnnotation = false;
    for (JavacNode child : field.down()) {
        if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
            AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
            // Definitely WONT have a getter.
            if (ann.getInstance().value() == AccessLevel.NONE)
                return null;
            hasGetterAnnotation = true;
        }
    }
    if (!hasGetterAnnotation && HandleGetter.fieldQualifiesForGetterGeneration(field)) {
        // Check if the class has @Getter or @Data annotation.
        JavacNode containingType = field.up();
        if (containingType != null)
            for (JavacNode child : containingType.down()) {
                if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Data.class, child))
                    hasGetterAnnotation = true;
                if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
                    AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
                    // Definitely WONT have a getter.
                    if (ann.getInstance().value() == AccessLevel.NONE)
                        return null;
                    hasGetterAnnotation = true;
                }
            }
    }
    if (hasGetterAnnotation) {
        String getterName = toGetterName(field);
        if (getterName == null)
            return null;
        return new GetterMethod(field.toName(getterName), decl.vartype);
    }
    return null;
}
Also used : JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacNode(lombok.javac.JavacNode) Getter(lombok.Getter) AnnotationValues(lombok.core.AnnotationValues) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 2 with Getter

use of lombok.Getter in project testcontainers-java by testcontainers.

the class DockerfileTrait method withDockerfileFromBuilder.

default SELF withDockerfileFromBuilder(Consumer<DockerfileBuilder> builderConsumer) {
    DockerfileBuilder builder = new DockerfileBuilder();
    builderConsumer.accept(builder);
    // return Transferable because we want to build Dockerfile's content lazily
    return ((SELF) this).withFileFromTransferable("Dockerfile", new Transferable() {

        @Getter(lazy = true)
        private final byte[] bytes = builder.build().getBytes();

        @Override
        public long getSize() {
            return getBytes().length;
        }

        @Override
        public String getDescription() {
            return "Dockerfile: " + builder;
        }
    });
}
Also used : DockerfileBuilder(org.testcontainers.images.builder.dockerfile.DockerfileBuilder) Getter(lombok.Getter) Transferable(org.testcontainers.images.builder.Transferable)

Example 3 with Getter

use of lombok.Getter in project lombok by rzwitserloot.

the class HandleGetter method handle.

public void handle(AnnotationValues<Getter> annotation, Annotation ast, EclipseNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.GETTER_FLAG_USAGE, "@Getter");
    EclipseNode node = annotationNode.up();
    Getter annotationInstance = annotation.getInstance();
    AccessLevel level = annotationInstance.value();
    boolean lazy = annotationInstance.lazy();
    if (lazy)
        handleFlagUsage(annotationNode, ConfigurationKeys.GETTER_LAZY_FLAG_USAGE, "@Getter(lazy=true)");
    if (level == AccessLevel.NONE) {
        if (lazy)
            annotationNode.addWarning("'lazy' does not work with AccessLevel.NONE.");
        return;
    }
    if (node == null)
        return;
    List<Annotation> onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@Getter(onMethod", annotationNode);
    switch(node.getKind()) {
        case FIELD:
            createGetterForFields(level, annotationNode.upFromAnnotationToFields(), annotationNode, annotationNode.get(), true, lazy, onMethod);
            break;
        case TYPE:
            if (!onMethod.isEmpty()) {
                annotationNode.addError("'onMethod' is not supported for @Getter on a type.");
            }
            if (lazy)
                annotationNode.addError("'lazy' is not supported for @Getter on a type.");
            generateGetterForType(node, annotationNode, level, false);
            break;
    }
}
Also used : Getter(lombok.Getter) EclipseNode(lombok.eclipse.EclipseNode) AccessLevel(lombok.AccessLevel) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation)

Example 4 with Getter

use of lombok.Getter in project lombok by rzwitserloot.

the class HandleGetter method handle.

@Override
public void handle(AnnotationValues<Getter> annotation, JCAnnotation ast, JavacNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.GETTER_FLAG_USAGE, "@Getter");
    Collection<JavacNode> fields = annotationNode.upFromAnnotationToFields();
    deleteAnnotationIfNeccessary(annotationNode, Getter.class);
    deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
    JavacNode node = annotationNode.up();
    Getter annotationInstance = annotation.getInstance();
    AccessLevel level = annotationInstance.value();
    boolean lazy = annotationInstance.lazy();
    if (lazy)
        handleFlagUsage(annotationNode, ConfigurationKeys.GETTER_LAZY_FLAG_USAGE, "@Getter(lazy=true)");
    if (level == AccessLevel.NONE) {
        if (lazy)
            annotationNode.addWarning("'lazy' does not work with AccessLevel.NONE.");
        return;
    }
    if (node == null)
        return;
    List<JCAnnotation> onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@Getter(onMethod", annotationNode);
    switch(node.getKind()) {
        case FIELD:
            createGetterForFields(level, fields, annotationNode, true, lazy, onMethod);
            break;
        case TYPE:
            if (!onMethod.isEmpty()) {
                annotationNode.addError("'onMethod' is not supported for @Getter on a type.");
            }
            if (lazy)
                annotationNode.addError("'lazy' is not supported for @Getter on a type.");
            generateGetterForType(node, annotationNode, level, false);
            break;
    }
}
Also used : JavacNode(lombok.javac.JavacNode) Getter(lombok.Getter) AccessLevel(lombok.AccessLevel) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 5 with Getter

use of lombok.Getter in project lombok by rzwitserloot.

the class EclipseHandlerUtil method findGetter.

private static GetterMethod findGetter(EclipseNode field) {
    FieldDeclaration fieldDeclaration = (FieldDeclaration) field.get();
    boolean forceBool = FieldDeclaration_booleanLazyGetter.get(fieldDeclaration);
    TypeReference fieldType = fieldDeclaration.type;
    boolean isBoolean = forceBool || isBoolean(fieldType);
    EclipseNode typeNode = field.up();
    for (String potentialGetterName : toAllGetterNames(field, isBoolean)) {
        for (EclipseNode potentialGetter : typeNode.down()) {
            if (potentialGetter.getKind() != Kind.METHOD)
                continue;
            if (!(potentialGetter.get() instanceof MethodDeclaration))
                continue;
            MethodDeclaration method = (MethodDeclaration) potentialGetter.get();
            if (!potentialGetterName.equalsIgnoreCase(new String(method.selector)))
                continue;
            /**
             * static getX() methods don't count.
             */
            if ((method.modifiers & ClassFileConstants.AccStatic) != 0)
                continue;
            /**
             * Nor do getters with a non-empty parameter list.
             */
            if (method.arguments != null && method.arguments.length > 0)
                continue;
            return new GetterMethod(method.selector, method.returnType);
        }
    }
    // Check if the field has a @Getter annotation.
    boolean hasGetterAnnotation = false;
    for (EclipseNode child : field.down()) {
        if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
            AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
            // Definitely WONT have a getter.
            if (ann.getInstance().value() == AccessLevel.NONE)
                return null;
            hasGetterAnnotation = true;
        }
    }
    if (!hasGetterAnnotation && HandleGetter.fieldQualifiesForGetterGeneration(field)) {
        // Check if the class has @Getter or @Data annotation.
        EclipseNode containingType = field.up();
        if (containingType != null)
            for (EclipseNode child : containingType.down()) {
                if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Data.class, child))
                    hasGetterAnnotation = true;
                if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
                    AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
                    // Definitely WONT have a getter.
                    if (ann.getInstance().value() == AccessLevel.NONE)
                        return null;
                    hasGetterAnnotation = true;
                }
            }
    }
    if (hasGetterAnnotation) {
        String getterName = toGetterName(field, isBoolean);
        if (getterName == null)
            return null;
        return new GetterMethod(getterName.toCharArray(), fieldType);
    }
    return null;
}
Also used : MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) Getter(lombok.Getter) AnnotationValues(lombok.core.AnnotationValues) EclipseNode(lombok.eclipse.EclipseNode) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Aggregations

Getter (lombok.Getter)5 AccessLevel (lombok.AccessLevel)2 AnnotationValues (lombok.core.AnnotationValues)2 EclipseNode (lombok.eclipse.EclipseNode)2 JavacNode (lombok.javac.JavacNode)2 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)1 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)1 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 ArrayQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference)1 ArrayTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference)1 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)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 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)1 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)1 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)1 Transferable (org.testcontainers.images.builder.Transferable)1