Example usage for java.lang Class isAnnotation

List of usage examples for java.lang Class isAnnotation

Introduction

In this page you can find the example usage for java.lang Class isAnnotation.

Prototype

public boolean isAnnotation() 

Source Link

Document

Returns true if this Class object represents an annotation type.

Usage

From source file:Main.java

public static void main(String[] unused) {
    try {//  ww  w.j  a va 2s .  c om
        String n = "java.lang.Deprecated";
        Class c = Class.forName(n);
        System.out.println(c.isAnnotation());

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static String canBeABeanType(Class<?> paramClass) {
    if (paramClass.isAnnotation())
        return "annotation";
    if (paramClass.isArray())
        return "array";
    if (paramClass.isEnum())
        return "enum";
    if (paramClass.isPrimitive())
        return "primitive";
    return null;//from  w ww . j a v  a2s  . c  om
}

From source file:Main.java

/**
 * @return Null if class might be a bean; type String (that identifies
 *   why it's not a bean) if not//from   ww  w . ja va 2  s  .  co m
 */
public static String canBeABeanType(Class<?> type) {
    // First: language constructs that ain't beans:
    if (type.isAnnotation()) {
        return "annotation";
    }
    if (type.isArray()) {
        return "array";
    }
    if (type.isEnum()) {
        return "enum";
    }
    if (type.isPrimitive()) {
        return "primitive";
    }

    // Anything else? Seems valid, then
    return null;
}

From source file:MyClass.java

public static String getClassDescription(Class c) {
    StringBuilder classDesc = new StringBuilder();
    int modifierBits = 0;
    String keyword = "";
    if (c.isInterface()) {
        modifierBits = c.getModifiers() & Modifier.interfaceModifiers();
        if (c.isAnnotation()) {
            keyword = "@interface";
        } else {//  w ww .  j  ava  2 s .  co  m
            keyword = "interface";
        }
    } else if (c.isEnum()) {
        modifierBits = c.getModifiers() & Modifier.classModifiers();
        keyword = "enum";
    }
    modifierBits = c.getModifiers() & Modifier.classModifiers();
    keyword = "class";

    String modifiers = Modifier.toString(modifierBits);
    classDesc.append(modifiers);
    classDesc.append(" " + keyword);
    String simpleName = c.getSimpleName();
    classDesc.append(" " + simpleName);

    String genericParms = getGenericTypeParams(c);
    classDesc.append(genericParms);

    Class superClass = c.getSuperclass();
    if (superClass != null) {
        String superClassSimpleName = superClass.getSimpleName();
        classDesc.append("  extends " + superClassSimpleName);
    }
    String interfaces = Main.getClassInterfaces(c);
    if (interfaces != null) {
        classDesc.append("  implements " + interfaces);
    }
    return classDesc.toString();
}

From source file:com.espertech.esper.epl.annotation.AnnotationUtil.java

public static Annotation findAnnotation(Annotation[] annotations, Class annotationClass) {
    if (!annotationClass.isAnnotation()) {
        throw new IllegalArgumentException(
                "Class " + annotationClass.getName() + " is not an annotation class");
    }/*from w  w w  . j  a v a 2s.  co  m*/
    if (annotations == null || annotations.length == 0) {
        return null;
    }
    for (Annotation anno : annotations) {
        if (JavaClassHelper.isImplementsInterface(anno.getClass(), annotationClass)) {
            return anno;
        }
    }
    return null;
}

From source file:com.espertech.esper.epl.annotation.AnnotationUtil.java

public static List<Annotation> findAnnotations(Annotation[] annotations, Class annotationClass) {
    if (!annotationClass.isAnnotation()) {
        throw new IllegalArgumentException(
                "Class " + annotationClass.getName() + " is not an annotation class");
    }/*from   w  w  w .jav a  2s . c  o  m*/
    if (annotations == null || annotations.length == 0) {
        return null;
    }
    List<Annotation> annotationsList = new ArrayList<Annotation>();
    for (Annotation anno : annotations) {
        if (JavaClassHelper.isImplementsInterface(anno.getClass(), annotationClass)) {
            annotationsList.add(anno);
        }
    }
    return annotationsList;
}

From source file:org.shept.util.BeanUtilsExtended.java

/**
 * Find all occurrences of targetClass in targetObject.
 * Be careful. This stuff may be recursive !
 * Should be improved to prevent endless recursive loops.
 * //from  w w  w  . j  a  va 2 s. c  o  m
 * @param
 * @return
 *
 * @param targetObject
 * @param targetClass
 * @param nestedPath
 * @return
 * @throws Exception 
 */
public static Map<String, ?> findNestedPaths(Object targetObject, Class<?> targetClass, String nestedPath,
        List<String> ignoreList, Set<Object> visited, int maxDepth) throws Exception {

    Assert.notNull(targetObject);
    Assert.notNull(targetClass);
    Assert.notNull(ignoreList);
    HashMap<String, Object> nestedPaths = new HashMap<String, Object>();
    if (maxDepth <= 0) {
        return nestedPaths;
    }

    nestedPath = (nestedPath == null ? "" : nestedPath);
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(targetObject);
    PropertyDescriptor[] props = bw.getPropertyDescriptors();

    for (PropertyDescriptor pd : props) {
        Class<?> clazz = pd.getPropertyType();
        if (clazz != null && !clazz.equals(Class.class) && !clazz.isAnnotation() && !clazz.isPrimitive()) {
            Object value = null;
            String pathName = pd.getName();
            if (!ignoreList.contains(pathName)) {
                try {
                    value = bw.getPropertyValue(pathName);
                } catch (Exception e) {
                } // ignore any exceptions here
                if (StringUtils.hasText(nestedPath)) {
                    pathName = nestedPath + PropertyAccessor.NESTED_PROPERTY_SEPARATOR + pathName;
                }
                // TODO break up this stuff into checking and excecution a la ReflectionUtils
                if (targetClass.isAssignableFrom(clazz)) {
                    nestedPaths.put(pathName, value);
                }
                // exclude objects already visited from further inspection to prevent circular references
                // unfortunately this stuff isn't fool proof as there are ConcurrentModificationExceptions
                // when adding objects to the visited list
                if (value != null && !isInstanceVisited(visited, value)) {
                    nestedPaths.putAll(
                            findNestedPaths(value, targetClass, pathName, ignoreList, visited, maxDepth - 1));
                }
            }
        }
    }
    return nestedPaths;
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * Helper method for comparing two objects of an array type.
 *
 * @param componentType the component type of the array
 * @param o1 the first object/*from   w  w w .ja va  2s  . c  om*/
 * @param o2 the second object
 * @return a flag whether these objects are equal
 */
private static boolean arrayMemberEquals(Class<?> componentType, Object o1, Object o2) {
    if (componentType.isAnnotation()) {
        return annotationArrayMemberEquals((Annotation[]) o1, (Annotation[]) o2);
    }
    if (componentType.equals(Byte.TYPE)) {
        return Arrays.equals((byte[]) o1, (byte[]) o2);
    }
    if (componentType.equals(Short.TYPE)) {
        return Arrays.equals((short[]) o1, (short[]) o2);
    }
    if (componentType.equals(Integer.TYPE)) {
        return Arrays.equals((int[]) o1, (int[]) o2);
    }
    if (componentType.equals(Character.TYPE)) {
        return Arrays.equals((char[]) o1, (char[]) o2);
    }
    if (componentType.equals(Long.TYPE)) {
        return Arrays.equals((long[]) o1, (long[]) o2);
    }
    if (componentType.equals(Float.TYPE)) {
        return Arrays.equals((float[]) o1, (float[]) o2);
    }
    if (componentType.equals(Double.TYPE)) {
        return Arrays.equals((double[]) o1, (double[]) o2);
    }
    if (componentType.equals(Boolean.TYPE)) {
        return Arrays.equals((boolean[]) o1, (boolean[]) o2);
    }
    return Arrays.equals((Object[]) o1, (Object[]) o2);
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * <p>Checks if the specified type is permitted as an annotation member.</p>
 *
 * <p>The Java language specification only permits certain types to be used
 * in annotations. These include {@link String}, {@link Class}, primitive
 * types, {@link Annotation}, {@link Enum}, and single-dimensional arrays of
 * these types.</p>//from w w w. j  ava  2  s. c  o m
 *
 * @param type the type to check, {@code null}
 * @return {@code true} if the type is a valid type to use in an annotation
 */
public static boolean isValidAnnotationMemberType(Class<?> type) {
    if (type == null) {
        return false;
    }
    if (type.isArray()) {
        type = type.getComponentType();
    }
    return type.isPrimitive() || type.isEnum() || type.isAnnotation() || String.class.equals(type)
            || Class.class.equals(type);
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * Helper method for checking whether two objects of the given type are
 * equal. This method is used to compare the parameters of two annotation
 * instances./*from  w  w  w. j ava  2s.  c  o  m*/
 *
 * @param type the type of the objects to be compared
 * @param o1 the first object
 * @param o2 the second object
 * @return a flag whether these objects are equal
 */
private static boolean memberEquals(Class<?> type, Object o1, Object o2) {
    if (o1 == o2) {
        return true;
    }
    if (o1 == null || o2 == null) {
        return false;
    }
    if (type.isArray()) {
        return arrayMemberEquals(type.getComponentType(), o1, o2);
    }
    if (type.isAnnotation()) {
        return equals((Annotation) o1, (Annotation) o2);
    }
    return o1.equals(o2);
}