Example usage for java.lang Short TYPE

List of usage examples for java.lang Short TYPE

Introduction

In this page you can find the example usage for java.lang Short TYPE.

Prototype

Class TYPE

To view the source code for java.lang Short TYPE.

Click Source Link

Document

The Class instance representing the primitive type short .

Usage

From source file:org.jdto.util.ClassUtils.java

/**
 * <p>Checks if one/*w w w  .jav a 2  s . co  m*/
 * <code>Class</code> can be assigned to a variable of another
 * <code>Class</code>.</p>
 *
 * <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method,
 * this method takes into account widenings of primitive classes and
 * <code>null</code>s.</p>
 *
 * <p>Primitive widenings allow an int to be assigned to a long, float or
 * double. This method returns the correct result for these cases.</p>
 *
 * <p><code>Null</code> may be assigned to any reference type. This method
 * will return
 * <code>true</code> if
 * <code>null</code> is passed in and the toClass is non-primitive.</p>
 *
 * <p>Specifically, this method tests whether the type represented by the
 * specified
 * <code>Class</code> parameter can be converted to the type represented by
 * this
 * <code>Class</code> object via an identity conversion widening primitive
 * or widening reference conversion. See <em><a
 * href="http://java.sun.com/docs/books/jls/">The Java Language
 * Specification</a></em>, sections 5.1.1, 5.1.2 and 5.1.4 for details.</p>
 *
 * @param cls the Class to check, may be null
 * @param toClass the Class to try to assign into, returns false if null
 * @param autoboxing whether to use implicit autoboxing/unboxing between
 * primitives and wrappers
 * @return
 * <code>true</code> if assignment possible
 * @since 2.5
 */
public static boolean isAssignable(Class cls, Class toClass, boolean autoboxing) {
    if (toClass == null) {
        return false;
    }
    // have to check for null, as isAssignableFrom doesn't
    if (cls == null) {
        return !(toClass.isPrimitive());
    }
    //autoboxing:
    if (autoboxing) {
        if (cls.isPrimitive() && !toClass.isPrimitive()) {
            cls = primitiveToWrapper(cls);
            if (cls == null) {
                return false;
            }
        }
        if (toClass.isPrimitive() && !cls.isPrimitive()) {
            cls = wrapperToPrimitive(cls);
            if (cls == null) {
                return false;
            }
        }
    }
    if (cls.equals(toClass)) {
        return true;
    }
    if (cls.isPrimitive()) {
        if (toClass.isPrimitive() == false) {
            return false;
        }
        if (Integer.TYPE.equals(cls)) {
            return Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
        }
        if (Long.TYPE.equals(cls)) {
            return Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
        }
        if (Boolean.TYPE.equals(cls)) {
            return false;
        }
        if (Double.TYPE.equals(cls)) {
            return false;
        }
        if (Float.TYPE.equals(cls)) {
            return Double.TYPE.equals(toClass);
        }
        if (Character.TYPE.equals(cls)) {
            return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass)
                    || Double.TYPE.equals(toClass);
        }
        if (Short.TYPE.equals(cls)) {
            return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass)
                    || Double.TYPE.equals(toClass);
        }
        if (Byte.TYPE.equals(cls)) {
            return Short.TYPE.equals(toClass) || Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass)
                    || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
        }
        // should never get here
        return false;
    }
    return toClass.isAssignableFrom(cls);
}

From source file:org.apache.struts.action.DynaActionForm.java

/**
 * <p>Return the value of a simple property with the specified name.</p>
 *
 * @param name Name of the property whose value is to be retrieved
 * @return The value of a simple property with the specified name.
 * @throws IllegalArgumentException if there is no property of the
 *                                  specified name
 * @throws NullPointerException     if the type specified for the property
 *                                  is invalid
 *//*from   www.  ja v  a2  s .  c om*/
