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:io.werval.runtime.util.TypeResolver.java

/**
 * Resolves the generic Type for the {@code targetType} by walking the type hierarchy upwards from the
 * {@code initialType}./*  ww  w. j av  a2s. 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:reflex.node.KernelExecutor.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Object convertValueToType(ReflexValue v, Type type) {

    if (type.equals(byte[].class)) {
        if (v.isFile()) {
            // Load file to bytes and return that
            return getContentFromFile(v.asFile());
        } else if (v.isByteArray()) {
            return v.asByteArray();
        } else if (v.isNull()) {
            return "".getBytes();
        } else {/*from w  w  w . ja  v a 2s  .  c om*/
            return v.toString().getBytes();
        }
    } else if (type.equals(String.class)) {
        if (v.isFile()) {
            return new String(getContentFromFile(v.asFile()));
        } else if (v.isNull()) {
            return null;
        } else {
            return v.toString();
        }
    } else if (type.equals(Double.class) || type.equals(double.class)) {
        return v.asDouble();
    } else if (type.equals(Integer.class) || type.equals(int.class)) {
        return v.asInt();
    } else if (type.equals(Long.class) || type.equals(long.class)) {
        return v.asLong();
    } else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
        return v.asBoolean();
    } else if (type instanceof ParameterizedType) {
        return handleParameterizedType(v, type);
    } else if (type.equals(Map.class) || type.equals(List.class)) {
        return handleParameterizedType(v, type);
    } else if (type instanceof Class && ((Class<?>) type).isEnum()) {
        return Enum.valueOf((Class<Enum>) type, v.asString());
    } else if (v.isMap()) {
        // If it's a map then JacksonUtil may be able to recreate the type
        return handleMap(v, type);
    }
    return v.asObject();
}

From source file:com.phoenixnap.oss.ramlapisync.naming.SchemaHelper.java

/**
 * Uses Jackson object mappers to convert a Pojo into its JSONSchema representation. If Javadoc is supplied, this
 * will be injected as comments/*from  w  w  w. j  ava 2  s.c o  m*/
 * 
 * @param clazz The Class to be inspected
 * @param responseDescription The description to be embedded in the response
 * @param javaDocStore Associated JavaDoc for this class that can be embedded in the schema
 * @return Json Schema representing the class in string format
 */
