Example usage for javax.lang.model.element ElementKind ANNOTATION_TYPE

List of usage examples for javax.lang.model.element ElementKind ANNOTATION_TYPE

Introduction

In this page you can find the example usage for javax.lang.model.element ElementKind ANNOTATION_TYPE.

Prototype

ElementKind ANNOTATION_TYPE

To view the source code for javax.lang.model.element ElementKind ANNOTATION_TYPE.

Click Source Link

Document

An annotation type.

Usage

From source file:com.airbnb.deeplinkdispatch.DeepLinkProcessor.java

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Set<Element> customAnnotations = new HashSet<>();
    for (Element annotation : annotations) {
        if (annotation.getAnnotation(DEEP_LINK_SPEC_CLASS) != null) {
            customAnnotations.add(annotation);
        }// www  .j  a v a 2s . com
    }

    Map<Element, String[]> prefixes = new HashMap<>();
    Set<Element> customAnnotatedElements = new HashSet<>();
    for (Element customAnnotation : customAnnotations) {
        ElementKind kind = customAnnotation.getKind();
        if (kind != ElementKind.ANNOTATION_TYPE) {
            error(customAnnotation, "Only annotation types can be annotated with @%s",
                    DEEP_LINK_SPEC_CLASS.getSimpleName());
        }
        String[] prefix = customAnnotation.getAnnotation(DEEP_LINK_SPEC_CLASS).prefix();
        if (Utils.hasEmptyOrNullString(prefix)) {
            error(customAnnotation, "Prefix property cannot have null or empty strings");
        }
        if (prefix.length == 0) {
            error(customAnnotation, "Prefix property cannot be empty");
        }
        prefixes.put(customAnnotation, prefix);
        for (Element customAnnotatedElement : roundEnv
                .getElementsAnnotatedWith(MoreElements.asType(customAnnotation))) {
            customAnnotatedElements.add(customAnnotatedElement);
        }
    }

    Set<Element> elementsToProcess = new HashSet<>(customAnnotatedElements);
    elementsToProcess.addAll(roundEnv.getElementsAnnotatedWith(DEEP_LINK_CLASS));

    List<DeepLinkAnnotatedElement> deepLinkElements = new ArrayList<>();
    for (Element element : elementsToProcess) {
        ElementKind kind = element.getKind();
        if (kind != ElementKind.METHOD && kind != ElementKind.CLASS) {
            error(element, "Only classes and methods can be annotated with @%s",
                    DEEP_LINK_CLASS.getSimpleName());
        }

        if (kind == ElementKind.METHOD) {
            Set<Modifier> methodModifiers = element.getModifiers();
            if (!methodModifiers.contains(Modifier.STATIC)) {
                error(element, "Only static methods can be annotated with @%s",
                        DEEP_LINK_CLASS.getSimpleName());
            }
        }

        DeepLink deepLinkAnnotation = element.getAnnotation(DEEP_LINK_CLASS);
        List<String> deepLinks = new ArrayList<>();
        if (deepLinkAnnotation != null) {
            deepLinks.addAll(Arrays.asList(deepLinkAnnotation.value()));
        }
        if (customAnnotatedElements.contains(element)) {
            deepLinks.addAll(enumerateCustomDeepLinks(element, prefixes));
        }
        DeepLinkEntry.Type type = kind == ElementKind.CLASS ? DeepLinkEntry.Type.CLASS
                : DeepLinkEntry.Type.METHOD;
        for (String deepLink : deepLinks) {
            try {
                deepLinkElements.add(new DeepLinkAnnotatedElement(deepLink, element, type));
            } catch (MalformedURLException e) {
                messager.printMessage(Diagnostic.Kind.ERROR, "Malformed Deep Link URL " + deepLink);
            }
        }
    }
    Set<? extends Element> deepLinkHandlerElements = roundEnv.getElementsAnnotatedWith(DeepLinkHandler.class);
    for (Element deepLinkHandlerElement : deepLinkHandlerElements) {
        Optional<AnnotationMirror> annotationMirror = getAnnotationMirror(deepLinkHandlerElement,
                DeepLinkHandler.class);
        if (annotationMirror.isPresent()) {
            Iterable<TypeMirror> klasses = getTypeValue(annotationMirror.get(), "value");
            List<TypeElement> typeElements = FluentIterable.from(klasses)
                    .transform(new Function<TypeMirror, TypeElement>() {
                        @Override
                        public TypeElement apply(TypeMirror klass) {
                            return MoreTypes.asTypeElement(klass);
                        }
                    }).toList();
            String packageName = processingEnv.getElementUtils().getPackageOf(deepLinkHandlerElement)
                    .getQualifiedName().toString();
            try {
                generateDeepLinkDelegate(packageName, typeElements);
            } catch (IOException e) {
                messager.printMessage(Diagnostic.Kind.ERROR, "Error creating file");
            } catch (RuntimeException e) {
                messager.printMessage(Diagnostic.Kind.ERROR,
                        "Internal error during annotation processing: " + e.getClass().getSimpleName());
            }
        }
    }

    Set<? extends Element> deepLinkModuleElements = roundEnv.getElementsAnnotatedWith(DeepLinkModule.class);
    for (Element deepLinkModuleElement : deepLinkModuleElements) {
        String packageName = processingEnv.getElementUtils().getPackageOf(deepLinkModuleElement)
                .getQualifiedName().toString();
        try {
            generateDeepLinkLoader(packageName, deepLinkModuleElement.getSimpleName().toString(),
                    deepLinkElements);
        } catch (IOException e) {
            messager.printMessage(Diagnostic.Kind.ERROR, "Error creating file");
        } catch (RuntimeException e) {
            messager.printMessage(Diagnostic.Kind.ERROR,
                    "Internal error during annotation processing: " + e.getClass().getSimpleName());
        }
    }

    return false;
}

