Example usage for java.lang Class equals

List of usage examples for java.lang Class equals

Introduction

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

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java

/**
 * Find the first {@link Class} in the inheritance hierarchy of the specified <code>clazz</code> (including the
 * specified <code>clazz</code> itself) which declares an annotation for the specified <code>annotationType</code>,
 * or <code>null</code> if not found. If the supplied <code>clazz</code> is <code>null</code>, <code>null</code>
 * will be returned./*  www.  j a v a  2  s .  c o  m*/
 * <p>
 * If the supplied <code>clazz</code> is an interface, only the interface itself will be checked; the inheritance
 * hierarchy for interfaces will not be traversed.
 * <p>
 * The standard {@link Class} API does not provide a mechanism for determining which class in an inheritance
 * hierarchy actually declares an {@link Annotation}, so we need to handle this explicitly.
 * 
 * @param annotationType
 *            the Class object corresponding to the annotation type
 * @param clazz
 *            the Class object corresponding to the class on which to check for the annotation, or <code>null</code>
 * @return the first {@link Class} in the inheritance hierarchy of the specified <code>clazz</code> which declares
 *         an annotation for the specified <code>annotationType</code>, or <code>null</code> if not found
 * @see Class#isAnnotationPresent(Class)
 * @see Class#getDeclaredAnnotations()
 */
public static Class<?> findAnnotationDeclaringClass(final Class<? extends Annotation> annotationType,
        final Class<?> clazz) {
    Validate.notNull(annotationType, "Annotation type must not be null");
    if ((clazz == null) || clazz.equals(Object.class)) {
        return null;
    }
    return (isAnnotationDeclaredLocally(annotationType, clazz)) ? clazz
            : findAnnotationDeclaringClass(annotationType, clazz.getSuperclass());
}

From source file:com.github.valdr.thirdparty.spring.AnnotationUtils.java

/**
 * Find the first {@link Class} in the inheritance hierarchy of the specified
 * {@code clazz} (including the specified {@code clazz} itself) which declares
 * at least one of the specified {@code annotationTypes}, or {@code null} if
 * none of the specified annotation types could be found.
 * <p>If the supplied {@code clazz} is {@code null}, {@code null} will be
 * returned./*w ww  . ja va 2 s .c  o m*/
 * <p>If the supplied {@code clazz} is an interface, only the interface itself
 * will be checked; the inheritance hierarchy for interfaces will not be traversed.
 * <p>The standard {@link Class} API does not provide a mechanism for determining
 * which class in an inheritance hierarchy actually declares one of several
 * candidate {@linkplain java.lang.annotation.Annotation annotations}, so we need to handle this
 * explicitly.
 * @param annotationTypes the list of Class objects corresponding to the
 * annotation types
 * @param clazz the Class object corresponding to the class on which to check
 * for the annotations, or {@code null}
 * @return the first {@link Class} in the inheritance hierarchy of the specified
 * {@code clazz} which declares an annotation of at least one of the specified
 * {@code annotationTypes}, or {@code null} if not found
 * @since 3.2.2
 * @see Class#isAnnotationPresent(Class)
 * @see Class#getDeclaredAnnotations()
 * @see #findAnnotationDeclaringClass(Class, Class)
 * @see #isAnnotationDeclaredLocally(Class, Class)
 */
public static Class<?> findAnnotationDeclaringClassForTypes(List<Class<? extends Annotation>> annotationTypes,
        Class<?> clazz) {
    if (clazz == null || clazz.equals(Object.class)) {
        return null;
    }
    for (Class<? extends Annotation> annotationType : annotationTypes) {
        if (isAnnotationDeclaredLocally(annotationType, clazz)) {
            return clazz;
        }
    }
    return findAnnotationDeclaringClassForTypes(annotationTypes, clazz.getSuperclass());
}

From source file:com.github.rvesse.airline.Accessor.java

private static Type[] getTypeParameters(Class<?> desiredType, Type type) {
    if (type instanceof Class) {
        Class<?> rawClass = (Class<?>) type;

        // if this is the collection class we're done
        if (desiredType.equals(type)) {
            return null;
        }/*  w  w  w .j  a  v a 2 s  .  c  o m*/

        for (Type iface : rawClass.getGenericInterfaces()) {
            Type[] collectionType = getTypeParameters(desiredType, iface);
            if (collectionType != null) {
                return collectionType;
            }
        }

        return getTypeParameters(desiredType, rawClass.getGenericSuperclass());
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;

        Type rawType = parameterizedType.getRawType();
        if (desiredType.equals(rawType)) {
            return parameterizedType.getActualTypeArguments();
        }

        Type[] collectionTypes = getTypeParameters(desiredType, rawType);
        if (collectionTypes != null) {
            for (int i = 0; i < collectionTypes.length; i++) {
                if (collectionTypes[i] instanceof TypeVariable) {
                    TypeVariable<?> typeVariable = (TypeVariable<?>) collectionTypes[i];
                    TypeVariable<?>[] rawTypeParams = ((Class<?>) rawType).getTypeParameters();
                    for (int j = 0; j < rawTypeParams.length; j++) {
                        if (typeVariable.getName().equals(rawTypeParams[j].getName())) {
                            collectionTypes[i] = parameterizedType.getActualTypeArguments()[j];
                        }
                    }
                }
            }
        }
        return collectionTypes;
    }
    return null;
}

