Пример #1
0
 private static Object decode(Class returnType, String valueStr, TypeResolver resolver) {
   if (returnType.isArray()) {
     int sIndex = valueStr.indexOf("{");
     int eIndex = valueStr.lastIndexOf("}");
     String content = valueStr.substring(sIndex + 1, eIndex).trim();
     StringTokenizer tok = new StringTokenizer(content, ",");
     Object ar =
         java.lang.reflect.Array.newInstance(returnType.getComponentType(), tok.countTokens());
     int j = 0;
     while (tok.hasMoreElements()) {
       java.lang.reflect.Array.set(
           ar, j++, decode(returnType.getComponentType(), tok.nextToken(), resolver));
     }
     return ar;
   } else if (returnType.isEnum()) {
     try {
       String value = valueStr.trim();
       if (value.indexOf('.') > 0) {
         value = valueStr.substring(valueStr.lastIndexOf(".") + 1);
       }
       return returnType.getMethod("valueOf", String.class).invoke(null, value);
     } catch (IllegalAccessException e) {
       e.printStackTrace(); // To change body of catch statement use File | Settings | File
       // Templates.
     } catch (InvocationTargetException e) {
       e.printStackTrace(); // To change body of catch statement use File | Settings | File
       // Templates.
     } catch (NoSuchMethodException e) {
       e.printStackTrace(); // To change body of catch statement use File | Settings | File
       // Templates.
     }
   } else if (String.class.equals(returnType)) {
     return unquote(valueStr);
   } else if (boolean.class.equals(returnType)) {
     return Boolean.valueOf(valueStr);
   } else if (int.class.equals(returnType)) {
     return Integer.valueOf(valueStr);
   } else if (double.class.equals(returnType)) {
     return Double.valueOf(valueStr);
   } else if (long.class.equals(returnType)) {
     return Long.valueOf(valueStr);
   } else if (float.class.equals(returnType)) {
     return Float.valueOf(valueStr);
   } else if (short.class.equals(returnType)) {
     return Short.valueOf(valueStr);
   } else if (char.class.equals(returnType)) {
     return unquote(valueStr).charAt(0);
   } else if (Class.class.equals(returnType)) {
     try {
       String cName = valueStr.trim().replace(".class", "");
       return resolver.resolveType(cName);
     } catch (ClassNotFoundException cnfe) {
       cnfe.printStackTrace();
       return Object.class;
     }
   }
   return null;
 }
Пример #2
0
 /**
  * Reallocates an array with a new size, and copies the contents of the old array to the new
  * array.
  *
  * @param oldArray the old array, to be reallocated.
  * @param newSize the new array size.
  * @return A new array with the same contents.
  */
 public static Object resizeArray(Object oldArray, int newSize) {
   int oldSize = java.lang.reflect.Array.getLength(oldArray);
   @SuppressWarnings("rawtypes")
   Class elementType = oldArray.getClass().getComponentType();
   Object newArray = java.lang.reflect.Array.newInstance(elementType, newSize);
   int preserveLength = Math.min(oldSize, newSize);
   if (preserveLength > 0) System.arraycopy(oldArray, 0, newArray, 0, preserveLength);
   return newArray;
 }
Пример #3
0
 // from
 // http://stackoverflow.com/questions/5606338/cast-primitive-type-array-into-object-array-in-java
 private static Object[] toObjectArray(Object val) {
   if (val instanceof Object[]) return (Object[]) val;
   int arrayLength = java.lang.reflect.Array.getLength(val);
   Object[] outputArray = new Object[arrayLength];
   for (int i = 0; i < arrayLength; ++i) {
     outputArray[i] = java.lang.reflect.Array.get(val, i);
   }
   return outputArray;
 }
Пример #4
0
  // math behide resizeArray.
  private Object resizeArray(Object oldArray, int newSize) {
    // This class was taken directly from "http://www.source-code.biz/snippets/java/3.htm"
    // You try to dynamically resizing an array, than come talk to me about coping code.
    int oldSize = java.lang.reflect.Array.getLength(oldArray);

    Class elementType = oldArray.getClass().getComponentType();

    Object newArray = java.lang.reflect.Array.newInstance(elementType, newSize);

    int preserveLength = Math.min(oldSize, newSize);

    if (preserveLength > 0) {
      System.arraycopy(oldArray, 0, newArray, 0, preserveLength);
    }
    return newArray;
  }
Пример #5
0
 public V[] getValueArray(V[] a) {
   // I hate this but just have to
   int size = v.length;
   if (a.length < size)
     a = (V[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
   System.arraycopy(v, 0, a, 0, size);
   return a;
 }
 @SuppressWarnings("unchecked")
 public <T> T[] toArray(T[] a) {
   if (a.length < pets.length)
     a = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), pets.length);
   T[] result = a;
   System.arraycopy(pets, 0, result, 0, pets.length);
   if (result.length > pets.length) result[pets.length] = null;
   return result;
 }
