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.plusub.lib.annotate.JsonParserUtils.java

/**
 * ?//from  w  ww. j a v a2s  .  co  m
 * <p>Title: getType
 * <p>Description: 
 * @param field ?
 * @param defaultValue (Json?String)
 * @return defaultValue?obj
 */
private static Object getType(Field field, Object defaultValue, String fieldName) {
    Object value = defaultValue;
    if (showLog) {
        Logger.i(TAG,
                "getType:" + field.getName() + " " + field.getType().getName() + " " + " " + defaultValue);
    }
    if (defaultValue == null) {
        return value;
    }

    String type = field.getType().getName();
    Class clazz = field.getType();
    try {
        if (isBaseDataType(field.getType())) {
            String str = defaultValue + "";
            if (clazz.equals(String.class)) {
                value = defaultValue;
            } else if (clazz.equals(Integer.class) || type.equals("int")) {
                value = Integer.parseInt(str);
            } else if (clazz.equals(Boolean.class) || type.equals("boolean")) {
                String defaultStr = str;
                String result = defaultStr.toLowerCase();
                if (result.equals("true")) {
                    value = true;
                } else if (result.equals("false")) {
                    value = false;
                } else {
                    value = Integer.parseInt(result) > 0 ? true : false;
                }
            } else if (clazz.equals(Double.class) || type.equals("double")) {
                value = Double.parseDouble(str);
            } else if (clazz.equals(Float.class) || type.equals("float")) {
                value = Float.parseFloat(str);
            } else if (clazz.equals(Short.class) || type.equals("short")) {
                value = Short.parseShort(str);
            } else if (clazz.equals(Long.class) || type.equals("long")) {
                value = Long.parseLong(str);
            } else if (clazz.equals(Byte.class) || type.equals("byte")) {
                value = Byte.parseByte(str);
            }
        } else { //?
            if (showLog) {
                Logger.i(TAG, "?, " + defaultValue);
            }
        }
    } catch (Exception e) {
        // TODO: handle exception
        if (showLog) {
            Logger.e(TAG, "?" + fieldName + "String-->" + field.getType().getName()
                    + ", " + value);
            e.printStackTrace();
        }
        return value;
    }
    return value;
}

From source file:ca.uhn.fhir.rest.method.BaseMethodBinding.java

private static boolean isResourceInterface(Class<?> theReturnTypeFromMethod) {
    return theReturnTypeFromMethod.equals(IBaseResource.class)
            || theReturnTypeFromMethod.equals(IResource.class)
            || theReturnTypeFromMethod.equals(IAnyResource.class);
}

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

