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:org.shept.util.BeanUtilsExtended.java

/**
 * Find all occurrences of targetClass in targetObject.
 * Be careful. This stuff may be recursive !
 * Should be improved to prevent endless recursive loops.
 * //  w  w  w.  j  av  a 2 s .  c om
 * @param
 * @return
 *
 * @param targetObject
 * @param targetClass
 * @param nestedPath
 * @return
 * @throws Exception 
 */
public static Map<String, ?> findNestedPaths(Object targetObject, Class<?> targetClass, String nestedPath,
        List<String> ignoreList, Set<Object> visited, int maxDepth) throws Exception {

    Assert.notNull(targetObject);
    Assert.notNull(targetClass);
    Assert.notNull(ignoreList);
    HashMap<String, Object> nestedPaths = new HashMap<String, Object>();
    if (maxDepth <= 0) {
        return nestedPaths;
    }

    nestedPath = (nestedPath == null ? "" : nestedPath);
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(targetObject);
    PropertyDescriptor[] props = bw.getPropertyDescriptors();

    for (PropertyDescriptor pd : props) {
        Class<?> clazz = pd.getPropertyType();
        if (clazz != null && !clazz.equals(Class.class) && !clazz.isAnnotation() && !clazz.isPrimitive()) {
            Object value = null;
            String pathName = pd.getName();
            if (!ignoreList.contains(pathName)) {
                try {
                    value = bw.getPropertyValue(pathName);
                } catch (Exception e) {
                } // ignore any exceptions here
                if (StringUtils.hasText(nestedPath)) {
                    pathName = nestedPath + PropertyAccessor.NESTED_PROPERTY_SEPARATOR + pathName;
                }
                // TODO break up this stuff into checking and excecution a la ReflectionUtils
                if (targetClass.isAssignableFrom(clazz)) {
                    nestedPaths.put(pathName, value);
                }
                // exclude objects already visited from further inspection to prevent circular references
                // unfortunately this stuff isn't fool proof as there are ConcurrentModificationExceptions
                // when adding objects to the visited list
                if (value != null && !isInstanceVisited(visited, value)) {
                    nestedPaths.putAll(
                            findNestedPaths(value, targetClass, pathName, ignoreList, visited, maxDepth - 1));
                }
            }
        }
    }
    return nestedPaths;
}

From source file:edu.sdsc.scigraph.neo4j.GraphUtil.java

static Object getNewPropertyValue(Object originalValue, Object newValue) {
    Class<?> clazz = checkNotNull(newValue).getClass();
    boolean reduceToString = false;
    if (null != originalValue && originalValue.getClass().isArray()) {
        Class<?> originalClazz = Array.get(originalValue, 0).getClass();
        if (!originalClazz.equals(clazz)) {
            reduceToString = true;//from w w  w.  j a v  a2s .  c  o m
            clazz = String.class;
        }
        newValue = reduceToString ? newValue.toString() : newValue;
        Object newArray = Array.newInstance(clazz, Array.getLength(originalValue) + 1);
        for (int i = 0; i < Array.getLength(originalValue); i++) {
            Object val = Array.get(originalValue, i);
            if (newValue.equals(val)) {
                return originalValue;
            }
            Array.set(newArray, i, reduceToString ? val.toString() : val);
        }
        Array.set(newArray, Array.getLength(originalValue), newValue);
        return newArray;
    } else if (null != originalValue) {
        if (!clazz.equals(originalValue.getClass())) {
            reduceToString = true;
            clazz = String.class;
        }
        originalValue = reduceToString ? originalValue.toString() : originalValue;
        newValue = reduceToString ? newValue.toString() : newValue;
        if (!originalValue.equals(newValue)) {
            Object newArray = Array.newInstance(clazz, 2);
            Array.set(newArray, 0, originalValue);
            Array.set(newArray, 1, newValue);
            return newArray;
        } else {
            return originalValue;
        }
    } else {
        return newValue;
    }
}

From source file:ca.uhn.fhir.rest.param.ParameterUtil.java

public static Object fromInteger(Class<?> theType, IntegerDt theArgument) {
    if (theType.equals(IntegerDt.class)) {
        if (theArgument == null) {
            return new IntegerDt();
        }/*from   w ww .j ava  2 s.c  o m*/
        return theArgument;
    }
    if (theType.equals(Integer.class)) {
        if (theArgument == null) {
            return null;
        }
        return theArgument.getValue();
    }
    throw new IllegalArgumentException("Invalid Integer type:" + theType);
}

From source file:Main.java

/**
 * @param aContainer/*from   w w  w.j a  v  a2  s  . c o  m*/
 * @param aComponentClass
 * @return
 */
private static Component findComponent(final Container aContainer,
        final Class<? extends Component> aComponentClass) {
    Component result = null;

    final int cnt = aContainer.getComponentCount();
    for (int i = 0; (result == null) && (i < cnt); i++) {
        final Component comp = aContainer.getComponent(i);
        if (aComponentClass.equals(comp.getClass())) {
            result = comp;
        } else if (comp instanceof Container) {
            result = findComponent((Container) comp, aComponentClass);
        }
    }
    return result;
}

From source file:ca.uhn.fhir.rest.param.ParameterUtil.java

public static Object fromInstant(Class<?> theType, InstantDt theArgument) {
    if (theType.equals(InstantDt.class)) {
        if (theArgument == null) {
            return new InstantDt();
        }//from   w  w w .jav  a2s  .c  om
        return theArgument;
    }
    if (theType.equals(Date.class)) {
        if (theArgument == null) {
            return null;
        }
        return theArgument.getValue();
    }
    if (theType.equals(Calendar.class)) {
        if (theArgument == null) {
            return null;
        }
        return DateUtils.toCalendar(theArgument.getValue());
    }
    throw new IllegalArgumentException("Invalid instant type:" + theType);
}

