Search in sources :

Example 1 with AnnotationModel

use of org.glassfish.hk2.classmodel.reflect.AnnotationModel in project Payara by payara.

the class WeldUtils method getCDIEnablingAnnotations.

/**
 * Get the names of any annotation types that are applied to beans, which should enable CDI
 * processing even in the absence of a beans.xml descriptor.
 *
 * @param context The DeploymentContext
 *
 * @return An array of annotation type names; The array could be empty if none are found.
 */
public static String[] getCDIEnablingAnnotations(DeploymentContext context) {
    List<String> result = new ArrayList<String>();
    Types types = getTypes(context);
    if (types != null) {
        Iterator<Type> typesIter = types.getAllTypes().iterator();
        while (typesIter.hasNext()) {
            Type type = typesIter.next();
            if (!(type instanceof AnnotationType)) {
                Iterator<AnnotationModel> annotations = type.getAnnotations().iterator();
                while (annotations.hasNext()) {
                    AnnotationModel am = annotations.next();
                    AnnotationType at = am.getType();
                    if (isCDIEnablingAnnotation(at)) {
                        if (!result.contains(at.getName())) {
                            result.add(at.getName());
                        }
                    }
                }
            }
        }
    }
    return result.toArray(new String[result.size()]);
}
Also used : Types(org.glassfish.hk2.classmodel.reflect.Types) AnnotationType(org.glassfish.hk2.classmodel.reflect.AnnotationType) Type(org.glassfish.hk2.classmodel.reflect.Type) ArrayList(java.util.ArrayList) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) AnnotationType(org.glassfish.hk2.classmodel.reflect.AnnotationType)

Example 2 with AnnotationModel

use of org.glassfish.hk2.classmodel.reflect.AnnotationModel in project Payara by payara.

the class GlassFishInjectionProvider method getAnnotatedClassesInCurrentModule.

@Override
public Map<String, List<ScannedAnnotation>> getAnnotatedClassesInCurrentModule(ServletContext servletContext) throws InjectionProviderException {
    Map<String, List<ScannedAnnotation>> classesByAnnotation = new HashMap<String, List<ScannedAnnotation>>();
    Collection<Type> allTypes = ((DeploymentContext) servletContext.getAttribute(DEPLOYMENT_CONTEXT_ATTRIBUTE)).getTransientAppMetaData(Types.class.getName(), Types.class).getAllTypes();
    for (Type type : allTypes) {
        for (AnnotationModel annotationModel : type.getAnnotations()) {
            String annotationName = annotationModel.getType().getName();
            List<ScannedAnnotation> classesWithThisAnnotation = classesByAnnotation.get(annotationName);
            if (classesWithThisAnnotation == null) {
                classesWithThisAnnotation = new ArrayList<ScannedAnnotation>();
                classesByAnnotation.put(annotationName, classesWithThisAnnotation);
            }
            ScannedAnnotation toAdd = new ScannedAnnotation() {

                @Override
                public boolean equals(Object obj) {
                    boolean result = false;
                    if (obj instanceof ScannedAnnotation) {
                        String otherName = ((ScannedAnnotation) obj).getFullyQualifiedClassName();
                        if (otherName != null) {
                            result = type.getName().equals(otherName);
                        }
                    }
                    return result;
                }

                @Override
                public int hashCode() {
                    String str = getFullyQualifiedClassName();
                    Collection<URI> obj = getDefiningURIs();
                    int result = str != null ? str.hashCode() : 0;
                    result = 31 * result + (obj != null ? obj.hashCode() : 0);
                    return result;
                }

                @Override
                public String getFullyQualifiedClassName() {
                    return type.getName();
                }

                @Override
                public Collection<URI> getDefiningURIs() {
                    return type.getDefiningURIs();
                }
            };
            if (!classesWithThisAnnotation.contains(toAdd)) {
                classesWithThisAnnotation.add(toAdd);
            }
        }
    }
    return classesByAnnotation;
}
Also used : Types(org.glassfish.hk2.classmodel.reflect.Types) HashMap(java.util.HashMap) URI(java.net.URI) Type(org.glassfish.hk2.classmodel.reflect.Type) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 3 with AnnotationModel

