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:net.mojodna.sprout.support.SproutUtils.java

/**
 * Bean initialization method.  Uses reflection to determine properties
 * for which auto-wiring may be appropriate.  Subsequently attempts to
 * retrieve appropriate beans from the WebApplicationContext and set them
 * locally.//from w w w.  j av  a2s. c o  m
 * 
 * @param bean Bean to initialize.
 * @param context WebApplicationContext containing Spring beans.
 * @param clazz Type of Sprout.  This is used to determine which declared
 * methods are candidates for auto-wiring.
 */
public static void initialize(final Object bean, final WebApplicationContext context, final Class clazz) {
    final Collection<Method> methods = SproutUtils.getDeclaredMethods(bean.getClass(), clazz);

    final PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(bean.getClass());
    for (final PropertyDescriptor descriptor : descriptors) {
        final Class type = descriptor.getPropertyType();

        // beans should never be of type String
        // there must be a write method present
        // the write method must exist within the relevant subset of declared methods
        if (!type.equals(String.class) && null != descriptor.getWriteMethod()
                && methods.contains(descriptor.getWriteMethod())) {
            final Object serviceBean = context.getBean(descriptor.getName());
            if (null != serviceBean) {
                try {
                    log.debug("Wiring property '" + descriptor.getName() + "' with bean of type "
                            + serviceBean.getClass().getName());
                    PropertyUtils.setProperty(bean, descriptor.getName(), serviceBean);
                } catch (final IllegalAccessException e) {
                    throw new RuntimeException(e);
                } catch (final InvocationTargetException e) {
                    throw new RuntimeException(e);
                } catch (final NoSuchMethodException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * TODO additional lifecycle interface callbacks as defined in BeanFactory
     * should be implemented here
     * @see org.springframework.beans.factory.BeanFactory
     */

    // InitializingBean callback
    if (bean instanceof InitializingBean) {
        try {
            ((InitializingBean) bean).afterPropertiesSet();
        } catch (final Exception e) {
            log.warn("Exception while running afterPropertiesSet() on an InitializingBean: " + e.getMessage(),
                    e);
        }
    }
}

From source file:com.alfresco.orm.ObjectFillHelper.java

/**
 * Method provided Type of object based on passed value return value is
 * manipulated by method logic/*  w w w  . j a  v  a 2  s .  c  o  m*/
 * 
 * @param <T>
 * @param type
 * @param serializableValue
 * @return
 */
@SuppressWarnings({ "unchecked" })
private static <T> T getManipulatedValue(final Class<T> type, final Serializable serializableValue) {
    T retT = null;
    if (null != serializableValue) {
        retT = (T) serializableValue;
    } else if (type.equals(Date.class)) {
        retT = null;
    } else if (type.equals(String.class)) {
        retT = (T) "";
    } else if (type.equals(Integer.class)) {
        retT = (T) Integer.valueOf(0);
    } else if (type.equals(Long.class)) {
        retT = (T) Long.valueOf(0);
    } else if (type.equals(Double.class)) {
        retT = (T) ZERO_DOUBLE;
    } else if (type.equals(Float.class)) {
        retT = (T) ZERO_FLOAT;
    } else if (type.equals(Boolean.class)) {
        retT = (T) Boolean.FALSE;
    }
    return retT;
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Finds field on a given type.//from  w  w w  .j av  a  2 s. c  o  m
 *
 * @param type      class containing the field
 * @param fieldName name of field to search for
 * @return found field
 */
public static Field getField(Class type, String fieldName) {
    Field field = null;
    Class currentType = type;
    while (field == null && !currentType.equals(Object.class)) {
        try {
            field = currentType.getDeclaredField(fieldName);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            currentType = currentType.getSuperclass();
        }
    }

    return field;
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Finds a method on a class./*from w ww  . j av  a2s .  c om*/
 *
 * @param type           class containing the method
 * @param methodName     name of the method to search for
 * @param parameterTypes parameter types declared in the method signature
 * @return found method
 */
public static Method getMethod(Class type, String methodName, Class<?>... parameterTypes) {
    Method method = null;
    Class currentType = type;
    while (method == null && !currentType.equals(Object.class)) {
        try {
            method = currentType.getDeclaredMethod(methodName, parameterTypes);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            currentType = currentType.getSuperclass();
        }
    }

    return method;
}

From source file:com.jaspersoft.jasperserver.ws.xml.Unmarshaller.java

public static Object unmarshal(Class c, Element elm) {
    //c instanceof ResourceDescriptor.class
    if (c.equals(ResourceDescriptor.class)) {
        return readResourceDescriptor(elm);
    }//from   w  w w.  j a  va  2s .com

    throw new UnsupportedOperationException();
}

From source file:net.servicefixture.util.ReflectionUtils.java

private static boolean isConcreteSubclass(Class<?> baseClass, Class<?> clazz) {
    return baseClass.isAssignableFrom(clazz) && !baseClass.equals(clazz) && !clazz.isInterface()
            && !Modifier.isAbstract(clazz.getModifiers());
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <C extends Collection<?>> C newCollection(Object reference) {
    Class<?> clazz = null;
    if (reference instanceof Collection<?>) {
        clazz = (Class<?>) reference.getClass();

    } else if (reference instanceof Class<?> && Collection.class.isAssignableFrom((Class<?>) reference)) {
        clazz = (Class<?>) reference;

        if (clazz.equals(List.class)) {
            clazz = (Class<?>) ArrayList.class;

        } else if (clazz.equals(Set.class)) {
            clazz = (Class<?>) LinkedHashSet.class;
        }/*  w  w w .j a v a  2  s  .c  o m*/
    }

    if (clazz != null) {
        try {
            return (C) clazz.newInstance();
        } catch (Exception e) {
        }
    }

    return null;
}

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./* w  ww  .j a  va  2s  .  c o  m*/
 * @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 (InvocationTargetException e) {
        if (e.getTargetException() instanceof Error)
            throw (Error) (e.getTargetException());

    }
    return null;
}

From source file:org.lightadmin.core.util.NumberUtils.java

@SuppressWarnings("unchecked")
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass)
        throws IllegalArgumentException {
    Assert.notNull(number, "Number must not be null");
    Assert.notNull(targetClass, "Target class must not be null");

    if (targetClass.isInstance(number)) {
        return (T) number;
    } else if (targetClass.equals(byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }/*from  ww  w .  j  a va  2 s  .c  om*/
        return (T) new Byte(number.byteValue());
    } else if (targetClass.equals(short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Short(number.shortValue());
    } else if (targetClass.equals(int.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Integer(number.intValue());
    } else if (targetClass.equals(long.class)) {
        return (T) new Long(number.longValue());
    } else if (targetClass.equals(float.class)) {
        return (T) Float.valueOf(number.toString());
    } else if (targetClass.equals(double.class)) {
        return (T) Double.valueOf(number.toString());
    } else {
        return org.springframework.util.NumberUtils.convertNumberToTargetClass(number, targetClass);
    }
}

From source file:unquietcode.tools.beanmachine.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.
 * <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 java.lang.annotation.Annotation}, so we need to handle
 * this explicitly./* w  w  w  . ja  va2  s.c om*/
 * @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(Class<? extends Annotation> annotationType,
        Class<?> clazz) {
    Assert.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());
}