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:Main.java

/**
 * @param type//from   w ww  .ja  v  a 2 s. c o m
 * @param fieldName
 * @return
 */
private static Field getField(Class<?> type, String fieldName) {
    Class<?> currentType = type;
    while (!currentType.equals(Object.class)) {
        for (Field field : currentType.getDeclaredFields()) {
            if (field.getName().equals(fieldName)) {
                return field;
            }
        }
        currentType = currentType.getSuperclass();
    }
    return null;
}

From source file:Main.java

/**
 * get all fields for a class/*from www.  j  a v a  2s . co  m*/
 *
 * @param type
 * @return all fields indexed by their names
 */
private static Map<String, Field> getAllFields(Class<?> type) {
    Map<String, Field> fields = new HashMap<String, Field>();
    Class<?> currentType = type;
    while (!currentType.equals(Object.class)) {
        for (Field field : currentType.getDeclaredFields()) {
            int mod = field.getModifiers();
            /*
             * by convention, our fields should not have any modifier
             */
            if (mod == 0 || Modifier.isProtected(mod) && !Modifier.isStatic(mod)) {
                fields.put(field.getName(), field);
            }
        }
        currentType = currentType.getSuperclass();
    }
    return fields;
}

From source file:Main.java

private static boolean isTypeMatch(Class<?> paramClass, Class<? extends Object> paramClass1) {
    if (paramClass.equals(paramClass1))
        return true;
    if (paramClass.isPrimitive()) {
        if ((paramClass.getName().equals("int")) && (paramClass1.getName().equals("java.lang.Integer")))
            return true;
        if ((paramClass.getName().equals("long")) && (paramClass1.getName().equals("java.lang.Long")))
            return true;
        if ((paramClass.getName().equals("float")) && (paramClass1.getName().equals("java.lang.Float")))
            return true;
        if ((paramClass.getName().equals("double")) && (paramClass1.getName().equals("java.lang.Double")))
            return true;
        if ((paramClass.getName().equals("char")) && (paramClass1.getName().equals("java.lang.Character")))
            return true;
        if ((paramClass.getName().equals("byte")) && (paramClass1.getName().equals("java.lang.Byte")))
            return true;
        if ((paramClass.getName().equals("short")) && (paramClass1.getName().equals("java.lang.Short")))
            return true;
        if ((paramClass.getName().equals("boolean")) && (paramClass1.getName().equals("java.lang.Boolean")))
            return true;
    }//from  ww w  .j  a  va  2 s .c o  m
    return false;
}

From source file:com.artofsolving.jodconverter.XmlDocumentFormatRegistry.java

private static XStream createXStream() {
    XStream xstream = new XStream(new DomDriver());
    xstream.setMode(XStream.NO_REFERENCES);
    xstream.alias("document-format", DocumentFormat.class);
    xstream.aliasField("mime-type", DocumentFormat.class, "mimeType");
    xstream.aliasField("file-extension", DocumentFormat.class, "fileExtension");
    xstream.aliasField("export-filters", DocumentFormat.class, "exportFilters");
    xstream.aliasField("export-options", DocumentFormat.class, "exportOptions");
    xstream.aliasField("import-options", DocumentFormat.class, "importOptions");
    xstream.alias("family", DocumentFamily.class);
    xstream.registerConverter(new AbstractSingleValueConverter() {
        public boolean canConvert(Class type) {
            return type.equals(DocumentFamily.class);
        }/*from  w  ww  .j  ava 2 s .  c om*/

        public Object fromString(String name) {
            return DocumentFamily.getFamily(name);
        }

        public String toString(Object object) {
            DocumentFamily family = (DocumentFamily) object;
            return family.getName();
        }
    });
    return xstream;
}

From source file:Main.java

private static Object convertParameter(String arg, Class<?> type) {
    if (type.equals(String.class)) {
        return arg;
    } else if (type.equals(Integer.class)) {
        return Integer.valueOf(arg, 10);
    } else if (type.equals(File.class)) {
        return new File(arg);
    } else {/*w w  w.  j a  v a  2  s  . co  m*/
        throw new UnsupportedOperationException("Unknown type " + type);
    }
}

From source file:Main.java

public static boolean isSuperclass(final Class<? extends Object> superclass,
        final Class<? extends Object> clazz) {
    if (superclass.equals(clazz)) {
        return true;
    }/* w w  w.  j av a2s .  co  m*/

    // all classes are superlcass of Object
    if (clazz.equals(Object.class)) {
        return false;
    }

    return isSuperclass(superclass, clazz.getSuperclass());
}

From source file:Main.java

/**
 * This method converts a given number into a target class. This method does not change the value (except when
 * explicitly casting to a more general type, e.g. from double to int), just the internal type representation. While
 * this is unnecessary while using normal java code, reflection based access to method parameters is a bit more
 * difficult. As far as possible, this method will prevent the ArgumentMismatch error when passing numbers as
 * parameters./*from  w ww.jav a 2  s  .c om*/
 * <p/>
 * If the value can not be converted to the given target class, it will be returned unchanged.
 *
 * @param targetClass Class to which the number should be converted, if possible.
 * @param value       Number value to convert.
 * @return 'value' converted to an instance of 'targetClass'.
 */