public Object get(String name) {
    // Return any non-null value for the specified property
    Object value = dynaValues.get(name);

    if (value != null) {
        return (value);
    }

    // Return a null value for a non-primitive property
    Class type = getDynaProperty(name).getType();

    if (type == null) {
        throw new NullPointerException("The type for property " + name + " is invalid");
    }

    if (!type.isPrimitive()) {
        return (value);
    }

    // Manufacture default values for primitive properties
    if (type == Boolean.TYPE) {
        return (Boolean.FALSE);
    } else if (type == Byte.TYPE) {
        return (new Byte((byte) 0));
    } else if (type == Character.TYPE) {
        return (new Character((char) 0));
    } else if (type == Double.TYPE) {
        return (new Double(0.0));
    } else if (type == Float.TYPE) {
        return (new Float((float) 0.0));
    } else if (type == Integer.TYPE) {
        return (new Integer(0));
    } else if (type == Long.TYPE) {
        return (new Long(0));
    } else if (type == Short.TYPE) {
        return (new Short((short) 0));
    } else {
        return (null);
    }
}

From source file:org.pentaho.ui.xul.impl.AbstractXulDomContainer.java

private Class unBoxPrimative(Class clazz) {
    if (clazz == Boolean.class) {
        return Boolean.TYPE;
    } else if (clazz == Integer.class) {
        return Integer.TYPE;
    } else if (clazz == Float.class) {
        return Float.TYPE;
    } else if (clazz == Double.class) {
        return Double.TYPE;
    } else if (clazz == Short.class) {
        return Short.TYPE;
    } else if (clazz == Long.class) {
        return Long.TYPE;
    } else {/*from w w w  .ja v  a 2s . c  om*/
        return clazz;
    }
}

From source file:com.adobe.acs.commons.data.Variant.java

@SuppressWarnings("squid:S3776")
public <T> T asType(Class<T> type) {
    if (type == Byte.TYPE || type == Byte.class) {
        return (T) apply(toLong(), Long::byteValue);
    } else if (type == Integer.TYPE || type == Integer.class) {
        return (T) apply(toLong(), Long::intValue);
    } else if (type == Long.TYPE || type == Long.class) {
        return (T) toLong();
    } else if (type == Short.TYPE || type == Short.class) {
        return (T) apply(toLong(), Long::shortValue);
    } else if (type == Float.TYPE || type == Float.class) {
        return (T) apply(toDouble(), Double::floatValue);
    } else if (type == Double.TYPE || type == Double.class) {
        return (T) toDouble();
    } else if (type == Boolean.TYPE || type == Boolean.class) {
        return (T) toBoolean();
    } else if (type == String.class) {
        return (T) toString();
    } else if (type == Date.class) {
        return (T) toDate();
    } else if (type == Instant.class) {
        return (T) toDate().toInstant();
    } else if (type == Calendar.class) {
        Calendar c = Calendar.getInstance();
        c.setTime(toDate());/*w  w  w .  j  av  a2  s  .  c om*/
        return (T) c;
    } else {
        return null;
    }
}

From source file:org.wisdom.template.thymeleaf.OgnlOpsByReflectionTest.java