Пример #7
0
 /**
  * Returns an array containing all of the elements in this deque in proper sequence (from first to
  * last element); the runtime type of the returned array is that of the specified array. If the
  * deque fits in the specified array, it is returned therein. Otherwise, a new array is allocated
  * with the runtime type of the specified array and the size of this deque.
  *
  * <p>
  *
  * <p>If this deque fits in the specified array with room to spare (i.e., the array has more
  * elements than this deque), the element in the array immediately following the end of the deque
  * is set to <tt>null</tt>.
  *
  * <p>
  *
  * <p>Like the {@link #toArray()} method, this method acts as bridge between array-based and
  * collection-based APIs. Further, this method allows precise control over the runtime type of the
  * output array, and may, under certain circumstances, be used to save allocation costs.
  *
  * <p>
  *
  * <p>Suppose <tt>x</tt> is a deque known to contain only strings. The following code can be used
  * to dump the deque into a newly allocated array of <tt>String</tt>:
  *
  * <p>
  *
  * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
  *
  * <p>Note that <tt>toArray(new Object[0])</tt> is identical in function to <tt>toArray()</tt>.
  *
  * @param a the array into which the elements of the deque are to be stored, if it is big enough;
  *     otherwise, a new array of the same runtime type is allocated for this purpose
  * @return an array containing all of the elements in this deque
  * @throws ArrayStoreException if the runtime type of the specified array is not a supertype of
  *     the runtime type of every element in this deque
  * @throws NullPointerException if the specified array is null
  */
 @SuppressWarnings("unchecked")
 public <T> T[] toArray(T[] a) {
   int size = size();
   if (a.length < size)
     a = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
   copyElements(a);
   if (a.length > size) a[size] = null;
   return a;
 }
Пример #8
0
 <T> T[] concat(T[] a, T[] b) {
   if ((b == null) || (b.length == 0)) {
     return a;
   }
   T[] r =
       (T[])
           java.lang.reflect.Array.newInstance(
               a.getClass().getComponentType(), a.length + b.length);
   System.arraycopy(a, 0, r, 0, a.length);
   System.arraycopy(b, 0, r, a.length, b.length);
   return r;
 }
  /**
   * Returns an array containing all of the elements in this queue, in proper sequence; the runtime
   * type of the returned array is that of the specified array. If the queue fits in the specified
   * array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the
   * specified array and the size of this queue.
   *
   * <p>If this queue fits in the specified array with room to spare (i.e., the array has more
   * elements than this queue), the element in the array immediately following the end of the queue
   * is set to <tt>null</tt>.
   *
   * <p>Like the {@link #toArray()} method, this method acts as bridge between array-based and
   * collection-based APIs. Further, this method allows precise control over the runtime type of the
   * output array, and may, under certain circumstances, be used to save allocation costs.
   *
   * <p>Suppose <tt>x</tt> is a queue known to contain only strings. The following code can be used
   * to dump the queue into a newly allocated array of <tt>String</tt>:
   *
   * <pre>
   *     String[] y = x.toArray(new String[0]);</pre>
   *
   * Note that <tt>toArray(new Object[0])</tt> is identical in function to <tt>toArray()</tt>.
   *
   * @param a the array into which the elements of the queue are to be stored, if it is big enough;
   *     otherwise, a new array of the same runtime type is allocated for this purpose
   * @return an array containing all of the elements in this queue
   * @throws ArrayStoreException if the runtime type of the specified array is not a supertype of
   *     the runtime type of every element in this queue
   * @throws NullPointerException if the specified array is null
   */
  public <T> T[] toArray(T[] a) {
    final E[] items = this.items;
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
      if (a.length < count)
        a = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), count);

      int k = 0;
      int i = takeIndex;
      while (k < count) {
        a[k++] = (T) items[i];
        i = inc(i);
      }
      if (a.length > count) a[count] = null;
      return a;
    } finally {
      lock.unlock();
    }
  }
