List of usage examples for java.lang Character TYPE
Class TYPE
To view the source code for java.lang Character TYPE.
Click Source Link
From source file:TypeUtil.java
/** Convert String value to instance. * @param type The class of the instance, which may be a primitive TYPE field. * @param value The value as a string.//from www. ja va2s. c o m * @return The value as an Object. */ public static Object valueOf(Class type, String value) { try { if (type.equals(java.lang.String.class)) return value; Method m = (Method) class2Value.get(type); if (m != null) return m.invoke(null, new Object[] { value }); if (type.equals(java.lang.Character.TYPE) || type.equals(java.lang.Character.class)) return new Character(value.charAt(0)); Constructor c = type.getConstructor(stringArg); return c.newInstance(new Object[] { value }); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof Error) throw (Error) (e.getTargetException()); } return null; }
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). *//* ww w. j a v a2s .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: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"); }// w w w .j a v a 2s.c om 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:TypeUtil.java
/** Convert String value to instance. * @param type The class of the instance, which may be a primitive TYPE field. * @param value The value as a string.//from w ww .j a v a 2 s .c om * @return The value as an Object. */ public static Object valueOf(Class type, String value) { try { if (type.equals(java.lang.String.class)) return value; Method m = (Method) class2Value.get(type); if (m != null) return m.invoke(null, new Object[] { value }); if (type.equals(java.lang.Character.TYPE) || type.equals(java.lang.Character.class)) return new Character(value.charAt(0)); Constructor c = type.getConstructor(stringArg); return c.newInstance(new Object[] { value }); } catch (NoSuchMethodException e) { // LogSupport.ignore(log,e); } catch (IllegalAccessException e) { // LogSupport.ignore(log,e); } catch (InstantiationException e) { // LogSupport.ignore(log,e); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof Error) throw (Error) (e.getTargetException()); // LogSupport.ignore(log,e); } return null; }
From source file:com.projity.util.ClassUtils.java
/** * Get the corresponding object class from a primitive class * @param clazz primitive class//w w w .j av a2s .c o m * @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:net.lightbody.bmp.proxy.jetty.util.TypeUtil.java
/** Convert String value to instance. * @param type The class of the instance, which may be a primitive TYPE field. * @param value The value as a string./*from www. j ava2 s.co m*/ * @return The value as an Object. */ public static Object valueOf(Class type, String value) { try { if (type.equals(java.lang.String.class)) return value; Method m = (Method) class2Value.get(type); if (m != null) return m.invoke(null, new Object[] { value }); if (type.equals(java.lang.Character.TYPE) || type.equals(java.lang.Character.class)) return new Character(value.charAt(0)); Constructor c = type.getConstructor(stringArg); return c.newInstance(new Object[] { value }); } catch (NoSuchMethodException e) { LogSupport.ignore(log, e); } catch (IllegalAccessException e) { LogSupport.ignore(log, e); } catch (InstantiationException e) { LogSupport.ignore(log, e); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof Error) throw (Error) (e.getTargetException()); LogSupport.ignore(log, e); } return null; }
From source file:net.sf.json.JSONUtils.java
/** * Tests if obj is a String or a char/*from ww w . j a va 2 s . com*/ */ public static boolean isString(Object obj) { if (obj instanceof String) { return true; } if (obj instanceof Character) { return true; } if ((obj != null) && (obj.getClass() == Character.TYPE)) { return true; } return false; }
From source file:com.xwtec.xwserver.util.json.JSONArray.java
/** * Creates a JSONArray.<br>/* ww w. j a v a 2s. c o m*/ * Inspects the object type to call the correct JSONArray factory method. * Accepts JSON formatted strings, arrays, Collections and Enums. * * @param object * @throws JSONException if the object can not be converted to a proper * JSONArray. */ public static JSONArray fromObject(Object object, JsonConfig jsonConfig) { if (object instanceof JSONString) { return _fromJSONString((JSONString) object, jsonConfig); } else if (object instanceof JSONArray) { return _fromJSONArray((JSONArray) object, jsonConfig); } else if (object instanceof Collection) { return _fromCollection((Collection) object, jsonConfig); } else if (object instanceof JSONTokener) { return _fromJSONTokener((JSONTokener) object, jsonConfig); } else if (object instanceof String) { return _fromString((String) object, jsonConfig); } else if (object != null && object.getClass().isArray()) { Class type = object.getClass().getComponentType(); if (!type.isPrimitive()) { return _fromArray((Object[]) object, jsonConfig); } else { if (type == Boolean.TYPE) { return _fromArray((boolean[]) object, jsonConfig); } else if (type == Byte.TYPE) { return _fromArray((byte[]) object, jsonConfig); } else if (type == Short.TYPE) { return _fromArray((short[]) object, jsonConfig); } else if (type == Integer.TYPE) { return _fromArray((int[]) object, jsonConfig); } else if (type == Long.TYPE) { return _fromArray((long[]) object, jsonConfig); } else if (type == Float.TYPE) { return _fromArray((float[]) object, jsonConfig); } else if (type == Double.TYPE) { return _fromArray((double[]) object, jsonConfig); } else if (type == Character.TYPE) { return _fromArray((char[]) object, jsonConfig); } else { throw new JSONException("Unsupported type"); } } } else if (JSONUtils.isBoolean(object) || JSONUtils.isFunction(object) || JSONUtils.isNumber(object) || JSONUtils.isNull(object) || JSONUtils.isString(object) || object instanceof JSON) { fireArrayStartEvent(jsonConfig); JSONArray jsonArray = new JSONArray().element(object, jsonConfig); fireElementAddedEvent(0, jsonArray.get(0), jsonConfig); fireArrayStartEvent(jsonConfig); return jsonArray; } else if (object instanceof Enum) { return _fromArray((Enum) object, jsonConfig); } else if (object instanceof Annotation || (object != null && object.getClass().isAnnotation())) { throw new JSONException("Unsupported type"); } else if (JSONUtils.isObject(object)) { fireArrayStartEvent(jsonConfig); JSONArray jsonArray = new JSONArray().element(JSONObject.fromObject(object, jsonConfig)); fireElementAddedEvent(0, jsonArray.get(0), jsonConfig); fireArrayStartEvent(jsonConfig); return jsonArray; } else { throw new JSONException("Unsupported type"); } }
From source file:org.eiichiro.bootleg.AbstractRequest.java
/** * Returns the default value of the specified primitive type. * // w w w . j a v a2 s . c o m * @param type The primitive type. * @return The default value of the specified primitive type. */ protected Object primitive(Type type) { Class<?> rawType = Types.getRawType(type); if (rawType.equals(Boolean.TYPE)) { return (boolean) false; } else if (rawType.equals(Character.TYPE)) { return (char) 0; } else if (rawType.equals(Byte.TYPE)) { return (byte) 0; } else if (rawType.equals(Double.TYPE)) { return (double) 0.0; } else if (rawType.equals(Float.TYPE)) { return (float) 0.0; } else if (rawType.equals(Integer.TYPE)) { return (int) 0; } else { // short. return (short) 0; } }
From source file:pe.com.mmh.sisgap.utils.BasicDynaBean.java
/** * Return the value of a simple property with the specified name. * * @param name Name of the property whose value is to be retrieved * * @exception IllegalArgumentException if there is no property * of the specified name//from w w w.j a va2 s . c om */ public Object get(String name) { // Return any non-null value for the specified property Object value = values.get(name); if (value != null) { return (value); } // Return a null value for a non-primitive property Class type = getDynaProperty(name).getType(); 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((double) 0.0)); } else if (type == Float.TYPE) { return (new Float((float) 0.0)); } else if (type == Integer.TYPE) { return (new Integer((int) 0)); } else if (type == Long.TYPE) { return (new Long((int) 0)); } else if (type == Short.TYPE) { return (new Short((short) 0)); } else { return (null); } }