Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jboss.weld.environment.util;
  
  import java.util.Arrays;
  
 
 public final class Reflections {
 
     private Reflections() {
     }
 
     public static boolean containsAnnotation(Class<?> javaClassClass<? extends AnnotationrequiredAnnotation) {
         for (Class<?> clazz = javaClassclazz != null && clazz != Object.classclazz = clazz.getSuperclass()) {
             // class level annotations
             if (clazz == javaClass || requiredAnnotation.isAnnotationPresent(Inherited.class)) {
                 if (containsAnnotations(clazz.getAnnotations(), requiredAnnotation)) {
                     return true;
                 }
             }
             // fields
             for (Field field : clazz.getDeclaredFields()) {
                 if (containsAnnotations(field.getAnnotations(), requiredAnnotation)) {
                     return true;
                 }
             }
             // constructors
             for (Constructor<?> constructor : clazz.getConstructors()) {
                 if (containsAnnotations(constructor.getAnnotations(), requiredAnnotation)) {
                     return true;
                 }
                 for (Annotation[] parameterAnnotations : constructor.getParameterAnnotations()) {
                     if (containsAnnotations(parameterAnnotationsrequiredAnnotation)) {
                         return true;
                     }
                 }
             }
             // methods
             for (Method method : clazz.getDeclaredMethods()) {
                 if (containsAnnotations(method.getAnnotations(), requiredAnnotation)) {
                     return true;
                 }
                 for (Annotation[] parameterAnnotations : method.getParameterAnnotations()) {
                     if (containsAnnotations(parameterAnnotationsrequiredAnnotation)) {
                         return true;
                     }
                 }
             }
         }
         return false;
     }
 
     public static <T> T newInstance(ResourceLoader loaderString classNameObject... parameters) {
         final Class<?>[] parameterTypes = new Class<?>[parameters.length];
         for (int i = 0; i < parameters.lengthi++) {
             parameterTypes[i] = parameters[i].getClass();
         }
         try {
             final Class<T> clazz = cast(loader.classForName(className));
             final Constructor<T> constructor = findConstructor(clazzparameters);
             return constructor.newInstance(parameters);
         } catch (Exception e) {
             throw ..unableToInstantiate(className, Arrays.toString(parameters), e);
         }
     }
 
     @SuppressWarnings("unchecked")
     public static <T> T cast(Object obj) {
         return (T) obj;
     }
 
     public static boolean isClassLoadable(String classNameResourceLoader resourceLoader) {
         return loadClass(classNameresourceLoader) != null;
     }

    
Tries to load a class using the specified ResourceLoader. Returns null if the class is not found.

Parameters:
className
resourceLoader
Returns:
the loaded class or null if the given class cannot be loaded
 
     public static <T> Class<T> loadClass(String classNameResourceLoader resourceLoader) {
         try {
             return cast(resourceLoader.classForName(className));
         } catch (ResourceLoadingException e) {
             return null;
         } catch (SecurityException e) {
             return null;
         }
     }

    

Parameters:
annotations
metaAnnotationType
Returns:
true if any of the annotations specified has the given meta annotation type specified, false otherwise
    public static boolean hasBeanDefiningMetaAnnotationSpecified(Annotation[] annotationsClass<? extends AnnotationmetaAnnotationType) {
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().isAnnotationPresent(metaAnnotationType)) {
                return true;
            }
        }
        return false;
    }

    
Does constructor lookup respecting parameter covariance.
    private static <T> Constructor<T> findConstructor(Class<T> clazzObject... parameters) {
        for (Constructor<?> constructor : clazz.getConstructors()) {
            boolean match = true;
            for (int i = 0; i < parameters.lengthi++) {
                if (!constructor.getParameterTypes()[i].isAssignableFrom(parameters[i].getClass())) {
                    match = false;
                    break;
                }
            }
            if (match) {
                return cast(constructor);
            }
        }
        throw ..unableToFindConstructor(clazz, Arrays.toString(parameters));
    }
    private static boolean containsAnnotations(Annotation[] annotationsClass<? extends AnnotationrequiredAnnotation) {
        return containsAnnotation(annotationsrequiredAnnotationtrue);
    }
    private static boolean containsAnnotation(Annotation[] annotationsClass<? extends AnnotationrequiredAnnotationboolean checkMetaAnnotations) {
        for (Annotation annotation : annotations) {
            Class<? extends AnnotationannotationType = annotation.annotationType();
            if (requiredAnnotation.equals(annotationType)) {
                return true;
            }
            if (checkMetaAnnotations && containsAnnotation(annotationType.getAnnotations(), requiredAnnotationfalse)) {
                return true;
            }
        }
        return false;
    }
New to GrepCode? Check out our FAQ X