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:com.github.erchu.beancp.MapperExecutorSelector.java

private static boolean classEqualsOrWrapper(final Class objectClass, final Class supportedClass) {
    if (objectClass.isPrimitive()) {
        Class<?> primitiveTypeWrapper = ClassUtils.primitiveToWrapper(objectClass);

        return objectClass.equals(supportedClass) || primitiveTypeWrapper.equals(supportedClass);
    } else if (supportedClass.isPrimitive()) {
        Class<?> primitiveTypeWrapper = ClassUtils.primitiveToWrapper(supportedClass);

        return objectClass.equals(supportedClass) || objectClass.equals(primitiveTypeWrapper);
    } else {/*from w  w w . java2s.co m*/
        return supportedClass.equals(objectClass);
    }
}

From source file:org.mashti.jetson.util.JsonParserUtil.java

/**
 * Reads a field value as the provided type.
 *
 * @param <Value> the value type/*from ww w .jav a 2 s  .  c om*/
 * @param parser the parser
 * @param expected_type the expected type of the value
 * @return the value
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static <Value> Value readValueAs(final JsonParser parser, final Class<Value> expected_type)
        throws IOException {

    final Value value;
    if (expected_type.equals(Void.TYPE)) {
        expectNullValue(parser);
        value = null;
    } else {
        parser.nextToken();
        value = parser.readValueAs(expected_type);
    }
    return value;
}

From source file:natalia.dymnikova.cluster.scheduler.impl.Codec.java

private static Object doSanitize(final Object f) {
    final Class<?> aClass = f.getClass();
    if (!isStatic(aClass.getModifiers())) {
        final Class<?> enclosingClass = aClass.getEnclosingClass();
        if (enclosingClass != null) {
            for (final Field field : aClass.getDeclaredFields()) {
                if (enclosingClass.equals(field.getType())) {
                    field.setAccessible(true);
                    try {
                        field.set(f, null);
                    } catch (final IllegalAccessException e) {
                        throw unchecked(e);
                    }/*from w  w  w  .  j av a  2s  .co  m*/
                }
            }
        }
    }
    return f;
}

From source file:io.pivotal.spring.xd.jdbcgpfdist.TestUtils.java

public static void setField(String name, Object target, Object value) throws Exception {
    Field field = null;/*from   w  w  w. j  av a2  s .com*/
    Class<?> clazz = target.getClass();
    do {
        try {
            field = clazz.getDeclaredField(name);
        } catch (Exception ex) {
        }

        clazz = clazz.getSuperclass();
    } while (field == null && !clazz.equals(Object.class));

    if (field == null)
        throw new IllegalArgumentException(
                "Cannot find field '" + name + "' in the class hierarchy of " + target.getClass());
    field.setAccessible(true);
    field.set(target, value);
}

From source file:io.pivotal.spring.xd.jdbcgpfdist.TestUtils.java

@SuppressWarnings("unchecked")
public static <T> T readField(String name, Object target) throws Exception {
    Field field = null;// ww  w  . j  a  va2  s . c  o m
    Class<?> clazz = target.getClass();
    do {
        try {
            field = clazz.getDeclaredField(name);
        } catch (Exception ex) {
        }

        clazz = clazz.getSuperclass();
    } while (field == null && !clazz.equals(Object.class));

    if (field == null)
        throw new IllegalArgumentException(
                "Cannot find field '" + name + "' in the class hierarchy of " + target.getClass());
    field.setAccessible(true);
    return (T) field.get(target);
}

From source file:com.serli.chell.framework.form.FormStructure.java

private static boolean isMultivaluedField(Class<?> fieldType) {
    return fieldType.equals(String[].class);
}

From source file:Main.java

private static Field findFieldRecursiveImpl(Class<?> clazz, String fieldName) throws NoSuchFieldException {
    try {//from   w  w  w  .  java2 s  . c om
        return clazz.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        while (true) {
            clazz = clazz.getSuperclass();
            if (clazz == null || clazz.equals(Object.class))
                break;

            try {
                return clazz.getDeclaredField(fieldName);
            } catch (NoSuchFieldException ignored) {
            }
        }
        throw e;
    }
}

From source file:com.facebook.stetho.json.ObjectMapper.java

private static boolean isWrapperOrPrimitiveType(Class<?> clazz) {
    return clazz.isPrimitive() || clazz.equals(Boolean.class) || clazz.equals(Integer.class)
            || clazz.equals(Character.class) || clazz.equals(Byte.class) || clazz.equals(Short.class)
            || clazz.equals(Double.class) || clazz.equals(Long.class) || clazz.equals(Float.class);
}

