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.expressui.core.util.BeanPropertyType.java

private static BeanPropertyType getBeanPropertyTypeImpl(Class clazz, String propertyPath) {
    String[] properties = propertyPath.split("\\.");
    Class containingType;//from  w w  w  .jav  a 2 s.  co m
    Class currentPropertyType = clazz;
    BeanPropertyType beanPropertyType = null;
    for (String property : properties) {
        Class propertyType = BeanUtils.findPropertyType(property, new Class[] { currentPropertyType });
        Assert.PROGRAMMING.isTrue(propertyType != null && !propertyType.equals(Object.class),
                "Invalid property path: " + clazz + "." + propertyPath);

        Class propertyPathType;
        Class collectionValueType = null;
        if (Collection.class.isAssignableFrom(propertyType)) {
            collectionValueType = ReflectionUtil.getCollectionValueType(currentPropertyType, property);
            propertyPathType = collectionValueType;
        } else {
            propertyPathType = propertyType;
        }

        containingType = currentPropertyType;
        currentPropertyType = propertyPathType;

        beanPropertyType = new BeanPropertyType(beanPropertyType, property, propertyType, containingType,
                collectionValueType);
    }

    return beanPropertyType;
}

From source file:cn.org.awcp.core.mybatis.mapper.EntityHelper.java

/**
 * ?Field// w  ww  .  j a  v  a 2  s . com
 * 
 * @param entityClass
 * @param fieldList
 * @return
 */
private static List<Field> getAllField(Class<?> entityClass, List<Field> fieldList) {
    if (fieldList == null) {
        fieldList = new ArrayList<Field>();
    }
    if (entityClass.equals(Object.class)) {
        return fieldList;
    }
    Field[] fields = entityClass.getDeclaredFields();
    for (Field field : fields) {
        // ??bug#2
        if (!Modifier.isStatic(field.getModifiers())) {
            fieldList.add(field);
        }
    }
    if (entityClass.getSuperclass() != null && !entityClass.getSuperclass().equals(Object.class)
            && !Map.class.isAssignableFrom(entityClass.getSuperclass())
            && !Collection.class.isAssignableFrom(entityClass.getSuperclass())) {
        return getAllField(entityClass.getSuperclass(), fieldList);
    }
    return fieldList;
}

From source file:com.mawujun.utils.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 w  w.j  a va2 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 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
 * @see Class#isAnnotationPresent(Class)
 * @see Class#getDeclaredAnnotations()
 * @see #findAnnotationDeclaringClass(Class, Class)
 * @see #isAnnotationDeclaredLocally(Class, Class)
 * @since 3.2.2
 */
