List of usage examples for java.lang.reflect Type getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:de.micromata.genome.util.runtime.ClassUtils.java
/** * Looks if generic supertype is paramized with given baseRequested. * * @param <T> the generic type// ww w . j av a2s . co m * @param genericSuperclass the generic superclass * @param baseRequested the base requested * @return the generic type argument from generic super type. null if not found. */ //CHECKSTYLE.OFF FinalParameter Precondition cast public static <T> Class<T> getGenericTypeArgumentFromGenericSuperType(Type genericSuperclass, Class<T> baseRequested) { Type loopVar = genericSuperclass; while (loopVar != null && !(ParameterizedType.class.isAssignableFrom(loopVar.getClass()))) { loopVar = ((Class<?>) loopVar).getGenericSuperclass(); } if (loopVar != null && ParameterizedType.class.isAssignableFrom(loopVar.getClass())) { Type[] typeArgs = ((ParameterizedType) loopVar).getActualTypeArguments(); for (Type typeArg : typeArgs) { if (typeArg instanceof ParameterizedType) { typeArg = ((ParameterizedType) typeArg).getRawType(); } if ((typeArg instanceof Class) == false) { continue; } if (baseRequested.isAssignableFrom((Class<?>) typeArg) == false) { continue; } return (Class<T>) typeArg; } } return null; }
From source file:com.github.dozermapper.core.util.ReflectionUtils.java
public static Class<?> determineGenericsType(Type type) { Class<?> result = null; if (type != null && ParameterizedType.class.isAssignableFrom(type.getClass())) { Type genericType = ((ParameterizedType) type).getActualTypeArguments()[0]; if (genericType != null) { if (genericType instanceof Class) { result = (Class<?>) genericType; } else if (genericType instanceof ParameterizedType) { Type rawType = ((ParameterizedType) genericType).getRawType(); result = (Class<?>) rawType; }//from www . j a v a 2 s. co m } } return result; }
From source file:com.proofpoint.http.client.SmileBodyGenerator.java
public static <T> SmileBodyGenerator<T> smileBodyGenerator(JsonCodec<T> jsonCodec, T instance) { ObjectMapper objectMapper = OBJECT_MAPPER_SUPPLIER.get(); ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator jsonGenerator;/* w w w.ja va2 s . co m*/ try { jsonGenerator = new SmileFactory().createGenerator(out); } catch (IOException e) { throw propagate(e); } Type genericType = jsonCodec.getType(); // 04-Mar-2010, tatu: How about type we were given? (if any) JavaType rootType = null; if (genericType != null && instance != null) { // 10-Jan-2011, tatu: as per [JACKSON-456], it's not safe to just force root // type since it prevents polymorphic type serialization. Since we really // just need this for generics, let's only use generic type if it's truly // generic. if (genericType.getClass() != Class.class) { // generic types are other implementations of 'java.lang.reflect.Type' // This is still not exactly right; should root type be further // specialized with 'value.getClass()'? Let's see how well this works before // trying to come up with more complete solution. rootType = objectMapper.getTypeFactory().constructType(genericType); // 26-Feb-2011, tatu: To help with [JACKSON-518], we better recognize cases where // type degenerates back into "Object.class" (as is the case with plain TypeVariable, // for example), and not use that. // if (rootType.getRawClass() == Object.class) { rootType = null; } } } try { if (rootType != null) { objectMapper.writerWithType(rootType).writeValue(jsonGenerator, instance); } else { objectMapper.writeValue(jsonGenerator, instance); } } catch (IOException e) { throw new IllegalArgumentException( String.format("%s could not be converted to SMILE", instance.getClass().getName()), e); } return new SmileBodyGenerator<>(out.toByteArray()); }
From source file:de.micromata.genome.util.runtime.ClassUtils.java
/** * Find generic type argument./*from w w w . j a v a2s. c o m*/ * * @param genericSuperclass the generic superclass * @param index the index * @return the class */ public static Class<?> findGenericTypeArgument(Type genericSuperclass, int index) { // CHECKSTYLE.OFF SIMULIERTE_POLYMORPHIE Necesssary for technical reasons (low level handling). while (genericSuperclass != null && !(ParameterizedType.class.isAssignableFrom(genericSuperclass.getClass()))) { genericSuperclass = ((Class) genericSuperclass).getGenericSuperclass(); } // CHECKSTYLE.ON if (genericSuperclass instanceof ParameterizedType) { Type o = ((ParameterizedType) genericSuperclass).getActualTypeArguments()[index]; if (o instanceof Class) { return (Class<?>) o; } else if (o instanceof ParameterizedType) { return (Class<?>) ((ParameterizedType) o).getRawType(); } } return null; }
From source file:ca.uhn.fhir.context.ModelScanner.java
private static Class<?> getGenericCollectionTypeOfCodedField(Field next) { Class<?> type;// ww w.ja v a2s. c o m ParameterizedType collectionType = (ParameterizedType) next.getGenericType(); Type firstArg = collectionType.getActualTypeArguments()[0]; if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) { ParameterizedType pt = ((ParameterizedType) firstArg); firstArg = pt.getActualTypeArguments()[0]; type = (Class<?>) firstArg; } else { type = (Class<?>) firstArg; } return type; }
From source file:net.ymate.platform.commons.util.ClassUtils.java
/** * @param clazz /*from www. j a v a 2 s .c om*/ * @return ????, ??RawType */ public static List<Class<?>> getParameterizedTypes(Class<?> clazz) { List<Class<?>> _clazzs = new ArrayList<Class<?>>(); Type _types = clazz.getGenericSuperclass(); if (ParameterizedType.class.isAssignableFrom(_types.getClass())) { for (Type _type : ((ParameterizedType) _types).getActualTypeArguments()) { if (ParameterizedType.class.isAssignableFrom(_type.getClass())) { _clazzs.add((Class<?>) ((ParameterizedType) _type).getRawType()); } else { _clazzs.add((Class<?>) _type); } } } else { _clazzs.add((Class<?>) _types); } return _clazzs; }
From source file:com.wavemaker.json.type.reflect.ReflectTypeUtils.java
/** * Return the type name for the corresponding class and fields. * //from w w w . j av a2 s .co m * @param klass Generally, the klass is sufficient to identify the class. * @param mapFields If klass is a Map type, this should be the generic parameters. * @return A String uniquely identifying this type. */ public static String getTypeName(Type type) { if (type instanceof Class) { return ((Class<?>) type).getName(); } else if (type instanceof ParameterizedType) { return type.toString().replace(" ", ""); } else { throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNPARAMTYPE, type, type != null ? type.getClass() : null); } }
From source file:org.evosuite.utils.generic.GenericAccessibleObject.java
/** * Checks if the given type is a class that is supposed to have type * parameters, but doesn't. In other words, if it's a really raw type. */// w w w . j a va 2 s . com protected static boolean isMissingTypeParameters(Type type) { if (type instanceof Class) { for (Class<?> clazz = (Class<?>) type; clazz != null; clazz = clazz.getEnclosingClass()) { if (clazz.getTypeParameters().length != 0) return true; } return false; } else if (type instanceof ParameterizedType) { return false; } else { throw new AssertionError("Unexpected type " + type.getClass()); } }
From source file:org.tdar.core.service.ReflectionService.java
/** * Get the Class of the return type/or generic type * /* w w w .j av a 2s .c om*/ * @param type * @return */ private static Class<?> getType(Type type) { Logger logger = LoggerFactory.getLogger(ReflectionService.class); if (WildcardType.class.isAssignableFrom(type.getClass())) { WildcardType subType = (WildcardType) type; logger.trace(" wildcard type: {} [{}]", type, type.getClass()); logger.trace(" lower: {} upper: {}", subType.getLowerBounds(), subType.getUpperBounds()); return subType.getUpperBounds().getClass(); } if (type instanceof ParameterizedType) { ParameterizedType collectionType = (ParameterizedType) type; logger.trace(" parameterized type: {} [{} - {}]", type, type.getClass(), collectionType.getActualTypeArguments()); Type subtype = collectionType.getActualTypeArguments()[0]; logger.trace(" type: {} subtype: {} ", type, subtype); if (subtype instanceof Type) { return getType(subtype); } return (Class<?>) subtype; } if (type instanceof Class<?>) { return (Class<?>) type; } return null; }
From source file:org.datalorax.populace.core.util.TypeUtils.java
/** * Ensures a consistent implementation of the different sub types i.e. {@link java.lang.reflect.ParameterizedType}, * {@link java.lang.Class}, {@link java.lang.reflect.TypeVariable} and {@link java.lang.reflect.GenericArrayType}. * <p>/*from w w w .j a v a2 s .c o m*/ * This is useful if you need to compare them, as implementations from different vendors do not generally compare as * equal, even if the type information they convey is equivalent. * <p> * Calling this method with two equivalent types, from two different implementations, will result in two {@code type}s * that will be equal i.e. {@code ensureTypeImpl(t1).equals(ensureTypeImpl(t2))} will return true. * * @param type the type, from any implementation, for which a consistent implementation is required. * @return the same type, but from a consistent implementation. */ public static Type ensureConsistentType(final Type type) { if (type instanceof Class) { return type; // Final class, so only one impl. } if (type instanceof ParameterizedType) { return ensureConsistentParameterisedType((ParameterizedType) type); } if (type instanceof TypeVariable) { return type; } if (type instanceof WildcardType) { return ensureConsistentWildcard((WildcardType) type); } if (type instanceof GenericArrayType) { return ensureConsistentGenericArrayType((GenericArrayType) type); } throw new IllegalArgumentException("Unexpected type: " + type.getClass()); }