public static String convertClassToJsonSchema(Type clazz, String responseDescription,
        JavaDocStore javaDocStore) {
    if (clazz == null || clazz.equals(Void.class)) {
        return "{}";
    }
    try {
        ObjectMapper m = new ObjectMapper();
        JsonSchema jsonSchema = extractSchemaInternal(clazz, TypeHelper.inferGenericType(clazz),
                responseDescription, javaDocStore, m);

        return m.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static boolean isSetter(Method method) {
    Type returnType = method.getGenericReturnType();
    if (!returnType.equals(void.class)) {
        return false; //should not return anything
    }//from www.  j  av a 2s .  c o m
    Type[] argumentTypes = method.getGenericParameterTypes();
    if (argumentTypes == null || argumentTypes.length != 1) {
        return false; //should accept exactly one argument
    }
    String name = method.getName();
    if (name.startsWith("set")) {
        if (name.length() < 4) {
            return false; //setSomething
        }
        String fourthChar = name.substring(3, 4);
        return fourthChar.toUpperCase(Locale.ROOT).equals(fourthChar); //setSomething (upper case)
    }
    return false;
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static Method findGetter(Class<?> clazz, String propName) {
    Set<String> expectedNames = new HashSet<>(
            Arrays.asList("get" + propName.substring(0, 1).toUpperCase(Locale.ROOT) + propName.substring(1),
                    "is" + propName.substring(0, 1).toUpperCase(Locale.ROOT) + propName.substring(1) //bool props
            ));//from www  . ja va  2 s.  co m
    for (Method method : clazz.getMethods()) {
        String methodName = method.getName();
        if (!expectedNames.contains(methodName)) {
            continue;
        }
        if (method.getParameterCount() != 0) {
            continue; //getters take no arguments
        }
        Type returnType = method.getGenericReturnType();
        if (returnType.equals(void.class)) {
            continue; //getters return something
        }
        if (methodName.startsWith("is")
                && !(returnType.equals(Boolean.class) || returnType.equals(boolean.class))) {
            continue; //isSomething() only valid for booleans
        }
        return method;
    }
    return null;
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static boolean isGetter(Method method) {
    Type returnType = method.getGenericReturnType();
    if (returnType.equals(void.class)) {
        return false; //should return something
    }/* www  .ja  v a 2s.  c om*/
    Type[] argumentTypes = method.getGenericParameterTypes();
    if (argumentTypes != null && argumentTypes.length != 0) {
        return false; //should not accept any arguments
    }
    String name = method.getName();
    if (name.startsWith("get")) {
        if (name.length() < 4) {
            return false; //has to be getSomething
        }
        String fourthChar = name.substring(3, 4);
        return fourthChar.toUpperCase(Locale.ROOT).equals(fourthChar); //getSomething (upper case)
    } else if (name.startsWith("is")) {
        if (name.length() < 3) {
            return false; //isSomething
        }
        String thirdChar = name.substring(2, 3);
        //noinspection SimplifiableIfStatement
        if (!thirdChar.toUpperCase(Locale.ROOT).equals(thirdChar)) {
            return false; //has to start with uppercase (or something that uppercases to itself, like a number?)
        }
        return returnType.equals(boolean.class) || returnType.equals(Boolean.class);
    } else {
        return false;
    }
}

From source file:GenericsUtil.java

/**
 * Returns the class defined for the type variable
 * of the given name. //from w w w.j av a  2 s  .co  m
 * @param clazz the class
 * @param genericClazz the generic class or interface to check the type for
 * @param name the name of the type variable
 * @param recursive whether or not to recurse up the
 * object's inheritance hierarchy.
 * @return the class
 */
public static Class<?> getTypeVariableClassByName(Class<?> clazz, Type genericClazz, String name,
        Boolean recursive) {

    // we hit the end of the line here :)
    if (clazz == null || clazz.equals(Object.class)) {
        return null;
    }

    // loop through all of the types implemented
    for (ParameterizedType pType : getGenericTypes(clazz)) {

        // do all of them, or one of them
        if (genericClazz == null || genericClazz.equals(pType.getRawType())) {

            // get super class type variables
            TypeVariable<?>[] typeVars = getGenericTypeParameters(clazz, pType.getRawType());
            for (int i = 0; i < typeVars.length; i++) {
                if ((genericClazz == null || genericClazz.equals(typeVars[i].getGenericDeclaration()))
                        && typeVars[i].getName().equals(name)) {

                    // get the type
                    Type type = pType.getActualTypeArguments()[i];

                    if (Class.class.isAssignableFrom(type.getClass())) {
                        return (Class<?>) type;
                    } else if (ParameterizedType.class.isAssignableFrom(type.getClass())) {
                        return (Class<?>) ((ParameterizedType) type).getRawType();
                    }
                }
            }

        }

    }

    // none found
    return (recursive) ? getTypeVariableClassByName(clazz.getSuperclass(), genericClazz, name, recursive)
            : null;
}

From source file:reflex.node.KernelExecutor.java

private static Object handleParameterizedType(ReflexValue v, Type type) {
    if (!(type instanceof ParameterizedType)) {
        if (type.equals(String.class))
            return v.asString();
        if (type.equals(Double.class))
            return v.asDouble();
        if (type.equals(Long.class))
            return v.asLong();
        if (type.equals(BigDecimal.class))
            return v.asBigDecimal();
        return v.asObject();
    }//from  w  ww .j a v a2s.co  m

    ParameterizedType pType = (ParameterizedType) type;
    if (pType.getRawType().equals(Map.class)) {
        if (!v.isMap()) {
            log.error(v.toString() + " is not a map");
            return null;
        }
        Map<String, Object> convertedMap = new LinkedHashMap<>();

        for (Entry<String, Object> entry : v.asMap().entrySet()) {
            Type[] innerType = pType.getActualTypeArguments();
            String key = null;
            if (!innerType[0].equals(String.class)) {
                // This could get tricky
                log.warn("Keys for maps should always be Strings");
            }
            key = entry.getKey().toString();
            Object value = entry.getValue();
            if (value instanceof ReflexValue)
                convertedMap.put(key, handleParameterizedType((ReflexValue) value, innerType[1]));
            else
                convertedMap.put(key, value);
        }
        return convertedMap;
    } else if (pType.getRawType().equals(List.class)) {
        if (!v.isList()) {
            log.error(v.toString() + " is not a list");
            return null;
        }
        List<ReflexValue> inner = v.asList();
        Type innerType = pType.getActualTypeArguments()[0];
        if (innerType.equals(String.class)) {
            List<String> ret = new ArrayList<String>(inner.size());
            for (ReflexValue vi : inner) {
                ret.add(vi.asString());
            }
            return ret;
        } else if (innerType.equals(Double.class)) {
            List<Double> ret = new ArrayList<>(inner.size());
            for (ReflexValue vi : inner) {
                ret.add(vi.asDouble());
            }
            return ret;
        } else if (innerType.equals(Long.class)) {
            List<Long> ret = new ArrayList<>(inner.size());
            for (ReflexValue vi : inner) {
                ret.add(vi.asLong());
            }
            return ret;
        } else if (innerType.equals(BigDecimal.class)) {
            List<BigDecimal> ret = new ArrayList<>(inner.size());
            for (ReflexValue vi : inner) {
                ret.add(vi.asBigDecimal());
            }
            return ret;
        } else if (innerType instanceof ParameterizedType || inner instanceof List) {
            List<Object> ret = new ArrayList<>();
            for (ReflexValue vi : inner) {
                ret.add(handleParameterizedType(vi, innerType));
            }
            return ret;
        } else {
            log.warn("Cannot convert " + v.toString() + " to " + type.toString());
        }
    } else if (v.isMap()) {
        // If it's a map then JacksonUtil may be able to recreate the type
        return handleMap(v, type);
    }
    return v.asObject();
}

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

/**
 * Returns the generic {@code type} using type variable information from the {@code subType} else {@code null} if the
 * generic type cannot be resolved./*  w  w w . ja  v a2  s  .com*/
 *
 * @param type to resolve generic type for
 * @param subType to extract type variable information from
 * @return generic {@code type} else {@code null} if it cannot be resolved
 */
public static Type resolveGenericType(Class<?> type, Type subType) {
    Class<?> rawType;
    if (subType instanceof ParameterizedType)
        rawType = (Class<?>) ((ParameterizedType) subType).getRawType();
    else
        rawType = (Class<?>) subType;

    if (type.equals(rawType))
        return subType;

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

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

    return null;
}

From source file:org.springframework.core.GenericTypeResolver.java

/**
 * Resolve the single type argument of the given generic interface against the given
 * target method which is assumed to return the given interface or an implementation
 * of it./*from   w ww .  jav  a  2 s .  c  om*/
 * @param method the target method to check the return type of
 * @param genericIfc the generic interface or superclass to resolve the type argument from
 * @return the resolved parameter type of the method return type, or {@code null}
 * if not resolvable or if the single argument is of type {@link WildcardType}.
 */
public static Class<?> resolveReturnTypeArgument(Method method, Class<?> genericIfc) {
    Assert.notNull(method, "method must not be null");
    Type returnType = method.getReturnType();
    Type genericReturnType = method.getGenericReturnType();
    if (returnType.equals(genericIfc)) {
        if (genericReturnType instanceof ParameterizedType) {
            ParameterizedType targetType = (ParameterizedType) genericReturnType;
            Type[] actualTypeArguments = targetType.getActualTypeArguments();
            Type typeArg = actualTypeArguments[0];
            if (!(typeArg instanceof WildcardType)) {
                return (Class<?>) typeArg;
            }
        } else {
            return null;
        }
    }
    return resolveTypeArgument((Class<?>) returnType, genericIfc);
}