public static Class<?> findAnnotationDeclaringClassForTypes(List<Class<? extends Annotation>> annotationTypes,
        Class<?> clazz) {
    Assert.notEmpty(annotationTypes, "The list of annotation types must not be empty");
    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.qingstor.sdk.utils.QSJSONUtil.java

private static void setParameterToMap(Method m, Object source, Field f, Object data) {
    if (data != null) {
        try {/* ww  w  .j  a v  a 2s  .c om*/
            if (data instanceof JSONArray || data instanceof JSONObject) {
                Class fClass = f.getType();
                if (fClass.equals(List.class)) {
                    List invokeData = new ArrayList();
                    ParameterizedType stringListType = (ParameterizedType) f.getGenericType();
                    Class<?> cls = (Class<?>) stringListType.getActualTypeArguments()[0];

                    if (cls.equals(String.class) || cls.equals(Integer.class) || cls.equals(Double.class)
                            || cls.equals(Long.class) || cls.equals(Float.class)) {
                        if (data instanceof JSONArray) {
                            JSONArray jsonData = (JSONArray) data;
                            for (int i = 0; i < jsonData.length(); i++) {

                                Object o = toObject(jsonData, i);
                                invokeData.add(o);
                            }
                        }
                    } else {
                        if (data instanceof JSONArray) {
                            JSONArray jsonData = (JSONArray) data;
                            for (int i = 0; i < jsonData.length(); i++) {
                                Object fObject = cls.newInstance();
                                JSONObject o = toJSONObject(jsonData, i);
                                Class tmpClass = fObject.getClass();
                                while (tmpClass != Object.class) {
                                    Field[] fields = tmpClass.getDeclaredFields();
                                    initParameter(o, fields, tmpClass, fObject);
                                    tmpClass = tmpClass.getSuperclass();
                                }
                                invokeData.add(fObject);
                            }
                        }
                    }

                    m.invoke(source, invokeData);

                } else if (fClass.equals(Map.class)) {
                    Map invokeData = new HashMap();
                    if (data instanceof JSONObject) {
                        JSONObject jsonData = (JSONObject) data;
                        for (int i = 0; i < jsonData.length(); i++) {
                            String key = toJSONObject(jsonData, i) + "";
                            Object value = toJSONObject(jsonData, key);
                            invokeData.put(key, value);
                        }
                    }
                    m.invoke(source, invokeData);
                } else {

                    Object invokeData = f.getType().newInstance();
                    Class tmpClass = invokeData.getClass();
                    while (tmpClass != Object.class) {
                        Field[] fields = tmpClass.getDeclaredFields();
                        initParameter((JSONObject) data, fields, tmpClass, invokeData);
                        tmpClass = tmpClass.getSuperclass();
                    }
                    m.invoke(source, invokeData);
                }

            } else {
                if (f.getType().equals(data.getClass())) {
                    m.invoke(source, data);
                } else {
                    m.invoke(source, getParseValue(f.getType(), data));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.plusub.lib.annotate.JsonParserUtils.java

/**
 * ??//from www. j  a va2 s  . c o  m
 * <p>Title: parserField
 * <p>Description: 
 * @param obj
 * @param jsonObj
 * @return
 * @throws Exception
 */
private static Object parserField(Object obj, JSONObject jsonObj) throws Exception {
    Field[] fields = obj.getClass().getDeclaredFields();
    if (fields != null && fields.length > 0) {
        for (Field field : fields) {
            JsonParserField jsonField = field.getAnnotation(JsonParserField.class);

            //JsonParserField
            if (jsonField != null) {
                String keyName = jsonField.praserKey(); //json
                String defaultValue = jsonField.defaultValue(); //
                boolean isList = jsonField.isList();

                //???
                String name = "";
                if (isEmpty(keyName)) {
                    name = field.getName();
                } else {
                    name = keyName;
                }

                try {
                    field.setAccessible(true);
                    //?
                    if (jsonObj.has(name)) {
                        if (!isList) { //?
                            setSingleValue(obj, field, jsonObj, name);
                        } else { //
                            Class listCls = jsonField.classType();
                            if (listCls.equals(Object.class)) {
                                if (showLog)
                                    Logger.e(TAG, "" + field.getName()
                                            + "classType?" + keyName);
                            }
                            JSONArray ja = null;
                            if (isEmpty(keyName)) {
                                ja = JSONUtils.getJSONArray(jsonObj, field.getName(), null);
                            } else {
                                ja = JSONUtils.getJSONArray(jsonObj, keyName, null);
                            }
                            if (ja != null) {
                                //
                                int size = ja.length();
                                Object[] data = new Object[size];

                                //?
                                for (int index = 0; index < size; index++) {
                                    Object oj = parserField(getInstance(listCls.getName()),
                                            ja.getJSONObject(index));
                                    data[index] = oj;
                                }

                                //?
                                Object listObj = field.getType();
                                if (listObj.equals(List.class)) {
                                    setFieldValue(obj, field, Arrays.asList(data));
                                } else {
                                    setFieldValue(obj, field, data);
                                }
                            }
                        }
                    } else {
                        setFieldValue(obj, field, defaultValue);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(
                            "parser " + obj.getClass().getName() + " error! \n" + e.getMessage());
                }
            } else { //JsonParserField
                setSingleValue(obj, field, jsonObj, field.getName());
            }
        }
    }
    return obj;
}

From source file:com.github.dozermapper.core.util.ReflectionUtils.java

/**
 * Checks if classA or classB can be auto boxed by the JVM
 *
 * @return {@code true}, if both classes are either primitive or wrapper classes and
 * autoboxing is possible between {@code classA} and {@code classB}
 *///from w w w  . ja  va  2  s  .  com
private static boolean canBeAutoboxed(Class<?> classA, Class<?> classB) {
    return ClassUtils.isPrimitiveOrWrapper(classA) && ClassUtils.isPrimitiveOrWrapper(classB)
            && (classB.equals(classA) // Same types
                    || ClassUtils.primitiveToWrapper(classB).equals(classA) // Matching primitive-wrapper pair, e.g. double - Double
                    || ClassUtils.primitiveToWrapper(classA).equals(classB)); // Matching wrapper-primitive pair, e.g. Long - long
}

From source file:io.werval.runtime.util.TypeResolver.java

/**
 * Resolves the generic Type for the {@code targetType} by walking the type hierarchy upwards from the
 * {@code initialType}./* w w  w . ja va2s.c o  m*/
 *
 * @param initialType Initial type
 * @param targetType  Target type
 *
 * @return Generic type
 */
public static Type resolveGenericType(Type initialType, Class<?> targetType) {
    Class<?> rawType;
    if (initialType instanceof ParameterizedType) {
        rawType = (Class<?>) ((ParameterizedType) initialType).getRawType();
    } else {
        rawType = (Class<?>) initialType;
    }

    if (targetType.equals(rawType)) {
        return initialType;
    }

    Type result;
    if (targetType.isInterface()) {
        for (Type superInterface : rawType.getGenericInterfaces()) {
            if (superInterface != null && !superInterface.equals(Object.class)) {
                result = resolveGenericType(superInterface, targetType);
                if (result != null) {
                    return result;
                }
            }
        }
    }

    Type superType = rawType.getGenericSuperclass();
    if (superType != null && !superType.equals(Object.class)) {
        result = resolveGenericType(superType, targetType);
        if (result != null) {
            return result;
        }
    }

    return null;
}

From source file:com.doitnext.jsonschema.generator.SchemaGen.java

private static boolean handleSimpleType(Class<?> classz, StringBuilder sb,
        JsonSchemaProperty propertyDescriptor, Map<String, String> declarations, String uriPrefix) {
    boolean result = false;
    if (classz.equals(String.class)) {
        sb.append("\"type\":\"string\"");
        result = true;/* w  w w  .  ja  v a  2 s.c om*/
    } else if (classz.equals(Float.class)) {
        sb.append("\"type\":\"number\"");
        result = true;
    } else if (classz.equals(Double.class)) {
        sb.append("\"type\":\"number\"");
        result = true;
    } else if (classz.equals(Integer.class)) {
        sb.append("\"type\":\"integer\"");
        result = true;
    } else if (classz.equals(Long.class)) {
        sb.append("\"type\":\"long\"");
        result = true;
    } else if (classz.equals(Boolean.class)) {
        sb.append("\"type\":\"boolean\"");
        result = true;
    }
    if (result) {
        Map<String, Boolean> flags = new HashMap<String, Boolean>();
        flags.put("hasTitle", false);
        flags.put("hasDescription", false);
        flags.put("hasType", true);
        flags.put("hasEnum", false);
        if (handlePropertyDescriptor(sb, propertyDescriptor, flags, ", ", result, uriPrefix)) {
            result = true;
        }
    }

    return result;
}

From source file:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java

private static boolean containsAnnotation(Annotation[] annotations, Class<?> annotation) {
    for (Annotation a : annotations) {
        if (annotation.equals(a.annotationType()))
            return true;
    }/*from w  w  w .j av a2 s . c  o m*/
    return false;
}

From source file:com.all.shared.sync.SyncGenericConverter.java

private static Object readValue(String attributeName, Class<?> clazz, Map<String, Object> map)
        throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException,
        InvocationTargetException, NoSuchMethodException {
    Object value = map.get(attributeName);
    if (value != null && !clazz.equals(value.getClass())) {
        if (clazz.equals(Date.class) && StringUtils.isNumeric(value.toString())) {
            return new Date(new Long(value.toString()));
        }//from   ww  w. ja va  2  s .c  om
        if (clazz.isEnum() && value instanceof String) {
            for (Object enumType : clazz.getEnumConstants()) {
                if (value.equals(enumType.toString())) {
                    return enumType;
                }
            }
        }
        if (PRIMITIVES.contains(clazz)) {
            return clazz.getConstructor(String.class).newInstance(value.toString());
        }
    }
    return value;
}