Пример #10
0
 private Object deserialize(Node node, boolean setProperty, boolean popBean) throws Exception {
   Object object = null;
   currentType = null;
   currentNode = node;
   currentName = node.getNodeName();
   boolean isNull = false;
   NamedNodeMap attrs = node.getAttributes();
   String arrayType = null;
   for (int i = 0; i < attrs.getLength(); i++) {
     String nodeName = attrs.item(i).getNodeName();
     String nodeValue = attrs.item(i).getNodeValue();
     if (nodeName.equals(NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":" + Constants.ATTR_TYPE))
       currentType = new StringBuffer(nodeValue).delete(0, nodeValue.indexOf(':') + 1).toString();
     else if (nodeName.equals(
         NamespaceConstants.NSPREFIX_SOAP_ENCODING + ":" + Constants.ATTR_ARRAY_TYPE))
       arrayType = new StringBuffer(nodeValue).delete(0, nodeValue.indexOf(':') + 1).toString();
     else if (nodeName.equals(NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":null"))
       isNull = nodeValue.equals("true");
   }
   Class cls = null;
   if (currentType != null) cls = getXsdTypeClass(currentType);
   // Handle array, Vector, ArrayList, LinkedList, Hashtable,
   // Properties, and HashMap data types
   if ((cls != null)
       && ((cls == java.lang.reflect.Array.class)
           || (cls == Vector.class)
           || (cls == ArrayList.class)
           || (cls == LinkedList.class)
           || (cls == Hashtable.class)
           || (cls == Properties.class)
           || (cls == HashMap.class)
           || (cls == SortedMap.class))) {
     parentNode = currentNode;
     String name = node.getNodeName();
     // Handle arrays
     if (cls == java.lang.reflect.Array.class) {
       int a = arrayType.indexOf("[");
       int b = arrayType.indexOf("]");
       String s = arrayType.substring(a + 1, b);
       int arrayLen = Integer.valueOf(s).intValue();
       arrayType = arrayType.substring(0, a);
       // check if the array element is a standard Java class
       Class arrayClass = getXsdTypeClass(arrayType);
       // otherwise try to get the class of the bean
       if (arrayClass == null)
         arrayClass = getClassOfBean((ClassLoader) classLoaderStrategy, arrayType);
       object = java.lang.reflect.Array.newInstance(arrayClass, arrayLen);
     } else {
       // Construct the list or map type
       Constructor ct = cls.getConstructor((Class[]) null);
       object = ct.newInstance((Object[]) null);
     }
     // deserialize the elements of the array, list, or map
     NodeList childNodes = node.getChildNodes();
     int arrayIndex = -1;
     Node childNode = null;
     Object nodeObj = null;
     for (int i = 0; i < childNodes.getLength(); i++) {
       childNode = childNodes.item(i);
       if (childNode.getNodeType() == Node.ELEMENT_NODE) {
         if ((cls == java.lang.reflect.Array.class)
             || (cls == Vector.class)
             || (cls == ArrayList.class)
             || (cls == LinkedList.class)) {
           nodeObj = deserialize(childNode, false, true);
           if (nodeObj != null) {
             if (cls == java.lang.reflect.Array.class)
               java.lang.reflect.Array.set(object, ++arrayIndex, nodeObj);
             else ((List) object).add(nodeObj);
           }
         } else if ((cls == Hashtable.class)
             || (cls == Properties.class)
             || (cls == HashMap.class)
             || (cls == SortedMap.class)) {
           if (childNode.getLocalName().equals("item")) {
             NodeList htNodes = childNode.getChildNodes();
             if (htNodes.getLength() == 2) {
               Object hashKey = deserialize(htNodes.item(0), false, false);
               Object hashValue = deserialize(htNodes.item(1), false, true);
               ((Map) object).put(hashKey, hashValue);
             }
           }
         }
       }
     }
     setBeanProperty(name, object);
     // Handle everything else (primitives & POJOs)
   } else {
     // recurse on each of the child nodes
     NodeList childNodes = node.getChildNodes();
     if ((childNodes != null) && (childNodes.getLength() > 0)) {
       for (int i = 0; i < childNodes.getLength(); i++) {
         Node childNode = childNodes.item(i);
         if (childNode.getNodeType() == Node.ELEMENT_NODE) {
           if (currentType != null)
             createObject(
                 node,
                 currentName,
                 currentPackage,
                 currentType,
                 childNode.getNodeValue(),
                 setProperty);
           parentNode = node;
           object = deserialize(childNode, true, true);
         } else if ((childNode.getNodeType() == Node.TEXT_NODE) && (currentType != null)) {
           object =
               createObject(
                   node,
                   currentName,
                   currentPackage,
                   currentType,
                   childNode.getNodeValue(),
                   setProperty);
         }
         currentType = null;
       }
     } else {
       if (!isNull)
         object = createObject(node, currentName, currentPackage, currentType, null, setProperty);
     }
     if (node.getParentNode() != parentNode) {
       parentNode = node.getParentNode();
       if (popBean) {
         Object bean = popBeanOffStack();
         if (bean != null) object = bean;
       }
     }
   }
   return object;
 }
Пример #11
0
 // The next entry is an array too!
 public Commands.ValueSender nestedArray(Commands.ValueObject arrayHeader) {
   InvokableValueSender sender = new InvokableValueSender(arrayHeader);
   // Take the newly created array and put it into the current array.
   java.lang.reflect.Array.set(array, index++, sender.getArray());
   return sender;
 }
Пример #12
0
 // A new value is being sent to the array
 public void sendValue(Commands.ValueObject value) {
   java.lang.reflect.Array.set(array, index++, getInvokableObject(value)); // add it to the array
 }
Пример #13
0
 public void initialize(Commands.ValueObject arrayHeader) {
   index = 0;
   Class arrayType = (Class) server.getObject(arrayHeader.classID);
   array = java.lang.reflect.Array.newInstance(arrayType, arrayHeader.anInt);
 }