List of usage examples for org.apache.commons.lang3.reflect TypeUtils getArrayComponentType
public static Type getArrayComponentType(final Type type)
From source file:com.wavemaker.tools.apidocs.tools.parser.util.TypeUtil.java
protected static TypeInformation getArrayTypeInformation(Type type) { Class<?> actualType;/*from w ww .j av a 2 s. c o m*/ 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:de.ks.flatadocdb.defaults.ReflectionLuceneDocumentExtractor.java
protected boolean filterField(Field f) { boolean noStatic = !Modifier.isStatic(f.getModifiers()); if (noStatic) { Class<?> type = f.getType(); boolean validType = isValidBaseType(type); Type genericType = f.getGenericType(); if (!validType && Collection.class.isAssignableFrom(type) && genericType instanceof ParameterizedType) { Type[] actualTypeArguments = ((ParameterizedType) genericType).getActualTypeArguments(); if (actualTypeArguments.length == 1 && actualTypeArguments[0] instanceof Class) { validType = isValidBaseType((Class<?>) actualTypeArguments[0]); }/*from w w w. j a v a 2s . c o m*/ } if (!validType && TypeUtils.isArrayType(type)) { Type arrayComponentType = TypeUtils.getArrayComponentType(type); if (arrayComponentType instanceof Class) { validType = isValidBaseType((Class<?>) arrayComponentType); } } return validType; } return false; }
From source file:org.apache.bval.jsr.ConstraintValidation.java
private static <A extends Annotation> Map<Type, Collection<Class<? extends ConstraintValidator<A, ?>>>> getValidatorsTypes( Class<? extends ConstraintValidator<A, ?>>[] constraintValidatorClasses) { final Map<Type, Collection<Class<? extends ConstraintValidator<A, ?>>>> validatorsTypes = new HashMap<Type, Collection<Class<? extends ConstraintValidator<A, ?>>>>(); for (Class<? extends ConstraintValidator<A, ?>> validatorType : constraintValidatorClasses) { Type validatedType = TypeUtils.getTypeArguments(validatorType, ConstraintValidator.class) .get(ConstraintValidator.class.getTypeParameters()[1]); if (validatedType == null) { throw new ValidationException( String.format("Could not detect validated type for %s", validatorType)); }/* w w w. ja v a 2 s . c o m*/ if (validatedType instanceof GenericArrayType) { final Type componentType = TypeUtils.getArrayComponentType(validatedType); if (componentType instanceof Class<?>) { validatedType = Array.newInstance((Class<?>) componentType, 0).getClass(); } } if (!validatorsTypes.containsKey(validatedType)) { validatorsTypes.put(validatedType, new ArrayList<Class<? extends ConstraintValidator<A, ?>>>()); } validatorsTypes.get(validatedType).add(validatorType); } return validatorsTypes; }
From source file:org.apache.bval.util.IndexedAccess.java
/** * Get the Java element type of a particular container type. * /*from www . j a v a 2 s . c o m*/ * @param containerType * @return Type or <code>null</code> if <code>containerType</code> is not * some type of {@link Iterable} or array */ public static Type getJavaElementType(Type containerType) { if (TypeUtils.isArrayType(containerType)) { return TypeUtils.getArrayComponentType(containerType); } if (TypeUtils.isAssignable(containerType, Iterable.class)) { Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Iterable.class); return ObjectUtils.defaultIfNull(TypeUtils.unrollVariables(typeArguments, ITERABLE_TYPE), Object.class); } return null; }
From source file:org.wrml.runtime.syntax.DefaultSyntaxLoader.java
@Override @SuppressWarnings("unchecked") public final <T> T[] parseSyntacticTextArray(final String[] textArray, final T[] destArray) { final Type arrayComponentType = TypeUtils.getArrayComponentType(destArray.getClass()); for (int i = 0; i < textArray.length; i++) { destArray[i] = (T) parseSyntacticText(textArray[i], arrayComponentType); }//from ww w . j a v a 2s . c o m return destArray; }
From source file:therian.operator.convert.DefaultToListConverter.java
@Override public boolean perform(TherianContext context, Convert<? extends Object, ? super List> convert) { final Object source = convert.getSourcePosition().getValue(); if (source == null) { return false; }/* ww w.j a va2 s .c o m*/ final List<?> list; if (TypeUtils.isArrayType(convert.getSourcePosition().getType())) { final Object[] array; if (source instanceof Object[]) { array = (Object[]) source; } else { final Class<?> primitiveType = (Class<?>) TypeUtils .getArrayComponentType(convert.getSourcePosition().getType()); final int len = Array.getLength(source); array = (Object[]) Array.newInstance(ClassUtils.primitiveToWrapper(primitiveType), len); for (int i = 0; i < len; i++) { array[i] = Array.get(source, i); } } list = Arrays.asList(array); } else { list = Collections.singletonList(convert.getSourcePosition().getValue()); } convert.getTargetPosition().setValue(list); return true; }
From source file:therian.operator.getelementtype.GetArrayElementType.java
@Override public boolean perform(TherianContext context, GetElementType<Object> op) { op.setResult(TypeUtils.getArrayComponentType(op.getTypedItem().getType())); return true;// w w w . j ava 2 s . co m }
From source file:therian.position.relative.Element.java
public static <T> PositionFactory<Object, T> atArrayIndex(int index) { @SuppressWarnings("unchecked") final PositionFactory<Object, T> result = new PositionFactory<Object, T>(index, new GetTypeMixin<T>(index) { @Override//from w w w . j av a 2s. c o m protected <P> Type evaluateElementType(Position<P> parentPosition) { return TypeUtils.getArrayComponentType(parentPosition.getType()); } }) { @Override public <P> RelativePosition.ReadWrite<P, T> of(Position.Readable<P> parentPosition) { final Type parentType = parentPosition.getType(); Validate.isTrue(TypeUtils.isArrayType(parentType), "%s is not an array type", parentType); return super.of(parentPosition); } }; return result; }