List of usage examples for java.lang.reflect Type getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:com.github.jasonruckman.sidney.core.JavaTypeRefBuilder.java
public static TypeRef buildTypeRef(Type type, TypeBindings parentBindings, Field field) { JavaType jt;/*from w ww . ja v a2 s . c o m*/ TypeBindings typeBindings; typeBindings = Types.binding(type, parentBindings); jt = Types.type(type, parentBindings); TypeRef ref; if (field == null) { ref = new TypeRef(jt.getRawClass()); } else { Hint hint = field.getAnnotation(Hint.class); if (hint != null) { ref = new TypeRef.TypeFieldRef(hint.value(), field); } else { ref = new TypeRef.TypeFieldRef(jt.getRawClass(), field); } } for (Field subField : Fields.getAllFields(jt.getRawClass())) { Type subType = subField.getGenericType(); TypeRef subRef = buildTypeRef(subType, typeBindings, subField); ref.addField((TypeRef.TypeFieldRef) subRef); } if (ParameterizedType.class.isAssignableFrom(type.getClass())) { ParameterizedType t = (ParameterizedType) type; for (Type actualTypeArg : t.getActualTypeArguments()) { ref.addTypeParameter(buildTypeRef(actualTypeArg, parentBindings, null)); } } else if (TypeVariable.class.isAssignableFrom(type.getClass())) { TypeVariable t = (TypeVariable) type; for (Type typeBound : t.getBounds()) { ref.addTypeParameter(buildTypeRef(typeBound, parentBindings, null)); } } else if (GenericArrayType.class.isAssignableFrom(type.getClass())) { GenericArrayType t = (GenericArrayType) type; ref.addTypeParameter(buildTypeRef(t.getGenericComponentType(), parentBindings, null)); } if (jt.isArrayType() && !GenericArrayType.class.isAssignableFrom(type.getClass())) { ArrayType arrType = (ArrayType) jt; ref.addTypeParameter(buildTypeRef(arrType.getContentType().getRawClass(), null, null)); } return ref; }
From source file:org.openflexo.antar.binding.TypeUtils.java
private static boolean checkFail(Method m) { Type t1 = m.getGenericParameterTypes()[0]; Type t2 = m.getGenericParameterTypes()[1]; System.out.println("checkFail " + (isTypeAssignableFrom(t1, t2, true) ? "NOK " : "OK ") + "Method " + m.getName() + " t1: " + t1 + " of " + t1.getClass().getSimpleName() + " t2: " + t2 + " of " + t2.getClass().getSimpleName()); return isTypeAssignableFrom(t1, t2, true); }
From source file:org.openflexo.antar.binding.TypeUtils.java
private static boolean checkSucceed(Method m) { Type t1 = m.getGenericParameterTypes()[0]; Type t2 = m.getGenericParameterTypes()[1]; System.out.println("checkSucceed " + (isTypeAssignableFrom(t1, t2, true) ? "OK " : "NOK ") + "Method " + m.getName() + " t1: " + t1 + " of " + t1.getClass().getSimpleName() + " t2: " + t2 + " of " + t2.getClass().getSimpleName()); return isTypeAssignableFrom(t1, t2, true); }
From source file:ca.oson.json.util.ObjectUtil.java
public static <E> Class<E> getTypeClass(java.lang.reflect.Type type) { Class cl = type.getClass(); if (ComponentType.class.isAssignableFrom(cl)) { ComponentType componentType = (ComponentType) type; return componentType.getClassType(); }//from w ww .j a va 2 s. co m //java.util.List<ca.oson.json.test.Dataset> String className = type.getTypeName(); try { int idx = className.indexOf('<'); if (idx > 0) { className = className.substring(0, idx); } return (Class<E>) Class.forName(className); } catch (ClassNotFoundException e) { e.printStackTrace(); } // Collection<String>, return String.class if (ParameterizedType.class.isAssignableFrom(cl)) { java.lang.reflect.ParameterizedType pt = (java.lang.reflect.ParameterizedType) type; return (Class<E>) pt.getRawType().getClass(); // GenericArrayType represents an array type whose component // type is either a parameterized type or a type variable. } else if (java.lang.reflect.GenericArrayType.class.isAssignableFrom(cl)) { java.lang.reflect.GenericArrayType pt = (java.lang.reflect.GenericArrayType) type; return (Class<E>) pt.getClass(); } else if (java.lang.reflect.TypeVariable.class.isAssignableFrom(cl)) { java.lang.reflect.TypeVariable pt = (java.lang.reflect.TypeVariable) type; return (Class<E>) pt.getClass(); } return cl; }
From source file:ca.oson.json.util.ObjectUtil.java
public static <E> Class getTypeComponentClass(java.lang.reflect.Type type) { Class cl = type.getClass(); if (ComponentType.class.isAssignableFrom(cl)) { ComponentType componentType = (ComponentType) type; return componentType.getMainComponentType(); }/* ww w. ja va 2 s .c om*/ //java.util.List<ca.oson.json.test.Dataset> String className = type.getTypeName(); Class ctype = getComponentType(className); if (ctype != null) { return ctype; } // Collection<String>, return String.class if (ParameterizedType.class.isAssignableFrom(cl)) { java.lang.reflect.ParameterizedType pt = (java.lang.reflect.ParameterizedType) type; if (pt.getActualTypeArguments().length > 0) { //return pt.getActualTypeArguments()[0].getClass(); className = pt.getActualTypeArguments()[0].getTypeName(); try { return Class.forName(className); } catch (ClassNotFoundException e) { // e.printStackTrace(); } } // GenericArrayType represents an array type whose component // type is either a parameterized type or a type variable. } else if (java.lang.reflect.GenericArrayType.class.isAssignableFrom(cl)) { java.lang.reflect.GenericArrayType pt = (java.lang.reflect.GenericArrayType) type; return getTypeClass(pt.getGenericComponentType()); } else if (java.lang.reflect.TypeVariable.class.isAssignableFrom(cl)) { java.lang.reflect.TypeVariable pt = (java.lang.reflect.TypeVariable) type; java.lang.reflect.Type[] types = pt.getBounds(); if (types != null && types.length > 0) { return getTypeClass(types[0]); } } return null; }
From source file:com.taobao.rpc.doclet.RPCAPIInfoHelper.java
/** * ?//from www . j a va 2 s .co m * * @param method * @return */ public static Object buildTypeStructure(Class<?> type, Type genericType, Type oriGenericType) { if ("void".equalsIgnoreCase(type.getName()) || ClassUtils.isPrimitiveOrWrapper(type) || String.class.isAssignableFrom(type) || Date.class.isAssignableFrom(type) || URL.class.isAssignableFrom(type)) { // return type.getName().replaceAll("java.lang.", "").replaceAll("java.util.", "").replaceAll("java.sql.", ""); } // end if if (type.isArray()) { // return new Object[] { buildTypeStructure(type.getComponentType(), type.getComponentType(), genericType) }; } // end if if (ClassUtils.isAssignable(Map.class, type)) { // Map return Map.class.getName(); } // end if if (type.isEnum()) { // Enum return Enum.class.getName(); } // end if boolean isCollection = type != null ? Collection.class.isAssignableFrom(type) : false; if (isCollection) { Type rawType = type; if (genericType != null) { if (genericType instanceof ParameterizedType) { ParameterizedType _type = (ParameterizedType) genericType; Type[] actualTypeArguments = _type.getActualTypeArguments(); rawType = actualTypeArguments[0]; } else if (genericType instanceof GenericArrayType) { rawType = ((GenericArrayType) genericType).getGenericComponentType(); } if (genericType instanceof WildcardType) { rawType = ((WildcardType) genericType).getUpperBounds()[0]; } } if (rawType == type) { return new Object[] { rawType.getClass().getName() }; } else { if (rawType.getClass().isAssignableFrom(TypeVariableImpl.class)) { return new Object[] { buildTypeStructure( (Class<?>) ((ParameterizedType) oriGenericType).getActualTypeArguments()[0], rawType, genericType) }; } else { if (rawType instanceof ParameterizedType) { if (((ParameterizedType) rawType).getRawType() == Map.class) { return new Object[] { Map.class.getName() }; } } if (oriGenericType == rawType) { return new Object[] { rawType.getClass().getName() }; } return new Object[] { buildTypeStructure((Class<?>) rawType, rawType, genericType) }; } } } if (type.isInterface()) { return type.getName(); } ClassInfo paramClassInfo = RPCAPIDocletUtil.getClassInfo(type.getName()); //added if (null == paramClassInfo) { System.out.println("failed to get paramClassInfo for :" + type.getName()); return null; } List<FieldInfo> typeConstructure = new ArrayList<FieldInfo>(); BeanWrapper bean = new BeanWrapperImpl(type); PropertyDescriptor[] propertyDescriptors = bean.getPropertyDescriptors(); Method readMethod; String name; for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { readMethod = propertyDescriptor.getReadMethod(); if (readMethod == null || "getClass".equals(readMethod.getName())) { continue; } name = propertyDescriptor.getName(); FieldInfo fieldInfo = paramClassInfo.getFieldInfo(name); if (readMethod.getReturnType().isAssignableFrom(type)) { String comment = "structure is the same with parent."; typeConstructure .add(FieldInfo.create(name, fieldInfo != null ? fieldInfo.getComment() : "", comment)); } else { typeConstructure.add( FieldInfo.create(name, fieldInfo != null ? fieldInfo.getComment() : "", buildTypeStructure( readMethod.getReturnType(), readMethod.getGenericReturnType(), genericType))); } // end if } return typeConstructure; }
From source file:fr.mtlx.odm.ClassAssistant.java
public boolean isCollection(Type type) { Class<?> c = type.getClass(); return asList(c.getInterfaces()).contains(Collection.class) || c.equals(Collection.class); }
From source file:com.github.luluvise.droid_utils.json.jackson.JacksonObjectParser.java
public Object parseAndClose(Reader reader, Type dataType) throws IOException { // TODO detect if passed class is consistent return mMapper.readValue(reader, dataType.getClass()); }
From source file:io.servicecomb.swagger.invocation.converter.ConverterMgr.java
protected Converter findArrayToCollection(Type src, Type target) { if (src.getClass().equals(Class.class) && ParameterizedType.class.isAssignableFrom(target.getClass())) { Class<?> srcCls = (Class<?>) src; ParameterizedType targetType = (ParameterizedType) target; Class<?> targetCls = (Class<?>) targetType.getRawType(); if (srcCls.isArray() && srcCls.getComponentType().equals(targetType.getActualTypeArguments()[0])) { if (List.class.isAssignableFrom(targetCls)) { return arrayToList; }//from w w w . j a va 2 s .c om if (Set.class.isAssignableFrom(targetCls)) { return arrayToSet; } } } return null; }
From source file:org.apache.servicecomb.swagger.invocation.converter.ConverterMgr.java
protected Converter findCollectionToArray(Type src, Type target) { if (ParameterizedType.class.isAssignableFrom(src.getClass()) && target.getClass().equals(Class.class)) { ParameterizedType srcType = (ParameterizedType) src; Class<?> srcCls = (Class<?>) srcType.getRawType(); Class<?> targetCls = (Class<?>) target; if (Collection.class.isAssignableFrom(srcCls) && targetCls.isArray() && srcType.getActualTypeArguments()[0].equals(targetCls.getComponentType())) { Converter converter = collectionToArrayMap.computeIfAbsent(target, k -> new SameElementCollectionToArray(targetCls.getComponentType())); return converter; }/*w w w . j a va 2 s .co m*/ } return null; }