Java Code Examples for java.lang.reflect.Type#getTypeName()
The following examples show how to use
java.lang.reflect.Type#getTypeName() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphQLSchemaGenerator.java From graphql-spqr with Apache License 2.0 | 6 votes |
private void checkType(Type type) { if (type == null) { throw TypeMappingException.unknownType(); } Class<?> clazz = ClassUtils.getRawType(type); if (ClassUtils.isProxy(clazz)) { throw new TypeMappingException("The registered object of type " + clazz.getName() + " appears to be a dynamically generated proxy, so its type can not be reliably determined." + " Provide the type explicitly when registering the bean." + " For details and solutions see " + Urls.Errors.DYNAMIC_PROXIES); } if (ClassUtils.isMissingTypeParameters(type)) { throw new TypeMappingException("The registered object is of generic type " + type.getTypeName() + "." + " Provide the full type explicitly when registering the bean." + " For details and solutions see " + Urls.Errors.TOP_LEVEL_GENERICS); } }
Example 2
Source File: Resource.java From zeppelin with Apache License 2.0 | 6 votes |
private Object [] convertParams(Type[] types, Object [] params) { Object [] converted = new Object[types.length]; for (int i = 0; i < types.length; i++) { Type type = types[i]; String typeName; if (type instanceof ParameterizedType) { typeName = ((ParameterizedType) type).getRawType().getTypeName(); } else { typeName = type.getTypeName(); } Object param = params[i]; if (param == null) { converted[i] = null; } else if (param.getClass().getName().equals(typeName)) { converted[i] = param; } else { // try to convert param converted[i] = gson.fromJson(gson.toJson(param), type); } } return converted; }
Example 3
Source File: ConfigSectionGenerator.java From bootique with Apache License 2.0 | 6 votes |
protected boolean isImpliedType(Type type) { String typeName = type.getTypeName(); switch (typeName) { case "boolean": case "java.lang.Boolean": case "int": case "java.lang.Integer": case "byte": case "java.lang.Byte": case "double": case "java.lang.Double": case "float": case "java.lang.Float": case "short": case "java.lang.Short": case "long": case "java.lang.Long": case "java.lang.String": case "io.bootique.resource.ResourceFactory": case "io.bootique.resource.FolderResourceFactory": return true; default: return false; } }
Example 4
Source File: DtoImplClientTemplate.java From che with Eclipse Public License 2.0 | 6 votes |
/** Returns the fully-qualified type name including parameters. */ private String getFqParameterizedName(Type type) { if (type instanceof Class<?>) { return ((Class<?>) type).getCanonicalName(); } else if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; StringBuilder sb = new StringBuilder(getRawClass(pType).getCanonicalName()); sb.append('<'); final Type[] actualTypeArguments = pType.getActualTypeArguments(); for (int i = 0; i < actualTypeArguments.length; i++) { if (i > 0) { sb.append(", "); } sb.append(getFqParameterizedName(actualTypeArguments[i])); } sb.append('>'); return sb.toString(); } else { throw new IllegalArgumentException( "Cannot handle type '" + type == null ? null : type.getTypeName() + "'."); } }
Example 5
Source File: DTOHelper.java From che with Eclipse Public License 2.0 | 6 votes |
/** Convert Java type to TypeScript type */ public static String convertType(Type type) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type rawType = parameterizedType.getRawType(); return convertParametrizedType(type, parameterizedType, rawType); } else if (String.class.equals(type) || (type instanceof Class && ((Class) type).isEnum())) { // Maybe find a better enum type for typescript return "string"; } else if (Integer.class.equals(type) || Integer.TYPE.equals(type) || Long.class.equals(type) || Long.TYPE.equals(type) || Double.class.equals(type) || Double.TYPE.equals(type) || Float.TYPE.equals(type)) { return "number"; } else if (Boolean.class.equals(type) || Boolean.TYPE.equals(type)) { return "boolean"; } else if (Serializable.class.equals(type)) { return "string | number | boolean"; } return type.getTypeName(); }
Example 6
Source File: ExecuteArgAnalyzer.java From lastaflute with Apache License 2.0 | 5 votes |
protected boolean isBeanActionFormType(Type parameterType) { final String typeName = parameterType.getTypeName(); if (isJavaStandardClass(typeName)) { return false; } return determineBeanActionFormTypeName(typeName); }
Example 7
Source File: MetadataService.java From agrest with Apache License 2.0 | 5 votes |
@Override public <T> AgEntity<T> getAgEntityByType(Type entityType) { @SuppressWarnings("unchecked") Class<T> typeClass = (Class<T>) Types.getClassForTypeArgument(entityType).orElse(Object.class); AgEntity<T> entity = getAgEntity(typeClass); if (entity == null) { throw new AgException(Status.INTERNAL_SERVER_ERROR, "EntityUpdate type '" + entityType.getTypeName() + "' is not an entity"); } return entity; }
Example 8
Source File: InstantiatorProvider.java From dalesbred with MIT License | 5 votes |
/** * Returns conversion for converting value of source-type to target-type, or throws exception if * there's no such conversion. */ public @NotNull TypeConversion getConversionFromDbValue(@NotNull Type source, @NotNull Type target) { TypeConversion conversion = findConversionFromDbValue(source, target).orElse(null); if (conversion != null) return conversion; else throw new InstantiationFailureException("could not find a conversion from " + source.getTypeName() + " to " + target.getTypeName()); }
Example 9
Source File: Helper.java From vertx-codegen with Apache License 2.0 | 5 votes |
/** * Return the type of a type parameter element of a given type element when that type parameter * element is parameterized by a sub type, directly or indirectly. When the type parameter cannot * be resolve, null is returned. * * @param type the sub type for which the type parameter is parameterized * @param typeParam the type parameter to resolve * @return the type parameterizing the type parameter */ public static <T> Type resolveTypeParameter(Type type, java.lang.reflect.TypeVariable<java.lang.Class<T>> typeParam) { if (type instanceof Class<?>) { Class<?> classType = (Class<?>) type; if (Stream.of(classType.getTypeParameters()).filter(tp -> tp.equals(typeParam)).findFirst().isPresent()) { return typeParam; } List<Type> superTypes = new ArrayList<>(); if (classType.getGenericSuperclass() != null) { superTypes.add(classType.getGenericSuperclass()); } Collections.addAll(superTypes, classType.getGenericInterfaces()); for (Type superType : superTypes) { Type resolved = resolveTypeParameter(superType, typeParam); if (resolved != null) { return resolved; } } } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type rawType = parameterizedType.getRawType(); Type resolvedType = resolveTypeParameter(rawType, typeParam); if (resolvedType instanceof java.lang.reflect.TypeVariable<?>) { GenericDeclaration owner = ((java.lang.reflect.TypeVariable) resolvedType).getGenericDeclaration(); if (owner.equals(rawType)) { java.lang.reflect.TypeVariable<?>[] typeParams = owner.getTypeParameters(); for (int i = 0;i < typeParams.length;i++) { if (typeParams[i].equals(resolvedType)) { return parameterizedType.getActualTypeArguments()[i]; } } } } } else { throw new UnsupportedOperationException("Todo " + type.getTypeName() + " " + type.getClass().getName()); } return null; }
Example 10
Source File: JavassistGrpcFactory.java From joyrpc with Apache License 2.0 | 5 votes |
@Override protected Class<?> buildRequestClass(final Class<?> clz, final Method method, final Naming naming) throws Exception { //ClassPool:CtClass对象的容器 ClassPool pool = ClassPool.getDefault(); //通过ClassPool生成一个public新类 CtClass ctClass = pool.makeClass(naming.getFullName()); ctClass.setInterfaces(new CtClass[]{pool.getCtClass(Serializable.class.getName()), pool.getCtClass(MethodArgs.class.getName())}); //添加字段 CtField ctField; String name; String typeName; Type type; int i = 0; StringBuilder toArgs = new StringBuilder(100).append("public Object[] toArgs(){\n\treturn new Object[]{"); StringBuilder toFields = new StringBuilder(200).append("public void toFields(Object[] args){\n"); for (Parameter parameter : method.getParameters()) { name = parameter.getName(); type = parameter.getParameterizedType(); typeName = type.getTypeName(); ctField = new CtField(pool.getCtClass(typeName), name, ctClass); ctField.setModifiers(Modifier.PRIVATE); ctClass.addField(ctField); if (i > 0) { toArgs.append(','); } toArgs.append(name); toFields.append('\t').append(name).append("=(").append(typeName).append(")args[").append(i).append("];\n"); name = name.substring(0, 1).toUpperCase() + name.substring(1); ctClass.addMethod(CtNewMethod.getter((boolean.class == type ? "is" : "get") + name, ctField)); ctClass.addMethod(CtNewMethod.setter("set" + name, ctField)); i++; } toArgs.append("};\n};"); toFields.append("};"); ctClass.addMethod(CtMethod.make(toArgs.toString(), ctClass)); ctClass.addMethod(CtMethod.make(toFields.toString(), ctClass)); return ctClass.toClass(); }
Example 11
Source File: ProtoBufSerializer.java From AutoLoadCache with Apache License 2.0 | 5 votes |
@Override public CacheWrapper<Object> deserialize(byte[] bytes, Type returnType) throws Exception { if (bytes == null || bytes.length == 0) { return null; } CacheWrapper<Object> cacheWrapper = new CacheWrapper<>(); val byteBuf = readPool.borrowObject(); byteBuf.setBytes(bytes); cacheWrapper.setExpire(byteBuf.readInt()); cacheWrapper.setLastLoadTime(byteBuf.readLong()); bytes = byteBuf.readableBytes(); readPool.returnObject(byteBuf); if (bytes == null || bytes.length == 0) { return cacheWrapper; } String typeName = null; if (!(returnType instanceof ParameterizedType)) { typeName = returnType.getTypeName(); } Class clazz = getMessageClass(typeName); if (null != clazz && Message.class.isAssignableFrom(clazz)) { Lambda lambda = getLambda(clazz); Object obj = lambda.invoke_for_Object(new ByteArrayInputStream(bytes)); cacheWrapper.setCacheObject(obj); } else { cacheWrapper.setCacheObject(MAPPER.readValue(bytes, MAPPER.constructType(returnType))); } return cacheWrapper; }
Example 12
Source File: EsMarshallingTest.java From apiman with Apache License 2.0 | 4 votes |
/** * Fabricates a new instance of the given bean type. Uses reflection to figure * out all the fields and assign generated values for each. */ 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; }
Example 13
Source File: InputParsingException.java From graphql-spqr with Apache License 2.0 | 4 votes |
public InputParsingException(Object input, Type targetType, Throwable cause) { super("Value: " + input + " could not be parsed into an instance of " + targetType.getTypeName(), cause); }
Example 14
Source File: MessageType.java From ja-micro with Apache License 2.0 | 4 votes |
public static MessageType of(Type t) { return new MessageType(t.getTypeName()); }
Example 15
Source File: CodecSubtype.java From datakernel with Apache License 2.0 | 4 votes |
/** * Add a subtype along with its codec and string tag which is extracted from the class name */ public CodecSubtype<T> with(Type type, StructuredCodec<? extends T> adapter) { String name = type.getTypeName(); name = name.substring(max(name.lastIndexOf('.'), name.lastIndexOf('$')) + 1); return with(type, name, adapter); }
Example 16
Source File: BeanFactory.java From core-ng-project with Apache License 2.0 | 4 votes |
private boolean isTypeOf(Object instance, Type type) { if (type instanceof Class) return ((Class<?>) type).isInstance(instance); if (type instanceof ParameterizedType) return isTypeOf(instance, ((ParameterizedType) type).getRawType()); throw new Error("not supported type, type=" + type.getTypeName()); }
Example 17
Source File: TypeMappingException.java From graphql-spqr with Apache License 2.0 | 4 votes |
public static TypeMappingException ambiguousType(Type type) { return new TypeMappingException("Type " + type.getTypeName() + " is unbounded or missing generic type parameters"); }
Example 18
Source File: ReflectionUtils.java From jkes with Apache License 2.0 | 3 votes |
public static String getTypeName(Method method) { Type type = method.getGenericReturnType(); String typeName = type.getTypeName(); // ex: java.util.List<com.timeyang.search.entity.Person>, java.lang.Long return typeName; }
Example 19
Source File: ReflectionUtils.java From jkes with Apache License 2.0 | 3 votes |
/** * If return type is genetic type, then return last parameterized type, else return the formal return type name of the method represented by this {@code Method} object. * @param method {@link Method} method * @return If return type is genetic type, then return last parameterized type, else return the formal return type name of the method represented by this {@code Method} object. */ public static String getInnermostType(Method method) { Type type = method.getGenericReturnType(); String typeName = type.getTypeName(); // ex: java.util.List<com.timeyang.search.entity.Person>, java.lang.Long String[] types = typeName.split(",\\s*|<|<|>+"); return types[types.length - 1]; }
Example 20
Source File: ReflectionUtils.java From jkes with Apache License 2.0 | 3 votes |
/** * If the field object is a generic type, return innermost type; else return field type name directly * @param field Field object * @return If the field object is a generic type, return innermost type; else return field type name directly */ public static String getInnermostType(Field field) { Type type = field.getGenericType(); String typeName = type.getTypeName(); // ex: java.util.List<com.timeyang.search.entity.Person>, java.lang.Long String[] types = typeName.split(",\\s*|<|<|>+"); return types[types.length - 1]; }