use of org.glassfish.hk2.classmodel.reflect.AnnotationModel in project Payara by payara.

the class WeldUtils method hasCDIEnablingAnnotations.

/**
 * Determine whether there are any beans annotated with annotations that should enable CDI
 * processing even in the absence of a beans.xml descriptor.
 *
 * @param context The DeploymentContext
 * @param paths   The paths to check for annotated beans
 *
 * @return true, if there is at least one bean annotated with a qualified annotation in the specified paths
 */
public static boolean hasCDIEnablingAnnotations(DeploymentContext context, Collection<URI> paths) {
    List<String> result = new ArrayList<String>();
    Types types = getTypes(context);
    if (types != null) {
        Iterator<Type> typesIter = types.getAllTypes().iterator();
        while (typesIter.hasNext()) {
            Type type = typesIter.next();
            if (!(type instanceof AnnotationType)) {
                Iterator<AnnotationModel> annotations = type.getAnnotations().iterator();
                while (annotations.hasNext()) {
                    AnnotationModel am = annotations.next();
                    AnnotationType at = am.getType();
                    if (isCDIEnablingAnnotation(at) && type.wasDefinedIn(paths)) {
                        if (!result.contains(at.getName())) {
                            result.add(at.getName());
                        }
                    }
                }
            }
        }
    }
    return !(result.isEmpty());
}
Also used : Types(org.glassfish.hk2.classmodel.reflect.Types) AnnotationType(org.glassfish.hk2.classmodel.reflect.AnnotationType) Type(org.glassfish.hk2.classmodel.reflect.Type) ArrayList(java.util.ArrayList) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) AnnotationType(org.glassfish.hk2.classmodel.reflect.AnnotationType)

Example 4 with AnnotationModel

use of org.glassfish.hk2.classmodel.reflect.AnnotationModel in project Payara by payara.

the class WeldUtils method getCDIAnnotatedClassNames.

/**
 * Get the names of any classes that are annotated with bean-defining annotations, which should
 * enable CDI processing even in the absence of a beans.xml descriptor.
 *
 * @param context The DeploymentContext
 *
 * @return A collection of class names; The collection could be empty if none are found.
 */
public static Collection<String> getCDIAnnotatedClassNames(DeploymentContext context) {
    Set<String> result = new HashSet<String>();
    Types types = getTypes(context);
    if (types != null) {
        Iterator<Type> typesIter = types.getAllTypes().iterator();
        while (typesIter.hasNext()) {
            Type type = typesIter.next();
            if (!(type instanceof AnnotationType)) {
                Iterator<AnnotationModel> annotations = type.getAnnotations().iterator();
                while (annotations.hasNext()) {
                    AnnotationModel am = annotations.next();
                    AnnotationType at = am.getType();
                    if (isCDIEnablingAnnotation(at)) {
                        if (!result.contains(at.getName())) {
                            result.add(type.getName());
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : Types(org.glassfish.hk2.classmodel.reflect.Types) AnnotationType(org.glassfish.hk2.classmodel.reflect.AnnotationType) Type(org.glassfish.hk2.classmodel.reflect.Type) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) AnnotationType(org.glassfish.hk2.classmodel.reflect.AnnotationType) HashSet(java.util.HashSet)

Aggregations

AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)4 Type (org.glassfish.hk2.classmodel.reflect.Type)4 Types (org.glassfish.hk2.classmodel.reflect.Types)4 ArrayList (java.util.ArrayList)3 AnnotationType (org.glassfish.hk2.classmodel.reflect.AnnotationType)3 URI (java.net.URI)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1