Example usage for com.google.common.primitives Primitives unwrap

List of usage examples for com.google.common.primitives Primitives unwrap

Introduction

In this page you can find the example usage for com.google.common.primitives Primitives unwrap.

Prototype

public static <T> Class<T> unwrap(Class<T> type) 

Source Link

Document

Returns the corresponding primitive type of type if it is a wrapper type; otherwise returns type itself.

Usage

From source file:com.google.api.tools.framework.snippet.Values.java

/**
 * Determines whether a value is 'true', where true is interpreted depending on the values
 * type./*from   ww  w. j  ava2  s . c  om*/
 */
@SuppressWarnings("rawtypes")
static boolean isTrue(Object v1) {
    if (v1 instanceof Number) {
        // For numbers, compare with  the default value (zero), which we obtain by
        // creating an array.
        return !Array.get(Array.newInstance(Primitives.unwrap(v1.getClass()), 1), 0).equals(v1);
    }
    if (v1 instanceof Boolean) {
        return (Boolean) v1;
    }
    if (v1 instanceof Doc) {
        return !((Doc) v1).isWhitespace();
    }
    if (v1 instanceof String) {
        return !Strings.isNullOrEmpty((String) v1);
    }
    if (v1 instanceof Iterable) {
        return ((Iterable) v1).iterator().hasNext();
    }
    return false;
}

From source file:co.paralleluniverse.galaxy.test.MockitoUtil.java

