Example usage for java.lang.reflect Type equals

List of usage examples for java.lang.reflect Type equals

Introduction

In this page you can find the example usage for java.lang.reflect Type 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

public static ContentValues objectToContentValues(Object object) {
    if (object == null) {
        throw new IllegalArgumentException("please check your argument");
    }//ww w.ja  va 2  s  . c  o m
    ContentValues contentValues = new ContentValues();
    try {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            String fieldName = field.getName();
            Type type = field.getType();
            field.setAccessible(true);
            if (type.equals(String.class)) {
                contentValues.put(fieldName, field.get(object).toString());
            } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
                contentValues.put(fieldName, field.getInt(object));
            } else if (type.equals(Float.class) || type.equals(Float.TYPE)) {
                contentValues.put(fieldName, field.getFloat(object));
            } else if (type.equals(Long.class) || type.equals(Long.TYPE)) {
                contentValues.put(fieldName, field.getLong(object));
            } else if (type.equals(Double.class) || type.equals(Double.TYPE)) {
                contentValues.put(fieldName, field.getDouble(object));
            } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
                contentValues.put(fieldName, field.getBoolean(object));
            }
        }
    } catch (IllegalAccessException | IllegalArgumentException e) {
        e.printStackTrace();
    }
    return contentValues;
}

From source file:org.zkoss.ganttz.timetracker.OnColumnsRowRenderer.java

private static boolean isTypeForInterface(Type type, Class<?> interfaceBeingSearched) {
    if (type instanceof ParameterizedType) {
        ParameterizedType p = (ParameterizedType) type;
        Type rawType = p.getRawType();

        return rawType.equals(interfaceBeingSearched);
    }/*from  w ww .  ja  va 2  s. c om*/

    return type.equals(interfaceBeingSearched);
}

From source file:TypeUtils.java

/**
 * Check if the right-hand side type may be assigned to the left-hand side
 * type following the Java generics rules.
 * @param lhsType the target type//from  w w  w. ja v  a  2 s  . co  m
 * @param rhsType the value type that should be assigned to the target type
 * @return true if rhs is assignable to lhs
 */
public static boolean isAssignable(Type lhsType, Type rhsType) {

    if (lhsType.equals(rhsType)) {
        return true;
    }
    if (lhsType instanceof Class && rhsType instanceof Class) {
        return ClassUtils.isAssignable((Class) lhsType, (Class) rhsType);
    }
    if (lhsType instanceof ParameterizedType && rhsType instanceof ParameterizedType) {
        return isAssignable((ParameterizedType) lhsType, (ParameterizedType) rhsType);
    }
    if (lhsType instanceof WildcardType) {
        return isAssignable((WildcardType) lhsType, rhsType);
    }
    return false;
}

From source file:org.romaframework.aspect.view.ViewHelper.java

public static Type getEmbeddedType(SchemaField iField) throws ConfigurationException {
    Type embeddedType = SchemaHelper.getEmbeddedType(iField);

    if (embeddedType.equals(Object.class)) {
        String selectionFieldName = (String) iField.getFeature(ViewFieldFeatures.SELECTION_FIELD);
        // NO EMBEDDED TYPE SETTED: TRY TO DETERMINE IT BY SELECTION FIELD
        if (selectionFieldName != null) {
            SchemaField selectionField = SchemaHelper.getFieldName(iField.getEntity(), selectionFieldName);

            if (selectionField == null) {
                throw new ConfigurationException("Cannot find the selection field called " + selectionFieldName
                        + " defined in the correlated field " + iField.getEntity().getSchemaClass().getName()
                        + "." + iField.getName());
            }/*from  ww w .ja va2 s. co m*/

            embeddedType = (Class<?>) selectionField.getLanguageType();
        }

        if (embeddedType == null) {
            throw new ConfigurationException("Cannot find embedded type definition for the field "
                    + iField.getEntity().getSchemaClass().getName() + "." + iField.getName());
        }
    }

    return embeddedType;
}

From source file:TypeUtils.java

private static boolean isAssignable(ParameterizedType lhsType, ParameterizedType rhsType) {
    if (lhsType.equals(rhsType)) {
        return true;
    }/*from  w w  w  .  ja  va 2  s  .  c o m*/
    Type[] lhsTypeArguments = lhsType.getActualTypeArguments();
    Type[] rhsTypeArguments = rhsType.getActualTypeArguments();
    if (lhsTypeArguments.length != rhsTypeArguments.length) {
        return false;
    }
    for (int size = lhsTypeArguments.length, i = 0; i < size; ++i) {
        Type lhsArg = lhsTypeArguments[i];
        Type rhsArg = rhsTypeArguments[i];
        if (!lhsArg.equals(rhsArg)
                && !(lhsArg instanceof WildcardType && isAssignable((WildcardType) lhsArg, rhsArg))) {
            return false;
        }
    }
    return true;
}

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

