List of usage examples for java.lang.reflect Type getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:com.wavemaker.json.type.reflect.ReflectTypeUtils.java
/** * Initializes a TypeDefinition from a given class. The first entry in the return list is the TypeDefinition for the * parameter class; any entries after that (if any) are TypeDefinitions for any other types that were required as * fields for that root TypeDefinition./* www . j av a 2 s . co m*/ * * @param klass The Class object to describe. * @param typeState The TypeState for the current operation. * @param strict True indicates that processing should stop on ambiguous entries; false indicates that null should * be entered. * @return A list of TypeDefinitions; the first entry is the root (corresponding with the klass parameter), any * other entries in the list were required to describe the root TypeDefinition's fields. The return may also * be null, if sufficient information was not provided to determine the type. */ public static TypeDefinition getTypeDefinition(Type type, TypeState typeState, boolean strict) { Class<?> klass; // we already know about this type; we're done if (typeState.isTypeKnown(ReflectTypeUtils.getTypeName(type))) { return typeState.getType(ReflectTypeUtils.getTypeName(type)); } // if the type is Object, return null, we can't figure out anything more if (type instanceof Class && Object.class.equals(type)) { return null; } // if we don't have enough information, return null if (!strict) { if (type instanceof Class && Map.class.isAssignableFrom((Class<?>) type) && !Properties.class.isAssignableFrom((Class<?>) type)) { if (!JSON.class.isAssignableFrom((Class<?>) type)) { logger.warn(MessageResource.JSON_TYPE_NOGENERICS.getMessage(type)); } return null; } else if (type instanceof Class && List.class.isAssignableFrom((Class<?>) type)) { if (!JSON.class.isAssignableFrom((Class<?>) type)) { logger.warn(MessageResource.JSON_TYPE_NOGENERICS.getMessage(type)); } return null; } } TypeDefinition ret; if (type instanceof Class && Properties.class.isAssignableFrom((Class<?>) type)) { MapReflectTypeDefinition mtdret = new MapReflectTypeDefinition(); mtdret.setTypeName(ReflectTypeUtils.getTypeName(type)); mtdret.setShortName(ReflectTypeUtils.getShortName(type)); typeState.addType(mtdret); klass = (Class<?>) type; mtdret.setKlass(klass); TypeDefinition stringType = getTypeDefinition(String.class, typeState, false); mtdret.setKeyFieldDefinition(new GenericFieldDefinition(stringType)); mtdret.setValueFieldDefinition(new GenericFieldDefinition(stringType)); ret = mtdret; } else if (type instanceof Class && JSONUtils.isPrimitive((Class<?>) type)) { PrimitiveReflectTypeDefinition ptret; if (((Class<?>) type).isEnum()) { ptret = new EnumPrimitiveReflectTypeDefinition(); } else { ptret = new PrimitiveReflectTypeDefinition(); } ptret.setTypeName(ReflectTypeUtils.getTypeName(type)); ptret.setShortName(ReflectTypeUtils.getShortName(type)); typeState.addType(ptret); klass = (Class<?>) type; ptret.setKlass(klass); ret = ptret; } else if (type instanceof Class) { klass = (Class<?>) type; if (Collection.class.isAssignableFrom(klass)) { throw new WMRuntimeException(MessageResource.JSON_TYPE_NOGENERICS, klass); } else if (klass.isArray()) { throw new WMRuntimeException(MessageResource.JSON_USE_FIELD_FOR_ARRAY, klass); } else if (Map.class.isAssignableFrom(klass)) { throw new WMRuntimeException(MessageResource.JSON_TYPE_NOGENERICS, klass); } else if (ClassUtils.isPrimitiveOrWrapper(klass) || CharSequence.class.isAssignableFrom(klass)) { PrimitiveReflectTypeDefinition ptret = new PrimitiveReflectTypeDefinition(); ptret.setTypeName(ReflectTypeUtils.getTypeName(type)); ptret.setShortName(ReflectTypeUtils.getShortName(type)); typeState.addType(ptret); ptret.setKlass(klass); ret = ptret; } else { ObjectReflectTypeDefinition otret = new ObjectReflectTypeDefinition(); otret.setTypeName(ReflectTypeUtils.getTypeName(type)); otret.setShortName(ReflectTypeUtils.getShortName(type)); otret.setKlass(klass); typeState.addType(otret); PropertyUtilsBean pub = ((ReflectTypeState) typeState).getPropertyUtilsBean(); PropertyDescriptor[] pds = pub.getPropertyDescriptors(klass); otret.setFields(new LinkedHashMap<String, FieldDefinition>(pds.length)); for (PropertyDescriptor pd : pds) { if (pd.getName().equals("class")) { continue; } Type paramType; if (pd.getReadMethod() != null) { paramType = pd.getReadMethod().getGenericReturnType(); } else if (pd.getWriteMethod() != null) { paramType = pd.getWriteMethod().getGenericParameterTypes()[0]; } else { logger.warn("No getter in type " + pd.getName()); continue; } otret.getFields().put(pd.getName(), getFieldDefinition(paramType, typeState, strict, pd.getName())); } ret = otret; } } else if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; if (pt.getRawType() instanceof Class && Map.class.isAssignableFrom((Class<?>) pt.getRawType())) { MapReflectTypeDefinition mtdret = new MapReflectTypeDefinition(); mtdret.setTypeName(ReflectTypeUtils.getTypeName(type)); mtdret.setShortName(ReflectTypeUtils.getShortName(type)); typeState.addType(mtdret); Type[] types = pt.getActualTypeArguments(); mtdret.setKeyFieldDefinition(getFieldDefinition(types[0], typeState, strict, null)); mtdret.setValueFieldDefinition(getFieldDefinition(types[1], typeState, strict, null)); mtdret.setKlass((Class<?>) pt.getRawType()); ret = mtdret; } else { throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNRAWTYPE, pt.getOwnerType(), pt); } } else { throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNPARAMTYPE, type, type != null ? type.getClass() : null); } return ret; }
From source file:ca.oson.json.Oson.java
<T> T fromJsonMap(String source, Type type) { try {/*from w ww.j a v a 2s . co m*/ Class<T> valueType = null; ComponentType componentType = null; boolean started = false; if (type != null) { if (ComponentType.class.isAssignableFrom(type.getClass())) { componentType = (ComponentType) type; } else { componentType = new ComponentType(type); } valueType = componentType.getClassType(); startCachedComponentTypes(componentType); started = true; } else { } return fromJsonMap(source, valueType, started); } catch (JSONException ex) { //ex.printStackTrace(); } return null; }
From source file:ca.oson.json.Oson.java
public <T> T deserialize(JSONObject source, Type type) { JSON_PROCESSOR processor = getJsonProcessor(); if (processor == JSON_PROCESSOR.JACKSON || processor == JSON_PROCESSOR.GSON) { return deserialize(source.toString(), type); }/*from w w w . j ava 2 s .c o m*/ Map<String, Object> map = (Map) fromJsonMap(source); Class<T> valueType = null; ComponentType componentType = null; if (type != null) { if (ComponentType.class.isAssignableFrom(type.getClass())) { componentType = (ComponentType) type; } else { componentType = new ComponentType(type); } valueType = componentType.getClassType(); startCachedComponentTypes(componentType); } return deserialize(source, valueType); }
From source file:ca.oson.json.Oson.java
public <T> String serialize(T source, Type type) { JSON_PROCESSOR processor = getJsonProcessor(); // should not reach here, just for reference if (processor == JSON_PROCESSOR.JACKSON) { try {// www.j a v a2 s . c om return getJackson().writeValueAsString(source); } catch (JsonProcessingException e) { if (getPrintErrorUseOsonInFailure()) { e.printStackTrace(); } else { throw new RuntimeException(e); } } } else if (processor == JSON_PROCESSOR.GSON) { try { return getGson().toJson(source, type); } catch (Exception e) { if (getPrintErrorUseOsonInFailure()) { e.printStackTrace(); } else { throw new RuntimeException(e); } } } Class<T> valueType = null; ComponentType componentType = null; if (type != null) { if (ComponentType.class.isAssignableFrom(type.getClass())) { componentType = (ComponentType) type; } else { componentType = new ComponentType(type); } valueType = componentType.getClassType(); } if (valueType == null) { valueType = (Class<T>) source.getClass(); } return object2Json(new FieldData(source, valueType, componentType, false)); }