/** Creates a {@link PluginPropertyField} based on the given field. */ private PluginPropertyField createPluginProperty(Field field, TypeToken<?> resolvingType) throws UnsupportedTypeException { TypeToken<?> fieldType = resolvingType.resolveType(field.getGenericType()); Class<?> rawType = fieldType.getRawType(); Name nameAnnotation = field.getAnnotation(Name.class); Description descAnnotation = field.getAnnotation(Description.class); String name = nameAnnotation == null ? field.getName() : nameAnnotation.value(); String description = descAnnotation == null ? "" : descAnnotation.value(); if (rawType.isPrimitive()) { return new PluginPropertyField(name, description, rawType.getName(), true); } rawType = Primitives.unwrap(rawType); if (!rawType.isPrimitive() && !String.class.equals(rawType)) { throw new UnsupportedTypeException("Only primitive and String types are supported"); } boolean required = true; for (Annotation annotation : field.getAnnotations()) { if (annotation.annotationType().getName().endsWith(".Nullable")) { required = false; break; } } return new PluginPropertyField( name, description, rawType.getSimpleName().toLowerCase(), required); }
/** * Gets all config properties for the given plugin. * * @return the name of the config field in the plugin class or {@code null} if the plugin doesn't * have a config field */ @Nullable private String getProperties(TypeToken<?> pluginType, Map<String, PluginPropertyField> result) throws UnsupportedTypeException { // Get the config field for (TypeToken<?> type : pluginType.getTypes().classes()) { for (Field field : type.getRawType().getDeclaredFields()) { TypeToken<?> fieldType = TypeToken.of(field.getGenericType()); if (PluginConfig.class.isAssignableFrom(fieldType.getRawType())) { // Pick up all config properties inspectConfigField(fieldType, result); return field.getName(); } } } return null; }
@SuppressWarnings("unchecked") public static <T> T cast(Object obj, Class<T> clazz) throws Exception { if (obj == null) { return null; } TypeToken<T> type = TypeToken.of(clazz).wrap(); TypeToken<?> objType = TypeToken.of(obj.getClass()).wrap(); if (type.isAssignableFrom(objType)) { return (T) obj; } if (TypeToken.of(List.class).isAssignableFrom(type) && objType.isArray()) { List<Object> list = Arrays.asList((Object[]) obj); return (T) list; } if (type.isArray() && TypeToken.of(List.class).isAssignableFrom(objType)) { List<?> list = (List<?>) obj; TypeToken<?> componentType = type.getComponentType(); Class<?> rawType; if (componentType == null) { rawType = Object.class; } else { rawType = componentType.getRawType(); } Object[] array = (Object[]) Array.newInstance(rawType, list.size()); return (T) list.toArray(array); } if (clazz.isEnum()) { return (T) Enum.valueOf((Class<? extends Enum>) clazz, obj.toString()); } if (TypeToken.of(String.class).isAssignableFrom(type)) { return (T) obj.toString(); } if (TypeToken.of(Number.class).isAssignableFrom(type)) { BigDecimal num = new BigDecimal(obj.toString()); if (TypeToken.of(Integer.class).isAssignableFrom(type)) { return (T) Integer.valueOf(num.intValue()); } if (TypeToken.of(Long.class).isAssignableFrom(type)) { return (T) Long.valueOf(num.longValue()); } if (TypeToken.of(Short.class).isAssignableFrom(type)) { return (T) Short.valueOf(num.shortValue()); } if (TypeToken.of(Byte.class).isAssignableFrom(type)) { return (T) Byte.valueOf(num.byteValue()); } if (TypeToken.of(Float.class).isAssignableFrom(type)) { return (T) Float.valueOf(num.floatValue()); } if (TypeToken.of(Double.class).isAssignableFrom(type)) { return (T) Double.valueOf(num.doubleValue()); } } return null; }
/** * Inspects the plugin config class and build up a map for {@link PluginPropertyField}. * * @param configType type of the config class * @param result map for storing the result * @throws UnsupportedTypeException if a field type in the config class is not supported */ private void inspectConfigField(TypeToken<?> configType, Map<String, PluginPropertyField> result) throws UnsupportedTypeException { for (TypeToken<?> type : configType.getTypes().classes()) { if (PluginConfig.class.equals(type.getRawType())) { break; } for (Field field : type.getRawType().getDeclaredFields()) { PluginPropertyField property = createPluginProperty(field, type); if (result.containsKey(property.getName())) { throw new IllegalArgumentException( "Plugin config with name " + property.getName() + " already defined in " + configType.getRawType()); } result.put(property.getName(), property); } } }
/** * Constructor. * * @param contentType the type of request this parser can handle */ protected ParserSupport(String contentType) { this.contentType = contentType; TypeToken<O> typeToken = new TypeToken<O>(getClass()) {}; if (typeToken.getType() instanceof Class) { @SuppressWarnings("unchecked") Class<O> rawType = (Class<O>) typeToken.getRawType(); this.optsType = rawType; } else { throw new IllegalArgumentException("Type parameter O of ParserSupport must be a Class"); } }
public static void main(String[] args) { ApplicationContext appContext = new AnnotationConfigApplicationContext(FieldsCheckerMain.class); FieldsChecker fieldsChecker = appContext.getBean(FieldsChecker.class); fieldsChecker.checkTableFields(); boolean resolved = false; TypeToken mapOfString = TypeTokens.mapOf(String.class, String.class); Type type = mapOfString.getType(); if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; Type[] types = pType.getActualTypeArguments(); if (types.length == 2) { TypeToken valueType = TypeToken.of(types[1]); resolved = valueType.getRawType().equals(String.class); } } System.out.println(resolved); }
public GenericMongoRepository(MongoOperations mongoOperations) { Assert.notNull(mongoOperations); this.mongoOperations = mongoOperations; TypeToken<T> typeToken = new TypeToken<T>(getClass()) {}; model = (Class<T>) typeToken.getRawType(); }
public void print() { System.out.println(((Class<T>) recordType.getRawType()).getSimpleName()); }
public Class<E> getType() { return (Class<E>) typeToken.getRawType(); }