List of usage examples for org.apache.commons.lang3.reflect TypeUtils getRawType
public static Class<?> getRawType(final Type type, final Type assigningType)
Get the raw type of a Java type, given its context.
From source file:com.wavemaker.tools.apidocs.tools.parser.util.TypeUtil.java
protected static TypeInformation getArrayTypeInformation(Type type) { Class<?> actualType;//from w w w . jav a2 s.com List<Class<?>> typeArguments = new LinkedList<>(); if (type instanceof GenericArrayType) { // for cases like T[] Type rawType = ((GenericArrayType) type).getGenericComponentType(); Class<?> rawComponentType = extractTypeInformation(rawType).getActualType(); actualType = Array.newInstance(rawComponentType, 0).getClass(); // instantiating array type typeArguments.add(rawComponentType); } else { actualType = TypeUtils.getRawType(type, null); typeArguments.add((Class<?>) TypeUtils.getArrayComponentType(type));// check type. } return new TypeInformation(actualType, typeArguments, true, type); }
From source file:cz.jirutka.validator.collection.CommonEachValidator.java
protected Class<?> determineTargetType(Class<? extends ConstraintValidator<?, ?>> validatorClass) { TypeVariable<?> typeVar = ConstraintValidator.class.getTypeParameters()[1]; return TypeUtils.getRawType(typeVar, validatorClass); }
From source file:com.grepcurl.random.ObjectGenerator.java
protected Class<?> _toClass(Type type) { return TypeUtils.getRawType(type, type); }
From source file:org.apache.bval.jsr.util.ValidationContextTraversal.java
/** * Set the type of the expression processed thus far. * /*from ww w . java 2 s . c o m*/ * @param type */ protected void setType(Type type) { this.rawType = TypeUtils.getRawType(type, this.type); this.type = type; }
From source file:org.apache.bval.model.MetaProperty.java
/** * Resolve the type of this property to a class. * @return Class, <code>null</code> if cannot be determined *///www. j a v a 2 s . c o m public Class<?> getTypeClass() { Type targetType = type instanceof DynaType ? ((DynaType) type).getRawType() : type; if (targetType == null) { return null; } Type assigningType = getParentMetaBean() == null ? null : getParentMetaBean().getBeanClass(); return TypeUtils.getRawType(targetType, assigningType); }
From source file:org.apache.bval.util.KeyedAccess.java
/** * {@inheritDoc}//from ww w . ja va 2s . c o m */ @Override public Object get(Object instance) { if (instance instanceof Map<?, ?>) { Map<?, ?> map = (Map<?, ?>) instance; Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Map.class); Type keyType = TypeUtils.unrollVariables(typeArguments, MAP_TYPEVARS[0]); if (key == null || keyType == null || TypeUtils.isInstance(key, keyType)) { return map.get(key); } if (key instanceof String) { String name = (String) key; Class<?> rawKeyType = TypeUtils.getRawType(keyType, containerType); if (rawKeyType.isEnum()) { @SuppressWarnings({ "unchecked", "rawtypes" }) final Object result = map.get(Enum.valueOf((Class<? extends Enum>) rawKeyType, name)); return result; } for (Map.Entry<?, ?> e : map.entrySet()) { if (name.equals(e.getKey())) { return e.getValue(); } } } } return null; }
From source file:org.grouplens.grapht.util.Types.java
/** * Get the type that is provided by a given implementation of * {@link Provider}.//w w w . j a v a2s. co m * * @param providerClass The provider's class * @return The provided class type * @throws IllegalArgumentException if the class doesn't actually implement * Provider */ public static Class<?> getProvidedType(Class<? extends Provider<?>> providerClass) { com.google.common.base.Preconditions.checkArgument(Provider.class.isAssignableFrom(providerClass), "class is not Provider class"); Map<TypeVariable<?>, Type> bindings = TypeUtils.getTypeArguments(providerClass, Provider.class); Type boundType = bindings.get(PROVIDER_TYPE_VAR); if (boundType == null || boundType instanceof TypeVariable) { throw new IllegalArgumentException("Class provided by " + providerClass.getName() + " is generic"); } final Class<?> inferredType = TypeUtils.getRawType(bindings.get(PROVIDER_TYPE_VAR), null); try { final Class<?> observedType = providerClass.getMethod("get").getReturnType(); if (inferredType != null && inferredType.isAssignableFrom(observedType)) { return observedType; } else { return inferredType; } } catch (NoSuchMethodException e) { throw new IllegalArgumentException("Class does not implement get()", e); } }
From source file:org.lenskit.gradle.LenskitPlugin.java
public void apply(Project project) { final LenskitExtension lenskit = project.getExtensions().create("lenskit", LenskitExtension.class, project); for (MetaProperty prop : DefaultGroovyMethods.getMetaClass(lenskit).getProperties()) { String prjProp = "lenskit." + prop.getName(); if (project.hasProperty(prjProp)) { Object val = project.findProperty(prjProp); String vstr = val != null ? val.toString() : null; logger.info("setting property {} to {}", prjProp, val); Class type = prop.getType(); Consumer<Object> set = (v) -> prop.setProperty(lenskit, v); if (type.equals(Property.class)) { Method m = null;/*from w ww . j a v a 2 s . c o m*/ try { m = lenskit.getClass().getMethod("get" + StringUtils.capitalize(prop.getName())); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } Type t = m.getGenericReturnType(); Map<TypeVariable<?>, Type> args = TypeUtils.getTypeArguments(t, Property.class); Type rt = args.get(Property.class.getTypeParameters()[0]); type = TypeUtils.getRawType(rt, Object.class); set = (v) -> ((Property) prop.getProperty(lenskit)).set(v); } if (type.equals(List.class)) {// if the type is list update the val using strtokenizer StringTokenizer tok = new StringTokenizer(vstr, StringMatcherFactory.INSTANCE.splitMatcher(), StringMatcherFactory.INSTANCE.quoteMatcher()); val = DefaultGroovyMethods.toList(tok); } else if (type.equals(String.class)) { val = vstr; } else { val = StringConvert.INSTANCE.convertFromString(type, vstr); } set.accept(val); } } }
From source file:org.nuxeo.ecm.core.io.registry.reflect.MarshallerInspector.java
/** * Get the Java class and generic type managed by this marshaller. If not found, search in the parent. * * @param clazz The marshaller class to analyse. * @since 7.2/*from w w w. j a v a 2 s .co m*/ */ private void loadMarshalledType(Class<?> clazz) { if (isWriter() || isReader()) { Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(clazz, Marshaller.class); for (Map.Entry<TypeVariable<?>, Type> entry : typeArguments.entrySet()) { if (Marshaller.class.equals(entry.getKey().getGenericDeclaration())) { genericType = TypeUtils.unrollVariables(typeArguments, entry.getValue()); marshalledType = TypeUtils.getRawType(genericType, null); break; } } } }
From source file:therian.Operation.java
/** * Learn whether {@code operator} seems to implement {@code this}. * * @param operator to check// ww w.ja va 2s . com * @return boolean */ public boolean matches(Operator<?> operator) { final Type expectedType = TypeUtils.unrollVariables( TypeUtils.getTypeArguments(operator.getClass(), Operator.class), Operator.class.getTypeParameters()[0]); if (!TypeUtils.isInstance(this, expectedType)) { return false; } final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(expectedType, Operation.class); for (Class<?> c : ClassUtils.hierarchy(TypeUtils.getRawType(expectedType, operator.getClass()))) { if (c.equals(Operation.class)) { break; } for (TypeVariable<?> var : c.getTypeParameters()) { Type type = Types.resolveAt(this, var, typeArguments); if (type == null || typeArguments == null) { continue; } if (type instanceof Class<?> && ((Class<?>) type).isPrimitive()) { type = ClassUtils.primitiveToWrapper((Class<?>) type); } if (!TypeUtils.isAssignable(type, TypeUtils.unrollVariables(typeArguments, var))) { return false; } } } return true; }