@Test
public void testConvertValue() throws Exception {
    Class[] classes = new Class[] { Object.class, Class.class };
    // Null value
    assertThat(invoke("convertValue", classes, null, Long.TYPE)).isEqualTo(0l);
    assertThat(invoke("convertValue", classes, null, String.class)).isNull();

    // Primitive//from www .  j a  va 2  s . co  m
    assertThat(invoke("convertValue", classes, "42", Integer.class)).isEqualTo(42);
    assertThat(invoke("convertValue", classes, "42", Integer.TYPE)).isEqualTo(42);
    assertThat(invoke("convertValue", classes, "42", Byte.class)).isEqualTo(Byte.valueOf("42"));
    assertThat(invoke("convertValue", classes, "42", Byte.TYPE)).isEqualTo(Byte.valueOf("42"));
    assertThat(invoke("convertValue", classes, "42", Short.class)).isEqualTo(Short.valueOf("42"));
    assertThat(invoke("convertValue", classes, "42", Short.TYPE)).isEqualTo(Short.valueOf("42"));
    assertThat(invoke("convertValue", classes, String.valueOf((int) 'c'), Character.class)).isEqualTo('c');
    assertThat(invoke("convertValue", classes, String.valueOf((int) 'c'), Character.TYPE)).isEqualTo('c');
    assertThat(invoke("convertValue", classes, "42", Long.class)).isEqualTo(42l);
    assertThat(invoke("convertValue", classes, "42", Long.TYPE)).isEqualTo(42l);
    assertThat(invoke("convertValue", classes, "true", Boolean.class)).isEqualTo(true);
    assertThat(invoke("convertValue", classes, "true", Boolean.TYPE)).isEqualTo(true);
    assertThat(invoke("convertValue", classes, "42", Double.class)).isEqualTo(42.0);
    assertThat(invoke("convertValue", classes, "42", Double.TYPE)).isEqualTo(42.0);
    assertThat(invoke("convertValue", classes, "42", Float.class)).isEqualTo(42.0f);
    assertThat(invoke("convertValue", classes, "42", Float.TYPE)).isEqualTo(42.0f);

    // BigInteger, BigDecimal and String
    assertThat(invoke("convertValue", classes, "42", BigDecimal.class)).isEqualTo(new BigDecimal("42"));
    assertThat(invoke("convertValue", classes, "42", BigInteger.class)).isEqualTo(new BigInteger("42"));
    assertThat(invoke("convertValue", classes, "42", String.class)).isEqualTo("42");

    //Array
    assertThat(invoke("convertValue", classes, new Object[] { 1, 2, 3 }, (new int[0]).getClass())).isNotNull();
}

From source file:org.romaframework.core.schema.SchemaHelper.java

public static Object assignValueToLiteral(String v, Class<?> type) {
    Object value;/*  w  w  w . j  a va2s .c  o m*/
    if (v.length() == 0) {
        value = null;
    } else {
        if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
            value = Integer.parseInt(v);
        } else if (type.equals(Long.class) || type.equals(Long.TYPE)) {
            value = Long.parseLong(v);
        } else if (type.equals(Short.class) || type.equals(Short.TYPE)) {
            value = Short.parseShort(v);
        } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
            value = Boolean.parseBoolean(v);
        } else if (type.equals(Float.class) || type.equals(Float.TYPE)) {
            value = Float.parseFloat(v);
        } else if (type.equals(Double.class) || type.equals(Double.TYPE)) {
            value = Double.parseDouble(v);
        } else if (type.equals(Byte.class) || type.equals(Byte.TYPE)) {
            value = Byte.parseByte(v);
        } else if (type.equals(Character.class) || type.equals(Character.TYPE)) {
            value = v.charAt(0);
        } else {
            value = v;
        }
    }
    return value;
}

From source file:com.netspective.commons.lang.ValueBeanGeneratorClassLoader.java

/**
 * Convert runtime java.lang.Class to BCEL Type object.
 *
 * @param cl Java class/*from  ww w. jav  a2s .  com*/
 *
 * @return corresponding Type object
 */
public static Type getBCELType(java.lang.Class cl) {
    if (cl == null) {
        throw new IllegalArgumentException("Class must not be null");
    }

    /* That's an amzingly easy case, because getName() returns
     * the signature. That's what we would have liked anyway.
     */
    if (cl.isArray()) {
        return Type.getType(cl.getName());
    } else if (cl.isPrimitive()) {
        if (cl == Integer.TYPE) {
            return Type.INT;
        } else if (cl == Void.TYPE) {
            return Type.VOID;
        } else if (cl == Double.TYPE) {
            return Type.DOUBLE;
        } else if (cl == Float.TYPE) {
            return Type.FLOAT;
        } else if (cl == Boolean.TYPE) {
            return Type.BOOLEAN;
        } else if (cl == Byte.TYPE) {
            return Type.BYTE;
        } else if (cl == Short.TYPE) {
            return Type.SHORT;
        } else if (cl == Long.TYPE) {
            return Type.LONG;
        } else if (cl == Character.TYPE) {
            return Type.CHAR;
        } else {
            throw new IllegalStateException("Ooops, what primitive type is " + cl);
        }
    } else { // "Real" class
        return new ObjectType(cl.getName());
    }
}