From source file:io.github.benas.jpopulator.impl.DefaultRandomizer.java

/**
 * Generate a random value for the given type.
 *
 * @param type the type for which a random value will be generated
 * @return a random value for the given type or null if the type is not supported
 *///  w  w  w .jav  a2 s. c  o m
public static Object getRandomValue(final Class type) {

    /*
     * String and Character types
     */
    if (type.equals(String.class)) {
        return RandomStringUtils.randomAlphabetic(ConstantsUtil.DEFAULT_STRING_LENGTH);
    }
    if (type.equals(Character.TYPE) || type.equals(Character.class)) {
        return RandomStringUtils.randomAlphabetic(1).charAt(0);
    }

    /*
     * Boolean type
     */
    if (type.equals(Boolean.TYPE) || type.equals(Boolean.class)) {
        return ConstantsUtil.RANDOM.nextBoolean();
    }

    /*
     * Numeric types
     */
    if (type.equals(Byte.TYPE) || type.equals(Byte.class)) {
        return (byte) (ConstantsUtil.RANDOM.nextInt());
    }
    if (type.equals(Short.TYPE) || type.equals(Short.class)) {
        return (short) (ConstantsUtil.RANDOM.nextInt());
    }
    if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
        return ConstantsUtil.RANDOM.nextInt();
    }
    if (type.equals(Long.TYPE) || type.equals(Long.class)) {
        return ConstantsUtil.RANDOM.nextLong();
    }
    if (type.equals(Double.TYPE) || type.equals(Double.class)) {
        return ConstantsUtil.RANDOM.nextDouble();
    }
    if (type.equals(Float.TYPE) || type.equals(Float.class)) {
        return ConstantsUtil.RANDOM.nextFloat();
    }
    if (type.equals(BigInteger.class)) {
        return new BigInteger(
                Math.abs(ConstantsUtil.RANDOM.nextInt(ConstantsUtil.DEFAULT_BIG_INTEGER_NUM_BITS_LENGTH)),
                ConstantsUtil.RANDOM);
    }
    if (type.equals(BigDecimal.class)) {
        return new BigDecimal(ConstantsUtil.RANDOM.nextDouble());
    }
    if (type.equals(AtomicLong.class)) {
        return new AtomicLong(ConstantsUtil.RANDOM.nextLong());
    }
    if (type.equals(AtomicInteger.class)) {
        return new AtomicInteger(ConstantsUtil.RANDOM.nextInt());
    }

    /*
     * Date and time types
     */
    if (type.equals(java.util.Date.class)) {
        return ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue();
    }
    if (type.equals(java.sql.Date.class)) {
        return new java.sql.Date(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(java.sql.Time.class)) {
        return new java.sql.Time(ConstantsUtil.RANDOM.nextLong());
    }
    if (type.equals(java.sql.Timestamp.class)) {
        return new java.sql.Timestamp(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(Calendar.class)) {
        return Calendar.getInstance();
    }
    if (type.equals(org.joda.time.DateTime.class)) {
        return new org.joda.time.DateTime(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(org.joda.time.LocalDate.class)) {
        return new org.joda.time.LocalDate(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(org.joda.time.LocalTime.class)) {
        return new org.joda.time.LocalTime(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(org.joda.time.LocalDateTime.class)) {
        return new org.joda.time.LocalDateTime(ConstantsUtil.DATE_RANGE_RANDOMIZER.getRandomValue().getTime());
    }
    if (type.equals(org.joda.time.Duration.class)) {
        return new org.joda.time.Duration(Math.abs(ConstantsUtil.RANDOM.nextLong()));
    }
    if (type.equals(org.joda.time.Period.class)) {
        return new org.joda.time.Period(Math.abs(ConstantsUtil.RANDOM.nextInt()));
    }
    if (type.equals(org.joda.time.Interval.class)) {
        long startDate = Math.abs(ConstantsUtil.RANDOM.nextInt());
        long endDate = startDate + Math.abs(ConstantsUtil.RANDOM.nextInt());
        return new org.joda.time.Interval(startDate, endDate);
    }

    /*
     * Enum type
     */
    if (type.isEnum() && type.getEnumConstants().length > 0) {
        Object[] enumConstants = type.getEnumConstants();
        return enumConstants[ConstantsUtil.RANDOM.nextInt(enumConstants.length)];
    }

    /*
     * Return null for any unsupported type
     */
    return null;

}

From source file:com.facebook.stetho.json.ObjectMapper.java

private static boolean canDirectlySerializeClass(Class clazz) {
    return isWrapperOrPrimitiveType(clazz) || clazz.equals(String.class);
}