@SuppressWarnings("unchecked")
private static Collection<?> prepareIndexedCollectionType(Class<?> collectionType, Object existingCollection,
        Object collectionEntry, int index) {
    Collection result = null;//ww  w  . j a v a  2  s.c  o m
    //Instantiation of the new Collection: can be interface or implementation class
    if (collectionType.isInterface()) {
        if (collectionType.equals(Set.class)) {
            result = new HashSet();
        } else if (collectionType.equals(List.class)) {
            result = new ArrayList();
        } else {
            throwMappingException(
                    "Only interface types java.util.Set and java.util.List are supported for java.util.Collection type indexed properties.");
        }
    } else {
        //It is an implementation class of Collection
        try {
            result = (Collection) collectionType.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //Fill in old values in new Collection
    if (existingCollection != null) {
        result.addAll((Collection) existingCollection);
    }

    //Add the new value:
    //For an ordered Collection
    if (result instanceof List) {
        while (result.size() < index + 1) {
            result.add(null);
        }
        ((List) result).set(index, collectionEntry);
    } else {
        //for an unordered Collection (index has no use here)
        result.add(collectionEntry);
    }
    return result;
}

From source file:cf.spring.servicebroker.AccessorUtils.java

/**
 * Finds a method annotated with the specified annotation. This method can
 * be defined in the specified class, or one of its parents.
 *
 * @return the matching method, or <tt>null</tt> if any
 *//*w w  w. ja v  a2 s .  c om*/
public static <T extends Annotation> Method findMethodWithAnnotation(Class<?> clazz, Class<T> annotationType) {
    Method annotatedMethod = null;
    for (Method method : clazz.getDeclaredMethods()) {
        T annotation = AnnotationUtils.findAnnotation(method, annotationType);
        if (annotation != null) {
            if (annotatedMethod != null) {
                throw new BeanCreationException("Only ONE method with @" + annotationType.getName()
                        + " is allowed on " + clazz.getName() + ".");
            }
            annotatedMethod = method;
        }
    }

    if ((annotatedMethod != null) || clazz.equals(Object.class)) {
        return annotatedMethod;
    } else {
        return findMethodWithAnnotation(clazz.getSuperclass(), annotationType);
    }
}

From source file:com.evolveum.midpoint.prism.xml.XmlTypeConverter.java

@SuppressWarnings("unchecked")
public static <T> T toJavaValue(String stringContent, Class<T> type, boolean exceptionOnUnknown) {
    if (type.equals(String.class)) {
        return (T) stringContent;
    } else if (type.equals(char.class)) {
        return (T) (new Character(stringContent.charAt(0)));
    } else if (type.equals(File.class)) {
        return (T) new File(stringContent);
    } else if (type.equals(Integer.class)) {
        return (T) Integer.valueOf(stringContent);
    } else if (type.equals(int.class)) {
        return (T) Integer.valueOf(stringContent);
    } else if (type.equals(Short.class) || type.equals(short.class)) {
        return (T) Short.valueOf(stringContent);
    } else if (type.equals(Long.class)) {
        return (T) Long.valueOf(stringContent);
    } else if (type.equals(long.class)) {
        return (T) Long.valueOf(stringContent);
    } else if (type.equals(Byte.class)) {
        return (T) Byte.valueOf(stringContent);
    } else if (type.equals(byte.class)) {
        return (T) Byte.valueOf(stringContent);
    } else if (type.equals(float.class)) {
        return (T) Float.valueOf(stringContent);
    } else if (type.equals(Float.class)) {
        return (T) Float.valueOf(stringContent);
    } else if (type.equals(double.class)) {
        return (T) Double.valueOf(stringContent);
    } else if (type.equals(Double.class)) {
        return (T) Double.valueOf(stringContent);
    } else if (type.equals(BigInteger.class)) {
        return (T) new BigInteger(stringContent);
    } else if (type.equals(BigDecimal.class)) {
        return (T) new BigDecimal(stringContent);
    } else if (type.equals(byte[].class)) {
        byte[] decodedData = Base64.decodeBase64(stringContent);
        return (T) decodedData;
    } else if (type.equals(boolean.class) || Boolean.class.isAssignableFrom(type)) {
        // TODO: maybe we will need more inteligent conversion, e.g. to trim spaces, case insensitive, etc.
        return (T) Boolean.valueOf(stringContent);
    } else if (type.equals(GregorianCalendar.class)) {
        return (T) getDatatypeFactory().newXMLGregorianCalendar(stringContent).toGregorianCalendar();
    } else if (XMLGregorianCalendar.class.isAssignableFrom(type)) {
        return (T) getDatatypeFactory().newXMLGregorianCalendar(stringContent);
    } else if (Duration.class.isAssignableFrom(type)) {
        return (T) getDatatypeFactory().newDuration(stringContent);
    } else if (type.equals(PolyString.class)) {
        return (T) new PolyString(stringContent);
    } else if (type.equals(ItemPath.class)) {
        throw new UnsupportedOperationException("Path conversion not supported yet");
    } else {//  w w  w .j  a  v a2 s.co  m
        if (exceptionOnUnknown) {
            throw new IllegalArgumentException("Unknown conversion type " + type);
        } else {
            return null;
        }
    }
}

From source file:com.qualogy.qafe.core.errorhandling.ErrorProcessor.java

private static Map<String, Class<?>> resolveExceptionClassHierarchy(ExternalException externalException) {
    Map<String, Class<?>> exceptionClassHierarchy = new LinkedHashMap<String, Class<?>>();
    if (externalException != null) {
        Class<?> exceptionClass = externalException.getCause().getClass();
        while (exceptionClass != null) {
            String qualifiedClassName = exceptionClass.getName();
            exceptionClassHierarchy.put(qualifiedClassName, exceptionClass);
            exceptionClass = exceptionClass.getSuperclass();
            if (exceptionClass.equals(Object.class)) {
                exceptionClass = null;/*w ww.j a va2s  . c  o m*/
            }
        }
    }
    return exceptionClassHierarchy;
}

From source file:com.unboundid.scim2.common.utils.SchemaUtils.java

/**
 * This method will determine if this attribute can have multieple
 * values, and set that in the builder.//from  w ww .  jav a  2  s.c  o m
 *
 * @param attributeBuilder builder for a scim attribute.
 * @param propertyDescriptor property descriptor for the field to build
 *                           the attribute for.
 * @param schemaProperty the schema property annotation for the field
 *                       to build an attribute for.
 * @return this.
 */
private static AttributeDefinition.Builder addMultiValued(final AttributeDefinition.Builder attributeBuilder,
        final PropertyDescriptor propertyDescriptor, final Attribute schemaProperty) {
    Class<?> multiValuedClass = schemaProperty.multiValueClass();
    boolean multiValued = !multiValuedClass.equals(NullType.class);
    boolean collectionOrArray = isCollectionOrArray(propertyDescriptor.getPropertyType());

    // if the multiValuedClass attribute is present in the annotation,
    // make sure this is a collection or array.
    if (multiValued && !collectionOrArray) {
        throw new RuntimeException("Property named " + propertyDescriptor.getName()
                + " is annotated with a multiValuedClass, " + "but is not a Collection or an array");
    }

    if (!multiValued && collectionOrArray) {
        throw new RuntimeException("Property named " + propertyDescriptor.getName()
                + " is not annotated with a multiValuedClass, " + "but is a Collection or an array");
    }

    attributeBuilder.setMultiValued(multiValued);

    return attributeBuilder;
}

From source file:net.jodah.typetools.TypeResolver.java

/**
 * Populates the {@code map} with variable/argument pairs for the {@code functionalInterface}.
 *//* ww  w  . j a  v a2 s. c o m*/
private static void populateLambdaArgs(Class<?> functionalInterface, final Class<?> lambdaType,
        Map<TypeVariable<?>, Type> map) {
    if (GET_CONSTANT_POOL != null) {
        try {
            // Find SAM
            for (Method m : functionalInterface.getMethods()) {
                if (!m.isDefault() && !Modifier.isStatic(m.getModifiers()) && !m.isBridge()) {
                    // Skip methods that override Object.class
                    Method objectMethod = OBJECT_METHODS.get(m.getName());
                    if (objectMethod != null
                            && Arrays.equals(m.getTypeParameters(), objectMethod.getTypeParameters()))
                        continue;

                    // Get functional interface's type params
                    Type returnTypeVar = m.getGenericReturnType();
                    Type[] paramTypeVars = m.getGenericParameterTypes();

                    // Get lambda's type arguments
                    ConstantPool constantPool = (ConstantPool) GET_CONSTANT_POOL.invoke(lambdaType);
                    String[] methodRefInfo = constantPool.getMemberRefInfoAt(
                            constantPool.getSize() - resolveMethodRefOffset(constantPool, lambdaType));

                    // Skip auto boxing methods
                    if (methodRefInfo[1].equals("valueOf") && constantPool.getSize() > 22) {
                        try {
                            methodRefInfo = constantPool.getMemberRefInfoAt(constantPool.getSize()
                                    - resolveAutoboxedMethodRefOffset(constantPool, lambdaType));
                        } catch (MethodRefOffsetResolutionFailed ignore) {
                        }
                    }

                    if (returnTypeVar instanceof TypeVariable) {
                        Class<?> returnType = TypeDescriptor.getReturnType(methodRefInfo[2])
                                .getType(lambdaType.getClassLoader());
                        if (!returnType.equals(Void.class))
                            map.put((TypeVariable<?>) returnTypeVar, returnType);
                    }

                    TypeDescriptor[] arguments = TypeDescriptor.getArgumentTypes(methodRefInfo[2]);

                    // Handle arbitrary object instance method references
                    int paramOffset = 0;
                    if (paramTypeVars[0] instanceof TypeVariable
                            && paramTypeVars.length == arguments.length + 1) {
                        Class<?> instanceType = TypeDescriptor.getObjectType(methodRefInfo[0])
                                .getType(lambdaType.getClassLoader());
                        map.put((TypeVariable<?>) paramTypeVars[0], instanceType);
                        paramOffset = 1;
                    }

                    // Handle local final variables from context that are passed as arguments.
                    int argOffset = 0;
                    if (paramTypeVars.length < arguments.length) {
                        argOffset = arguments.length - paramTypeVars.length;
                    }

                    for (int i = 0; i + argOffset < arguments.length; i++) {
                        if (paramTypeVars[i] instanceof TypeVariable) {
                            map.put((TypeVariable<?>) paramTypeVars[i + paramOffset],
                                    arguments[i + argOffset].getType(lambdaType.getClassLoader()));
                        }
                    }
                    break;
                }
            }

        } catch (Exception ignore) {
        }
    }
}

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

public static void checkLegalCloudInvocationInfoDef(Object obj) {

    for (Field f : obj.getClass().getDeclaredFields()) {
        if (f.getAnnotation(CloudInvocationInfos.class) != null) {

            Type[] genericTypes = ((ParameterizedType) f.getGenericType()).getActualTypeArguments();
            Class<?> typeParameter = (Class<?>) genericTypes[0];

            if (!f.getType().equals(List.class) || genericTypes.length != 1
                    || !typeParameter.equals(InvocationInfo.class))
                throw new IllegalDefinitionException("Illegal field type " + f.getType().getName()
                        + " annotated with "
                        + "@CloudInvocationInfos. You may only annotate java.util.List<InvocationInfo> fields.");

            if (Modifier.isStatic(f.getModifiers()))
                throw new IllegalDefinitionException("Illegal field " + f.getName()
                        + " annotated with @CloudInvocationInfos. " + "Field has to be non-static.");

        }//w w  w .  ja v  a 2 s .c  o  m
    }

}

From source file:org.paxml.util.ReflectUtils.java

/**
 * Check if a class is subclassing another class.
 * /*from  w  w w  .ja  v a2  s . c o  m*/
 * @param subClass
 *            the sub class
 * @param superClass
 *            the super class
 * @param matchNameOnly
 *            true to only do name string comparison, false also compares
 *            the class loader.
 * @return true if yes, false not
 */
public static boolean isSubClass(Class<?> subClass, final Class<?> superClass, final boolean matchNameOnly) {
    if (subClass.equals(superClass)) {
        return false;
    }
    if (superClass.equals(Object.class)) {
        return true;
    }
    Object result = traverseInheritance(subClass, superClass, subClass.isInterface(),
            new IClassVisitor<Object>() {

                public Object onVisit(Class<?> clazz) {
                    if (matchNameOnly) {
                        return clazz.getName().equals(superClass.getName()) ? new Object() : null;
                    } else {
                        return clazz.equals(superClass) ? new Object() : null;
                    }
                }

            });
    return result != null;
}