List of usage examples for java.lang Short TYPE
Class TYPE
To view the source code for java.lang Short TYPE.
Click Source Link
From source file:org.briljantframework.data.resolver.Resolve.java
private static Resolver<Integer> initializeIntegerResolver() { Resolver<Integer> resolver = new Resolver<>(Integer.class); resolver.put(Number.class, Number::intValue); resolver.put(Double.class, Number::intValue); resolver.put(Double.TYPE, Number::intValue); resolver.put(Float.class, Number::intValue); resolver.put(Float.TYPE, Number::intValue); resolver.put(Long.class, Number::intValue); resolver.put(Long.TYPE, Number::intValue); resolver.put(Integer.class, Number::intValue); resolver.put(Integer.TYPE, Number::intValue); resolver.put(Short.class, Number::intValue); resolver.put(Short.TYPE, Number::intValue); resolver.put(Byte.class, Number::intValue); resolver.put(Byte.TYPE, Number::intValue); resolver.put(String.class, s -> { try {//from w w w .java 2s .c o m return NumberUtils.createNumber(s).intValue(); } catch (Exception e) { return null; } }); return resolver; }
From source file:org.getobjects.appserver.publisher.GoJavaMethod.java
@SuppressWarnings("unchecked") public Object coerceFormValueToArgumentType(final Object[] _v, final Class<?> _argType) { // FIXME: All this isn't nice. Cleanup and do it properly. // FIXME: Cache all the dynamic lookup if (_v == null) return null; if (_argType.isAssignableFrom(_v.getClass())) return _v; int vCount = _v.length; /* check whether the argument is some array-ish thing */ if (_argType.isArray()) { final Class<?> itemType = _argType.getComponentType(); final Object typedArray = java.lang.reflect.Array.newInstance(itemType, vCount); for (int i = 0; i < vCount; i++) { Object[] v = { _v[i] }; Object sv = this.coerceFormValueToArgumentType(v, itemType); java.lang.reflect.Array.set(typedArray, i, sv); }// w ww.j ava 2s.c o m return typedArray; } if (_argType.isAssignableFrom(List.class)) return UList.asList(_v); if (_argType.isAssignableFrom(Set.class)) return new HashSet(UList.asList(_v)); if (_argType.isAssignableFrom(Collection.class)) return UList.asList(_v); /* empty assignment */ if (vCount == 0) { if (!_argType.isPrimitive()) return null; // all objects, return null if (_argType == Boolean.TYPE) return new Boolean(false); if (_argType == Integer.TYPE) return new Integer(-1); if (_argType == Double.TYPE) return new Double(-1.0); if (_argType == Float.TYPE) return new Float(-1.0); if (_argType == Short.TYPE) return new Integer(-1); if (_argType == Long.TYPE) return new Long(-1); log.error("Unexpected primitive arg type: " + _argType); return new GoInternalErrorException("Unexpected primitive type!"); } /* check whether it is a directly assignable type */ if (vCount == 1) { /* some type coercion. Can we reuse anything from KVC here? */ // Note: Go supports various Zope form value formats, e.g. 'age:int' // Check WOServletRequest for more. final Object v = _v[0]; if (_argType.isAssignableFrom(v.getClass())) return v; /* some basic coercion */ if (_argType.isPrimitive()) { if (_argType == Boolean.TYPE) return new Boolean(UObject.boolValue(v)); if (_argType == Integer.TYPE || _argType == Short.TYPE) return new Integer(UObject.intValue(v)); if (_argType == Long.TYPE) return new Long(UObject.intOrLongValue(v).longValue()); } else if (_argType.isAssignableFrom(String.class)) return v.toString(); return v; // might crash } /* error out, return exception as value */ log.error("Cannot convert form value to Java argument " + _argType + ": " + _v); return new GoInternalErrorException("Cannot convert form value to Java parameter"); }
From source file:org.primeframework.mvc.parameter.convert.converters.NumberConverterTest.java
/** * Test the conversion from Strings.//from w w w . j a v a2s .co m */ @Test public void toStrings() { GlobalConverter converter = new NumberConverter(new MockConfiguration()); String str = converter.convertToString(Integer.class, null, "testExpr", null); assertNull(str); str = converter.convertToString(Byte.class, null, "testExpr", (byte) 42); assertEquals(str, "42"); str = converter.convertToString(Byte.TYPE, null, "testExpr", (byte) 42); assertEquals(str, "42"); str = converter.convertToString(Short.class, null, "testExpr", (short) 42); assertEquals(str, "42"); str = converter.convertToString(Short.TYPE, null, "testExpr", (short) 42); assertEquals(str, "42"); str = converter.convertToString(Integer.class, null, "testExpr", 42); assertEquals(str, "42"); str = converter.convertToString(Integer.class, null, "testExpr", 42); assertEquals(str, "42"); str = converter.convertToString(Long.class, null, "testExpr", 42l); assertEquals(str, "42"); str = converter.convertToString(Long.TYPE, null, "testExpr", 42l); assertEquals(str, "42"); str = converter.convertToString(Float.class, null, "testExpr", 42f); assertEquals(str, "42.0"); str = converter.convertToString(Float.TYPE, null, "testExpr", 42f); assertEquals(str, "42.0"); str = converter.convertToString(Double.class, null, "testExpr", 42.0); assertEquals(str, "42.0"); str = converter.convertToString(Double.TYPE, null, "testExpr", 42.0); assertEquals(str, "42.0"); }
From source file:com.projity.util.ClassUtils.java
/** * Get the corresponding object class from a primitive class * @param clazz primitive class/* w ww . j av a 2 s.c om*/ * @return Object class. * @throws ClassCastException if class is unknown primitive */ public static Class primitiveToObjectClass(Class clazz) { // return MethodUtils.toNonPrimitiveClass(clazz); if (clazz == Boolean.TYPE) return Boolean.class; else if (clazz == Character.TYPE) return Character.class; else if (clazz == Byte.TYPE) return Byte.class; else if (clazz == Short.TYPE) return Short.class; else if (clazz == Integer.TYPE) return Integer.class; else if (clazz == Long.TYPE) return Long.class; else if (clazz == Float.TYPE) return Float.class; else if (clazz == Double.TYPE) return Double.class; throw new ClassCastException("Cannot convert class" + clazz + " to an object class"); }
From source file:org.apache.struts.config.FormPropertyConfig.java
/** * Return a Class corresponds to the value specified for the * <code>type</code> property, taking into account the trailing "[]" for * arrays (as well as the ability to specify primitive Java types). *//*from w w w . j av a 2 s . c o m*/ public Class getTypeClass() { // Identify the base class (in case an array was specified) String baseType = getType(); boolean indexed = false; if (baseType.endsWith("[]")) { baseType = baseType.substring(0, baseType.length() - 2); indexed = true; } // Construct an appropriate Class instance for the base class Class baseClass = null; if ("boolean".equals(baseType)) { baseClass = Boolean.TYPE; } else if ("byte".equals(baseType)) { baseClass = Byte.TYPE; } else if ("char".equals(baseType)) { baseClass = Character.TYPE; } else if ("double".equals(baseType)) { baseClass = Double.TYPE; } else if ("float".equals(baseType)) { baseClass = Float.TYPE; } else if ("int".equals(baseType)) { baseClass = Integer.TYPE; } else if ("long".equals(baseType)) { baseClass = Long.TYPE; } else if ("short".equals(baseType)) { baseClass = Short.TYPE; } else { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = this.getClass().getClassLoader(); } try { baseClass = classLoader.loadClass(baseType); } catch (ClassNotFoundException ex) { log.error("Class '" + baseType + "' not found for property '" + name + "'"); baseClass = null; } } // Return the base class or an array appropriately if (indexed) { return (Array.newInstance(baseClass, 0).getClass()); } else { return (baseClass); } }
From source file:org.hyperic.hq.product.jmx.MBeanUtil.java
private static void initConverters() { addConverter(Object.class, new Converter() { public Object convert(String param) { return param; }/*www . j a v a2 s . c om*/ }); addConverter(Short.class, new Converter() { public Object convert(String param) { return Short.valueOf(param); } }); addConverter(Integer.class, new Converter() { public Object convert(String param) { return Integer.valueOf(param); } }); addConverter(Long.class, new Converter() { public Object convert(String param) { return Long.valueOf(param); } }); addConverter(Double.class, new Converter() { public Object convert(String param) { return Double.valueOf(param); } }); addConverter(Boolean.class, new Converter() { public Object convert(String param) { return Boolean.valueOf(param); } }); addConverter(File.class, new Converter() { public Object convert(String param) { return new File(param); } }); addConverter(URL.class, new Converter() { public Object convert(String param) { try { return new URL(param); } catch (MalformedURLException e) { throw invalid(param, e); } } }); addConverter(ObjectName.class, new Converter() { public Object convert(String param) { try { return new ObjectName(param); } catch (MalformedObjectNameException e) { throw invalid(param, e); } } }); addConverter(List.class, new ListConverter() { public Object convert(String[] params) { return Arrays.asList(params); } }); addConverter(String[].class, new ListConverter() { public Object convert(String[] params) { return params; } }); addConverter(Long[].class, new ListConverter() { public Object convert(String[] params) { Long[] args = new Long[params.length]; for (int i = 0; i < params.length; i++) { args[i] = Long.valueOf(params[i]); } return args; } }); addConverter(Integer[].class, new ListConverter() { public Object convert(String[] params) { Integer[] args = new Integer[params.length]; for (int i = 0; i < params.length; i++) { args[i] = Integer.valueOf(params[i]); } return args; } }); addConverter(Double[].class, new ListConverter() { public Object convert(String[] params) { Double[] args = new Double[params.length]; for (int i = 0; i < params.length; i++) { args[i] = Double.valueOf(params[i]); } return args; } }); addConverter(Short[].class, new ListConverter() { public Object convert(String[] params) { Short[] args = new Short[params.length]; for (int i = 0; i < params.length; i++) { args[i] = Short.valueOf(params[i]); } return args; } }); addConverter(Boolean[].class, new ListConverter() { public Object convert(String[] params) { Boolean[] args = new Boolean[params.length]; for (int i = 0; i < params.length; i++) { args[i] = Boolean.valueOf(params[i]); } return args; } }); addConverter(long[].class, new ListConverter() { public Object convert(String[] params) { long[] args = new long[params.length]; for (int i = 0; i < params.length; i++) { args[i] = Long.parseLong(params[i]); } return args; } }); addConverter(int[].class, new ListConverter() { public Object convert(String[] params) { int[] args = new int[params.length]; for (int i = 0; i < params.length; i++) { args[i] = Integer.parseInt(params[i]); } return args; } }); addConverter(double[].class, new ListConverter() { public Object convert(String[] params) { double[] args = new double[params.length]; for (int i = 0; i < params.length; i++) { args[i] = Double.parseDouble(params[i]); } return args; } }); addConverter(short[].class, new ListConverter() { public Object convert(String[] params) { short[] args = new short[params.length]; for (int i = 0; i < params.length; i++) { args[i] = Short.parseShort(params[i]); } return args; } }); addConverter(boolean[].class, new ListConverter() { public Object convert(String[] params) { boolean[] args = new boolean[params.length]; for (int i = 0; i < params.length; i++) { args[i] = params[i].equals("true") ? true : false; } return args; } }); Class[][] aliases = { { String.class, Object.class }, { Short.TYPE, Short.class }, { Integer.TYPE, Integer.class }, { Long.TYPE, Long.class }, { Double.TYPE, Double.class }, { Boolean.TYPE, Boolean.class }, }; for (int i = 0; i < aliases.length; i++) { addConverter(aliases[i][0], aliases[i][1]); } }
From source file:org.romaframework.core.schema.SchemaField.java
protected Object convertValue(Object iFieldValue) { if (type == null || isArray()) return iFieldValue; SchemaClass typeClass = getType().getSchemaClass(); if (typeClass.equals(Roma.schema().getSchemaClass(iFieldValue))) return iFieldValue; String textValue = null;// ww w.j ava2 s .c o m if (iFieldValue instanceof String) { textValue = (String) iFieldValue; } else if (iFieldValue != null) { textValue = iFieldValue.toString(); } Object value = null; if (textValue != null) { // TRY A SOFT CONVERSION if (typeClass.isOfType(Integer.class) || typeClass.isOfType(Integer.TYPE)) { try { value = textValue.equals("") ? null : Integer.parseInt(textValue); } catch (Exception e) { value = textValue.equals("") ? null : Double.valueOf(textValue).intValue(); } } else if (typeClass.isOfType(Long.class) || typeClass.isOfType(Long.TYPE)) { value = textValue.equals("") ? null : Long.parseLong(textValue); } else if (typeClass.isOfType(Short.class) || typeClass.isOfType(Short.TYPE)) { value = textValue.equals("") ? null : Short.parseShort(textValue); } else if (typeClass.isOfType(Byte.class) || typeClass.isOfType(Byte.TYPE)) { value = textValue.equals("") ? null : Byte.parseByte(textValue); } else if (typeClass.isOfType(Character.class) || typeClass.isOfType(Character.TYPE)) { if (textValue.length() > 0) { value = new Character(textValue.charAt(0)); } } else if (typeClass.isOfType(Float.class) || typeClass.isOfType(Float.TYPE)) { value = textValue.equals("") ? null : Float.parseFloat(textValue); } else if (typeClass.isOfType(Double.class) || typeClass.isOfType(Double.TYPE)) { value = textValue.equals("") ? null : Double.parseDouble(textValue); } else if (typeClass.isOfType(BigDecimal.class)) { value = textValue.equals("") ? null : new BigDecimal(textValue); } else if (iFieldValue != null && !typeClass.isArray() && iFieldValue.getClass().isArray()) { // DESTINATION VALUE IS NOT AN ARRAY: ASSIGN THE FIRST ONE ELEMENT value = ((Object[]) iFieldValue)[0]; } else { value = iFieldValue; } } if (value != null) { // TODO is this the right place to do this...? Class<?> valueClass = value.getClass(); // SUCH A MONSTER!!! MOVE THIS LOGIC IN SchemaClass.isAssignableFrom... if (value instanceof VirtualObject && !(typeClass.getLanguageType() instanceof Class<?> && ((Class<?>) typeClass.getLanguageType()).isAssignableFrom(VirtualObject.class)) && ((VirtualObject) value).getSuperClassObject() != null) { if (ComposedEntity.class .isAssignableFrom(((VirtualObject) value).getSuperClassObject().getClass())) { value = ((VirtualObject) value).getSuperClassObject(); valueClass = value.getClass(); } } if (value instanceof ComposedEntity<?> && !typeClass.isAssignableFrom(valueClass)) { value = ((ComposedEntity<?>) value).getEntity(); } } if (value == null && typeClass.isPrimitive()) { log.warn("Cannot set the field value to null for primitive types! Field: " + getEntity() + "." + name + " of class " + getType().getName() + ". Setting value to 0."); // SET THE VALUE TO 0 value = SchemaHelper.assignDefaultValueToLiteral(typeClass); } return value; }
From source file:RealFunctionValidation.java
public static Object readAndWritePrimitiveValue(final DataInputStream in, final DataOutputStream out, final Class<?> type) throws IOException { if (!type.isPrimitive()) { throw new IllegalArgumentException("type must be primitive"); }/*from w w w . ja va2s. c o m*/ if (type.equals(Boolean.TYPE)) { final boolean x = in.readBoolean(); out.writeBoolean(x); return Boolean.valueOf(x); } else if (type.equals(Byte.TYPE)) { final byte x = in.readByte(); out.writeByte(x); return Byte.valueOf(x); } else if (type.equals(Character.TYPE)) { final char x = in.readChar(); out.writeChar(x); return Character.valueOf(x); } else if (type.equals(Double.TYPE)) { final double x = in.readDouble(); out.writeDouble(x); return Double.valueOf(x); } else if (type.equals(Float.TYPE)) { final float x = in.readFloat(); out.writeFloat(x); return Float.valueOf(x); } else if (type.equals(Integer.TYPE)) { final int x = in.readInt(); out.writeInt(x); return Integer.valueOf(x); } else if (type.equals(Long.TYPE)) { final long x = in.readLong(); out.writeLong(x); return Long.valueOf(x); } else if (type.equals(Short.TYPE)) { final short x = in.readShort(); out.writeShort(x); return Short.valueOf(x); } else { // This should never occur. throw new IllegalStateException(); } }
From source file:org.evosuite.testcase.fm.MethodDescriptor.java
public Object executeMatcher(int i) throws IllegalArgumentException { if (i < 0 || i >= getNumberOfInputParameters()) { throw new IllegalArgumentException("Invalid index: " + i); }/*from w ww.j a va 2 s . com*/ Type[] types = method.getParameterTypes(); Type type = types[i]; try { if (type.equals(Integer.TYPE) || type.equals(Integer.class)) { return Mockito.anyInt(); } else if (type.equals(Long.TYPE) || type.equals(Long.class)) { return Mockito.anyLong(); } else if (type.equals(Boolean.TYPE) || type.equals(Boolean.class)) { return Mockito.anyBoolean(); } else if (type.equals(Double.TYPE) || type.equals(Double.class)) { return Mockito.anyDouble(); } else if (type.equals(Float.TYPE) || type.equals(Float.class)) { return Mockito.anyFloat(); } else if (type.equals(Short.TYPE) || type.equals(Short.class)) { return Mockito.anyShort(); } else if (type.equals(Character.TYPE) || type.equals(Character.class)) { return Mockito.anyChar(); } else if (type.equals(String.class)) { return Mockito.anyString(); } else { return Mockito.any(type.getClass()); } } catch (Exception e) { logger.error("Failed to executed Mockito matcher n{} of type {} in {}.{}: {}", i, type, className, methodName, e.getMessage()); throw new EvosuiteError(e); } }
From source file:org.seasar.struts.lessconfig.factory.AbstractValidatorAnnotationHandler.java
protected String getAutoTypeValidatorName(PropertyDesc propDesc) { Class paramType = propDesc.getPropertyType(); if (paramType.isArray()) { paramType = paramType.getComponentType(); }// w w w . j a v a2 s . c o m if (paramType.equals(Byte.class) || paramType.equals(Byte.TYPE)) { return "byte"; } else if (Date.class.isAssignableFrom(paramType)) { return "date"; } else if (paramType.equals(Double.class) || paramType.equals(Double.TYPE)) { return "double"; } else if (paramType.equals(Float.class) || paramType.equals(Float.TYPE)) { return "float"; } else if (paramType.equals(Integer.class) || paramType.equals(Integer.TYPE)) { return "integer"; } else if (paramType.equals(Long.class) || paramType.equals(Long.TYPE)) { return "long"; } else if (paramType.equals(Short.class) || paramType.equals(Short.TYPE)) { return "short"; } return null; }