public static Object capture(Object mock, VerificationMode mode, String methodName, Object... args) {
    try {//  ww  w  . ja va  2  s  .co m
        int argIndex = -1;
        Class argType = null;
        Class[] types = new Class[args.length];
        for (int i = 0; i < args.length; i++) {

            if (args[i] instanceof Placeholder) {
                if (argType != null)
                    throw new IllegalArgumentException("More than one argument placeholder provided");
                argIndex = i;
                argType = ((Placeholder) args[i]).clazz;
                types[i] = argType;
            } else
                types[i] = Primitives.unwrap(args[i].getClass());
        }

        final Method method = mock.getClass().getDeclaredMethod(methodName, types);
        method.setAccessible(true);

        ArgumentCaptor captor = ArgumentCaptor.forClass(argType);
        method.invoke(verify(mock, mode), replace(args, argIndex, captor));
        return captor.getValue();
    } catch (IllegalAccessException ex) {
        throw new RuntimeException(ex);
    } catch (IllegalArgumentException ex) {
        throw new RuntimeException(ex);
    } catch (InvocationTargetException ex) {
        throw new RuntimeException(ex.getCause());
    } catch (NoSuchMethodException ex) {
        throw new RuntimeException(ex);
    } catch (SecurityException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.stjs.generator.type.PrimitiveTypes.java

public static java.lang.reflect.Type expressionResultType(java.lang.reflect.Type left,
        java.lang.reflect.Type right) {
    Integer orderLeft = left != null ? coercionOrder.get(Primitives.unwrap(ClassUtils.getRawClazz(left)))
            : null;//from w  w w  .j  a va2s .com
    if (orderLeft == null) {
        return String.class;
    }
    Integer orderRight = right != null ? coercionOrder.get(Primitives.unwrap(ClassUtils.getRawClazz(right)))
            : null;
    if (orderRight == null) {
        return String.class;
    }
    return orderLeft > orderRight ? left : right;
}

From source file:com.logsniffer.fields.FieldBaseTypes.java

public static FieldBaseTypes resolveType(final Object v) {
    if (v != null) {
        for (final FieldBaseTypes t : values()) {
            if (t.serializationType == null) {
                continue;
            }//from   w ww . j  a v a 2 s  . com
            Class<?> vc = v.getClass();
            if (Primitives.isWrapperType(vc)) {
                vc = Primitives.unwrap(vc);
            }
            if (t.serializationType.isAssignableFrom(vc)) {
                return t;
            }
        }
    }
    return OBJECT;
}

From source file:com.javadeobfuscator.deobfuscator.executor.values.JavaValue.java

public <T> T as(Class<T> clazz) {
    if (Primitives.unwrap(clazz) != clazz) {
        throw new ExecutionException("Cannot call as(Class<T> clazz) with a primitive class");
    }/*from  www. ja  v a  2 s .c om*/
    if (value() instanceof Character && clazz == char.class) {
        return (T) value();
    }
    if (value() instanceof Integer && clazz == boolean.class) {
        return (T) Boolean.valueOf(intValue() != 0 ? true : false);
    }
    if (value() instanceof Byte && clazz == char.class) {
        return (T) Character.valueOf((char) ((JavaByte) this).byteValue());
    }
    return clazz.cast(value());
}

From source file:de.ks.option.Options.java

@SuppressWarnings("unchecked")
public static <T> T store(Object value, Class<T> optionsDefiningClass) {
    ProxyFactory factory = new ProxyFactory();
    factory.setSuperclass(optionsDefiningClass);
    Class proxy = factory.createClass();

    Object retval = objenesis.newInstance(proxy);
    ((Proxy) retval).setHandler((self, thisMethod, proceed, args) -> {
        OptionSource optionSource = CDI.current().select(OptionSource.class).get();
        String key = thisMethod.getDeclaringClass().getName() + "." + thisMethod.getName();
        Class<?> retvalType = Primitives.unwrap(thisMethod.getReturnType());
        Class<?> argType = Primitives.unwrap(value.getClass());
        if (retvalType.isAssignableFrom(argType)) {
            optionSource.saveOption(key, value);
            log.debug("Saving value for option '{}'. Value={}", key, value);
        } else {//  ww w . j av  a2  s. c om
            throw new IllegalArgumentException("Trying to save option with wrong type. Expected='" + retvalType
                    + "', actual='" + argType + "'");
        }
        return value;
    });
    return (T) retval;
}

From source file:org.jage.platform.reflect.Classes.java

/**
 * Checks whether the first class is the wrapper of the primitive second class.
 *
 * @param wrapper//w ww .  j  av  a  2 s . com
 *            the wrapper class
 * @param primitive
 *            the primitive class
 * @return true if {@code primitive} is a primitive class and {@code wrapper} is its wrapper class
 * @since 2.6
 */
public static boolean isWrapperOfPrimitive(final Class<?> wrapper, final Class<?> primitive) {
    return primitive.isPrimitive() && wrapper != primitive && Primitives.unwrap(wrapper) == primitive;
}

From source file:edu.mit.streamjit.util.bytecode.types.WrapperType.java

public PrimitiveType unwrap() {
    //Wrapper types are always backed by Classes.
    return getTypeFactory()
            .getPrimitiveType(getModule().getKlass(Primitives.unwrap(getKlass().getBackingClass())));
}

From source file:org.stjs.generator.type.PrimitiveTypes.java

/**
 * it only takes into consideration the coercion order!
 * //from  w  w w. ja  v  a 2s  .  c  o  m
 * @param left
 * @param right
 * @return
 */
public static boolean isAssignableFrom(java.lang.reflect.Type t1, java.lang.reflect.Type t2) {
    Integer order1 = t1 != null ? coercionOrder.get(Primitives.unwrap(ClassUtils.getRawClazz(t1))) : null;
    if (order1 == null) {
        return false;
    }
    Integer order2 = t2 != null ? coercionOrder.get(Primitives.unwrap(ClassUtils.getRawClazz(t2))) : null;
    if (order2 == null) {
        return false;
    }
    return order1 >= order2;
}

From source file:com.github.radium226.common.ListenerList.java

public void fire(String methodName, Object... parameters) {
    for (T listener : listeners) {
        try {/*from   ww  w  .j  av  a 2s .c o m*/
            Class<?>[] parameterTypes = Lists.transform(Arrays.asList(parameters), (Object parameter) -> {
                Class<?> type = parameter.getClass();
                return Primitives.isWrapperType(type) ? Primitives.unwrap(type) : type;
            }).toArray(new Class<?>[0]);
            Method method = listenerClass.getMethod(methodName, parameterTypes);
            method.invoke(listener, parameters);
        } catch (IllegalAccessException | SecurityException | NoSuchMethodException | IllegalArgumentException
                | InvocationTargetException e) {
            LOGGER.warn("Unable to invoke {}", methodName, e);
        }
    }
}