From source file:io.orchestrate.client.ResponseConverterUtil.java

@SuppressWarnings("unchecked")
static <T> T jsonToDomainObject(ObjectMapper mapper, JsonNode json, String rawValue, Class<T> clazz)
        throws IOException {
    if (clazz == null || clazz == Void.class || json == null || json.isNull()) {
        return null;
    }/* w  w  w  .ja  va  2s.  co  m*/

    if (clazz.equals(String.class)) {
        if (rawValue != null) {
            return (T) rawValue;
        }
        return (T) mapper.writeValueAsString(json);
    }
    return mapper.treeToValue(json, clazz);
}

From source file:com.zhuangjy.dao.ReflectionUtil.java

private static void getFieldsByAnnotation(List<Field> list, Class<? extends Annotation> annotation, Class clz) {
    if (clz.equals(Object.class)) {
        return;/*from   w w  w  . j  a v a 2s .  c  om*/
    }
    Field[] fields = clz.getDeclaredFields();
    if (fields == null || fields.length == 0) {
        return;
    }
    for (Field f : fields) {
        f.setAccessible(true);
        if (f.getAnnotation(annotation) != null) {
            list.add(f);
        }
    }
}

From source file:com.jaspersoft.jasperserver.remote.utils.RepositoryHelper.java

protected static Object getMultiParameterValues(JRParameter parameter, Collection values) {
    Object parameterValue;//  w ww .j ava  2  s.  c o m
    Class parameterType = parameter.getValueClass();
    if (parameterType.equals(Object.class) || parameterType.equals(Collection.class)
            || parameterType.equals(Set.class) || parameterType.equals(List.class)) {
        Collection paramValues;
        if (parameterType.equals(List.class)) {
            //if the parameter type is list, use a list
            paramValues = new ArrayList(values.size());
        } else {
            //else use an ordered set
            paramValues = new ListOrderedSet();
        }

        Class componentType = parameter.getNestedType();
        for (Iterator it = values.iterator(); it.hasNext();) {
            Object val = (Object) it.next();
            Object paramValue;
            if (componentType == null || !(val instanceof String)) {
                //no conversion if no nested type set for the parameter
                paramValue = val;
            } else {
                paramValue = stringToValue((String) val, componentType);
            }
            paramValues.add(paramValue);
        }
        parameterValue = paramValues;
    } else if (parameterType.isArray()) {
        Class componentType = parameterType.getComponentType();
        parameterValue = Array.newInstance(componentType, values.size());
        int idx = 0;
        for (Iterator iter = values.iterator(); iter.hasNext(); ++idx) {
            Object val = iter.next();
            Object paramValue;
            if (val instanceof String) {
                paramValue = stringToValue((String) val, componentType);
            } else {
                paramValue = val;
            }
            Array.set(parameterValue, idx, paramValue);
        }
    } else {
        parameterValue = values;
    }
    return parameterValue;
}

From source file:net.ostis.sc.memory.SCKeynodesBase.java

protected static void init(SCSession session, Class<? extends SCKeynodesBase> klass) {
    if (log.isDebugEnabled())
        log.debug("Start retrieving keynodes for " + klass);

    try {//from   w  ww  .ja  va2s  .c o m
        //
        // Search default segment for keynodes
        //
        SCSegment defaultSegment = null;
        {
            DefaultSegment annotation = klass.getAnnotation(DefaultSegment.class);
            if (annotation != null) {
                defaultSegment = session.openSegment(annotation.value());
                Validate.notNull(defaultSegment, "Default segment \"{0}\" not found", annotation.value());
                klass.getField(annotation.fieldName()).set(null, defaultSegment);
            }
        }

        Field[] fields = klass.getFields();
        for (Field field : fields) {
            int modifiers = field.getModifiers();

            if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) {
                Class<?> type = field.getType();
                if (type.equals(SCSegment.class)) {
                    //
                    // We have segment field. Load segment by uri.
                    //
                    SegmentURI annotation = field.getAnnotation(SegmentURI.class);

                    if (annotation != null) {
                        String uri = annotation.value();
                        SCSegment segment = session.openSegment(uri);
                        field.set(null, segment);
                    } else {
                        // May be it already has value?
                        if (log.isWarnEnabled()) {
                            if (field.get(null) == null)
                                log.warn(field + " doesn't have value");
                        }
                    }
                } else {
                    if (!(checkKeynode(session, defaultSegment, field) || checkKeynodeURI(session, field)
                            || checkKeynodesNumberPatternURI(session, klass, field))) {

                        if (log.isWarnEnabled()) {
                            if (field.get(null) == null)
                                log.warn(field + " doesn't have annotations and value");
                        }

                    }
                }
            }
        }
    } catch (Exception e) {
        // TODO: handle
        e.printStackTrace();
    }
}

From source file:io.orchestrate.client.ResponseConverterUtil.java

@SuppressWarnings("unchecked")
@Deprecated//from  ww  w .  j  a v  a 2 s . c  om
public static <T> T jsonToDomainObject(ObjectMapper mapper, String rawValue, Class<T> clazz)
        throws IOException {
    if (clazz == null || clazz == Void.class || rawValue == null || rawValue.isEmpty()) {
        return null;
    }

    if (clazz.equals(String.class)) {
        return (T) rawValue;
    }
    return mapper.readValue(rawValue, clazz);
}