/** Try to convert a string value into a numerical value. */ private static Number createNumberFromStringValue(String value) throws NumberFormatException { final String suffix = value.substring(value.length() - 1); if ("L".equalsIgnoreCase(suffix)) { return Long.valueOf(value.substring(0, value.length() - 1)); } if ("F".equalsIgnoreCase(suffix)) { return Float.valueOf(value.substring(0, value.length() - 1)); } if ("D".equalsIgnoreCase(suffix)) { return Double.valueOf(value.substring(0, value.length() - 1)); } try { return Integer.valueOf(value); } catch (NumberFormatException e) { // OK: Ignore exception... } try { return Long.valueOf(value); } catch (NumberFormatException e1) { // OK: Ignore exception... } try { return Double.valueOf(value); } catch (NumberFormatException e2) { // OK: Ignore exception... } throw new NumberFormatException( "Cannot convert string value '" + value + "' into a numerical value"); }
/** * Create a channel with the given capacity and semaphore implementations instantiated from the * supplied class * * @exception IllegalArgumentException if capacity less or equal to zero. * @exception NoSuchMethodException If class does not have constructor that intializes permits * @exception SecurityException if constructor information not accessible * @exception InstantiationException if semaphore class is abstract * @exception IllegalAccessException if constructor cannot be called * @exception InvocationTargetException if semaphore constructor throws an exception */ public SemaphoreControlledChannel(int capacity, Class semaphoreClass) throws IllegalArgumentException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException { if (capacity <= 0) throw new IllegalArgumentException(); capacity_ = capacity; Class[] longarg = {Long.TYPE}; Constructor ctor = semaphoreClass.getDeclaredConstructor(longarg); Object[] cap = {Long.valueOf(capacity)}; putGuard_ = (Semaphore) (ctor.newInstance(/*(Object[]) GemStoneAddition*/ cap)); Object[] zero = {Long.valueOf(0)}; takeGuard_ = (Semaphore) (ctor.newInstance(/*(Object[]) GemStoneAddition*/ zero)); }
private static Object parseValue(String value, Prop p, Class type) { Object v = value; if (type.isArray()) { StringTokenizer st = new StringTokenizer(value, ","); Class ctype = type.getComponentType(); v = Array.newInstance(ctype, st.countTokens()); for (int i = 0; st.hasMoreTokens(); i++) Array.set(v, i, parseValue(st.nextToken(), p, ctype)); } else if (type == boolean.class) { v = Boolean.valueOf(value); } else if (type == double.class) { v = Double.valueOf(value); } else if (type == int.class) { v = Integer.valueOf(value); } else if (p.field.isAnnotationPresent(TimeIntervalProp.class)) { if (value.endsWith("s")) { v = (long) (Double.parseDouble(value.substring(0, value.length() - 1)) * SEC); } else if (value.endsWith("m")) { v = (long) (Double.parseDouble(value.substring(0, value.length() - 1)) * MIN); } else { v = Long.valueOf(value); } } return v; }
/** * Helper method used to get default value for wrappers used for primitive types (0 for Integer * etc) * * @since 1.6.1 */ public static Object defaultValue(Class<?> cls) { if (cls == Integer.TYPE) { return Integer.valueOf(0); } if (cls == Long.TYPE) { return Long.valueOf(0L); } if (cls == Boolean.TYPE) { return Boolean.FALSE; } if (cls == Double.TYPE) { return Double.valueOf(0.0); } if (cls == Float.TYPE) { return Float.valueOf(0.0f); } if (cls == Byte.TYPE) { return Byte.valueOf((byte) 0); } if (cls == Short.TYPE) { return Short.valueOf((short) 0); } if (cls == Character.TYPE) { return '\0'; } throw new IllegalArgumentException("Class " + cls.getName() + " is not a primitive type"); }
private Object createObject( Node node, String name, String classPackage, String type, String value, boolean setProperty) { Bean parentBean = null; if (beansStack.size() > 0) parentBean = (Bean) beansStack.peek(); Object object = null; // param is either an XSD type or a bean, // check if it can be converted to an XSD type XSDatatype dt = null; try { dt = DatatypeFactory.getTypeByName(type); } catch (DatatypeException dte) { // the type is not a valid XSD data type } // convert null value to default if ((dt != null) && (value == null)) { Class objType = dt.getJavaObjectType(); if (objType == String.class) value = ""; else if ((objType == java.math.BigInteger.class) || (objType == Long.class) || (objType == Integer.class) || (objType == Short.class) || (objType == Byte.class)) value = "0"; else if ((objType == java.math.BigDecimal.class) || (objType == Double.class) || (objType == Float.class)) value = "0.0"; else if (objType == Boolean.class) value = "false"; else if (objType == java.util.Date.class) value = DateUtils.getCurrentDate(); else if (objType == java.util.Calendar.class) value = DateUtils.getCurrentDateTime(); } // check whether the type was converted to an XSD datatype if ((dt != null) && dt.isValid(value, null)) { // create and return an XSD Java object (e.g. String, Integer, Boolean, etc) object = dt.createJavaObject(value, null); if (object instanceof java.util.Calendar) { // check that the object is truly a Calendar // because DatatypeFactory converts xsd:date // types to GregorianCalendar instead of Date. if (type.equals("date")) { java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd"); try { object = df.parse(value); } catch (java.text.ParseException pe) { object = new java.util.Date(); } } } } else { // Create a bean object if (topLevelBean == null) topLevelBean = parentBean; object = pushBeanOnStack(classPackage, type); // Check fields to see if this property is the 'value' for the object Field[] fields = object.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (fields[i].isAnnotationPresent(ObjectXmlAsValue.class)) { try { StringBuffer fieldName = new StringBuffer(fields[i].getName()); char c = fieldName.charAt(0); if (c >= 'a' && c <= 'z') { c += 'A' - 'a'; } fieldName.setCharAt(0, c); fieldName.insert(0, "set"); Method method = object .getClass() .getMethod(fieldName.toString(), new Class[] {fields[i].getType()}); method.invoke(object, value); break; } catch (Exception e) { System.err.println(e.getMessage()); } } } NamedNodeMap nodeAttrs = node.getAttributes(); // iterate attributes and set field values for property attributes for (int i = 0; i < nodeAttrs.getLength(); i++) { String nodePrefix = nodeAttrs.item(i).getPrefix(); if (nodePrefix.equals(nsPrefix)) { String nodeName = nodeAttrs.item(i).getLocalName(); String nodeValue = nodeAttrs.item(i).getNodeValue(); try { Field field = object.getClass().getDeclaredField(nodeName); if (field.isAnnotationPresent(ObjectXmlAsAttribute.class)) { StringBuffer fieldName = new StringBuffer(field.getName()); char c = fieldName.charAt(0); if (c >= 'a' && c <= 'z') { c += 'A' - 'a'; } fieldName.setCharAt(0, c); fieldName.insert(0, "set"); Method method = object.getClass().getMethod(fieldName.toString(), new Class[] {field.getType()}); if (field.getType() == String.class) method.invoke(object, nodeValue); else if (field.getType() == Boolean.TYPE) method.invoke(object, StringUtils.strToBool(nodeValue, "true")); else if (field.getType() == Byte.TYPE) method.invoke(object, Byte.valueOf(nodeValue).byteValue()); else if (field.getType() == Character.TYPE) method.invoke(object, Character.valueOf(nodeValue.charAt(0))); else if (field.getType() == Double.TYPE) method.invoke(object, Double.valueOf(nodeValue).doubleValue()); else if (field.getType() == Float.TYPE) method.invoke(object, Float.valueOf(nodeValue).floatValue()); else if (field.getType() == Integer.TYPE) method.invoke(object, Integer.valueOf(nodeValue).intValue()); else if (field.getType() == Long.TYPE) method.invoke(object, Long.valueOf(nodeValue).longValue()); else if (field.getType() == Short.TYPE) method.invoke(object, Short.valueOf(nodeValue).shortValue()); } } catch (Exception e) { System.err.println(e.getMessage()); } } } } if ((parentBean != null) && (setProperty)) { parentBean.setProperty(name, object); } return object; }
private TypedValues guessUntypedValue(String name, String value) { TypedValues list = new TypedValues(name); if (value.startsWith("java:")) { // possible java static reference int dot; if (value.equals("java:null")) { list.add(new TypedValue(Object.class, null)); } else if (value.charAt(5) == '$') { // reference to a local object (assemble @id) ElementInfo element = _local.get(value.substring(6)); if (element != null) { list.add(new TypedValue(element.type, element.data)); } } else if ((dot = value.lastIndexOf('.')) > 5) { try { // public static refernce? (don't override access control) _logger.fine("public static refernce? " + value); Object object = Class.forName(value.substring(5, dot)).getField(value.substring(dot + 1)).get(null); Class<?> type = object.getClass(); list.add(new TypedValue(type, object)); // automatic upscaling to larger types if (type == Byte.class) { object = new Short(((Byte) object).byteValue()); list.add(new TypedValue(Short.class, object)); type = Short.class; } if (type == Short.class) { object = new Integer(((Short) object).shortValue()); list.add(new TypedValue(Integer.class, object)); type = Integer.class; } if (type == Integer.class) { object = new Long(((Integer) object).intValue()); list.add(new TypedValue(Long.class, object)); type = Long.class; } if (type == Long.class) { object = new Float(((Long) object).longValue()); list.add(new TypedValue(Float.class, object)); type = Float.class; } if (type == Float.class) { object = new Double(((Float) object).floatValue()); list.add(new TypedValue(Double.class, object)); } } catch (Exception x) { _logger.fine(value + " looked like a static reference but is not: " + x.getMessage()); // class reference? guessClassReference(list, value.substring(5)); } } else { // no '.' at all // class reference? guessClassReference(list, value.substring(5)); } } else if (value.equals("true")) { list.add(new TypedValue(Boolean.class, Boolean.TRUE)); } else if (value.equals("false")) { list.add(new TypedValue(Boolean.class, Boolean.FALSE)); } else { // numbers? multiple possibilities try { list.add(new TypedValue(Integer.class, Integer.valueOf(value))); } catch (Exception x) { } try { list.add(new TypedValue(Long.class, Long.valueOf(value))); } catch (Exception x) { } try { list.add(new TypedValue(Float.class, Float.valueOf(value))); } catch (Exception x) { } try { list.add(new TypedValue(Double.class, Double.valueOf(value))); } catch (Exception x) { } } // always try String as the last resort list.add(new TypedValue(String.class, value)); return list; }