2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - 2011 - Calixte DENIZET <calixte@contrib.scilab.org>
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
13 package org.scilab.modules.external_objects_java;
15 import java.beans.BeanInfo;
16 import java.beans.IntrospectionException;
17 import java.beans.Introspector;
18 import java.beans.MethodDescriptor;
19 import java.beans.PropertyDescriptor;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.PrintStream;
23 import java.lang.reflect.Array;
24 import java.lang.reflect.Field;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.lang.reflect.Modifier;
28 import java.nio.Buffer;
29 import java.nio.ByteBuffer;
30 import java.nio.ByteOrder;
31 import java.nio.CharBuffer;
32 import java.nio.DoubleBuffer;
33 import java.nio.FloatBuffer;
34 import java.nio.IntBuffer;
35 import java.nio.LongBuffer;
36 import java.nio.ShortBuffer;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.List;
42 import java.util.TreeSet;
43 import java.util.logging.FileHandler;
44 import java.util.logging.Level;
45 import java.util.logging.Logger;
46 import java.util.logging.SimpleFormatter;
49 * Main class to communicate with Scilab via jni interface generated with Giws.
50 * @autor Calixte DENIZET
52 @SuppressWarnings(value = {"unchecked", "serial"})
53 public class ScilabJavaObject {
55 private static final int INITIALCAPACITY = 1024;
56 private static final Map<Class, Integer> unwrappableType = new HashMap<Class, Integer>(51);
57 private static final Map<Class, Integer> listBaseType = new HashMap<Class, Integer>(9);
58 private static final Class[] returnType = new Class[1];
60 static final Map<Class, Class> primTypes = new HashMap<Class, Class>(8);
62 private static int currentPos = 1;
63 private static FreePlace freePlace = new FreePlace();
67 static FileHandler handler;
69 protected static int currentCapacity = INITIALCAPACITY;
70 protected static ScilabJavaObject[] arraySJO = new ScilabJavaObject[currentCapacity];
73 primTypes.put(double.class, Double.class);
74 primTypes.put(float.class, Float.class);
75 primTypes.put(int.class, Integer.class);
76 primTypes.put(short.class, Short.class);
77 primTypes.put(byte.class, Byte.class);
78 primTypes.put(char.class, Character.class);
79 primTypes.put(long.class, Long.class);
80 primTypes.put(boolean.class, Boolean.class);
82 unwrappableType.put(double.class, 2);
83 unwrappableType.put(double[].class, 3);
84 unwrappableType.put(double[][].class, 4);
85 unwrappableType.put(String.class, 5);
86 unwrappableType.put(String[].class, 6);
87 unwrappableType.put(String[][].class, 7);
88 unwrappableType.put(boolean.class, 8);
89 unwrappableType.put(boolean[].class, 9);
90 unwrappableType.put(boolean[][].class, 10);
91 unwrappableType.put(byte.class, 11);
92 unwrappableType.put(byte[].class, 12);
93 unwrappableType.put(byte[][].class, 13);
94 unwrappableType.put(short.class, 17);
95 unwrappableType.put(short[].class, 18);
96 unwrappableType.put(short[][].class, 19);
97 unwrappableType.put(char.class, 20);
98 unwrappableType.put(char[].class, 21);
99 unwrappableType.put(char[][].class, 22);
100 unwrappableType.put(int.class, 23);
101 unwrappableType.put(int[].class, 24);
102 unwrappableType.put(int[][].class, 25);
103 unwrappableType.put(long.class, 29);
104 unwrappableType.put(long[].class, 30);
105 unwrappableType.put(long[][].class, 31);
106 unwrappableType.put(float.class, 35);
107 unwrappableType.put(float[].class, 36);
108 unwrappableType.put(float[][].class, 37);
109 unwrappableType.put(Double.class, 2);
110 unwrappableType.put(Double[].class, 3);
111 unwrappableType.put(Double[][].class, 4);
112 unwrappableType.put(Integer.class, 23);
113 unwrappableType.put(Integer[].class, 24);
114 unwrappableType.put(Integer[][].class, 25);
115 unwrappableType.put(Long.class, 29);
116 unwrappableType.put(Long[].class, 30);
117 unwrappableType.put(Long[][].class, 31);
118 unwrappableType.put(Byte.class, 11);
119 unwrappableType.put(Byte[].class, 12);
120 unwrappableType.put(Byte[][].class, 13);
121 unwrappableType.put(Character.class, 20);
122 unwrappableType.put(Character[].class, 21);
123 unwrappableType.put(Character[][].class, 22);
124 unwrappableType.put(Boolean.class, 8);
125 unwrappableType.put(Boolean[].class, 9);
126 unwrappableType.put(Boolean[][].class, 10);
127 unwrappableType.put(Float.class, 35);
128 unwrappableType.put(Float[].class, 36);
129 unwrappableType.put(Float[][].class, 37);
130 unwrappableType.put(Short.class, 17);
131 unwrappableType.put(Short[].class, 18);
132 unwrappableType.put(Short[][].class, 19);
134 listBaseType.put(Double.class, 3);
135 listBaseType.put(Integer.class, 24);
136 listBaseType.put(Long.class, 30);
137 listBaseType.put(Byte.class, 12);
138 listBaseType.put(Character.class, 21);
139 listBaseType.put(Boolean.class, 9);
140 listBaseType.put(Float.class, 36);
141 listBaseType.put(Short.class, 18);
142 listBaseType.put(String.class, 6);
144 arraySJO[0] = new ScilabJavaObject(null, null);
147 protected Object object;
148 protected Class clazz;
153 * @param obj the Java Object to wrap
155 public ScilabJavaObject(Object obj) {
156 this(obj, obj == null ? null : obj.getClass());
161 * @param obj the Java Object to wrap
162 * @param clazz the Java Object class
164 public ScilabJavaObject(Object obj, Class clazz) {
169 int fp = freePlace.getFreePlace();
171 this.id = currentPos;
177 arraySJO[this.id] = this;
180 logger.log(Level.INFO, "Object creation with id=" + this.id + " and class=" + clazz.toString());
183 if (currentPos >= currentCapacity) {
184 currentCapacity = currentCapacity * 2;
185 ScilabJavaObject[] arr = new ScilabJavaObject[currentCapacity];
186 System.arraycopy(arraySJO, 0, arr, 0, currentPos);
189 logger.log(Level.INFO, "Scope copy");
194 logger.log(Level.INFO, "Object creation with id=0");
203 public static final void initScilabJavaObject() { }
206 * @param filename the log file
208 public static final void enableTrace(String filename) throws ScilabJavaException {
214 logger = Logger.getLogger("JIMS");
217 handler = new FileHandler(filename, true);
218 logger.addHandler(handler);
219 logger.setLevel(Level.ALL);
220 SimpleFormatter formatter = new SimpleFormatter();
221 handler.setFormatter(formatter);
222 } catch (SecurityException e) {
224 throw new ScilabJavaException("A security exception has been thrown:\n" + e);
225 } catch (IOException e) {
227 throw new ScilabJavaException("I/O problem:\n" + e);
231 public static final void writeLog(String s) {
233 logger.log(Level.INFO, s);
240 public static final void disableTrace() {
241 if (debug && logger != null && handler != null) {
242 logger.removeHandler(handler);
253 public String toString() {
254 if (object == null) {
258 String str = object.toString();
263 return "Instance of " + object.getClass() + " with hashcode " + System.identityHashCode(object);
267 * Create a new identical reference to a java object
268 * @return A deep copy of this {@link ScilabJavaObject}
271 protected ScilabJavaObject clone() {
272 return new ScilabJavaObject(object, clazz);
276 * Get info as returned by java -version
279 public static final String[] getInfos() {
281 Class c = Class.forName("sun.misc.Version");
282 Method m = c.getMethod("print", new Class[] { PrintStream.class });
283 ByteArrayOutputStream baos = new ByteArrayOutputStream();
284 PrintStream out = new PrintStream(baos);
287 String[] ret = baos.toString().split("\n");
292 } catch (Exception e) {
298 * @param id the Java Object id
299 * @return the string to represent this object
301 public static final String getRepresentation(final int id) {
302 if (arraySJO[id] != null) {
303 return arraySJO[id].toString();
306 return "Invalid Java object";
310 * @param id the Java Object id
311 * @return true if the object is valid
313 public static final boolean isValidJavaObject(final int id) {
314 return id == 0 || (id > 0 && arraySJO[id] != null);
318 * @param id the Java Object id
319 * @param index an array of index
320 * @return the id of the element at position given by index in id
322 public static final int getArrayElement(final int id, final int[] index) throws ScilabJavaException {
325 StringBuffer buf = new StringBuffer();
327 if (index.length > 0) {
329 for (; i < index.length - 1; i++) {
330 buf.append(Integer.toString(index[i]));
333 buf.append(Integer.toString(index[i]));
336 logger.log(Level.INFO, "Get array element: array id=" + id + " at position " + buf.toString());
339 if (arraySJO[id] == null) {
340 throw new ScilabJavaException("Invalid Java object");
343 return new ScilabJavaObject(ScilabJavaArray.get(arraySJO[id].object, index)).id;
345 throw new ScilabJavaException("null is not an array");
349 * @param id the Java Object id
350 * @param index an array of index
351 * @param idArg the id of an element to put at the position given by index
353 public static final void setArrayElement(final int id, final int[] index, final int idArg) throws ScilabJavaException {
356 StringBuffer buf = new StringBuffer();
358 if (index.length > 0) {
360 for (; i < index.length - 1; i++) {
361 buf.append(Integer.toString(index[i]));
364 buf.append(Integer.toString(index[i]));
367 logger.log(Level.INFO, "Set array element: array id=" + id + " at position " + buf.toString() + " and element id=" + idArg);
370 if (arraySJO[id] == null) {
371 throw new ScilabJavaException("Invalid Java object");
374 ScilabJavaArray.set(arraySJO[id].object, index, arraySJO[idArg].object);
376 throw new ScilabJavaException("null is not an array");
381 * @param id the Java Object id
382 * @return the accessibles methods and fields corresponding to the given path
384 public static final String[] getCompletion(final int id, final String[] fieldPath) throws ScilabJavaException {
387 logger.log(Level.INFO, "Get accessible methods and fields in object id=" + id + " with path " + Arrays.deepToString(fieldPath));
390 if (arraySJO[id] == null) {
391 throw new ScilabJavaException("Invalid Java object");
394 Class clazz = arraySJO[id].clazz;
395 final boolean isClass = arraySJO[id].object == clazz;
397 if (fieldPath.length == 0) {
398 return getFieldsAndMethods(clazz, isClass);
402 // We have a class object
404 Field f = clazz.getField(fieldPath[0]);
405 int modifiers = f.getModifiers();
406 if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) {
409 return new String[0];
411 } catch (Exception e) {
412 return new String[0];
416 for (int i = (isClass ? 1 : 0); i < fieldPath.length; i++) {
418 Field f = clazz.getField(fieldPath[i]);
419 if (Modifier.isPublic(f.getModifiers())) {
422 return new String[0];
424 } catch (Exception e) {
425 return new String[0];
429 return getFieldsAndMethods(clazz, false);
431 return new String[0];
436 * Get fields and methods in a Class
437 * @param clazz the base class
440 private static final String[] getFieldsAndMethods(final Class clazz, final boolean staticOnly) {
441 if (clazz.isArray()) {
442 return new String[] {"length"};
446 final Field[] fs = clazz.getFields();
447 final Method[] ms = clazz.getMethods();
449 Set<String> set = new TreeSet<String>();
451 final int modifiers = f.getModifiers();
452 if (Modifier.isPublic(modifiers) && (!staticOnly || Modifier.isStatic(modifiers))) {
453 set.add(f.getName());
457 for (Method m : ms) {
458 final int modifiers = m.getModifiers();
459 if (Modifier.isPublic(modifiers) && (!staticOnly || Modifier.isStatic(modifiers))) {
460 set.add(m.getName());
464 // Append beans properties (and remove accessor methods)
466 final BeanInfo info = Introspector.getBeanInfo(clazz);
468 final PropertyDescriptor[] properties = info.getPropertyDescriptors();
469 if (properties != null) {
470 for (PropertyDescriptor p : properties) {
471 set.add(p.getName());
473 final Method getter = p.getReadMethod();
474 final Method setter = p.getWriteMethod();
475 if (getter != null) {
476 set.remove(getter.getName());
478 if (setter != null) {
479 set.remove(setter.getName());
483 } catch (IntrospectionException e) {
486 return set.toArray(new String[set.size()]);
487 } catch (Exception e) {
488 return new String[0];
493 * @param id the Java Object id
494 * @return the accessibles methods in the object represented by id
496 public static final String[] getAccessibleMethods(final int id) throws ScilabJavaException {
499 logger.log(Level.INFO, "Get accessible methods in object id=" + id);
502 if (arraySJO[id] == null) {
503 throw new ScilabJavaException("Invalid Java object");
506 final Method[] ms = arraySJO[id].clazz.getMethods();
507 Set<String> set = new TreeSet<String>();
508 for (Method m : ms) {
509 if (Modifier.isPublic(m.getModifiers())) {
510 set.add(m.getName());
514 return set.toArray(new String[set.size()]);
516 return new String[0];
521 * @param id the Java Object id
522 * @return the accessibles fields in the object represented by id
524 public static final String[] getAccessibleFields(final int id) throws ScilabJavaException {
527 logger.log(Level.INFO, "Get accessible fields in object id=" + id);
530 if (arraySJO[id] == null) {
531 throw new ScilabJavaException("Invalid Java object");
534 if (arraySJO[id].clazz.isArray()) {
535 return new String[] {"length"};
538 final Field[] f = arraySJO[id].clazz.getFields();
539 final String[] sf = new String[f.length];
540 for (int i = 0; i < f.length; i++) {
541 if (Modifier.isPublic(f[i].getModifiers())) {
542 sf[i] = f[i].getName();
547 return new String[0];
552 * @param id the Java Object id
553 * @return the class name of the object represented by id
555 public static final String getClassName(final int id) throws ScilabJavaException {
558 logger.log(Level.INFO, "Get class name of object id=" + id);
561 if (arraySJO[id] == null) {
562 throw new ScilabJavaException("Invalid Java object");
565 return arraySJO[id].clazz.getName();
572 * @param id the Java Object id
573 * @param fieldName the field name to set
574 * @param idArg the id of the element to set
576 public static final void setField(final int id, final String fieldName, final int idarg) throws ScilabJavaException {
581 logger.log(Level.INFO, "Set field \'" + fieldName + "\' in object id=" + id + " with value id=" + idarg);
584 if (arraySJO[id] == null) {
585 throw new ScilabJavaException("Invalid Java object");
589 f = arraySJO[id].clazz.getField(fieldName);
591 // standard field access
593 f.set(arraySJO[id].object, arraySJO[idarg].object);
594 } catch (IllegalArgumentException e) {
595 if (f != null && f.getType() == int.class && (arraySJO[idarg].clazz == double.class || arraySJO[idarg].clazz == Double.class) && ((Double) arraySJO[idarg].object).intValue() == ((Double) arraySJO[idarg].object).doubleValue()) {
596 f.set(arraySJO[id].object, ((Double) arraySJO[idarg].object).intValue());
602 } catch (NoSuchFieldException e) {
605 // lookup for a bean property
606 final PropertyDescriptor p = lookupBeanProperty(id, fieldName);
607 final Method method = p.getWriteMethod();
608 if (method == null) {
609 throw new ScilabJavaException("Cannot read the property " + fieldName + " in object " + getClassName(id));
611 method.invoke(arraySJO[id].object, arraySJO[idarg].object);
612 } catch (IllegalArgumentException e) {
613 throw new ScilabJavaException("Bad argument value for field " + fieldName + " in object " + getClassName(id));
614 } catch (IllegalAccessException e) {
615 throw new ScilabJavaException("Cannot access to the field " + fieldName + " in object " + getClassName(id));
616 } catch (InvocationTargetException e) {
617 throw new ScilabJavaException("Exception occurs on write access to the property " + fieldName + " in object " + getClassName(id));
620 throw new ScilabJavaException("null is not an object");
625 * @param id the Java Object id
626 * @param fieldName the field name to set
627 * @return the id of the got object
629 public static final int getField(final int id, final String fieldName) throws ScilabJavaException {
633 logger.log(Level.INFO, "Get field \'" + fieldName + "\' in object id=" + id);
636 if (arraySJO[id] == null) {
637 throw new ScilabJavaException("Invalid Java object");
640 if (arraySJO[id].clazz.isArray() && fieldName.equals("length")) {
641 return new ScilabJavaObject(Array.getLength(arraySJO[id].object), int.class).id;
644 if (arraySJO[id].object == arraySJO[id].clazz && fieldName.equals("class")) {
645 return new ScilabJavaObject(arraySJO[id].object, arraySJO[id].object.getClass()).id;
649 final Field f = arraySJO[id].clazz.getField(fieldName);
650 final Class cl = f.getType();
651 if (cl == int.class) {
652 return new ScilabJavaObject(f.getInt(arraySJO[id].object), int.class).id;
653 } else if (cl == double.class) {
654 return new ScilabJavaObject(f.getDouble(arraySJO[id].object), double.class).id;
655 } else if (cl == boolean.class) {
656 return new ScilabJavaObject(f.getBoolean(arraySJO[id].object), boolean.class).id;
657 } else if (cl == short.class) {
658 return new ScilabJavaObject(f.getShort(arraySJO[id].object), short.class).id;
659 } else if (cl == char.class) {
660 return new ScilabJavaObject(f.getChar(arraySJO[id].object), char.class).id;
661 } else if (cl == float.class) {
662 return new ScilabJavaObject(f.getFloat(arraySJO[id].object), float.class).id;
663 } else if (cl == byte.class) {
664 return new ScilabJavaObject(f.getByte(arraySJO[id].object), byte.class).id;
665 } else if (cl == long.class) {
666 return new ScilabJavaObject(f.getLong(arraySJO[id].object), long.class).id;
669 return new ScilabJavaObject(f.get(arraySJO[id].object)).id;
670 } catch (NoSuchFieldException e) {
673 // lookup for a bean property
674 final PropertyDescriptor p = lookupBeanProperty(id, fieldName);
675 final Method method = p.getReadMethod();
676 if (method == null) {
677 throw new ScilabJavaException("Cannot read the field or property " + fieldName + " in object " + getClassName(id));
679 final Object retValue = method.invoke(arraySJO[id].object);
680 final Class cl = retValue.getClass();
681 if (cl == int.class) {
682 return new ScilabJavaObject(retValue, int.class).id;
683 } else if (cl == double.class) {
684 return new ScilabJavaObject(retValue, double.class).id;
685 } else if (cl == boolean.class) {
686 return new ScilabJavaObject(retValue, boolean.class).id;
687 } else if (cl == short.class) {
688 return new ScilabJavaObject(retValue, short.class).id;
689 } else if (cl == char.class) {
690 return new ScilabJavaObject(retValue, char.class).id;
691 } else if (cl == float.class) {
692 return new ScilabJavaObject(retValue, float.class).id;
693 } else if (cl == byte.class) {
694 return new ScilabJavaObject(retValue, byte.class).id;
695 } else if (cl == long.class) {
696 return new ScilabJavaObject(retValue, long.class).id;
698 return new ScilabJavaObject(retValue).id;
699 } catch (IllegalArgumentException e) {
700 throw new ScilabJavaException("Bad argument value for field " + fieldName + " in object " + getClassName(id));
701 } catch (IllegalAccessException e) {
702 throw new ScilabJavaException("Cannot access to the field " + fieldName + " in object " + getClassName(id));
703 } catch (InvocationTargetException e) {
704 throw new ScilabJavaException("Exception occurs on read access to the property " + fieldName + " in object " + getClassName(id));
707 throw new ScilabJavaException("null is not an object");
712 * @param id the Java Object id
713 * @param fieldName the field name to set
714 * @return the type of the field in object represented by id:
719 public static final int getFieldType(final int id, final String fieldName) {
720 if (id > 0 && arraySJO[id] != null) {
722 logger.log(Level.INFO, "Get field type of \'" + fieldName + "\' in object id=" + id);
725 if (isValidMethod(id, fieldName)) {
729 if (arraySJO[id].clazz.isArray()) {
730 if (fieldName.equals("length")) {
737 if (arraySJO[id].object == arraySJO[id].clazz && fieldName.equals("class")) {
742 Field f = arraySJO[id].clazz.getField(fieldName);
744 } catch (NoSuchFieldException e) {
747 // lookup for a bean property
748 lookupBeanProperty(id, fieldName);
750 } catch (IllegalArgumentException e) {
752 } catch (ScilabJavaException e) {
760 private static final boolean isValidMethod(int id, String methName) {
763 info = Introspector.getBeanInfo(arraySJO[id].clazz);
764 } catch (IntrospectionException e) {
768 final MethodDescriptor[] methods = info.getMethodDescriptors();
769 if (methods == null) {
773 for (MethodDescriptor m : methods) {
774 if (methName.equals(m.getName())) {
782 private static final PropertyDescriptor lookupBeanProperty(int id, String fieldName) throws ScilabJavaException {
785 info = Introspector.getBeanInfo(arraySJO[id].clazz);
786 } catch (IntrospectionException e) {
787 throw new ScilabJavaException("Unable to get properties of object " + getClassName(id));
790 final PropertyDescriptor[] properties = info.getPropertyDescriptors();
791 if (properties == null) {
792 throw new ScilabJavaException("No property " + fieldName + " in object " + getClassName(id));
795 for (PropertyDescriptor p : properties) {
796 if (fieldName.equals(p.getName())) {
801 throw new ScilabJavaException("No property " + fieldName + " in object " + getClassName(id));
805 * @param id the Java Object id
806 * @param methName the method name to invoke
807 * @param args an array containing the id of the arguments
808 * @return the id of the invocation result
810 public static final int invoke(final int id, final String methName, final int[] args) throws ScilabJavaException {
813 StringBuffer buf = new StringBuffer();
815 if (args.length > 0) {
817 for (; i < args.length - 1; i++) {
818 buf.append(Integer.toString(args[i]));
821 buf.append(Integer.toString(args[i]));
824 logger.log(Level.INFO, "Invoke method \'" + methName + "\' in object id=" + id + " with arguments id=" + buf.toString());
827 if (arraySJO[id] != null) {
828 Object ret = ScilabJavaMethod.invoke(methName, arraySJO[id].clazz, arraySJO[id].object, returnType, args);
829 if (ret == null && returnType[0] == Void.TYPE) {
832 return new ScilabJavaObject(ret, returnType[0]).id;
835 throw new ScilabJavaException("Invalid Java object");
838 throw new ScilabJavaException("null is not an object");
843 * @param id the Java Object id
844 * @param args an array containing the id of the arguments
845 * @return the id of the invocation result
847 public static final int extract(final int id, final int[] args) throws ScilabJavaException {
850 StringBuffer buf = new StringBuffer();
852 if (args.length > 0) {
854 for (; i < args.length - 1; i++) {
855 buf.append(Integer.toString(args[i]));
858 buf.append(Integer.toString(args[i]));
861 logger.log(Level.INFO, "Extract in object id=" + id + " with arguments id=" + buf.toString());
864 for (int i = 0; i < args.length; i++) {
865 if (args[i] < 0 || arraySJO[args[i]] == null) {
866 throw new ScilabJavaException("Invalid Java object at position " + i);
870 if (arraySJO[id] != null) {
871 Object o = arraySJO[id].object;
872 for (int i = 0; i < args.length; i++) {
873 Object a = args[i] == 0 ? null : arraySJO[args[i]].object;
874 if (o instanceof Map) {
875 o = ((Map) o).get(a);
876 } else if (o instanceof List) {
879 if (a instanceof Double) {
880 // Scilab index begins at 1
881 pos = ((Double) a).intValue() - 1;
882 } else if (a instanceof Integer) {
883 pos = ((Integer) a).intValue() - 1;
887 if (pos >= 0 || pos < l.size()) {
890 throw new ScilabJavaException("Cannot get object at position " + (i + 1));
892 } else if (o.getClass().isArray()) {
894 if (a instanceof Double) {
895 pos = ((Double) a).intValue();
896 } else if (a instanceof Integer) {
897 pos = ((Integer) a).intValue();
900 o = ScilabJavaArray.get(o, new int[] {pos - 1});
902 throw new ScilabJavaException("Invalid field " + (a == null ? "null" : a.toString()));
910 return new ScilabJavaObject(o).id;
912 throw new ScilabJavaException("Invalid Java object");
914 throw new ScilabJavaException("null is not an object");
919 * @param id the Java Object id
920 * @param keys an array containing the id of the arguments
921 * @param value the id of the value
923 public static final void insert(final int id, final int[] keys, final int value) throws ScilabJavaException {
926 StringBuffer buf = new StringBuffer();
928 if (keys.length > 0) {
930 for (; i < keys.length - 1; i++) {
931 buf.append(Integer.toString(keys[i]));
934 buf.append(Integer.toString(keys[i]));
937 logger.log(Level.INFO, "Insert in object id=" + id + " with arguments id=" + buf.toString() + " and the value id=" + value);
940 if (arraySJO[id] != null) {
941 Object o = arraySJO[id].object;
942 for (int i = 0; i < keys.length - 1; i++) {
943 Object a = keys[i] == 0 ? null : arraySJO[keys[i]].object;
944 if (o instanceof Map) {
945 o = ((Map) o).get(a);
946 } else if (o instanceof List) {
949 if (a instanceof Double) {
950 // Scilab index begins at 1
951 pos = ((Double) a).intValue() - 1;
952 } else if (a instanceof Integer) {
953 pos = ((Integer) a).intValue() - 1;
957 if (pos >= 0 || pos < l.size()) {
960 throw new ScilabJavaException("Cannot get object at position " + (i + 1));
962 } else if (o.getClass().isArray()) {
964 if (a instanceof Double) {
965 pos = ((Double) a).intValue();
966 } else if (a instanceof Integer) {
967 pos = ((Integer) a).intValue();
970 o = ScilabJavaArray.get(o, new int[] {pos - 1});
972 throw new ScilabJavaException("Invalid field " + (a == null ? "null" : a.toString()));
980 int last = keys[keys.length - 1];
981 Object a = last == 0 ? null : arraySJO[last].object;
982 if (o instanceof Map) {
983 ((Map) o).put(a, arraySJO[value].object);
984 } else if (o instanceof List) {
987 if (a instanceof Double) {
988 // Scilab index begins at 1
989 pos = ((Double) a).intValue() - 1;
990 } else if (a instanceof Integer) {
991 pos = ((Integer) a).intValue() - 1;
995 if (pos >= 0 || pos < l.size()) {
996 l.set(pos, arraySJO[value].object);
997 } else if (pos < 0) {
998 l.add(0, arraySJO[value].object);
1000 l.add(arraySJO[value].object);
1002 } else if (o.getClass().isArray()) {
1004 if (a instanceof Double) {
1005 pos = ((Double) a).intValue();
1006 } else if (a instanceof Integer) {
1007 pos = ((Integer) a).intValue();
1010 ScilabJavaArray.set(o, new int[] {pos - 1}, arraySJO[value].object);
1012 throw new ScilabJavaException("Invalid field " + (a == null ? "null" : a.toString()));
1017 throw new ScilabJavaException("Invalid Java object");
1019 throw new ScilabJavaException("null is not an object");
1024 * @param id the Java Object id
1025 * @param className the target class name
1026 * @return the id of the cast result
1028 public static final int javaCast(final int id, final String className) throws ScilabJavaException {
1031 logger.log(Level.INFO, "Cast object id=" + id + " to class " + className);
1034 final int idC = ScilabClassLoader.loadJavaClass(className, false);
1035 final Class clazz = (Class) arraySJO[idC].object;
1037 int idR = new ScilabJavaObject(clazz.cast(arraySJO[id].object), clazz).id;
1038 removeScilabJavaObject(idC);
1040 } catch (ClassCastException e) {
1041 throw new ScilabJavaException("Cannot cast object " + getClassName(id) + " into " + getClassName(idC));
1044 throw new ScilabJavaException("null is not an object");
1049 * @param id the Java Object id
1050 * @param classId the target class id
1051 * @return the id of the cast result
1053 public static final int javaCast(final int id, final int classId) throws ScilabJavaException {
1056 logger.log(Level.INFO, "Cast object id=" + id + " to class with id=" + classId);
1059 final Class clazz = (Class) arraySJO[classId].object;
1061 return new ScilabJavaObject(clazz.cast(arraySJO[id].object), clazz).id;
1062 } catch (ClassCastException e) {
1063 throw new ScilabJavaException("Cannot cast object " + getClassName(id) + " into " + getClassName(classId));
1066 throw new ScilabJavaException("null is not an object");
1072 * @param id the Java Object id
1074 public static final void removeScilabJavaObject(final int id) {
1075 if (id > 0 && arraySJO[id] != null) {
1077 logger.log(Level.INFO, "Remove object id=" + id);
1079 freePlace.addFreePlace(id);
1080 if (arraySJO[id] instanceof ScilabJavaClass) {
1081 ScilabClassLoader.removeID(id);
1083 arraySJO[id] = null;
1089 * @param ids the Java Object ids
1091 public static final void removeScilabJavaObject(final int[] ids) {
1092 for (int id : ids) {
1093 removeScilabJavaObject(id);
1098 * Set the limit of a direct buffer to 0 to make it unusable.
1099 * @param id the Java Object id
1101 public static final void limitDirectBuffer(final int id) {
1102 if (id > 0 && arraySJO[id] != null && (arraySJO[id].object instanceof Buffer)) {
1104 logger.log(Level.INFO, "Limit direct buffer with id=" + id);
1107 ((Buffer) arraySJO[id].object).limit(0);
1112 * Remove all the objects and start a garbage collection
1114 public static final void garbageCollect() {
1116 logger.log(Level.INFO, "Garbage collection");
1119 currentCapacity = INITIALCAPACITY;
1120 arraySJO = new ScilabJavaObject[currentCapacity];
1121 arraySJO[0] = new ScilabJavaObject(null, null);
1122 freePlace = new FreePlace();
1123 ScilabClassLoader.clazz.clear();
1128 * @param x the variable to wrap into a Java Object
1129 * @return the corresponding id
1131 public static final int wrapAsDirectByteBuffer(final ByteBuffer x) {
1132 x.order(ByteOrder.nativeOrder());
1133 return new ScilabJavaObject(x, ByteBuffer.class).id;
1137 * @param x the variable to wrap into a Java Object
1138 * @return the corresponding id
1140 public static final int wrapAsDirectDoubleBuffer(final ByteBuffer x) {
1141 x.order(ByteOrder.nativeOrder());
1142 return new ScilabJavaObject(x.asDoubleBuffer(), DoubleBuffer.class).id;
1146 * @param x the variable to wrap into a Java Object
1147 * @return the corresponding id
1149 public static final int wrapAsDirectIntBuffer(final ByteBuffer x) {
1150 x.order(ByteOrder.nativeOrder());
1151 return new ScilabJavaObject(x.asIntBuffer(), IntBuffer.class).id;
1155 * @param x the variable to wrap into a Java Object
1156 * @return the corresponding id
1158 public static final int wrapAsDirectCharBuffer(final ByteBuffer x) {
1159 x.order(ByteOrder.nativeOrder());
1160 return new ScilabJavaObject(x.asCharBuffer(), CharBuffer.class).id;
1164 * @param x the variable to wrap into a Java Object
1165 * @return the corresponding id
1167 public static final int wrapAsDirectFloatBuffer(final ByteBuffer x) {
1168 x.order(ByteOrder.nativeOrder());
1169 return new ScilabJavaObject(x.asFloatBuffer(), FloatBuffer.class).id;
1173 * @param x the variable to wrap into a Java Object
1174 * @return the corresponding id
1176 public static final int wrapAsDirectLongBuffer(final ByteBuffer x) {
1177 x.order(ByteOrder.nativeOrder());
1178 return new ScilabJavaObject(x.asLongBuffer(), LongBuffer.class).id;
1182 * @param x the variable to wrap into a Java Object
1183 * @return the corresponding id
1185 public static final int wrapAsDirectShortBuffer(final ByteBuffer x) {
1186 x.order(ByteOrder.nativeOrder());
1187 return new ScilabJavaObject(x.asShortBuffer(), ShortBuffer.class).id;
1191 * @param x the variable to wrap into a Java Object
1192 * @return the corresponding id
1194 public static final int wrap(final double x) {
1195 return new ScilabJavaObject(x, double.class).id;
1199 * @param x the variable to wrap into a Java Object
1200 * @return the corresponding id
1202 public static final int wrap(final double[] x) {
1203 return new ScilabJavaObject(x, double[].class).id;
1207 * @param x the variable to wrap into a Java Object
1208 * @return the corresponding id
1210 public static final int wrap(final double[][] x) {
1211 return new ScilabJavaObject(x, double[][].class).id;
1215 * @param x the variable to wrap into a Java Object
1216 * @return the corresponding id
1218 public static final int wrap(final int x) {
1219 return new ScilabJavaObject(x, int.class).id;
1223 * @param x the variable to wrap into a Java Object
1224 * @return the corresponding id
1226 public static final int wrap(final int[] x) {
1227 return new ScilabJavaObject(x, int[].class).id;
1231 * @param x the variable to wrap into a Java Object
1232 * @return the corresponding id
1234 public static final int wrap(final int[][] x) {
1235 return new ScilabJavaObject(x, int[][].class).id;
1239 * @param x the variable to wrap into a Java Object
1240 * @return the corresponding id
1242 public static final int wrap(final long x) {
1243 return new ScilabJavaObject(x, long.class).id;
1247 * @param x the variable to wrap into a Java Object
1248 * @return the corresponding id
1250 public static final int wrap(final long[] x) {
1251 return new ScilabJavaObject(x, long[].class).id;
1255 * @param x the variable to wrap into a Java Object
1256 * @return the corresponding id
1258 public static final int wrap(final long[][] x) {
1259 return new ScilabJavaObject(x, long[][].class).id;
1263 * @param x the variable to wrap into a Java Object
1264 * @return the corresponding id
1266 public static final int wrap(final byte x) {
1267 return new ScilabJavaObject(x, byte.class).id;
1271 * @param x the variable to wrap into a Java Object
1272 * @return the corresponding id
1274 public static final int wrap(final byte[] x) {
1275 return new ScilabJavaObject(x, byte[].class).id;
1279 * @param x the variable to wrap into a Java Object
1280 * @return the corresponding id
1282 public static final int wrap(final byte[][] x) {
1283 return new ScilabJavaObject(x, byte[][].class).id;
1287 * @param x the variable to wrap into a Java Object
1288 * @return the corresponding id
1290 public static final int wrap(final short x) {
1291 return new ScilabJavaObject(x, short.class).id;
1295 * @param x the variable to wrap into a Java Object
1296 * @return the corresponding id
1298 public static final int wrap(final short[] x) {
1299 return new ScilabJavaObject(x, short[].class).id;
1303 * @param x the variable to wrap into a Java Object
1304 * @return the corresponding id
1306 public static final int wrap(final short[][] x) {
1307 return new ScilabJavaObject(x, short[][].class).id;
1311 * @param x the variable to wrap into a Java Object
1312 * @return the corresponding id
1314 public static final int wrap(final String x) {
1315 return new ScilabJavaObject(x, String.class).id;
1319 * @param x the variable to wrap into a Java Object
1320 * @return the corresponding id
1322 public static final int wrap(final String[] x) {
1323 return new ScilabJavaObject(x, String[].class).id;
1327 * @param x the variable to wrap into a Java Object
1328 * @return the corresponding id
1330 public static final int wrap(final String[][] x) {
1331 return new ScilabJavaObject(x, String[][].class).id;
1335 * @param x the variable to wrap into a Java Object
1336 * @return the corresponding id
1338 public static final int wrap(final boolean x) {
1339 return new ScilabJavaObject(x, boolean.class).id;
1343 * @param x the variable to wrap into a Java Object
1344 * @return the corresponding id
1346 public static final int wrap(final boolean[] x) {
1347 return new ScilabJavaObject(x, boolean[].class).id;
1351 * @param x the variable to wrap into a Java Object
1352 * @return the corresponding id
1354 public static final int wrap(final boolean[][] x) {
1355 return new ScilabJavaObject(x, boolean[][].class).id;
1359 * @param x the variable to wrap into a Java Object
1360 * @return the corresponding id
1362 public static final int wrap(final char x) {
1363 return new ScilabJavaObject(x, char.class).id;
1367 * @param x the variable to wrap into a Java Object
1368 * @return the corresponding id
1370 public static final int wrap(final char[] x) {
1371 return new ScilabJavaObject(x, char[].class).id;
1375 * @param x the variable to wrap into a Java Object
1376 * @return the corresponding id
1378 public static final int wrap(final char[][] x) {
1379 return new ScilabJavaObject(x, char[][].class).id;
1383 * @param x the variable to wrap into a Java Object
1384 * @return the corresponding id
1386 public static final int wrap(final float x) {
1387 return new ScilabJavaObject(x, float.class).id;
1391 * @param x the variable to wrap into a Java Object
1392 * @return the corresponding id
1394 public static final int wrap(final float[] x) {
1395 return new ScilabJavaObject(x, float[].class).id;
1399 * @param x the variable to wrap into a Java Object
1400 * @return the corresponding id
1402 public static final int wrap(final float[][] x) {
1403 return new ScilabJavaObject(x, float[][].class).id;
1407 * @param id the Java Object id
1408 * @return the resulting unwrapping
1410 public static final double unwrapDouble(final int id) {
1411 return ((Double) (arraySJO[id].object)).doubleValue();
1415 * @param id the Java Object id
1416 * @return the resulting unwrapping
1418 public static final Object unwrapRowDouble(final int id) {
1419 if (arraySJO[id].object instanceof List) {
1420 return ScilabJavaArray.toDoubleArray((List<Double>) arraySJO[id].object);
1421 } else if (arraySJO[id].object instanceof Double[]) {
1422 return ScilabJavaArray.toPrimitive((Double[]) arraySJO[id].object);
1423 } else if (arraySJO[id].object instanceof DoubleBuffer && !((DoubleBuffer) arraySJO[id].object).isDirect()) {
1424 return ((DoubleBuffer) arraySJO[id].object).array();
1427 return arraySJO[id].object;
1431 * @param id the Java Object id
1432 * @return the resulting unwrapping
1434 public static final double[][] unwrapMatDouble(final int id) {
1435 if (arraySJO[id].object instanceof Double[][]) {
1436 Object o = ScilabJavaArray.toPrimitive(arraySJO[id].object);
1437 return (double[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1440 return (double[][]) arraySJO[id].object;
1444 * @param id the Java Object id
1445 * @return the resulting unwrapping
1447 public static final int unwrapInt(final int id) {
1448 return ((Integer) (arraySJO[id].object)).intValue();
1452 * @param id the Java Object id
1453 * @return the resulting unwrapping
1455 public static final Object unwrapRowInt(final int id) {
1456 if (arraySJO[id].object instanceof List) {
1457 return ScilabJavaArray.toIntArray((List<Integer>) arraySJO[id].object);
1458 } else if (arraySJO[id].object instanceof Integer[]) {
1459 return ScilabJavaArray.toPrimitive((Integer[]) arraySJO[id].object);
1460 } else if (arraySJO[id].object instanceof IntBuffer && !((IntBuffer) arraySJO[id].object).isDirect()) {
1461 return ((IntBuffer) arraySJO[id].object).array();
1464 return arraySJO[id].object;
1468 * @param id the Java Object id
1469 * @return the resulting unwrapping
1471 public static final int[][] unwrapMatInt(final int id) {
1472 if (arraySJO[id].object instanceof Integer[][]) {
1473 return (int[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1476 return (int[][]) (arraySJO[id].object);
1480 * @param id the Java Object id
1481 * @return the resulting unwrapping
1483 public static final short unwrapShort(final int id) {
1484 return ((Short) (arraySJO[id].object)).shortValue();
1488 * @param id the Java Object id
1489 * @return the resulting unwrapping
1491 public static final Object unwrapRowShort(final int id) {
1492 if (arraySJO[id].object instanceof List) {
1493 return ScilabJavaArray.toShortArray((List<Short>) arraySJO[id].object);
1494 } else if (arraySJO[id].object instanceof Short[]) {
1495 return ScilabJavaArray.toPrimitive((Short[]) arraySJO[id].object);
1496 } else if (arraySJO[id].object instanceof ShortBuffer && !((ShortBuffer) arraySJO[id].object).isDirect()) {
1497 return ((ShortBuffer) arraySJO[id].object).array();
1500 return arraySJO[id].object;
1504 * @param id the Java Object id
1505 * @return the resulting unwrapping
1507 public static final short[][] unwrapMatShort(final int id) {
1508 if (arraySJO[id].object instanceof Short[][]) {
1509 return (short[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1512 return (short[][]) (arraySJO[id].object);
1516 * @param id the Java Object id
1517 * @return the resulting unwrapping
1519 public static final byte unwrapByte(final int id) {
1520 return ((Byte) (arraySJO[id].object)).byteValue();
1524 * @param id the Java Object id
1525 * @return the resulting unwrapping
1527 public static final Object unwrapRowByte(final int id) {
1528 if (arraySJO[id].object instanceof List) {
1529 return ScilabJavaArray.toByteArray((List<Byte>) arraySJO[id].object);
1530 } else if (arraySJO[id].object instanceof Byte[]) {
1531 return ScilabJavaArray.toPrimitive((Byte[]) arraySJO[id].object);
1532 } else if (arraySJO[id].object instanceof ByteBuffer && !((ByteBuffer) arraySJO[id].object).isDirect()) {
1533 return ((ByteBuffer) arraySJO[id].object).array();
1536 return arraySJO[id].object;
1540 * @param id the Java Object id
1541 * @return the resulting unwrapping
1543 public static final byte[][] unwrapMatByte(final int id) {
1544 if (arraySJO[id].object instanceof Byte[][]) {
1545 return (byte[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1548 return (byte[][]) (arraySJO[id].object);
1552 * @param id the Java Object id
1553 * @return the resulting unwrapping
1555 public static final String unwrapString(final int id) {
1556 return (String) (arraySJO[id].object);
1560 * @param id the Java Object id
1561 * @return the resulting unwrapping
1563 public static final String[] unwrapRowString(final int id) {
1564 if (arraySJO[id].object instanceof List) {
1565 return ScilabJavaArray.toStringArray((List<String>) arraySJO[id].object);
1568 return (String[]) (arraySJO[id].object);
1572 * @param id the Java Object id
1573 * @return the resulting unwrapping
1575 public static final String[][] unwrapMatString(final int id) {
1576 return (String[][]) (arraySJO[id].object);
1580 * @param id the Java Object id
1581 * @return the resulting unwrapping
1583 public static final boolean unwrapBoolean(final int id) {
1584 return (Boolean) (arraySJO[id].object);
1588 * @param id the Java Object id
1589 * @return the resulting unwrapping
1591 public static final boolean[] unwrapRowBoolean(final int id) {
1592 if (arraySJO[id].object instanceof List) {
1593 return ScilabJavaArray.toBooleanArray((List<Boolean>) arraySJO[id].object);
1594 } else if (arraySJO[id].object instanceof Boolean[]) {
1595 return ScilabJavaArray.toPrimitive((Boolean[]) arraySJO[id].object);
1598 return (boolean[]) (arraySJO[id].object);
1602 * @param id the Java Object id
1603 * @return the resulting unwrapping
1605 public static final boolean[][] unwrapMatBoolean(final int id) {
1606 if (arraySJO[id].object instanceof Boolean[][]) {
1607 return (boolean[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1610 return (boolean[][]) (arraySJO[id].object);
1614 * @param id the Java Object id
1615 * @return the resulting unwrapping
1617 public static final char unwrapChar(final int id) {
1618 return (Character) (arraySJO[id].object);
1622 * @param id the Java Object id
1623 * @return the resulting unwrapping
1625 public static final Object unwrapRowChar(final int id) {
1626 if (arraySJO[id].object instanceof List) {
1627 return ScilabJavaArray.toCharArray((List<Character>) arraySJO[id].object);
1628 } else if (arraySJO[id].object instanceof Character[]) {
1629 return ScilabJavaArray.toPrimitive((Character[]) arraySJO[id].object);
1630 } else if (arraySJO[id].object instanceof CharBuffer && !((CharBuffer) arraySJO[id].object).isDirect()) {
1631 return ((CharBuffer) arraySJO[id].object).array();
1634 return arraySJO[id].object;
1638 * @param id the Java Object id
1639 * @return the resulting unwrapping
1641 public static final char[][] unwrapMatChar(final int id) {
1642 if (arraySJO[id].object instanceof Character[][]) {
1643 return (char[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1646 return (char[][]) (arraySJO[id].object);
1650 * @param id the Java Object id
1651 * @return the resulting unwrapping
1653 public static final float unwrapFloat(final int id) {
1654 return (Float) (arraySJO[id].object);
1658 * @param id the Java Object id
1659 * @return the resulting unwrapping
1661 public static final Object unwrapRowFloat(final int id) {
1662 if (arraySJO[id].object instanceof List) {
1663 return ScilabJavaArray.toFloatArray((List<Float>) arraySJO[id].object);
1664 } else if (arraySJO[id].object instanceof Double[]) {
1665 return ScilabJavaArray.toPrimitive((Float[]) arraySJO[id].object);
1666 } else if (arraySJO[id].object instanceof FloatBuffer && !((FloatBuffer) arraySJO[id].object).isDirect()) {
1667 return ((FloatBuffer) arraySJO[id].object).array();
1670 return arraySJO[id].object;
1674 * @param id the Java Object id
1675 * @return the resulting unwrapping
1677 public static final float[][] unwrapMatFloat(final int id) {
1678 if (arraySJO[id].object instanceof Float[][]) {
1679 return (float[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1682 return (float[][]) (arraySJO[id].object);
1686 * @param id the Java Object id
1687 * @return the resulting unwrapping
1689 public static final long unwrapLong(final int id) {
1690 return (Long) (arraySJO[id].object);
1694 * @param id the Java Object id
1695 * @return the resulting unwrapping
1697 public static final Object unwrapRowLong(final int id) {
1698 if (arraySJO[id].object instanceof List) {
1699 return ScilabJavaArray.toLongArray((List<Long>) arraySJO[id].object);
1700 } else if (arraySJO[id].object instanceof Long[]) {
1701 return ScilabJavaArray.toPrimitive((Long[]) arraySJO[id].object);
1702 } else if (arraySJO[id].object instanceof LongBuffer && !((LongBuffer) arraySJO[id].object).isDirect()) {
1703 return ((LongBuffer) arraySJO[id].object).array();
1706 return arraySJO[id].object;
1710 * @param id the Java Object id
1711 * @return the resulting unwrapping
1713 public static final long[][] unwrapMatLong(final int id) {
1714 if (arraySJO[id].object instanceof Long[][]) {
1715 return (long[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1718 return (long[][]) (arraySJO[id].object);
1722 * @param id the Java Object id
1723 * @return the identifier of the unwrappable type or -1 if nothing
1725 public static final int isUnwrappable(final int id) {
1727 System.out.println("unwrappable=0");
1732 Integer t = unwrappableType.get(arraySJO[id].clazz);
1735 } else if (arraySJO[id].object instanceof List) {
1736 List l = (List) arraySJO[id].object;
1737 if (l.size() == 0 || l.get(0) == null) {
1740 Class cl = l.get(0).getClass();
1741 Integer i = listBaseType.get(cl);
1743 for (Object o : l) {
1744 if (o.getClass() != cl) {
1756 } else if (arraySJO[id].object instanceof Buffer) {
1757 if (arraySJO[id].object instanceof DoubleBuffer) {
1758 unwrappableType.put(arraySJO[id].clazz, 3);
1760 } else if (arraySJO[id].object instanceof ByteBuffer) {
1761 unwrappableType.put(arraySJO[id].clazz, 12);
1763 } else if (arraySJO[id].object instanceof IntBuffer) {
1764 unwrappableType.put(arraySJO[id].clazz, 24);
1766 } else if (arraySJO[id].object instanceof CharBuffer) {
1767 unwrappableType.put(arraySJO[id].clazz, 21);
1769 } else if (arraySJO[id].object instanceof FloatBuffer) {
1770 unwrappableType.put(arraySJO[id].clazz, 36);
1772 } else if (arraySJO[id].object instanceof LongBuffer) {
1773 unwrappableType.put(arraySJO[id].clazz, 30);
1775 } else if (arraySJO[id].object instanceof ShortBuffer) {
1776 unwrappableType.put(arraySJO[id].clazz, 18);
1787 * Inner class to manage the free places
1789 private static final class FreePlace {
1795 this.fp = new int[INITIALCAPACITY];
1799 final void addFreePlace(final int n) {
1800 if (currentPos == fp.length - 1) {
1801 int[] newFp = new int[(int) (1.5 * fp.length)];
1802 System.arraycopy(fp, 0, newFp, 0, fp.length);
1805 fp[++currentPos] = n;
1808 final int getFreePlace() {
1809 if (currentPos == -1) {
1812 return fp[currentPos--];
1815 final int[] getCurrentFreePlace() {
1816 if (currentPos == -1) {
1820 int[] ret = new int[currentPos + 1];
1821 System.arraycopy(fp, 0, ret, 0, ret.length);