From source file:org.immutables.value.processor.meta.ValueType.java

public boolean isAnnotationType() {
    return element.getKind() == ElementKind.ANNOTATION_TYPE;
}

From source file:org.immutables.value.processor.meta.ValueType.java

public String getInheritsKeyword() {
    return element.getKind() == ElementKind.INTERFACE || element.getKind() == ElementKind.ANNOTATION_TYPE
            ? "implements"
            : "extends";
}

From source file:org.jsweet.transpiler.typescript.Java2TypeScriptAdapter.java

@Override
public String needsImport(JCImport importDecl, String qualifiedName) {
    if (isJSweetPath(qualifiedName) || typesMapping.containsKey(qualifiedName)
            || langTypesMapping.containsKey(qualifiedName) || qualifiedName.startsWith("java.util.function.")
            || qualifiedName.endsWith(GLOBALS_PACKAGE_NAME + "." + GLOBALS_CLASS_NAME)) {
        return null;
    }/*from w w  w .ja v a 2s.  co m*/
    if (importDecl.qualid.type != null) {
        if (context.hasAnnotationType(importDecl.qualid.type.tsym, ANNOTATION_ERASED, ANNOTATION_OBJECT_TYPE)) {
            return null;
        }
        if (importDecl.qualid.type.tsym.getKind() == ElementKind.ANNOTATION_TYPE
                && !context.hasAnnotationType(importDecl.qualid.type.tsym, JSweetConfig.ANNOTATION_DECORATOR)) {
            return null;
        }
    }
    if (importDecl.isStatic()) {
        if (importDecl.getQualifiedIdentifier() instanceof JCFieldAccess) {
            JCFieldAccess fa = (JCFieldAccess) importDecl.getQualifiedIdentifier();
            switch (fa.selected.toString()) {
            case "java.lang.Math":
                return null;
            }
            String name = getRootRelativeName(fa.selected.type.tsym, context.useModules);
            String methodName = fa.name.toString();

            // function is a top-level global function (no need to import)
            if (GLOBALS_CLASS_NAME.equals(name)) {
                return null;
            }
            if (!context.useModules && name.endsWith(GLOBALS_PACKAGE_NAME + "." + GLOBALS_CLASS_NAME)) {
                return null;
            }

            if (JSweetConfig.TS_STRICT_MODE_KEYWORDS.contains(methodName.toLowerCase())) {
                // if method name is a reserved ts keyword, we have to fully
                // qualify calls to it (hence discarding any import)
                return null;
            }
            boolean globals = name.endsWith("." + JSweetConfig.GLOBALS_CLASS_NAME);
            if (globals) {
                name = name.substring(0, name.length() - JSweetConfig.GLOBALS_CLASS_NAME.length() - 1);
            }
            // function belong to the current package (no need to
            // import)
            String current = getRootRelativeName(getCompilationUnit().packge, context.useModules);
            if (context.useModules) {
                if (current.equals(name)) {
                    return null;
                }
            } else {
                if (current.startsWith(name)) {
                    return null;
                }
            }
            Symbol nameSymbol = fa.sym;
            if (nameSymbol == null) {
                TypeSymbol t = fa.selected.type.tsym;
                nameSymbol = Util.findFirstDeclarationInType(t, methodName);
            }

            return StringUtils.isBlank(name) ? null
                    : name + "." + (nameSymbol == null ? methodName : getIdentifier(nameSymbol));
        } else {
            return null;
        }
    } else {
        if (context.useModules) {
            // check if inner class and do not import
            if (importDecl.qualid instanceof JCFieldAccess) {
                JCFieldAccess qualified = (JCFieldAccess) importDecl.qualid;
                if (qualified.sym instanceof ClassSymbol
                        && qualified.sym.getEnclosingElement() instanceof ClassSymbol) {
                    return null;
                }
            }
        }
    }
    return super.needsImport(importDecl, qualifiedName);
}