public static Object convertNumber(final Class targetClass, final Number value) {
    if (targetClass.equals(Double.class) || targetClass.equals(Double.TYPE))
        return value.doubleValue();
    if (targetClass.equals(Integer.class) || targetClass.equals(Integer.TYPE))
        return value.intValue();
    if (targetClass.equals(Long.class) || targetClass.equals(Long.TYPE))
        return value.longValue();
    if (targetClass.equals(Short.class) || targetClass.equals(Short.TYPE))
        return value.shortValue();
    if (targetClass.equals(Byte.class) || targetClass.equals(Byte.TYPE))
        return value.byteValue();
    if (targetClass.equals(Character.class) || targetClass.equals(Character.TYPE))
        return value.intValue();
    if (targetClass.equals(Float.class) || targetClass.equals(Float.TYPE))
        return value.floatValue();
    return value;
}

From source file:Main.java

public static <T> T generateRandom(Class<T> objectClass) {
    Random r = new Random();
    if (objectClass.equals(String.class)) {
        String s = "";
        for (int i = 0; i < 10; i++) {
            char c = (char) (Math.abs(r.nextInt()) % ('Z' - 'A') + 'A');
            s = s + c;//  w  w  w .  j ava 2  s .  c  om
        }
        return objectClass.cast(s);
    } else if (objectClass.equals(Integer.class)) {
        Integer s = r.nextInt();
        return objectClass.cast(s);
    } else if (objectClass.equals(Long.class)) {
        Long s = r.nextLong();
        return objectClass.cast(s);
    } else if (objectClass.equals(java.util.Date.class)) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.set(java.util.Calendar.MONTH, Math.abs(r.nextInt()) % 12);
        c.set(java.util.Calendar.DAY_OF_MONTH, Math.abs(r.nextInt()) % 30);
        return objectClass.cast(c.getTime());
    }
    return null;
}

From source file:cz.muni.fi.pa165.creatures.rest.client.utils.DTOBuilder.java

public static AbstractDTO get(Class<?> clazz, CommandLine line) {
    if (clazz.equals(WeaponDTO.class)) {
        WeaponDTO dto = new WeaponDTO();
        if (line.hasOption("n")) {
            dto.setName(line.getOptionValue("n"));
        }//from   www.ja  v  a2  s .  co m
        if (line.hasOption("m")) {
            dto.setAmmunition(new Integer(line.getOptionValue("m")));
        }
        if (line.hasOption("g")) {
            dto.setRange(new Integer(line.getOptionValue("g")));
        }
        if (line.hasOption("i")) {
            dto.setId(line.getOptionValue("i"));
        }
        return dto;
    } else if (clazz.equals(RegionDTO.class)) {
        RegionDTO dto = new RegionDTO();
        if (line.hasOption("n")) {
            dto.setName(line.getOptionValue("n"));
        }
        if (line.hasOption("d")) {
            dto.setDescription(line.getOptionValue("d"));
        }
        if (line.hasOption("a")) {
            dto.setArea(Long.parseLong(line.getOptionValue("a")));
        }
        if (line.hasOption("i")) {
            dto.setId(line.getOptionValue("i"));
        }
        return dto;
    } else {
        return null;
    }
}

From source file:io.github.benas.jpopulator.randomizers.validation.MaxValueRandomizer.java

/**
 * Generate a random value for the given type.
 *
 * @param type the type for which a random value will be generated
 * @param maxValue the maximum threshold for the generated value
 * @return a random value (lower than maxValue) for the given type or null if the type is not supported
 *///from w w w  .ja v a 2 s .  c  om
public static Object getRandomValue(final Class type, final long maxValue) {

    if (type.equals(Byte.TYPE) || type.equals(Byte.class)) {
        return (byte) randomDataGenerator.nextLong(Byte.MIN_VALUE, maxValue);
    }
    if (type.equals(Short.TYPE) || type.equals(Short.class)) {
        return (short) randomDataGenerator.nextLong(Short.MIN_VALUE, maxValue);
    }
    if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
        return (int) randomDataGenerator.nextLong(Integer.MIN_VALUE, maxValue);
    }
    if (type.equals(Long.TYPE) || type.equals(Long.class)) {
        return randomDataGenerator.nextLong(Long.MIN_VALUE, maxValue);
    }
    if (type.equals(BigInteger.class)) {
        return new BigInteger(String.valueOf(randomDataGenerator.nextLong(Long.MIN_VALUE, maxValue)));
    }
    if (type.equals(BigDecimal.class)) {
        return new BigDecimal(randomDataGenerator.nextLong(Long.MIN_VALUE, maxValue));
    }
    return null;
}