Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2008, Red Hat Middleware LLC, and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * http://www.apache.org/licenses/LICENSE-2.0
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.weld.environment.util;
 
 
Reflection utilities.

Author(s):
Pete Muir
Martin Kouba
 
 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;
     }
 
     @SuppressWarnings("unchecked")
     public static <T> T cast(Object obj) {
         return (T) obj;
     }

    

Parameters:
resourceLoader
className
Returns:
true if a class with the given name can be loaded, false otherwise
See also:
loadClass(org.jboss.weld.resources.spi.ResourceLoader,java.lang.String)
 
     public static boolean isClassLoadable(ResourceLoader resourceLoaderString className) {
         return loadClass(resourceLoaderclassName) != null;
     }

    

Parameters:
resourceLoader
className
Returns:
the loaded class or null if the given class cannot be loaded
See also:
classForName(org.jboss.weld.resources.spi.ResourceLoader,java.lang.String)
    public static <T> Class<T> loadClass(ResourceLoader resourceLoaderString className) {
        try {
            return classForName(resourceLoaderclassName);
        } catch (Exception e) {
            return null;
        }
    }

    
First try to load a class using the specified ResourceLoader. If not successful, try java.lang.Class.forName(java.lang.String) as a fallback.

Parameters:
resourceLoader
className
Returns:
the loaded class
    public static <T> Class<T> classForName(ResourceLoader resourceLoaderString className) {
        try {
            return cast(resourceLoader.classForName(className));
        } catch (Exception e) {
            ..cannotLoadClassUsingResourceLoader(className);
            ..catchingTrace(e);
        }
        try {
            return cast(Class.forName(className));
        } catch (Exception e) {
            throw ..cannotLoadClass(classNamee);
        }
    }

    

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;
    }
    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