From source file:org.jsweet.transpiler.extension.Java2TypeScriptAdapter.java

@Override
public String needsImport(ImportElement importElement, String qualifiedName) {
    JCImport importDecl = ((ImportElementSupport) importElement).getTree();
    if (isJSweetPath(qualifiedName) || isMappedType(qualifiedName)
            || context.getLangTypeMappings().containsKey(qualifiedName)
            || qualifiedName.startsWith("java.util.function.")
            || qualifiedName.endsWith(GLOBALS_PACKAGE_NAME + "." + GLOBALS_CLASS_NAME)) {
        return null;
    }//from  w w  w  .  j  av a  2s .  co  m
    if (importDecl.qualid.type != null) {
        if (context.hasAnnotationType(importDecl.qualid.type.tsym, ANNOTATION_ERASED, ANNOTATION_OBJECT_TYPE)) {
            return null;
        }
        if (importDecl.qualid.type.tsym.getKind() == ElementKind.ANNOTATION_TYPE
                && !context.hasAnnotationType(importDecl.qualid.type.tsym, JSweetConfig.ANNOTATION_DECORATOR)) {
            return null;
        }
    }
    if (importDecl.isStatic()) {
        if (importDecl.getQualifiedIdentifier() instanceof JCFieldAccess) {
            JCFieldAccess fa = (JCFieldAccess) importDecl.getQualifiedIdentifier();
            switch (fa.selected.toString()) {
            case "java.lang.Math":
                return null;
            }
            String name = getRootRelativeName(fa.selected.type.tsym, context.useModules);
            String methodName = fa.name.toString();

            // function is a top-level global function (no need to import)
            if (GLOBALS_CLASS_NAME.equals(name)) {
                return null;
            }
            if (!context.useModules && name.endsWith(GLOBALS_PACKAGE_NAME + "." + GLOBALS_CLASS_NAME)) {
                return null;
            }

            if (JSweetConfig.TS_STRICT_MODE_KEYWORDS.contains(methodName.toLowerCase())) {
                // if method name is a reserved ts keyword, we have to fully
                // qualify calls to it (hence discarding any import)
                return null;
            }
            boolean globals = name.endsWith("." + JSweetConfig.GLOBALS_CLASS_NAME);
            if (globals) {
                name = name.substring(0, name.length() - JSweetConfig.GLOBALS_CLASS_NAME.length() - 1);
            }
            // function belong to the current package (no need to
            // import)
            String current = getRootRelativeName(getCompilationUnit().packge, context.useModules);
            if (context.useModules) {
                if (current.equals(name)) {
                    return null;
                }
            } else {
                if (current.startsWith(name)) {
                    return null;
                }
            }
            Symbol nameSymbol = fa.sym;
            if (nameSymbol == null) {
                TypeSymbol t = fa.selected.type.tsym;
                nameSymbol = Util.findFirstDeclarationInType(t, methodName);
            }

            return StringUtils.isBlank(name) ? null
                    : name + "." + (nameSymbol == null ? methodName : getIdentifier(nameSymbol));
        } else {
            return null;
        }
    } else {
        if (context.useModules) {
            // check if inner class and do not import
            if (importDecl.qualid instanceof JCFieldAccess) {
                JCFieldAccess qualified = (JCFieldAccess) importDecl.qualid;
                if (qualified.sym instanceof ClassSymbol
                        && qualified.sym.getEnclosingElement() instanceof ClassSymbol) {
                    return null;
                }
            }
        }
    }
    return super.needsImport(importElement, qualifiedName);
}

From source file:org.jsweet.transpiler.JSweetContext.java

/**
 * Returns true if this class declaration is not to be output by the
 * transpiler.//  w w  w. j  av  a  2 s .co  m
 */
public boolean isIgnored(JCClassDecl classdecl) {
    if (hasAnnotationType(classdecl.type.tsym, JSweetConfig.ANNOTATION_OBJECT_TYPE)) {
        // object types are ignored
        return true;
    }
    if (hasAnnotationType(classdecl.type.tsym, JSweetConfig.ANNOTATION_ERASED)) {
        // erased types are ignored
        return true;
    }
    if (classdecl.type.tsym.getKind() == ElementKind.ANNOTATION_TYPE) {
        // annotation types are ignored
        return true;
    }
    return false;
}