/**
 * Reads a field value as the provided type. This method supports parametrised types.
 *
 * @param parser the parser/*from   www  . ja  v  a  2 s .co  m*/
 * @param expected_type the expected type of the value
 * @return the value
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static Object readValueAs(final JsonParser parser, final Type expected_type) throws IOException {

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

From source file:GenericsUtil.java

/**
 * Returns all of the {@link TypeVariables}s implemented
 * by the given class.  If none are implemented then an array
 * of zero length is returned./* www  . j av  a2 s  .  c  om*/
 * @param clazz the class
 * @param genericClazz the generic class or interface to return
 * the TypeVariables from
 * @return an array of TypeVariable
 */
public static TypeVariable<?>[] getGenericTypeParameters(Class<?> clazz, Type genericClazz) {
    List<TypeVariable<?>> vars = new ArrayList<TypeVariable<?>>();

    // add superclass
    for (TypeVariable<?> var : clazz.getSuperclass().getTypeParameters()) {
        if (genericClazz == null || genericClazz.equals(var.getGenericDeclaration())) {
            vars.add(var);
        }
    }

    // add interfaces
    for (Class<?> iface : clazz.getInterfaces()) {
        for (TypeVariable<?> var : iface.getTypeParameters()) {
            if (genericClazz == null || genericClazz.equals(var.getGenericDeclaration())) {
                vars.add(var);
            }
        }
    }

    // return list
    return vars.toArray(new TypeVariable<?>[0]);
}

From source file:antre.TypeResolver.java

/**
 * Resolves the generic Type for the {@code targetType} by walking the type hierarchy upwards from
 * the {@code initialType}.// ww w  .  j  a v  a2s  .c  om
 */
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))
                if ((result = resolveGenericType(superInterface, targetType)) != null)
                    return result;
    }

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

    return null;
}

From source file:at.tuwien.ifs.somtoolbox.apps.SOMToolboxMain.java

/**
 * @param screenWidth the with of the screen
 * @param runnables {@link ArrayList} of available runnables.
 *//*from   w w w  .  j  a va 2  s  .c o m*/
private static void printAvailableRunnables(int screenWidth,
        ArrayList<Class<? extends SOMToolboxApp>> runnables) {
    Collections.sort(runnables, SOMToolboxApp.TYPE_GROUPED_COMPARATOR);

    ArrayList<Class<? extends SOMToolboxApp>> runnableClassList = new ArrayList<Class<? extends SOMToolboxApp>>();
    ArrayList<String> runnableNamesList = new ArrayList<String>();
    ArrayList<String> runnableDeskrList = new ArrayList<String>();

    for (Class<? extends SOMToolboxApp> c : runnables) {
        try {
            // Ignore abstract classes and interfaces
            if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) {
                continue;
            }
            runnableClassList.add(c);
            runnableNamesList.add(c.getSimpleName());

            String desk = null;
            try {
                Field f = c.getDeclaredField("DESCRIPTION");
                desk = (String) f.get(null);
            } catch (Exception e) {
            }

            if (desk != null) {
                runnableDeskrList.add(desk);
            } else {
                runnableDeskrList.add("");
            }
        } catch (SecurityException e) {
            // Should not happen - no Security
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
    StringBuilder sb = new StringBuilder();
    String lineSep = System.getProperty("line.separator", "\n");

    int maxLen = StringUtils.getLongestStringLength(runnableNamesList);
    sb.append("Runnable classes:").append(lineSep);
    for (int i = 0; i < runnableNamesList.size(); i++) {
        final Type cType = Type.getType(runnableClassList.get(i));
        if (i == 0 || !cType.equals(Type.getType(runnableClassList.get(i - 1)))) {
            sb.append(String.format("-- %s %s%s", cType.toString(),
                    StringUtils.repeatString(screenWidth - (8 + cType.toString().length()), "-"), lineSep));
        }
        sb.append("    ");
        sb.append(runnableNamesList.get(i));
        sb.append(StringUtils.getSpaces(4 + maxLen - runnableNamesList.get(i).length())).append("- ");
        sb.append(runnableDeskrList.get(i));
        sb.append(lineSep);
    }
    System.out.println(StringUtils.wrap(sb.toString(), screenWidth, StringUtils.getSpaces(maxLen + 10), true));
}

From source file:org.tinygroup.tinyioc.impl.BeanContainerImpl.java

public static boolean isSubClass(Class a, Class b) {
    Type genericSuperclass = a.getGenericSuperclass();
    for (Type type : a.getGenericInterfaces()) {
        if (type.equals(b)) {
            return true;
        }//from   w  w  w .ja  v  a2 s. c  om
        boolean is = isSubClass((Class) type, b);
        if (is) {
            return true;
        }
    }
    if (genericSuperclass != null) {
        if (genericSuperclass.equals(b)) {
            return true;
        } else {
            boolean is = isSubClass((Class) genericSuperclass, b);
            if (is) {
                return true;
            }
        }
    }
    return false;
}