Search in sources :

Example 1 with AnnotationValueDecodeFail

use of lombok.core.AnnotationValues.AnnotationValueDecodeFail in project lombok by rzwitserloot.

the class PatchExtensionMethod method getApplicableExtensionMethods.

static List<Extension> getApplicableExtensionMethods(EclipseNode typeNode, Annotation ann, TypeBinding receiverType) {
    List<Extension> extensions = new ArrayList<Extension>();
    if ((typeNode != null) && (ann != null) && (receiverType != null)) {
        BlockScope blockScope = ((TypeDeclaration) typeNode.get()).initializerScope;
        EclipseNode annotationNode = typeNode.getNodeFor(ann);
        AnnotationValues<ExtensionMethod> annotation = createAnnotation(ExtensionMethod.class, annotationNode);
        boolean suppressBaseMethods = false;
        try {
            suppressBaseMethods = annotation.getInstance().suppressBaseMethods();
        } catch (AnnotationValueDecodeFail fail) {
            fail.owner.setError(fail.getMessage(), fail.idx);
        }
        for (Object extensionMethodProvider : annotation.getActualExpressions("value")) {
            if (extensionMethodProvider instanceof ClassLiteralAccess) {
                TypeBinding binding = ((ClassLiteralAccess) extensionMethodProvider).type.resolveType(blockScope);
                if (binding == null)
                    continue;
                if (!binding.isClass() && !binding.isEnum())
                    continue;
                Extension e = new Extension();
                e.extensionMethods = getApplicableExtensionMethodsDefinedInProvider(typeNode, (ReferenceBinding) binding, receiverType);
                e.suppressBaseMethods = suppressBaseMethods;
                extensions.add(e);
            }
        }
    }
    return extensions;
}
Also used : AnnotationValueDecodeFail(lombok.core.AnnotationValues.AnnotationValueDecodeFail) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) ArrayList(java.util.ArrayList) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) BlockScope(org.eclipse.jdt.internal.compiler.lookup.BlockScope) EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) ExtensionMethod(lombok.experimental.ExtensionMethod) ClassLiteralAccess(org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess)

Example 2 with AnnotationValueDecodeFail

use of lombok.core.AnnotationValues.AnnotationValueDecodeFail in project lombok by rzwitserloot.

the class HandlerLibrary method handleAnnotation.

/**
 * Handles the provided annotation node by first finding a qualifying instance of
 * {@link JavacAnnotationHandler} and if one exists, calling it with a freshly cooked up
 * instance of {@link lombok.core.AnnotationValues}.
 *
 * Note that depending on the printASTOnly flag, the {@link lombok.core.PrintAST} annotation
 * will either be silently skipped, or everything that isn't {@code PrintAST} will be skipped.
 *
 * The HandlerLibrary will attempt to guess if the given annotation node represents a lombok annotation.
 * For example, if {@code lombok.*} is in the import list, then this method will guess that
 * {@code Getter} refers to {@code lombok.Getter}, presuming that {@link lombok.javac.handlers.HandleGetter}
 * has been loaded.
 *
 * @param unit The Compilation Unit that contains the Annotation AST Node.
 * @param node The Lombok AST Node representing the Annotation AST Node.
 * @param annotation 'node.get()' - convenience parameter.
 */
public void handleAnnotation(JCCompilationUnit unit, JavacNode node, JCAnnotation annotation, long priority) {
    TypeResolver resolver = new TypeResolver(node.getImportList());
    String rawType = annotation.annotationType.toString();
    String fqn = resolver.typeRefToFullyQualifiedName(node, typeLibrary, rawType);
    if (fqn == null)
        return;
    AnnotationHandlerContainer<?> container = annotationHandlers.get(fqn);
    if (container == null)
        return;
    try {
        if (container.getPriority() == priority) {
            if (checkAndSetHandled(annotation))
                container.handle(node);
        }
    } catch (AnnotationValueDecodeFail fail) {
        fail.owner.setError(fail.getMessage(), fail.idx);
    } catch (Throwable t) {
        String sourceName = "(unknown).java";
        if (unit != null && unit.sourcefile != null)
            sourceName = unit.sourcefile.getName();
        javacError(String.format("Lombok annotation handler %s failed on " + sourceName, container.handler.getClass()), t);
    }
}
Also used : AnnotationValueDecodeFail(lombok.core.AnnotationValues.AnnotationValueDecodeFail) TypeResolver(lombok.core.TypeResolver)

Example 3 with AnnotationValueDecodeFail

use of lombok.core.AnnotationValues.AnnotationValueDecodeFail in project lombok by rzwitserloot.

the class HandlerLibrary method handleAnnotation.

/**
 * Handles the provided annotation node by first finding a qualifying instance of
 * {@link EclipseAnnotationHandler} and if one exists, calling it with a freshly cooked up
 * instance of {@link AnnotationValues}.
 *
 * Note that depending on the printASTOnly flag, the {@link lombok.core.PrintAST} annotation
 * will either be silently skipped, or everything that isn't {@code PrintAST} will be skipped.
 *
 * The HandlerLibrary will attempt to guess if the given annotation node represents a lombok annotation.
 * For example, if {@code lombok.*} is in the import list, then this method will guess that
 * {@code Getter} refers to {@code lombok.Getter}, presuming that {@link lombok.eclipse.handlers.HandleGetter}
 * has been loaded.
 *
 * @param ast The Compilation Unit that contains the Annotation AST Node.
 * @param annotationNode The Lombok AST Node representing the Annotation AST Node.
 * @param annotation 'node.get()' - convenience parameter.
 */
public void handleAnnotation(CompilationUnitDeclaration ast, EclipseNode annotationNode, org.eclipse.jdt.internal.compiler.ast.Annotation annotation, long priority) {
    TypeResolver resolver = new TypeResolver(annotationNode.getImportList());
    TypeReference rawType = annotation.type;
    if (rawType == null)
        return;
    String fqn = resolver.typeRefToFullyQualifiedName(annotationNode, typeLibrary, toQualifiedName(annotation.type.getTypeName()));
    if (fqn == null)
        return;
    AnnotationHandlerContainer<?> container = annotationHandlers.get(fqn);
    if (container == null)
        return;
    if (priority != container.getPriority())
        return;
    if (!annotationNode.isCompleteParse() && container.deferUntilPostDiet()) {
        if (needsHandling(annotation))
            container.preHandle(annotation, annotationNode);
        return;
    }
    try {
        if (checkAndSetHandled(annotation))
            container.handle(annotation, annotationNode);
    } catch (AnnotationValueDecodeFail fail) {
        fail.owner.setError(fail.getMessage(), fail.idx);
    } catch (Throwable t) {
        error(ast, String.format("Lombok annotation handler %s failed", container.handler.getClass()), t);
    }
}
Also used : AnnotationValueDecodeFail(lombok.core.AnnotationValues.AnnotationValueDecodeFail) TypeResolver(lombok.core.TypeResolver) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference)

Aggregations

AnnotationValueDecodeFail (lombok.core.AnnotationValues.AnnotationValueDecodeFail)3 TypeResolver (lombok.core.TypeResolver)2 ArrayList (java.util.ArrayList)1 EclipseNode (lombok.eclipse.EclipseNode)1 ExtensionMethod (lombok.experimental.ExtensionMethod)1 ClassLiteralAccess (org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess)1 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)1 BlockScope (org.eclipse.jdt.internal.compiler.lookup.BlockScope)1 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)1 TypeBinding (org.eclipse.jdt.internal.compiler.lookup.TypeBinding)1