From source file:hermes.impl.LoaderSupport.java

/**
 * Indicates if a class or interface implements or extends the specified
 * interfaces or classes. If the specified <CODE>Class</CODE> is a class,
 * this method will recursively test if this class, its superclass or one of
 * the implemented interfaces of this class implements the specified
 * interface.<BR>/*  w ww  .  j ava2  s.  co  m*/
 * If the specified <CODE>Class</CODE> is an interface, this method will
 * recursively test if this interface or one of the implemented interfaces of
 * this interface implements the specified interface.<BR>
 * 
 * @param clazz
 *           the class or interface in question
 * @param testInterface
 *           the class or interface to test against
 * @return <CODE>true</CODE> if the specified interfaces is implemented by
 *         this class or one of its super-classes or interfaces
 */
public static boolean implementsOrExtends(Class clazz, Class testInterface) {
    Class[] implementedInterfaces = clazz.getInterfaces();

    // test interface
    if (clazz.equals(testInterface)) {
        return true; // possibly the end of the recursion
    }

    for (int i = 0; i < implementedInterfaces.length; i++) {
        if (implementsOrExtends(implementedInterfaces[i], testInterface)) {
            return true; // recursion
        }
    }

    // maybe the superclass implements this interface ?
    Class superClass = clazz.getSuperclass();
    if (superClass != null && implementsOrExtends(superClass, testInterface)) {
        return true; // recursion
    }

    return false;
}

From source file:cc.kave.commons.utils.json.JsonUtils.java

private static Set<Class<?>> getAllTypesFromHierarchyExceptObject(Class<?> elem, boolean includeElem) {
    Set<Class<?>> hierarchy = Sets.newHashSet();
    if (elem == null || elem.equals(Object.class)) {
        return hierarchy;
    }/*from   www .jav a  2 s.  c o m*/

    if (includeElem) {
        hierarchy.add(elem);
    }

    for (Class<?> i : elem.getInterfaces()) {
        hierarchy.addAll(getAllTypesFromHierarchyExceptObject(i, true));
    }
    hierarchy.addAll(getAllTypesFromHierarchyExceptObject(elem.getSuperclass(), true));

    return hierarchy;
}

From source file:fit.TypeAdapter.java

public static TypeAdapter adapterFor(Class<?> type) throws UnsupportedOperationException {
    if (type.isPrimitive()) {
        if (type.equals(byte.class))
            return new ByteAdapter();
        if (type.equals(short.class))
            return new ShortAdapter();
        if (type.equals(int.class))
            return new IntAdapter();
        if (type.equals(long.class))
            return new LongAdapter();
        if (type.equals(float.class))
            return new FloatAdapter();
        if (type.equals(double.class))
            return new DoubleAdapter();
        if (type.equals(char.class))
            return new CharAdapter();
        if (type.equals(boolean.class))
            return new BooleanAdapter();
        throw new UnsupportedOperationException("can't yet adapt " + type);
    } else {//w  w  w  .  j  a v  a2  s .  co m
        Object delegate = PARSE_DELEGATES.get(type);
        if (delegate instanceof DelegateClassAdapter)
            return (TypeAdapter) ((DelegateClassAdapter) delegate).clone();
        if (delegate instanceof DelegateObjectAdapter)
            return (TypeAdapter) ((DelegateObjectAdapter) delegate).clone();
        if (type.equals(Byte.class))
            return new ClassByteAdapter();
        if (type.equals(Short.class))
            return new ClassShortAdapter();
        if (type.equals(Integer.class))
            return new ClassIntegerAdapter();
        if (type.equals(Long.class))
            return new ClassLongAdapter();
        if (type.equals(Float.class))
            return new ClassFloatAdapter();
        if (type.equals(Double.class))
            return new ClassDoubleAdapter();
        if (type.equals(Character.class))
            return new ClassCharacterAdapter();
        if (type.equals(Boolean.class))
            return new ClassBooleanAdapter();
        if (type.isArray())
            return new ArrayAdapter();
        return new TypeAdapter();
    }
}

From source file:com.xiongyingqi.util.MethodInvoker.java

