Example usage for org.apache.commons.lang3.reflect TypeUtils isArrayType

List of usage examples for org.apache.commons.lang3.reflect TypeUtils isArrayType

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect TypeUtils isArrayType.

Prototype

public static boolean isArrayType(final Type type) 

Source Link

Document

Learn whether the specified type denotes an array type.

Usage

From source file:com.wavemaker.tools.apidocs.tools.parser.util.TypeUtil.java

/**
 * It will scan for given {@link Type}./* w w w  .  ja v a  2 s  . c  om*/
 * <p/>
 * For eg: Type is <code>List&ltString&gt</code>. It will Returns: Actual Type: List Type Arguments: String
 *
 * @param type type to scan
 * @return {@link TypeInformation} of given type.
 */
public static TypeInformation extractTypeInformation(Type type) {
    Class<?> actualType;
    if (TypeUtils.isArrayType(type)) { // conditions order is important.
        return getArrayTypeInformation(type);
    } else if (type instanceof Class<?>) {
        actualType = (Class<?>) type; // enums take strings only.
    } else if (type instanceof ParameterizedType) {
        return getParameterizedTypeTypeInformation((ParameterizedType) type);
    } else { // cases like WildCard Type and TypeVariable
        actualType = Object.class; // sending null doesn't make sense
    }
    return new TypeInformation(actualType, Collections.EMPTY_LIST, false, type);
}

From source file:de.ks.flatadocdb.defaults.ReflectionLuceneDocumentExtractor.java

protected DocField createDocField(Field f) {
    try {//from   ww  w . j a v  a  2s.  c  o m
        Class<?> type = f.getType();
        f.setAccessible(true);
        MethodHandle getter = MethodHandles.lookup().unreflectGetter(f);

        if (TypeUtils.isArrayType(type)) {
            return createArrayDocField(f, getter);
        } else if (Collection.class.isAssignableFrom(type)) {
            return createCollectionDocField(f, getter);
        } else if (String.class.equals(type)) {
            return createStringDocField(f, getter);
        } else if (LocalDateTime.class.equals(type)) {
            return createLocalDateTimeDocField(f, getter);
        } else {
            return new DocField(f, getter, (id, value) -> new StringField(id, String.valueOf(value),
                    org.apache.lucene.document.Field.Store.YES));
        }
    } catch (Exception e) {
        log.error("Could not extract docfield from {}", f, e);
        throw new RuntimeException(e);
    }
}

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]);
            }//w w w.j a  v  a  2 s. c  om
        }
        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.util.IndexedAccess.java

/**
 * Get the Java element type of a particular container type.
 * /* www. ja  v a2 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.evosuite.testcase.ImportsTestCodeVisitor.java

private void collectFromParameters(Type[] parameterTypes, List<VariableReference> parameters,
        boolean isGenericMethod, boolean isOverloaded, int startPos) {

    for (int i = startPos; i < parameters.size(); i++) {
        Type declaredParamType = parameterTypes[i];
        Type actualParamType = parameters.get(i).getType();
        getClassName(declaredParamType);
        getClassName(parameters.get(i));

        Class<?> rawParamClass = declaredParamType instanceof WildcardType ? Object.class
                : GenericTypeReflector.erase(declaredParamType);
        if (rawParamClass.isPrimitive()) {
            getClassName(rawParamClass);
            getClassName(ClassUtils.primitiveToWrapper(rawParamClass));
        } else if (isGenericMethod && !(declaredParamType instanceof WildcardType)) {

        } else if (!GenericClass.isAssignable(declaredParamType, actualParamType)) {

            if (TypeUtils.isArrayType(declaredParamType) && TypeUtils.isArrayType(actualParamType)) {
                Class<?> componentClass = GenericTypeReflector.erase(declaredParamType).getComponentType();
                if (componentClass.equals(Object.class)) {
                    GenericClass genericComponentClass = new GenericClass(componentClass);
                    if (genericComponentClass.hasWildcardOrTypeVariables()) {
                        // If we are assigning a generic array, then we don't need to cast
                    } else {
                        // If we are assigning a non-generic array, then we do need to cast
                        getClassName(declaredParamType);
                    }/* www .  ja  v  a  2  s . c  om*/
                } else { //if (!GenericClass.isAssignable(GenericTypeReflector.getArrayComponentType(declaredParamType), GenericTypeReflector.getArrayComponentType(actualParamType))) {
                    getClassName(declaredParamType);
                }
            } else if (!(actualParamType instanceof ParameterizedType)) {
                getClassName(declaredParamType);
            }
        } else {
            // We have to cast between wrappers and primitives in case there
            // are overloaded signatures. This could be optimized by checking
            // if there actually is a problem of overloaded signatures
            GenericClass parameterClass = new GenericClass(declaredParamType);
            if (parameterClass.isWrapperType() && parameters.get(i).isPrimitive()) {
                getClassName(declaredParamType);
            } else if (parameterClass.isPrimitive() && parameters.get(i).isWrapperType()) {
                getClassName(declaredParamType);
            } else if (isOverloaded) {
                // If there is an overloaded method, we need to cast to make sure we use the right version
                if (!declaredParamType.equals(actualParamType)) {
                    getClassName(declaredParamType);
                }
            }
        }
    }
}

From source file:org.evosuite.testcase.TestCodeVisitor.java

