List of usage examples for java.lang.reflect Type getTypeName
default String getTypeName()
From source file:api.wiki.WikiGenerator.java
private static String typeDisplayName(Type type) { return stripPackage(type.getTypeName()); }
From source file:ch.ifocusit.plantuml.utils.ClassUtils.java
public static String getSimpleName(Type type) { if (type instanceof ParameterizedType) { ParameterizedType genericType = (ParameterizedType) type; return getSimpleName(genericType.getRawType()) + getParameterizedTypeName(genericType); }/*www . jav a 2s.c om*/ if (type instanceof Class) { return _getSimpleName((Class) type); } return type.getTypeName(); }
From source file:io.apiman.manager.api.es.EsMarshallingTest.java
/** * Fabricates a new instance of the given bean type. Uses reflection to figure * out all the fields and assign generated values for each. *//*from w w w . j a v a2 s . c om*/ private static <T> T createBean(Class<T> beanClass) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException { T bean = beanClass.newInstance(); Map<String, String> beanProps = BeanUtils.describe(bean); for (String key : beanProps.keySet()) { try { Field declaredField = beanClass.getDeclaredField(key); Class<?> fieldType = declaredField.getType(); if (fieldType == String.class) { BeanUtils.setProperty(bean, key, StringUtils.upperCase(key)); } else if (fieldType == Boolean.class || fieldType == boolean.class) { BeanUtils.setProperty(bean, key, Boolean.TRUE); } else if (fieldType == Date.class) { BeanUtils.setProperty(bean, key, new Date(1)); } else if (fieldType == Long.class || fieldType == long.class) { BeanUtils.setProperty(bean, key, 17L); } else if (fieldType == Integer.class || fieldType == long.class) { BeanUtils.setProperty(bean, key, 11); } else if (fieldType == Set.class) { // Initialize to a linked hash set so that order is maintained. BeanUtils.setProperty(bean, key, new LinkedHashSet()); Type genericType = declaredField.getGenericType(); String typeName = genericType.getTypeName(); String typeClassName = typeName.substring(14, typeName.length() - 1); Class<?> typeClass = Class.forName(typeClassName); Set collection = (Set) BeanUtilsBean.getInstance().getPropertyUtils().getProperty(bean, key); populateSet(collection, typeClass); } else if (fieldType == Map.class) { Map<String, String> map = new LinkedHashMap<String, String>(); map.put("KEY-1", "VALUE-1"); map.put("KEY-2", "VALUE-2"); BeanUtils.setProperty(bean, key, map); } else if (fieldType.isEnum()) { BeanUtils.setProperty(bean, key, fieldType.getEnumConstants()[0]); } else if (fieldType.getPackage() != null && fieldType.getPackage().getName().startsWith("io.apiman.manager.api.beans")) { Object childBean = createBean(fieldType); BeanUtils.setProperty(bean, key, childBean); } else { throw new IllegalAccessException( "Failed to handle property named [" + key + "] type: " + fieldType.getSimpleName()); } // String capKey = StringUtils.capitalize(key); // System.out.println(key);; } catch (NoSuchFieldException e) { // Skip it - there is not really a bean property with this name! } } return bean; }
From source file:uk.gov.gchq.koryphe.signature.Signature.java
protected static Class getTypeClass(final Type type, final Map<TypeVariable<?>, Type> typeArgs) { Type rawType = type; if (rawType instanceof ParameterizedType) { rawType = ((ParameterizedType) type).getRawType(); }/*from w w w .j ava 2s . c o m*/ if (rawType instanceof Class) { return (Class) rawType; } if (rawType instanceof TypeVariable) { final Type t = typeArgs.get(rawType); if (null != t) { return getTypeClass(t, typeArgs); } } try { return Class.forName(rawType.getTypeName()); } catch (final ClassNotFoundException e) { // cannot resolve - default to UnknownGenericType; return UnknownGenericType.class; } }
From source file:org.opencb.commons.utils.CommandLineUtils.java
private static String getType(Type genericType) { String type;/* w w w . ja v a2 s .co m*/ if (genericType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) genericType; Type rawType = parameterizedType.getRawType(); if (rawType instanceof Class && Collection.class.isAssignableFrom((Class) rawType)) { return getType(parameterizedType.getActualTypeArguments()[0]) + "*"; } } type = genericType.getTypeName(); type = type.substring(1 + Math.max(type.lastIndexOf("."), type.lastIndexOf("$"))); type = Arrays.asList(org.apache.commons.lang3.StringUtils.splitByCharacterTypeCamelCase(type)).stream() .map(String::toUpperCase).collect(Collectors.joining("_")); if (type.equals("INTEGER")) { type = "INT"; } return type; }
From source file:io.servicecomb.swagger.generator.core.utils.ParamUtils.java
public static void setParameterType(Swagger swagger, Method method, int paramIdx, AbstractSerializableParameter<?> parameter) { Type paramType = ParamUtils.getGenericParameterType(method, paramIdx); ParamUtils.addDefinitions(swagger, paramType); Property property = ModelConverters.getInstance().readAsProperty(paramType); if (isComplexProperty(property)) { // ?????? String msg = String.format( "not allow complex type for %s parameter, method=%s:%s, paramIdx=%d, type=%s", parameter.getIn(), method.getDeclaringClass().getName(), method.getName(), paramIdx, paramType.getTypeName()); throw new Error(msg); }/*from w ww .j ava 2 s . c om*/ parameter.setProperty(property); }
From source file:org.apache.servicecomb.swagger.generator.core.utils.ParamUtils.java
public static void setParameterType(Swagger swagger, Method method, int paramIdx, AbstractSerializableParameter<?> parameter) { Type paramType = ParamUtils.getGenericParameterType(method, paramIdx); ParamUtils.addDefinitions(swagger, paramType); Property property = ModelConverters.getInstance().readAsProperty(paramType); if (isComplexProperty(property)) { // cannot set a simple parameter(header, query, etc.) as complex type String msg = String.format( "not allow complex type for %s parameter, method=%s:%s, paramIdx=%d, type=%s", parameter.getIn(), method.getDeclaringClass().getName(), method.getName(), paramIdx, paramType.getTypeName()); throw new Error(msg); }//from w w w. j a v a 2 s. co m parameter.setProperty(property); }
From source file:org.xwiki.edit.internal.DefaultEditConfiguration.java
@Override public String getDefaultEditor(Type dataType, String category) { String dataTypeName = dataType.getTypeName(); if (!StringUtils.isEmpty(category)) { dataTypeName += "#" + category; }// ww w . j ava 2 s. c o m // Get the default editor configured using the standard configuration properties and sources. String defaultEditor = getDefaultEditor(dataTypeName); if (StringUtils.isEmpty(defaultEditor)) { // A custom configuration can look for the default editor in custom configuration sources, using custom // properties, or it can simply return the default editor where there's no one configured. EditorConfiguration<?> customConfig = getCustomConfiguration(dataType); if (customConfig != null) { defaultEditor = customConfig.getDefaultEditor(category); } } return defaultEditor; }
From source file:io.servicecomb.common.rest.codec.param.BodyProcessorCreator.java
@Override public ParamValueProcessor create(Parameter parameter, Type genericParamType) { JavaType targetType = TypeFactory.defaultInstance().constructType(genericParamType); boolean rawJson = ClassUtils.isRawJsonType(parameter); if (genericParamType.getTypeName().equals(String.class.getTypeName()) && rawJson) { return new RawJsonBodyProcessor(targetType); }//from www . ja v a2 s . c o m return new BodyProcessor(targetType); }
From source file:ca.oson.json.util.ObjectUtil.java
public static <E> Class<E> getTypeClass(java.lang.reflect.Type type) { Class cl = type.getClass();//from ww w . j a va 2 s . c o m if (ComponentType.class.isAssignableFrom(cl)) { ComponentType componentType = (ComponentType) type; return componentType.getClassType(); } //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; }