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.ArrayList;
38 import java.util.Arrays;
39 import java.util.HashMap;
40 import java.util.List;
43 import java.util.TreeSet;
44 import java.util.logging.FileHandler;
45 import java.util.logging.Level;
46 import java.util.logging.Logger;
47 import java.util.logging.SimpleFormatter;
50 * Main class to communicate with Scilab via jni interface generated with Giws.
51 * @autor Calixte DENIZET
53 @SuppressWarnings(value = {"unchecked", "serial"})
54 public class ScilabJavaObject {
56 private static final int INITIALCAPACITY = 1024;
57 private static final Map<Class, Integer> unwrappableType = new HashMap<Class, Integer>(51);
58 private static final Map<Class, Integer> listBaseType = new HashMap<Class, Integer>(9);
59 private static final Class[] returnType = new Class[1];
61 static final Map<Class, Class> primTypes = new HashMap<Class, Class>(8);
63 private static int currentPos = 1;
64 private static FreePlace freePlace = new FreePlace();
68 static FileHandler handler;
70 protected static int currentCapacity = INITIALCAPACITY;
71 protected static ScilabJavaObject[] arraySJO = new ScilabJavaObject[currentCapacity];
73 private static final class Poly {
74 private final double[] coefs;
75 public Poly(double[] coefs) {
81 primTypes.put(double.class, Double.class);
82 primTypes.put(float.class, Float.class);
83 primTypes.put(int.class, Integer.class);
84 primTypes.put(short.class, Short.class);
85 primTypes.put(byte.class, Byte.class);
86 primTypes.put(char.class, Character.class);
87 primTypes.put(long.class, Long.class);
88 primTypes.put(boolean.class, Boolean.class);
90 unwrappableType.put(double.class, 2);
91 unwrappableType.put(double[].class, 3);
92 unwrappableType.put(double[][].class, 4);
93 unwrappableType.put(String.class, 5);
94 unwrappableType.put(String[].class, 6);
95 unwrappableType.put(String[][].class, 7);
96 unwrappableType.put(boolean.class, 8);
97 unwrappableType.put(boolean[].class, 9);
98 unwrappableType.put(boolean[][].class, 10);
99 unwrappableType.put(byte.class, 11);
100 unwrappableType.put(byte[].class, 12);
101 unwrappableType.put(byte[][].class, 13);
102 unwrappableType.put(short.class, 17);
103 unwrappableType.put(short[].class, 18);
104 unwrappableType.put(short[][].class, 19);
105 unwrappableType.put(char.class, 20);
106 unwrappableType.put(char[].class, 21);
107 unwrappableType.put(char[][].class, 22);
108 unwrappableType.put(int.class, 23);
109 unwrappableType.put(int[].class, 24);
110 unwrappableType.put(int[][].class, 25);
111 unwrappableType.put(long.class, 29);
112 unwrappableType.put(long[].class, 30);
113 unwrappableType.put(long[][].class, 31);
114 unwrappableType.put(float.class, 35);
115 unwrappableType.put(float[].class, 36);
116 unwrappableType.put(float[][].class, 37);
117 unwrappableType.put(Double.class, 2);
118 unwrappableType.put(Double[].class, 3);
119 unwrappableType.put(Double[][].class, 4);
120 unwrappableType.put(Integer.class, 23);
121 unwrappableType.put(Integer[].class, 24);
122 unwrappableType.put(Integer[][].class, 25);
123 unwrappableType.put(Long.class, 29);
124 unwrappableType.put(Long[].class, 30);
125 unwrappableType.put(Long[][].class, 31);
126 unwrappableType.put(Byte.class, 11);
127 unwrappableType.put(Byte[].class, 12);
128 unwrappableType.put(Byte[][].class, 13);
129 unwrappableType.put(Character.class, 20);
130 unwrappableType.put(Character[].class, 21);
131 unwrappableType.put(Character[][].class, 22);
132 unwrappableType.put(Boolean.class, 8);
133 unwrappableType.put(Boolean[].class, 9);
134 unwrappableType.put(Boolean[][].class, 10);
135 unwrappableType.put(Float.class, 35);
136 unwrappableType.put(Float[].class, 36);
137 unwrappableType.put(Float[][].class, 37);
138 unwrappableType.put(Short.class, 17);
139 unwrappableType.put(Short[].class, 18);
140 unwrappableType.put(Short[][].class, 19);
142 listBaseType.put(Double.class, 3);
143 listBaseType.put(Integer.class, 24);
144 listBaseType.put(Long.class, 30);
145 listBaseType.put(Byte.class, 12);
146 listBaseType.put(Character.class, 21);
147 listBaseType.put(Boolean.class, 9);
148 listBaseType.put(Float.class, 36);
149 listBaseType.put(Short.class, 18);
150 listBaseType.put(String.class, 6);
152 arraySJO[0] = new ScilabJavaObject(null, null);
155 protected Object object;
156 protected Class clazz;
161 * @param obj the Java Object to wrap
163 public ScilabJavaObject(Object obj) {
164 this(obj, obj == null ? null : obj.getClass());
169 * @param obj the Java Object to wrap
170 * @param clazz the Java Object class
172 public ScilabJavaObject(Object obj, Class clazz) {
177 int fp = freePlace.getFreePlace();
179 this.id = currentPos;
185 arraySJO[this.id] = this;
188 logger.log(Level.INFO, "Object creation with id=" + this.id + " and class=" + clazz.toString());
191 if (currentPos >= currentCapacity) {
192 currentCapacity = currentCapacity * 2;
193 ScilabJavaObject[] arr = new ScilabJavaObject[currentCapacity];
194 System.arraycopy(arraySJO, 0, arr, 0, currentPos);
197 logger.log(Level.INFO, "Scope copy");
202 logger.log(Level.INFO, "Object creation with id=0");
211 public static final void initScilabJavaObject() { }
214 * @param filename the log file
216 public static final void enableTrace(String filename) throws ScilabJavaException {
222 logger = Logger.getLogger("JIMS");
225 handler = new FileHandler(filename, true);
226 logger.addHandler(handler);
227 logger.setLevel(Level.ALL);
228 SimpleFormatter formatter = new SimpleFormatter();
229 handler.setFormatter(formatter);
230 } catch (SecurityException e) {
232 throw new ScilabJavaException("A security exception has been thrown:\n" + e);
233 } catch (IOException e) {
235 throw new ScilabJavaException("I/O problem:\n" + e);
239 public static final void writeLog(String s) {
241 logger.log(Level.INFO, s);
248 public static final void disableTrace() {
249 if (debug && logger != null && handler != null) {
250 logger.removeHandler(handler);
261 public String toString() {
262 if (object == null) {
266 String str = object.toString();
271 return "Instance of " + object.getClass() + " with hashcode " + System.identityHashCode(object);
275 * Create a new identical reference to a java object
276 * @return A deep copy of this {@link ScilabJavaObject}
279 protected ScilabJavaObject clone() {
280 return new ScilabJavaObject(object, clazz);
284 * Get info as returned by java -version
287 public static final String[] getInfos() {
289 Class c = Class.forName("sun.misc.Version");
290 Method m = c.getMethod("print", new Class[] { PrintStream.class });
291 ByteArrayOutputStream baos = new ByteArrayOutputStream();
292 PrintStream out = new PrintStream(baos);
295 String[] ret = baos.toString().split("\n");
300 } catch (Exception e) {
306 * @param id the Java Object id
307 * @return the string to represent this object
309 public static final String getRepresentation(final int id) {
310 if (arraySJO[id] != null) {
311 return arraySJO[id].toString();
314 return "Invalid Java object";
318 * @param id the Java Object id
319 * @return true if the object is valid
321 public static final boolean isValidJavaObject(final int id) {
322 return id == 0 || (id > 0 && arraySJO[id] != null);
326 * @param id the Java Object id
327 * @param index an array of index
328 * @return the id of the element at position given by index in id
330 public static final int getArrayElement(final int id, final int[] index) throws ScilabJavaException {
333 StringBuffer buf = new StringBuffer();
335 if (index.length > 0) {
337 for (; i < index.length - 1; i++) {
338 buf.append(Integer.toString(index[i]));
341 buf.append(Integer.toString(index[i]));
344 logger.log(Level.INFO, "Get array element: array id=" + id + " at position " + buf.toString());
347 if (arraySJO[id] == null) {
348 throw new ScilabJavaException("Invalid Java object");
351 return new ScilabJavaObject(ScilabJavaArray.get(arraySJO[id].object, index)).id;
353 throw new ScilabJavaException("null is not an array");
357 * @param id the Java Object id
358 * @param index an array of index
359 * @param idArg the id of an element to put at the position given by index
361 public static final void setArrayElement(final int id, final int[] index, final int idArg) throws ScilabJavaException {
364 StringBuffer buf = new StringBuffer();
366 if (index.length > 0) {
368 for (; i < index.length - 1; i++) {
369 buf.append(Integer.toString(index[i]));
372 buf.append(Integer.toString(index[i]));
375 logger.log(Level.INFO, "Set array element: array id=" + id + " at position " + buf.toString() + " and element id=" + idArg);
378 if (arraySJO[id] == null) {
379 throw new ScilabJavaException("Invalid Java object");
382 ScilabJavaArray.set(arraySJO[id].object, index, arraySJO[idArg].object);
384 throw new ScilabJavaException("null is not an array");
389 * @param id the Java Object id
390 * @return the accessibles methods and fields corresponding to the given path
392 public static final String[] getCompletion(final int id, final String[] fieldPath) throws ScilabJavaException {
395 logger.log(Level.INFO, "Get accessible methods and fields in object id=" + id + " with path " + Arrays.deepToString(fieldPath));
398 if (arraySJO[id] == null) {
399 throw new ScilabJavaException("Invalid Java object");
402 Class clazz = arraySJO[id].clazz;
403 final boolean isClass = arraySJO[id].object == clazz;
405 if (fieldPath.length == 0) {
406 return getFieldsAndMethods(clazz, isClass);
410 // We have a class object
412 Field f = clazz.getField(fieldPath[0]);
413 int modifiers = f.getModifiers();
414 if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) {
417 return new String[0];
419 } catch (Exception e) {
420 return new String[0];
424 for (int i = (isClass ? 1 : 0); i < fieldPath.length; i++) {
426 Field f = clazz.getField(fieldPath[i]);
427 if (Modifier.isPublic(f.getModifiers())) {
430 return new String[0];
432 } catch (Exception e) {
433 return new String[0];
437 return getFieldsAndMethods(clazz, false);
439 return new String[0];
444 * Get fields and methods in a Class
445 * @param clazz the base class
448 private static final String[] getFieldsAndMethods(final Class clazz, final boolean staticOnly) {
449 if (clazz.isArray()) {
450 return new String[] {"length"};
454 final Field[] fs = clazz.getFields();
455 final Method[] ms = clazz.getMethods();
457 Set<String> set = new TreeSet<String>();
459 final int modifiers = f.getModifiers();
460 if (Modifier.isPublic(modifiers) && (!staticOnly || Modifier.isStatic(modifiers))) {
461 set.add(f.getName());
465 for (Method m : ms) {
466 final int modifiers = m.getModifiers();
467 if (Modifier.isPublic(modifiers) && (!staticOnly || Modifier.isStatic(modifiers))) {
468 set.add(m.getName());
472 // Append beans properties (and remove accessor methods)
474 final BeanInfo info = Introspector.getBeanInfo(clazz);
476 final PropertyDescriptor[] properties = info.getPropertyDescriptors();
477 if (properties != null) {
478 for (PropertyDescriptor p : properties) {
479 set.add(p.getName());
481 final Method getter = p.getReadMethod();
482 final Method setter = p.getWriteMethod();
483 if (getter != null) {
484 set.remove(getter.getName());
486 if (setter != null) {
487 set.remove(setter.getName());
491 } catch (IntrospectionException e) {
494 return set.toArray(new String[set.size()]);
495 } catch (Exception e) {
496 return new String[0];
501 * @param id the Java Object id
502 * @return the accessibles methods in the object represented by id
504 public static final String[] getAccessibleMethods(final int id) throws ScilabJavaException {
507 logger.log(Level.INFO, "Get accessible methods in object id=" + id);
510 if (arraySJO[id] == null) {
511 throw new ScilabJavaException("Invalid Java object");
514 final Method[] ms = arraySJO[id].clazz.getMethods();
515 Set<String> set = new TreeSet<String>();
516 for (Method m : ms) {
517 if (Modifier.isPublic(m.getModifiers())) {
518 set.add(m.getName());
522 return set.toArray(new String[set.size()]);
524 return new String[0];
529 * @param id the Java Object id
530 * @return the accessibles fields in the object represented by id
532 public static final String[] getAccessibleFields(final int id) throws ScilabJavaException {
535 logger.log(Level.INFO, "Get accessible fields in object id=" + id);
538 if (arraySJO[id] == null) {
539 throw new ScilabJavaException("Invalid Java object");
542 if (arraySJO[id].clazz.isArray()) {
543 return new String[] {"length"};
546 final Field[] f = arraySJO[id].clazz.getFields();
547 final String[] sf = new String[f.length];
548 for (int i = 0; i < f.length; i++) {
549 if (Modifier.isPublic(f[i].getModifiers())) {
550 sf[i] = f[i].getName();
555 return new String[0];
560 * @param id the Java Object id
561 * @return the class name of the object represented by id
563 public static final String getClassName(final int id) throws ScilabJavaException {
566 logger.log(Level.INFO, "Get class name of object id=" + id);
569 if (arraySJO[id] == null) {
570 throw new ScilabJavaException("Invalid Java object");
573 return arraySJO[id].clazz.getName();
580 * @param id the Java Object id
581 * @param fieldName the field name to set
582 * @param idArg the id of the element to set
584 public static final void setField(final int id, final String fieldName, final int idarg) throws ScilabJavaException {
589 logger.log(Level.INFO, "Set field \'" + fieldName + "\' in object id=" + id + " with value id=" + idarg);
592 if (arraySJO[id] == null) {
593 throw new ScilabJavaException("Invalid Java object");
597 f = arraySJO[id].clazz.getField(fieldName);
599 // standard field access
601 f.set(arraySJO[id].object, arraySJO[idarg].object);
602 } catch (IllegalArgumentException e) {
603 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()) {
604 f.set(arraySJO[id].object, ((Double) arraySJO[idarg].object).intValue());
610 } catch (NoSuchFieldException e) {
613 // lookup for a bean property
614 final PropertyDescriptor p = lookupBeanProperty(id, fieldName);
615 final Method method = p.getWriteMethod();
616 if (method == null) {
617 throw new ScilabJavaException("Cannot read the property " + fieldName + " in object " + getClassName(id));
619 method.invoke(arraySJO[id].object, arraySJO[idarg].object);
620 } catch (IllegalArgumentException e) {
621 throw new ScilabJavaException("Bad argument value for field " + fieldName + " in object " + getClassName(id));
622 } catch (IllegalAccessException e) {
623 throw new ScilabJavaException("Cannot access to the field " + fieldName + " in object " + getClassName(id));
624 } catch (InvocationTargetException e) {
625 throw new ScilabJavaException("Exception occurs on write access to the property " + fieldName + " in object " + getClassName(id));
628 throw new ScilabJavaException("null is not an object");
633 * @param id the Java Object id
634 * @param fieldName the field name to set
635 * @return the id of the got object
637 public static final int getField(final int id, final String fieldName) throws ScilabJavaException {
641 logger.log(Level.INFO, "Get field \'" + fieldName + "\' in object id=" + id);
644 if (arraySJO[id] == null) {
645 throw new ScilabJavaException("Invalid Java object");
648 if (arraySJO[id].clazz.isArray() && fieldName.equals("length")) {
649 return new ScilabJavaObject(Array.getLength(arraySJO[id].object), int.class).id;
652 if (arraySJO[id].object == arraySJO[id].clazz && fieldName.equals("class")) {
653 return new ScilabJavaObject(arraySJO[id].object, arraySJO[id].object.getClass()).id;
657 final Field f = arraySJO[id].clazz.getField(fieldName);
658 final Class cl = f.getType();
659 if (cl == int.class) {
660 return new ScilabJavaObject(f.getInt(arraySJO[id].object), int.class).id;
661 } else if (cl == double.class) {
662 return new ScilabJavaObject(f.getDouble(arraySJO[id].object), double.class).id;
663 } else if (cl == boolean.class) {
664 return new ScilabJavaObject(f.getBoolean(arraySJO[id].object), boolean.class).id;
665 } else if (cl == short.class) {
666 return new ScilabJavaObject(f.getShort(arraySJO[id].object), short.class).id;
667 } else if (cl == char.class) {
668 return new ScilabJavaObject(f.getChar(arraySJO[id].object), char.class).id;
669 } else if (cl == float.class) {
670 return new ScilabJavaObject(f.getFloat(arraySJO[id].object), float.class).id;
671 } else if (cl == byte.class) {
672 return new ScilabJavaObject(f.getByte(arraySJO[id].object), byte.class).id;
673 } else if (cl == long.class) {
674 return new ScilabJavaObject(f.getLong(arraySJO[id].object), long.class).id;
677 return new ScilabJavaObject(f.get(arraySJO[id].object)).id;
678 } catch (NoSuchFieldException e) {
681 // lookup for a bean property
682 final PropertyDescriptor p = lookupBeanProperty(id, fieldName);
683 final Method method = p.getReadMethod();
684 if (method == null) {
685 throw new ScilabJavaException("Cannot read the field or property " + fieldName + " in object " + getClassName(id));
687 final Object retValue = method.invoke(arraySJO[id].object);
688 if (retValue == null) {
689 return new ScilabJavaObject(retValue).id;
692 final Class cl = retValue.getClass();
693 if (cl == int.class) {
694 return new ScilabJavaObject(retValue, int.class).id;
695 } else if (cl == double.class) {
696 return new ScilabJavaObject(retValue, double.class).id;
697 } else if (cl == boolean.class) {
698 return new ScilabJavaObject(retValue, boolean.class).id;
699 } else if (cl == short.class) {
700 return new ScilabJavaObject(retValue, short.class).id;
701 } else if (cl == char.class) {
702 return new ScilabJavaObject(retValue, char.class).id;
703 } else if (cl == float.class) {
704 return new ScilabJavaObject(retValue, float.class).id;
705 } else if (cl == byte.class) {
706 return new ScilabJavaObject(retValue, byte.class).id;
707 } else if (cl == long.class) {
708 return new ScilabJavaObject(retValue, long.class).id;
710 return new ScilabJavaObject(retValue).id;
711 } catch (IllegalArgumentException e) {
712 throw new ScilabJavaException("Bad argument value for field " + fieldName + " in object " + getClassName(id));
713 } catch (IllegalAccessException e) {
714 throw new ScilabJavaException("Cannot access to the field " + fieldName + " in object " + getClassName(id));
715 } catch (InvocationTargetException e) {
716 throw new ScilabJavaException("Exception occurs on read access to the property " + fieldName + " in object " + getClassName(id));
719 throw new ScilabJavaException("null is not an object");
724 * @param id the Java Object id
725 * @param fieldName the field name to set
726 * @return the type of the field in object represented by id:
731 public static final int getFieldType(final int id, final String fieldName) {
732 if (id > 0 && arraySJO[id] != null) {
734 logger.log(Level.INFO, "Get field type of \'" + fieldName + "\' in object id=" + id);
737 if (isValidMethod(id, fieldName)) {
741 if (arraySJO[id].clazz.isArray()) {
742 if (fieldName.equals("length")) {
749 if (arraySJO[id].object == arraySJO[id].clazz && fieldName.equals("class")) {
754 Field f = arraySJO[id].clazz.getField(fieldName);
756 } catch (NoSuchFieldException e) {
759 // lookup for a bean property
760 lookupBeanProperty(id, fieldName);
762 } catch (IllegalArgumentException e) {
764 } catch (ScilabJavaException e) {
772 private static final boolean isValidMethod(int id, String methName) {
775 info = Introspector.getBeanInfo(arraySJO[id].clazz);
776 } catch (IntrospectionException e) {
780 final MethodDescriptor[] methods = info.getMethodDescriptors();
781 if (methods == null) {
785 for (MethodDescriptor m : methods) {
786 if (methName.equals(m.getName())) {
794 private static final PropertyDescriptor lookupBeanProperty(int id, String fieldName) throws ScilabJavaException {
797 info = Introspector.getBeanInfo(arraySJO[id].clazz);
798 } catch (IntrospectionException e) {
799 throw new ScilabJavaException("Unable to get properties of object " + getClassName(id));
802 final PropertyDescriptor[] properties = info.getPropertyDescriptors();
803 if (properties == null) {
804 throw new ScilabJavaException("No property " + fieldName + " in object " + getClassName(id));
807 for (PropertyDescriptor p : properties) {
808 if (fieldName.equals(p.getName())) {
813 throw new ScilabJavaException("No property " + fieldName + " in object " + getClassName(id));
817 * @param id the Java Object id
818 * @param methName the method name to invoke
819 * @param args an array containing the id of the arguments
820 * @return the id of the invocation result
822 public static final int invoke(final int id, final String methName, final int[] args) throws ScilabJavaException {
825 StringBuffer buf = new StringBuffer();
827 if (args.length > 0) {
829 for (; i < args.length - 1; i++) {
830 buf.append(Integer.toString(args[i]));
833 buf.append(Integer.toString(args[i]));
836 logger.log(Level.INFO, "Invoke method \'" + methName + "\' in object id=" + id + " with arguments id=" + buf.toString());
839 if (arraySJO[id] != null) {
840 Object ret = ScilabJavaMethod.invoke(methName, arraySJO[id].clazz, arraySJO[id].object, returnType, args);
841 if (ret == null && returnType[0] == Void.TYPE) {
844 return new ScilabJavaObject(ret, returnType[0]).id;
847 throw new ScilabJavaException("Invalid Java object");
850 throw new ScilabJavaException("null is not an object");
855 * @param id the Java Object id
856 * @param args an array containing the id of the arguments
857 * @return the id of the invocation result
859 public static final int extract(final int id, final int[] args) throws ScilabJavaException {
862 StringBuffer buf = new StringBuffer();
864 if (args.length > 0) {
866 for (; i < args.length - 1; i++) {
867 buf.append(Integer.toString(args[i]));
870 buf.append(Integer.toString(args[i]));
873 logger.log(Level.INFO, "Extract in object id=" + id + " with arguments id=" + buf.toString());
876 for (int i = 0; i < args.length; i++) {
877 if (args[i] < 0 || arraySJO[args[i]] == null) {
878 throw new ScilabJavaException("Invalid Java object at position " + i);
882 if (arraySJO[id] != null) {
883 Object o = arraySJO[id].object;
884 for (int i = 0; i < args.length; i++) {
885 Object a = args[i] == 0 ? null : arraySJO[args[i]].object;
886 if (o instanceof Map) {
887 o = ((Map) o).get(a);
888 } else if (o instanceof List) {
891 if (a instanceof Double) {
892 // Scilab index begins at 1
893 pos = ((Double) a).intValue() - 1;
894 } else if (a instanceof Integer) {
895 pos = ((Integer) a).intValue() - 1;
896 } else if (a instanceof Poly) {
897 /* this '$' polynomial coefs */
898 pos = ((int) horner(l.size(), (Poly) a)) - 1;
902 if (pos >= 0 || pos < l.size()) {
905 throw new ScilabJavaException("Cannot get object at position " + (i + 1));
907 } else if (o.getClass().isArray()) {
909 if (a instanceof Double) {
910 pos = ((Double) a).intValue();
911 } else if (a instanceof Integer) {
912 pos = ((Integer) a).intValue();
915 o = ScilabJavaArray.get(o, new int[] {pos - 1});
917 throw new ScilabJavaException("Invalid field " + (a == null ? "null" : a.toString()));
925 return new ScilabJavaObject(o).id;
927 throw new ScilabJavaException("Invalid Java object");
929 throw new ScilabJavaException("null is not an object");
934 * @param id the Java Object id
935 * @param keys an array containing the id of the arguments
936 * @param value the id of the value
938 public static final void insert(final int id, final int[] keys, final int value) throws ScilabJavaException {
941 StringBuffer buf = new StringBuffer();
943 if (keys.length > 0) {
945 for (; i < keys.length - 1; i++) {
946 buf.append(Integer.toString(keys[i]));
949 buf.append(Integer.toString(keys[i]));
952 logger.log(Level.INFO, "Insert in object id=" + id + " with arguments id=" + buf.toString() + " and the value id=" + value);
955 if (arraySJO[id] != null) {
956 Object o = arraySJO[id].object;
957 for (int i = 0; i < keys.length - 1; i++) {
958 Object a = keys[i] == 0 ? null : arraySJO[keys[i]].object;
959 if (o instanceof Map) {
960 o = ((Map) o).get(a);
961 } else if (o instanceof List) {
964 if (a instanceof Double) {
965 // Scilab index begins at 1
966 pos = ((Double) a).intValue() - 1;
967 } else if (a instanceof Integer) {
968 pos = ((Integer) a).intValue() - 1;
972 if (pos >= 0 || pos < l.size()) {
975 throw new ScilabJavaException("Cannot get object at position " + (i + 1));
977 } else if (o.getClass().isArray()) {
979 if (a instanceof Double) {
980 pos = ((Double) a).intValue();
981 } else if (a instanceof Integer) {
982 pos = ((Integer) a).intValue();
985 o = ScilabJavaArray.get(o, new int[] {pos - 1});
987 throw new ScilabJavaException("Invalid field " + (a == null ? "null" : a.toString()));
995 int last = keys[keys.length - 1];
996 Object a = last == 0 ? null : arraySJO[last].object;
997 if (o instanceof Map) {
998 ((Map) o).put(a, arraySJO[value].object);
999 } else if (o instanceof List) {
1002 if (a instanceof Double) {
1003 // Scilab index begins at 1
1004 pos = ((Double) a).intValue() - 1;
1005 } else if (a instanceof Integer) {
1006 pos = ((Integer) a).intValue() - 1;
1007 } else if (a instanceof Poly) {
1008 /* this '$' polynomial coefs */
1009 pos = ((int) horner(l.size(), (Poly) a)) - 1;
1014 // the last element should be add-ed instead of set-ed
1015 if (pos >= 0 && pos < l.size()) {
1016 l.set(pos, arraySJO[value].object);
1017 } else if (pos < 0) {
1018 l.add(0, arraySJO[value].object);
1020 l.add(pos, arraySJO[value].object);
1022 } else if (o.getClass().isArray()) {
1024 if (a instanceof Double) {
1025 pos = ((Double) a).intValue();
1026 } else if (a instanceof Integer) {
1027 pos = ((Integer) a).intValue();
1030 ScilabJavaArray.set(o, new int[] {pos - 1}, arraySJO[value].object);
1032 throw new ScilabJavaException("Invalid field " + (a == null ? "null" : a.toString()));
1037 throw new ScilabJavaException("Invalid Java object");
1039 throw new ScilabJavaException("null is not an object");
1044 * Compute expected index using the coefs dans the actual size
1046 * @param size the x value
1047 * @param coefs the a_n values
1048 * @see http://en.wikipedia.org/wiki/Horner's_method
1050 private static final double horner(double size, Poly p) {
1052 for (int i = p.coefs.length - 1; i >= 0; i--) {
1053 result = result * size + p.coefs[i];
1059 * @param id the Java Object id
1060 * @param className the target class name
1061 * @return the id of the cast result
1063 public static final int javaCast(final int id, final String className) throws ScilabJavaException {
1066 logger.log(Level.INFO, "Cast object id=" + id + " to class " + className);
1069 final int idC = ScilabClassLoader.loadJavaClass(className, false);
1070 final Class clazz = (Class) arraySJO[idC].object;
1072 int idR = new ScilabJavaObject(clazz.cast(arraySJO[id].object), clazz).id;
1073 removeScilabJavaObject(idC);
1075 } catch (ClassCastException e) {
1076 throw new ScilabJavaException("Cannot cast object " + getClassName(id) + " into " + getClassName(idC));
1079 throw new ScilabJavaException("null is not an object");
1084 * @param id the Java Object id
1085 * @param classId the target class id
1086 * @return the id of the cast result
1088 public static final int javaCast(final int id, final int classId) throws ScilabJavaException {
1091 logger.log(Level.INFO, "Cast object id=" + id + " to class with id=" + classId);
1094 final Class clazz = (Class) arraySJO[classId].object;
1096 return new ScilabJavaObject(clazz.cast(arraySJO[id].object), clazz).id;
1097 } catch (ClassCastException e) {
1098 throw new ScilabJavaException("Cannot cast object " + getClassName(id) + " into " + getClassName(classId));
1101 throw new ScilabJavaException("null is not an object");
1107 * @param id the Java Object id
1109 public static final void removeScilabJavaObject(final int id) {
1110 if (id > 0 && arraySJO[id] != null) {
1112 logger.log(Level.INFO, "Remove object id=" + id);
1114 freePlace.addFreePlace(id);
1115 if (arraySJO[id] instanceof ScilabJavaClass) {
1116 ScilabClassLoader.removeID(id);
1118 arraySJO[id] = null;
1124 * @param ids the Java Object ids
1126 public static final void removeScilabJavaObject(final int[] ids) {
1127 for (int id : ids) {
1128 removeScilabJavaObject(id);
1133 * Set the limit of a direct buffer to 0 to make it unusable.
1134 * @param id the Java Object id
1136 public static final void limitDirectBuffer(final int id) {
1137 if (id > 0 && arraySJO[id] != null && (arraySJO[id].object instanceof Buffer)) {
1139 logger.log(Level.INFO, "Limit direct buffer with id=" + id);
1142 ((Buffer) arraySJO[id].object).limit(0);
1147 * Remove all the objects and start a garbage collection
1149 public static final void garbageCollect() {
1151 logger.log(Level.INFO, "Garbage collection");
1154 currentCapacity = INITIALCAPACITY;
1155 arraySJO = new ScilabJavaObject[currentCapacity];
1156 arraySJO[0] = new ScilabJavaObject(null, null);
1157 freePlace = new FreePlace();
1158 ScilabClassLoader.clazz.clear();
1163 * @param x the variable to wrap into a Java Object
1164 * @return the corresponding id
1166 public static final int wrapAsDirectByteBuffer(final ByteBuffer x) {
1167 x.order(ByteOrder.nativeOrder());
1168 return new ScilabJavaObject(x, ByteBuffer.class).id;
1172 * @param x the variable to wrap into a Java Object
1173 * @return the corresponding id
1175 public static final int wrapAsDirectDoubleBuffer(final ByteBuffer x) {
1176 x.order(ByteOrder.nativeOrder());
1177 return new ScilabJavaObject(x.asDoubleBuffer(), DoubleBuffer.class).id;
1181 * @param x the variable to wrap into a Java Object
1182 * @return the corresponding id
1184 public static final int wrapAsDirectIntBuffer(final ByteBuffer x) {
1185 x.order(ByteOrder.nativeOrder());
1186 return new ScilabJavaObject(x.asIntBuffer(), IntBuffer.class).id;
1190 * @param x the variable to wrap into a Java Object
1191 * @return the corresponding id
1193 public static final int wrapAsDirectCharBuffer(final ByteBuffer x) {
1194 x.order(ByteOrder.nativeOrder());
1195 return new ScilabJavaObject(x.asCharBuffer(), CharBuffer.class).id;
1199 * @param x the variable to wrap into a Java Object
1200 * @return the corresponding id
1202 public static final int wrapAsDirectFloatBuffer(final ByteBuffer x) {
1203 x.order(ByteOrder.nativeOrder());
1204 return new ScilabJavaObject(x.asFloatBuffer(), FloatBuffer.class).id;
1208 * @param x the variable to wrap into a Java Object
1209 * @return the corresponding id
1211 public static final int wrapAsDirectLongBuffer(final ByteBuffer x) {
1212 x.order(ByteOrder.nativeOrder());
1213 return new ScilabJavaObject(x.asLongBuffer(), LongBuffer.class).id;
1217 * @param x the variable to wrap into a Java Object
1218 * @return the corresponding id
1220 public static final int wrapAsDirectShortBuffer(final ByteBuffer x) {
1221 x.order(ByteOrder.nativeOrder());
1222 return new ScilabJavaObject(x.asShortBuffer(), ShortBuffer.class).id;
1226 * @param x the variable to wrap into a Java Object
1227 * @return the corresponding id
1229 public static final int wrap(final double x) {
1230 return new ScilabJavaObject(x, double.class).id;
1234 * @param x the variable to wrap into a Java Object
1235 * @return the corresponding id
1237 public static final int wrap(final double[] x) {
1238 return new ScilabJavaObject(x, double[].class).id;
1242 * @param x the variable to wrap into a Java Object
1243 * @return the corresponding id
1245 public static final int wrap(final double[][] x) {
1246 return new ScilabJavaObject(x, double[][].class).id;
1250 * @param x the variable to wrap into a Java Object
1251 * @return the corresponding id
1253 public static final int wrap(final int x) {
1254 return new ScilabJavaObject(x, int.class).id;
1258 * @param x the variable to wrap into a Java Object
1259 * @return the corresponding id
1261 public static final int wrap(final int[] x) {
1262 return new ScilabJavaObject(x, int[].class).id;
1266 * @param x the variable to wrap into a Java Object
1267 * @return the corresponding id
1269 public static final int wrap(final int[][] x) {
1270 return new ScilabJavaObject(x, int[][].class).id;
1274 * @param x the variable to wrap into a Java Object
1275 * @return the corresponding id
1277 public static final int wrap(final long x) {
1278 return new ScilabJavaObject(x, long.class).id;
1282 * @param x the variable to wrap into a Java Object
1283 * @return the corresponding id
1285 public static final int wrap(final long[] x) {
1286 return new ScilabJavaObject(x, long[].class).id;
1290 * @param x the variable to wrap into a Java Object
1291 * @return the corresponding id
1293 public static final int wrap(final long[][] x) {
1294 return new ScilabJavaObject(x, long[][].class).id;
1298 * @param x the variable to wrap into a Java Object
1299 * @return the corresponding id
1301 public static final int wrap(final byte x) {
1302 return new ScilabJavaObject(x, byte.class).id;
1306 * @param x the variable to wrap into a Java Object
1307 * @return the corresponding id
1309 public static final int wrap(final byte[] x) {
1310 return new ScilabJavaObject(x, byte[].class).id;
1314 * @param x the variable to wrap into a Java Object
1315 * @return the corresponding id
1317 public static final int wrap(final byte[][] x) {
1318 return new ScilabJavaObject(x, byte[][].class).id;
1322 * @param x the variable to wrap into a Java Object
1323 * @return the corresponding id
1325 public static final int wrap(final short x) {
1326 return new ScilabJavaObject(x, short.class).id;
1330 * @param x the variable to wrap into a Java Object
1331 * @return the corresponding id
1333 public static final int wrap(final short[] x) {
1334 return new ScilabJavaObject(x, short[].class).id;
1338 * @param x the variable to wrap into a Java Object
1339 * @return the corresponding id
1341 public static final int wrap(final short[][] x) {
1342 return new ScilabJavaObject(x, short[][].class).id;
1346 * @param x the variable to wrap into a Java Object
1347 * @return the corresponding id
1349 public static final int wrap(final String x) {
1350 return new ScilabJavaObject(x, String.class).id;
1354 * @param x the variable to wrap into a Java Object
1355 * @return the corresponding id
1357 public static final int wrap(final String[] x) {
1358 return new ScilabJavaObject(x, String[].class).id;
1362 * @param x the variable to wrap into a Java Object
1363 * @return the corresponding id
1365 public static final int wrap(final String[][] x) {
1366 return new ScilabJavaObject(x, String[][].class).id;
1370 * @param x the variable to wrap into a Java Object
1371 * @return the corresponding id
1373 public static final int wrap(final boolean x) {
1374 return new ScilabJavaObject(x, boolean.class).id;
1378 * @param x the variable to wrap into a Java Object
1379 * @return the corresponding id
1381 public static final int wrap(final boolean[] x) {
1382 return new ScilabJavaObject(x, boolean[].class).id;
1386 * @param x the variable to wrap into a Java Object
1387 * @return the corresponding id
1389 public static final int wrap(final boolean[][] x) {
1390 return new ScilabJavaObject(x, boolean[][].class).id;
1394 * @param x the variable to wrap into a Java Object
1395 * @return the corresponding id
1397 public static final int wrap(final char x) {
1398 return new ScilabJavaObject(x, char.class).id;
1402 * @param x the variable to wrap into a Java Object
1403 * @return the corresponding id
1405 public static final int wrap(final char[] x) {
1406 return new ScilabJavaObject(x, char[].class).id;
1410 * @param x the variable to wrap into a Java Object
1411 * @return the corresponding id
1413 public static final int wrap(final char[][] x) {
1414 return new ScilabJavaObject(x, char[][].class).id;
1418 * @param x the variable to wrap into a Java Object
1419 * @return the corresponding id
1421 public static final int wrap(final float x) {
1422 return new ScilabJavaObject(x, float.class).id;
1426 * @param x the variable to wrap into a Java Object
1427 * @return the corresponding id
1429 public static final int wrap(final float[] x) {
1430 return new ScilabJavaObject(x, float[].class).id;
1434 * @param x the variable to wrap into a Java Object
1435 * @return the corresponding id
1437 public static final int wrap(final float[][] x) {
1438 return new ScilabJavaObject(x, float[][].class).id;
1442 * Wrap the ids into a Java collection
1444 * The implementation is a raw {@link java.util.ArrayList}.
1446 * @param ids the java object to put into
1447 * @return a collection id
1449 public static final int wrapList(final int[] ids) {
1450 final ArrayList<Object> list = new ArrayList<Object>(ids.length);
1451 for (int i = 0; i < ids.length; i++) {
1452 list.add(arraySJO[ids[i]]);
1455 return new ScilabJavaObject(list, ArrayList.class).id;
1459 * Wrap the ids into a Java collection
1461 * The implementation is a raw {@link java.util.ArrayList}.
1463 * @param ids the java object to put into
1464 * @return a collection id
1466 public static final int wrapPoly(final double[] coefs) {
1467 final Poly p = new Poly(coefs);
1468 return new ScilabJavaObject(p, Poly.class).id;
1472 * @param id the Java Object id
1473 * @return the resulting unwrapping
1475 public static final double unwrapDouble(final int id) {
1476 return ((Double) (arraySJO[id].object)).doubleValue();
1480 * @param id the Java Object id
1481 * @return the resulting unwrapping
1483 public static final Object unwrapRowDouble(final int id) {
1484 if (arraySJO[id].object instanceof List) {
1485 return ScilabJavaArray.toDoubleArray((List<Double>) arraySJO[id].object);
1486 } else if (arraySJO[id].object instanceof Double[]) {
1487 return ScilabJavaArray.toPrimitive((Double[]) arraySJO[id].object);
1488 } else if (arraySJO[id].object instanceof DoubleBuffer && !((DoubleBuffer) arraySJO[id].object).isDirect()) {
1489 return ((DoubleBuffer) arraySJO[id].object).array();
1492 return arraySJO[id].object;
1496 * @param id the Java Object id
1497 * @return the resulting unwrapping
1499 public static final double[][] unwrapMatDouble(final int id) {
1500 if (arraySJO[id].object instanceof Double[][]) {
1501 Object o = ScilabJavaArray.toPrimitive(arraySJO[id].object);
1502 return (double[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1505 return (double[][]) arraySJO[id].object;
1509 * @param id the Java Object id
1510 * @return the resulting unwrapping
1512 public static final int unwrapInt(final int id) {
1513 return ((Integer) (arraySJO[id].object)).intValue();
1517 * @param id the Java Object id
1518 * @return the resulting unwrapping
1520 public static final Object unwrapRowInt(final int id) {
1521 if (arraySJO[id].object instanceof List) {
1522 return ScilabJavaArray.toIntArray((List<Integer>) arraySJO[id].object);
1523 } else if (arraySJO[id].object instanceof Integer[]) {
1524 return ScilabJavaArray.toPrimitive((Integer[]) arraySJO[id].object);
1525 } else if (arraySJO[id].object instanceof IntBuffer && !((IntBuffer) arraySJO[id].object).isDirect()) {
1526 return ((IntBuffer) arraySJO[id].object).array();
1529 return arraySJO[id].object;
1533 * @param id the Java Object id
1534 * @return the resulting unwrapping
1536 public static final int[][] unwrapMatInt(final int id) {
1537 if (arraySJO[id].object instanceof Integer[][]) {
1538 return (int[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1541 return (int[][]) (arraySJO[id].object);
1545 * @param id the Java Object id
1546 * @return the resulting unwrapping
1548 public static final short unwrapShort(final int id) {
1549 return ((Short) (arraySJO[id].object)).shortValue();
1553 * @param id the Java Object id
1554 * @return the resulting unwrapping
1556 public static final Object unwrapRowShort(final int id) {
1557 if (arraySJO[id].object instanceof List) {
1558 return ScilabJavaArray.toShortArray((List<Short>) arraySJO[id].object);
1559 } else if (arraySJO[id].object instanceof Short[]) {
1560 return ScilabJavaArray.toPrimitive((Short[]) arraySJO[id].object);
1561 } else if (arraySJO[id].object instanceof ShortBuffer && !((ShortBuffer) arraySJO[id].object).isDirect()) {
1562 return ((ShortBuffer) arraySJO[id].object).array();
1565 return arraySJO[id].object;
1569 * @param id the Java Object id
1570 * @return the resulting unwrapping
1572 public static final short[][] unwrapMatShort(final int id) {
1573 if (arraySJO[id].object instanceof Short[][]) {
1574 return (short[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1577 return (short[][]) (arraySJO[id].object);
1581 * @param id the Java Object id
1582 * @return the resulting unwrapping
1584 public static final byte unwrapByte(final int id) {
1585 return ((Byte) (arraySJO[id].object)).byteValue();
1589 * @param id the Java Object id
1590 * @return the resulting unwrapping
1592 public static final Object unwrapRowByte(final int id) {
1593 if (arraySJO[id].object instanceof List) {
1594 return ScilabJavaArray.toByteArray((List<Byte>) arraySJO[id].object);
1595 } else if (arraySJO[id].object instanceof Byte[]) {
1596 return ScilabJavaArray.toPrimitive((Byte[]) arraySJO[id].object);
1597 } else if (arraySJO[id].object instanceof ByteBuffer && !((ByteBuffer) arraySJO[id].object).isDirect()) {
1598 return ((ByteBuffer) arraySJO[id].object).array();
1601 return arraySJO[id].object;
1605 * @param id the Java Object id
1606 * @return the resulting unwrapping
1608 public static final byte[][] unwrapMatByte(final int id) {
1609 if (arraySJO[id].object instanceof Byte[][]) {
1610 return (byte[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1613 return (byte[][]) (arraySJO[id].object);
1617 * @param id the Java Object id
1618 * @return the resulting unwrapping
1620 public static final String unwrapString(final int id) {
1621 return (String) (arraySJO[id].object);
1625 * @param id the Java Object id
1626 * @return the resulting unwrapping
1628 public static final String[] unwrapRowString(final int id) {
1629 if (arraySJO[id].object instanceof List) {
1630 return ScilabJavaArray.toStringArray((List<String>) arraySJO[id].object);
1633 return (String[]) (arraySJO[id].object);
1637 * @param id the Java Object id
1638 * @return the resulting unwrapping
1640 public static final String[][] unwrapMatString(final int id) {
1641 return (String[][]) (arraySJO[id].object);
1645 * @param id the Java Object id
1646 * @return the resulting unwrapping
1648 public static final boolean unwrapBoolean(final int id) {
1649 return (Boolean) (arraySJO[id].object);
1653 * @param id the Java Object id
1654 * @return the resulting unwrapping
1656 public static final boolean[] unwrapRowBoolean(final int id) {
1657 if (arraySJO[id].object instanceof List) {
1658 return ScilabJavaArray.toBooleanArray((List<Boolean>) arraySJO[id].object);
1659 } else if (arraySJO[id].object instanceof Boolean[]) {
1660 return ScilabJavaArray.toPrimitive((Boolean[]) arraySJO[id].object);
1663 return (boolean[]) (arraySJO[id].object);
1667 * @param id the Java Object id
1668 * @return the resulting unwrapping
1670 public static final boolean[][] unwrapMatBoolean(final int id) {
1671 if (arraySJO[id].object instanceof Boolean[][]) {
1672 return (boolean[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1675 return (boolean[][]) (arraySJO[id].object);
1679 * @param id the Java Object id
1680 * @return the resulting unwrapping
1682 public static final char unwrapChar(final int id) {
1683 return (Character) (arraySJO[id].object);
1687 * @param id the Java Object id
1688 * @return the resulting unwrapping
1690 public static final Object unwrapRowChar(final int id) {
1691 if (arraySJO[id].object instanceof List) {
1692 return ScilabJavaArray.toCharArray((List<Character>) arraySJO[id].object);
1693 } else if (arraySJO[id].object instanceof Character[]) {
1694 return ScilabJavaArray.toPrimitive((Character[]) arraySJO[id].object);
1695 } else if (arraySJO[id].object instanceof CharBuffer && !((CharBuffer) arraySJO[id].object).isDirect()) {
1696 return ((CharBuffer) arraySJO[id].object).array();
1699 return arraySJO[id].object;
1703 * @param id the Java Object id
1704 * @return the resulting unwrapping
1706 public static final char[][] unwrapMatChar(final int id) {
1707 if (arraySJO[id].object instanceof Character[][]) {
1708 return (char[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1711 return (char[][]) (arraySJO[id].object);
1715 * @param id the Java Object id
1716 * @return the resulting unwrapping
1718 public static final float unwrapFloat(final int id) {
1719 return (Float) (arraySJO[id].object);
1723 * @param id the Java Object id
1724 * @return the resulting unwrapping
1726 public static final Object unwrapRowFloat(final int id) {
1727 if (arraySJO[id].object instanceof List) {
1728 return ScilabJavaArray.toFloatArray((List<Float>) arraySJO[id].object);
1729 } else if (arraySJO[id].object instanceof Double[]) {
1730 return ScilabJavaArray.toPrimitive((Float[]) arraySJO[id].object);
1731 } else if (arraySJO[id].object instanceof FloatBuffer && !((FloatBuffer) arraySJO[id].object).isDirect()) {
1732 return ((FloatBuffer) arraySJO[id].object).array();
1735 return arraySJO[id].object;
1739 * @param id the Java Object id
1740 * @return the resulting unwrapping
1742 public static final float[][] unwrapMatFloat(final int id) {
1743 if (arraySJO[id].object instanceof Float[][]) {
1744 return (float[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1747 return (float[][]) (arraySJO[id].object);
1751 * @param id the Java Object id
1752 * @return the resulting unwrapping
1754 public static final long unwrapLong(final int id) {
1755 return (Long) (arraySJO[id].object);
1759 * @param id the Java Object id
1760 * @return the resulting unwrapping
1762 public static final Object unwrapRowLong(final int id) {
1763 if (arraySJO[id].object instanceof List) {
1764 return ScilabJavaArray.toLongArray((List<Long>) arraySJO[id].object);
1765 } else if (arraySJO[id].object instanceof Long[]) {
1766 return ScilabJavaArray.toPrimitive((Long[]) arraySJO[id].object);
1767 } else if (arraySJO[id].object instanceof LongBuffer && !((LongBuffer) arraySJO[id].object).isDirect()) {
1768 return ((LongBuffer) arraySJO[id].object).array();
1771 return arraySJO[id].object;
1775 * @param id the Java Object id
1776 * @return the resulting unwrapping
1778 public static final long[][] unwrapMatLong(final int id) {
1779 if (arraySJO[id].object instanceof Long[][]) {
1780 return (long[][]) ScilabJavaArray.toPrimitive(arraySJO[id].object);
1783 return (long[][]) (arraySJO[id].object);
1787 * @param id the Java Object id
1788 * @return the identifier of the unwrappable type or -1 if nothing
1790 public static final int isUnwrappable(final int id) {
1792 System.out.println("unwrappable=0");
1797 Integer t = unwrappableType.get(arraySJO[id].clazz);
1800 } else if (arraySJO[id].object instanceof List) {
1801 List l = (List) arraySJO[id].object;
1802 if (l.size() == 0 || l.get(0) == null) {
1805 Class cl = l.get(0).getClass();
1806 Integer i = listBaseType.get(cl);
1808 for (Object o : l) {
1809 if (o.getClass() != cl) {
1821 } else if (arraySJO[id].object instanceof Buffer) {
1822 if (arraySJO[id].object instanceof DoubleBuffer) {
1823 unwrappableType.put(arraySJO[id].clazz, 3);
1825 } else if (arraySJO[id].object instanceof ByteBuffer) {
1826 unwrappableType.put(arraySJO[id].clazz, 12);
1828 } else if (arraySJO[id].object instanceof IntBuffer) {
1829 unwrappableType.put(arraySJO[id].clazz, 24);
1831 } else if (arraySJO[id].object instanceof CharBuffer) {
1832 unwrappableType.put(arraySJO[id].clazz, 21);
1834 } else if (arraySJO[id].object instanceof FloatBuffer) {
1835 unwrappableType.put(arraySJO[id].clazz, 36);
1837 } else if (arraySJO[id].object instanceof LongBuffer) {
1838 unwrappableType.put(arraySJO[id].clazz, 30);
1840 } else if (arraySJO[id].object instanceof ShortBuffer) {
1841 unwrappableType.put(arraySJO[id].clazz, 18);
1852 * Inner class to manage the free places
1854 private static final class FreePlace {
1860 this.fp = new int[INITIALCAPACITY];
1864 final void addFreePlace(final int n) {
1865 if (currentPos == fp.length - 1) {
1866 int[] newFp = new int[(int) (1.5 * fp.length)];
1867 System.arraycopy(fp, 0, newFp, 0, fp.length);
1870 fp[++currentPos] = n;
1873 final int getFreePlace() {
1874 if (currentPos == -1) {
1877 return fp[currentPos--];
1880 final int[] getCurrentFreePlace() {
1881 if (currentPos == -1) {
1885 int[] ret = new int[currentPos + 1];
1886 System.arraycopy(fp, 0, ret, 0, ret.length);