/**
 * Algorithm that judges the match between the declared parameter types of a candidate method
 * and a specific list of arguments that this method is supposed to be invoked with.
 * <p>Determines a weight that represents the class hierarchy difference between types and
 * arguments. A direct match, i.e. type Integer -> arg of class Integer, does not increase
 * the result - all direct matches means weight 0. A match between type Object and arg of
 * class Integer would increase the weight by 2, due to the superclass 2 steps up in the
 * hierarchy (i.e. Object) being the last one that still matches the required type Object.
 * Type Number and class Integer would increase the weight by 1 accordingly, due to the
 * superclass 1 step up the hierarchy (i.e. Number) still matching the required type Number.
 * Therefore, with an arg of type Integer, a constructor (Integer) would be preferred to a
 * constructor (Number) which would in turn be preferred to a constructor (Object).
 * All argument weights get accumulated.
 * <p>Note: This is the algorithm used by MethodInvoker itself and also the algorithm
 * used for constructor and factory method selection in Spring's bean container (in case
 * of lenient constructor resolution which is the default for regular bean definitions).
 *
 * @param paramTypes the parameter types to match
 * @param args       the arguments to match
 * @return the accumulated weight for all arguments
 *//*from w  w w. j ava2 s . c  o  m*/
public static int getTypeDifferenceWeight(Class<?>[] paramTypes, Object[] args) {
    int result = 0;
    for (int i = 0; i < paramTypes.length; i++) {
        if (!ClassUtils.isAssignableValue(paramTypes[i], args[i])) {
            return Integer.MAX_VALUE;
        }
        if (args[i] != null) {
            Class<?> paramType = paramTypes[i];
            Class<?> superClass = args[i].getClass().getSuperclass();
            while (superClass != null) {
                if (paramType.equals(superClass)) {
                    result = result + 2;
                    superClass = null;
                } else if (ClassUtils.isAssignable(paramType, superClass)) {
                    result = result + 2;
                    superClass = superClass.getSuperclass();
                } else {
                    superClass = null;
                }
            }
            if (paramType.isInterface()) {
                result = result + 1;
            }
        }
    }
    return result;
}

From source file:com.autobizlogic.abl.util.BeanUtil.java

/**
 * Unfortunately, int.class.isAssignableFrom(Integer.class) returns false, so we need
 * to convert primitive types to their class equivalent.
 * @param cls Any class/*from ww  w.j  a  v  a 2s . co  m*/
 * @return If the class is a primitive class (e.g. int.class, boolean.class, etc...)
 * return the class equivalent (e.g. Integer.class, Boolean.class, etc...), otherwise
 * return the given class.
 */
private static Class<?> getGenericType(Class<?> cls) {
    if (cls.equals(byte.class))
        return Byte.class;
    if (cls.equals(short.class))
        return Short.class;
    if (cls.equals(int.class))
        return Integer.class;
    if (cls.equals(long.class))
        return Long.class;
    if (cls.equals(float.class))
        return Float.class;
    if (cls.equals(double.class))
        return Double.class;
    if (cls.equals(boolean.class))
        return Boolean.class;
    if (cls.equals(char.class))
        return Character.class;
    return cls;
}

From source file:TypeUtil.java

/** Convert String value to instance.
 * @param type The class of the instance, which may be a primitive TYPE field.
 * @param value The value as a string./* ww w  .  j ava 2s.  c  om*/
 * @return The value as an Object.
 */
public static Object valueOf(Class type, String value) {
    try {
        if (type.equals(java.lang.String.class))
            return value;

        Method m = (Method) class2Value.get(type);
        if (m != null)
            return m.invoke(null, new Object[] { value });

        if (type.equals(java.lang.Character.TYPE) || type.equals(java.lang.Character.class))
            return new Character(value.charAt(0));

        Constructor c = type.getConstructor(stringArg);
        return c.newInstance(new Object[] { value });
    } catch (NoSuchMethodException e) {
        // LogSupport.ignore(log,e);
    } catch (IllegalAccessException e) {
        // LogSupport.ignore(log,e);
    } catch (InstantiationException e) {
        // LogSupport.ignore(log,e);
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof Error)
            throw (Error) (e.getTargetException());
        // LogSupport.ignore(log,e);
    }
    return null;
}

From source file:com.wavemaker.commons.util.TypeConversionUtils.java

/**
 * Returns true iff the Class clazz represents a primitive (boolean, int) or a primitive wrapper (Integer),
 * including Big{Integer,Decimal} and Atomic{Integer,Long}. Also, Strings and Dates are included.
 *
 * @param clazz//from  w  ww.  ja v a  2s.  co  m
 * @return
 */
public static boolean isPrimitiveOrWrapper(Class<?> clazz) {

    if (clazz.isPrimitive()) {
        return true;
    }

    if (clazz.equals(String.class)) {
        return true;
    }

    if (Date.class.isAssignableFrom(clazz)) {
        return true;
    }

    if (LocalDateTime.class.isAssignableFrom(clazz)) {
        return true;
    }

    if (PRIMITIVE_WRAPPERS.contains(clazz)) {
        return true;
    }

    return false;
}