private String getParameterString(Type[] parameterTypes, List<VariableReference> parameters,
        boolean isGenericMethod, boolean isOverloaded, int startPos) {
    String parameterString = "";

    for (int i = startPos; i < parameters.size(); i++) {
        if (i > startPos) {
            parameterString += ", ";
        }//from w w w . j ava2 s.c om
        Type declaredParamType = parameterTypes[i];
        Type actualParamType = parameters.get(i).getType();
        String name = getVariableName(parameters.get(i));
        Class<?> rawParamClass = declaredParamType instanceof WildcardType ? Object.class
                : GenericTypeReflector.erase(declaredParamType);
        if (rawParamClass.isPrimitive() && name.equals("null")) {
            parameterString += getPrimitiveNullCast(rawParamClass);
        } else if (isGenericMethod && !(declaredParamType instanceof WildcardType)) {
            if (!declaredParamType.equals(actualParamType) || name.equals("null")) {
                parameterString += "(" + getTypeName(declaredParamType) + ") ";
                if (name.contains("(short"))
                    name = name.replace("(short)", "");
                if (name.contains("(byte"))
                    name = name.replace("(byte)", "");

            }
        } else if (name.equals("null")) {
            parameterString += "(" + getTypeName(declaredParamType) + ") ";
        } else if (!GenericClass.isAssignable(declaredParamType, actualParamType)) {

            if (TypeUtils.isArrayType(declaredParamType) && TypeUtils.isArrayType(actualParamType)) {
                Class<?> componentClass = GenericTypeReflector.erase(declaredParamType).getComponentType();
                if (componentClass.equals(Object.class)) {
                    GenericClass genericComponentClass = new GenericClass(componentClass);
                    if (genericComponentClass.hasWildcardOrTypeVariables()) {
                        // If we are assigning a generic array, then we don't need to cast

                    } else {
                        // If we are assigning a non-generic array, then we do need to cast
                        parameterString += "(" + getTypeName(declaredParamType) + ") ";
                    }
                } else { //if (!GenericClass.isAssignable(GenericTypeReflector.getArrayComponentType(declaredParamType), GenericTypeReflector.getArrayComponentType(actualParamType))) {
                    parameterString += "(" + getTypeName(declaredParamType) + ") ";
                }
            } else if (!(actualParamType instanceof ParameterizedType)) {
                parameterString += "(" + getTypeName(declaredParamType) + ") ";
            }
            if (name.contains("(short"))
                name = name.replace("(short)", "");
            if (name.contains("(byte"))
                name = name.replace("(byte)", "");
            //}
        } else {
            // We have to cast between wrappers and primitives in case there
            // are overloaded signatures. This could be optimized by checking
            // if there actually is a problem of overloaded signatures
            GenericClass parameterClass = new GenericClass(declaredParamType);
            if (parameterClass.isWrapperType() && parameters.get(i).isPrimitive()) {
                parameterString += "(" + getTypeName(declaredParamType) + ") ";
            } else if (parameterClass.isPrimitive() && parameters.get(i).isWrapperType()) {
                parameterString += "(" + getTypeName(declaredParamType) + ") ";
            } else if (isOverloaded) {
                // If there is an overloaded method, we need to cast to make sure we use the right version
                if (!declaredParamType.equals(actualParamType)) {
                    parameterString += "(" + getTypeName(declaredParamType) + ") ";
                }
            }
        }

        parameterString += name;
    }

    return parameterString;
}

From source file:org.xwiki.job.handler.internal.question.QuestionJobResourceReferenceHandler.java

private boolean isIterable(PropertyDescriptor propertyDescriptor) {
    Type type = propertyDescriptor.getPropertyType();

    if (TypeUtils.isArrayType(type)) {
        return true;
    }//ww w  . j a  v  a 2s  .com

    return TypeUtils.isAssignable(type, Iterable.class);
}

From source file:therian.operator.add.AddToArray.java

@Override
public boolean supports(TherianContext context, Add<?, ?> add) {
    if (!(TypeUtils.isArrayType(add.getTargetPosition().getType())
            && Positions.isWritable(add.getTargetPosition()))) {
        return false;
    }/*from w ww. j  ava  2 s  . com*/
    final Type targetElementType = context.eval(GetElementType.of(add.getTargetPosition()));
    return TypeUtils.isInstance(add.getSourcePosition().getValue(), targetElementType);
}

From source file:therian.operator.addall.AddAllToArray.java

@Override
public boolean supports(TherianContext context, AddAll<?, ?> addAll) {
    if (!(TypeUtils.isArrayType(addAll.getTargetPosition().getType())
            && Positions.isWritable(addAll.getTargetPosition()))) {
        return false;
    }/*from www .  j av a2s. c  o m*/
    final GetElementType<?> getSourceElementType = GetElementType.of(addAll.getSourcePosition());
    final GetElementType<?> getTargetElementType = GetElementType.of(addAll.getTargetPosition());
    if (!(context.supports(getSourceElementType) && context.supports(getTargetElementType))) {
        return false;
    }
    final Type sourceElementType = context.eval(getSourceElementType);
    final Type targetElementType = context.eval(getTargetElementType);
    return TypeUtils.isAssignable(sourceElementType, targetElementType) && context.supports(
            Convert.to(Positions.readWrite(addAll.getTargetPosition().getType()), addAll.getSourcePosition()));
}

From source file:therian.operator.convert.CollectionToArray.java

@Override
public boolean supports(TherianContext context, Convert<? extends Collection, ?> convert) {
    if (!super.supports(context, convert)) {
        return false;
    }//w w  w . j a  v  a 2 s.  com
    if (convert.getSourcePosition().getValue() == null) {
        return false;
    }
    if (!TypeUtils.isArrayType(convert.getTargetPosition().getType())) {
        return false;
    }
    final GetElementType<?> getTargetElementType = GetElementType.of(convert.getTargetPosition());
    if (!context.supports(getTargetElementType)) {
        return false;
    }
    final Type targetElementType = context.eval(getTargetElementType);

    for (Object element : convert.getSourcePosition().getValue()) {
        if (!TypeUtils.isInstance(element, targetElementType)) {
            return false;
        }
    }
    return true;
}