From source file:org.apache.struts.action.TestDynaActionForm.java

/**
 * Positive getDynaProperty on property <code>shortProperty</code>.
 *///from w ww  .j  a  v a 2s .c  om
public void testGetDescriptorShort() {
    testGetDescriptorBase("shortProperty", Short.TYPE);
}

From source file:de.ma.it.common.sm.transition.MethodTransition.java

private boolean match(Class<?> paramType, Object arg, Class<?> argType) {
    if (paramType.isPrimitive()) {
        if (paramType.equals(Boolean.TYPE)) {
            return arg instanceof Boolean;
        }/*from w ww . j  a  v a2  s  .  c om*/
        if (paramType.equals(Integer.TYPE)) {
            return arg instanceof Integer;
        }
        if (paramType.equals(Long.TYPE)) {
            return arg instanceof Long;
        }
        if (paramType.equals(Short.TYPE)) {
            return arg instanceof Short;
        }
        if (paramType.equals(Byte.TYPE)) {
            return arg instanceof Byte;
        }
        if (paramType.equals(Double.TYPE)) {
            return arg instanceof Double;
        }
        if (paramType.equals(Float.TYPE)) {
            return arg instanceof Float;
        }
        if (paramType.equals(Character.TYPE)) {
            return arg instanceof Character;
        }
    }
    return argType.isAssignableFrom(paramType) && paramType.isAssignableFrom(arg.getClass());
}

From source file:jp.terasoluna.fw.web.struts.reset.ResetterImpl.java

/**
 * ANVtH?[wv?peBZbg?B/*ww  w  . j av  a2s. c  o  m*/
 * v?peB^boolean^?ABoolean^??false??B
 * v~eBu^bp?[^???A0??B
 * v?peB^bp?[^OObject^??null??B<br>
 * ??A?entrynulln?B
 *
 * @param form ?NGXggpANVtH?[
 * @param entry Zbg?v?peB?lGg
 * @throws PropertyAccessException v?peBl?s??
 */
protected void resetValue(FormEx form, Entry<String, Object> entry) {
    if (log.isDebugEnabled()) {
        log.debug("resetValue(" + form + ", " + entry.getKey() + ") called.");
    }
    String propName = entry.getKey();
    try {
        Object value = entry.getValue();
        if (value == null) {
            return;
        }
        Class type = null;
        type = value.getClass();
        if (type != null) {
            // ^???B
            if (type == Boolean.TYPE || type == Boolean.class) {
                BeanUtil.setBeanProperty(form, propName, Boolean.FALSE);
            } else if (type == Byte.TYPE || type == Byte.class) {
                BeanUtil.setBeanProperty(form, propName, new Byte((byte) 0));
            } else if (type == Character.TYPE || type == Character.class) {
                BeanUtil.setBeanProperty(form, propName, new Character((char) 0));
            } else if (type == Double.TYPE || type == Double.class) {
                BeanUtil.setBeanProperty(form, propName, new Double(0.0));
            } else if (type == Float.TYPE || type == Float.class) {
                BeanUtil.setBeanProperty(form, propName, new Float((float) 0.0));
            } else if (type == Integer.TYPE || type == Integer.class) {
                BeanUtil.setBeanProperty(form, propName, new Integer(0));
            } else if (type == Long.TYPE || type == Long.class) {
                BeanUtil.setBeanProperty(form, propName, new Long(0));
            } else if (type == Short.TYPE || type == Short.class) {
                BeanUtil.setBeanProperty(form, propName, new Short((short) 0));
            } else {
                // v~eBu^?Abp?[^??null?
                BeanUtil.setBeanProperty(form, propName, null);
            }
        }
    } catch (PropertyAccessException e) {
        log.error("cannot access property " + form + "." + propName, e);
    }
}