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

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

Introduction

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

Prototype

public static Class<?> getRawType(final Type type, final Type assigningType) 

Source Link

Document

Get the raw type of a Java type, given its context.

Usage

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

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//from   ww w. j a  v a 2s .c o m
public boolean perform(TherianContext context, Add<?, ?> add) {
    final Type targetElementType = context.eval(GetElementType.of(add.getTargetPosition()));
    final Position.Writable target = (Position.Writable) add.getTargetPosition();
    final int origSize = context.eval(Size.of(add.getTargetPosition()));
    final Object enlarged = Array.newInstance(TypeUtils.getRawType(targetElementType, null), origSize + 1);
    if (origSize > 0) {
        System.arraycopy(add.getTargetPosition().getValue(), 0, enlarged, 0, origSize);
    }
    Array.set(enlarged, origSize, add.getSourcePosition().getValue());
    target.setValue(enlarged);
    add.setResult(true);
    return true;
}

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

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//  w w w . ja  va  2  s . c o  m
public boolean perform(TherianContext context, AddAll<?, ?> addAll) {
    Object array = context.eval(Convert.to(Positions.<Object>readWrite(addAll.getTargetPosition().getType()),
            addAll.getSourcePosition()));

    final Position.Writable target = (Position.Writable) addAll.getTargetPosition();
    final int origSize = context.eval(Size.of(addAll.getTargetPosition()));
    if (origSize > 0) {
        final int toAdd = context.eval(Size.of(Positions.readOnly(array)));
        final Type targetElementType = context.eval(GetElementType.of(addAll.getTargetPosition()));
        final Object enlarged = Array.newInstance(TypeUtils.getRawType(targetElementType, null),
                origSize + toAdd);
        System.arraycopy(addAll.getTargetPosition().getValue(), 0, enlarged, 0, origSize);
        System.arraycopy(array, 0, enlarged, origSize, toAdd);
        array = enlarged;
    }
    target.setValue(array);
    addAll.setResult(true);
    return true;
}

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

/**
 * Learn whether a {@link Convert} operation's source value is (already) an instance of its target type,
 *
 * @param convert/*from  w  ww. jav  a  2  s .c om*/
 * @return boolean
 */
protected final boolean isNoop(Convert<?, ?> convert) {
    Type sourceType = convert.getSourcePosition().getType();
    if (ParameterizedType.class.isInstance(sourceType) && convert.getSourcePosition().getValue() != null) {
        sourceType = Types.narrowestParameterizedType(convert.getSourcePosition().getValue().getClass(),
                (ParameterizedType) sourceType);
    }
    if (TypeUtils.isAssignable(sourceType, convert.getTargetPosition().getType())) {
        if (ParameterizedType.class.isInstance(convert.getTargetPosition().getType())) {
            // make sure all type params of target position are accounted for by source before declaring it a noop:
            final Class<?> rawTargetType = TypeUtils.getRawType(convert.getTargetPosition().getType(), null);
            final Map<TypeVariable<?>, Type> typeMappings = TypeUtils.getTypeArguments(sourceType,
                    rawTargetType);

            for (TypeVariable<?> v : rawTargetType.getTypeParameters()) {
                if (typeMappings.get(v) == null) {
                    return false;
                }
                if (typeMappings.get(v) instanceof TypeVariable<?>) {
                    return false;
                }
            }
        }
        return true;
    }
    return false;
}

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

@SuppressWarnings("unchecked")
@Override//from  w ww  . jav a 2s  . c om
public boolean perform(TherianContext context, Convert<? extends Collection, ?> convert) {
    final Type targetElementType = context.eval(GetElementType.of(convert.getTargetPosition()));
    final int size = convert.getSourcePosition().getValue().size();

    final Object result = Array.newInstance(TypeUtils.getRawType(targetElementType, null), size);

    final Object[] toFill;
    final boolean primitiveTargetElementType = targetElementType instanceof Class<?>
            && ((Class<?>) targetElementType).isPrimitive();

    if (primitiveTargetElementType) {
        toFill = (Object[]) Array.newInstance(ClassUtils.primitiveToWrapper((Class<?>) targetElementType),
                size);
    } else {
        toFill = (Object[]) result;
    }
    convert.getSourcePosition().getValue().toArray(toFill);
    if (primitiveTargetElementType) {
        for (int i = 0; i < size; i++) {
            Array.set(result, i, toFill[i]);
        }
    }
    ((Position.Writable<Object>) convert.getTargetPosition()).setValue(result);
    return true;
}

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

private static Class<?> getRawTargetType(Convert<?, ?> operation) {
    return TypeUtils.getRawType(operation.getTargetPosition().getType(), null);
}

From source file:therian.util.BeanProperties.java

public static Set<String> getPropertyNames(ReturnProperties returnProperties, TherianContext context,
        Position.Readable<?> position) {

    List<? extends FeatureDescriptor> descriptors;
    // first try ELResolver:

    try {//from  w ww  .  j  a va 2 s .c  o m
        descriptors = IteratorUtils
                .toList(context.getELResolver().getFeatureDescriptors(context, position.getValue()));
    } catch (Exception e) {
        descriptors = null;
    }

    if (CollectionUtils.isEmpty(descriptors)) {
        // java.beans introspection; on RT type if available, else raw position type:
        final Class<?> beanType;
        if (position.getValue() == null) {
            beanType = TypeUtils.getRawType(position.getType(), null);
        } else {
            beanType = position.getValue().getClass();
        }
        try {
            descriptors = Arrays.asList(Introspector.getBeanInfo(beanType).getPropertyDescriptors());
        } catch (IntrospectionException e1) {
            return Collections.emptySet();
        }
    }

    final Set<String> result = new HashSet<>();
    for (final FeatureDescriptor fd : descriptors) {
        final String name = fd.getName();
        if (returnProperties == ReturnProperties.WRITABLE) {
            try {
                if (context.getELResolver().isReadOnly(context, position.getValue(), name)) {
                    continue;
                }
            } catch (Exception e) {
                // if we can't even _check_ for readOnly, assume not writable:
                continue;
            }
        }
        result.add(name);
    }
    return result;
}