SharedObject.java revision 1239:77609e069f9f
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.nashorn.api.javaaccess.test;
27
28import javax.script.Invocable;
29import javax.script.ScriptEngine;
30import javax.script.ScriptException;
31
32@SuppressWarnings("javadoc")
33public class SharedObject {
34
35    // Public fields
36    public String                 publicString                  = "PublicString";
37    public String[]               publicStringArray             = { "ArrayString[0]", "ArrayString[1]", "ArrayString[2]", "ArrayString[3]" };
38    public Person                 publicObject                  = new Person(256);
39    public Person[]               publicObjectArray             = { new Person(4), new Person(-422), new Person(14) };
40    public boolean                publicBoolean                 = true;
41    public boolean[]              publicBooleanArray            = { true, false, false, true };
42    public Boolean                publicBooleanBox              = true;
43    public long                   publicLong                    = 933333333333333333L;
44    public long[]                 publicLongArray               = { 99012333333333L, -124355555L, 89777777777L };
45    public Long                   publicLongBox                 = 9333333333L;
46    public int                    publicInt                     = 2076543123;
47    public int[]                  publicIntArray                = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
48    public Integer                publicIntBox                  = 20765123;
49    public byte                   publicByte                    = -128;
50    public byte[]                 publicByteArray               = { 1, 2, 4, 8, 16, 32, 64, 127, -128 };
51    public Byte                   publicByteBox                 = 127;
52    public short                  publicShort                   = 32000;
53    public short[]                publicShortArray              = { 3240, 8900, -16789, 1, 12 };
54    public Short                  publicShortBox                = Short.MIN_VALUE;
55    public float                  publicFloat                   = 0.7f;
56    public float[]                publicFloatArray              = { -32.01f, 89.3f, -1.3e8f, 3.1f };
57    public Float                  publicFloatBox                = 1.377e4f;
58    public double                 publicDouble                  = 1.34e20;
59    public double[]               publicDoubleArray             = { 0.75e80, 8e-43, 1.000077, 0.123e10 };
60    public Double                 publicDoubleBox               = 1.4e-19;
61    public char                   publicChar                    = 'A';
62    public char[]                 publicCharArray               = "Hello Nashorn".toCharArray();
63    public Character              publicCharBox                 = 'B';
64    // Public static fields
65    public static String          publicStaticString            = "PublicStaticString";
66    public static String[]        publicStaticStringArray       = { "StaticArrayString[0]", "StaticArrayString[1]", "StaticArrayString[2]", "StaticArrayString[3]" };
67    public static Person          publicStaticObject            = new Person(512);
68    public static Person[]        publicStaticObjectArray       = { new Person(40), new Person(-22), new Person(18) };
69    public static boolean         publicStaticBoolean           = true;
70    public static boolean[]       publicStaticBooleanArray      = { false, false, false, true };
71    public static Boolean         publicStaticBooleanBox        = true;
72    public static long            publicStaticLong              = 13333333333333333L;
73    public static long[]          publicStaticLongArray         = { 19012333333333L, -224355555L, 39777777777L };
74    public static Long            publicStaticLongBox           = 9333333334L;
75    public static int             publicStaticInt               = 207654323;
76    public static int[]           publicStaticIntArray          = { 5, 8, 13, 21, 34 };
77    public static Integer         publicStaticIntBox            = 2075123;
78    public static byte            publicStaticByte              = -12;
79    public static byte[]          publicStaticByteArray         = { 16, 32, 64, 127, -128 };
80    public static Byte            publicStaticByteBox           = 17;
81    public static short           publicStaticShort             = 320;
82    public static short[]         publicStaticShortArray        = { 1240, 900, -1789, 100, 12 };
83    public static Short           publicStaticShortBox          = -16777;
84    public static float           publicStaticFloat             = 7.7e8f;
85    public static float[]         publicStaticFloatArray        = { -131.01f, 189.3f, -31.3e8f, 3.7f };
86    public static Float           publicStaticFloatBox          = 1.37e4f;
87    public static double          publicStaticDouble            = 1.341e20;
88    public static double[]        publicStaticDoubleArray       = { 0.75e80, 0.123e10, 8e-43, 1.000077 };
89    public static Double          publicStaticDoubleBox         = 1.41e-12;
90    public static char            publicStaticChar              = 'C';
91    public static char[]          publicStaticCharArray         = "Nashorn".toCharArray();
92    public static Character       publicStaticCharBox           = 'D';
93    // Public final fields
94    public final String           publicFinalString             = "PublicFinalString";
95    public final String[]         publicFinalStringArray        = { "FinalArrayString[0]", "FinalArrayString[1]", "FinalArrayString[2]", "FinalArrayString[3]" };
96    public final Person           publicFinalObject             = new Person(1024);
97    public final Person[]         publicFinalObjectArray        = { new Person(-900), new Person(1000), new Person(180) };
98    public final boolean          publicFinalBoolean            = true;
99    public final boolean[]        publicFinalBooleanArray       = { false, false, true, false };
100    public final Boolean          publicFinalBooleanBox         = true;
101    public final long             publicFinalLong               = 13353333333333333L;
102    public final long[]           publicFinalLongArray          = { 1901733333333L, -2247355555L, 3977377777L };
103    public final Long             publicFinalLongBox            = 9377333334L;
104    public final int              publicFinalInt                = 20712023;
105    public final int[]            publicFinalIntArray           = { 50, 80, 130, 210, 340 };
106    public final Integer          publicFinalIntBox             = 207512301;
107    public final byte             publicFinalByte               = -7;
108    public final byte[]           publicFinalByteArray          = { 1, 3, 6, 17, -128 };
109    public final Byte             publicFinalByteBox            = 19;
110    public final short            publicFinalShort              = 31220;
111    public final short[]          publicFinalShortArray         = { 12240, 9200, -17289, 1200, 12 };
112    public final Short            publicFinalShortBox           = -26777;
113    public final float            publicFinalFloat              = 7.72e8f;
114    public final float[]          publicFinalFloatArray         = { -131.012f, 189.32f, -31.32e8f, 3.72f };
115    public final Float            publicFinalFloatBox           = 1.372e4f;
116    public final double           publicFinalDouble             = 1.3412e20;
117    public final double[]         publicFinalDoubleArray        = { 0.725e80, 0.12e10, 8e-3, 1.00077 };
118    public final Double           publicFinalDoubleBox          = 1.412e-12;
119    public final char             publicFinalChar               = 'E';
120    public final char[]           publicFinalCharArray          = "Nashorn hello".toCharArray();
121    public final Character        publicFinalCharBox            = 'F';
122    // Public static final fields
123    public static final String    publicStaticFinalString       = "PublicStaticFinalString";
124    public static final String[]  publicStaticFinalStringArray  = { "StaticFinalArrayString[0]", "StaticFinalArrayString[1]", "StaticFinalArrayString[2]", "StaticFinalArrayString[3]" };
125    public static final Person    publicStaticFinalObject       = new Person(2048);
126    public static final Person[]  publicStaticFinalObjectArray  = { new Person(-9), new Person(110), new Person(Integer.MAX_VALUE) };
127    public static final boolean   publicStaticFinalBoolean      = true;
128    public static final boolean[] publicStaticFinalBooleanArray = { false, true, false, false };
129    public static final Boolean   publicStaticFinalBooleanBox   = true;
130    public static final long      publicStaticFinalLong         = 8333333333333L;
131    public static final long[]    publicStaticFinalLongArray    = { 19017383333L, -2247358L, 39773787L };
132    public static final Long      publicStaticFinalLongBox      = 9377388334L;
133    public static final int       publicStaticFinalInt          = 207182023;
134    public static final int[]     publicStaticFinalIntArray     = { 1308, 210, 340 };
135    public static final Integer   publicStaticFinalIntBox       = 2078301;
136    public static final byte      publicStaticFinalByte         = -70;
137    public static final byte[]    publicStaticFinalByteArray    = { 17, -128, 81 };
138    public static final Byte      publicStaticFinalByteBox      = 91;
139    public static final short     publicStaticFinalShort        = 8888;
140    public static final short[]   publicStaticFinalShortArray   = { 8240, 9280, -1289, 120, 812 };
141    public static final Short     publicStaticFinalShortBox     = -26;
142    public static final float     publicStaticFinalFloat        = 0.72e8f;
143    public static final float[]   publicStaticFinalFloatArray   = { -8131.012f, 9.32f, -138.32e8f, 0.72f };
144    public static final Float     publicStaticFinalFloatBox     = 1.2e4f;
145    public static final double    publicStaticFinalDouble       = 1.8e12;
146    public static final double[]  publicStaticFinalDoubleArray  = { 8.725e80, 0.82e10, 18e-3, 1.08077 };
147    public static final Double    publicStaticFinalDoubleBox    = 1.5612e-13;
148    public static final char      publicStaticFinalChar         = 'K';
149    public static final char[]    publicStaticFinalCharArray    = "StaticString".toCharArray();
150    public static final Character publicStaticFinalCharBox      = 'L';
151
152    // Special vars
153    public volatile boolean       volatileBoolean               = true;
154    public transient boolean      transientBoolean              = true;
155
156    // For methods testing
157    public boolean                isAccessed                    = false;
158    public volatile boolean       isFinished                    = false;
159
160    private ScriptEngine engine;
161
162    public ScriptEngine getEngine() {
163        return engine;
164    }
165
166    public void setEngine(final ScriptEngine engine) {
167        this.engine = engine;
168    }
169
170    public void voidMethod() {
171        isAccessed = true;
172    }
173
174    public boolean booleanMethod(final boolean arg) {
175        return !arg;
176    }
177
178    public Boolean booleanBoxingMethod(final Boolean arg) {
179        return !arg.booleanValue();
180    }
181
182    public boolean[] booleanArrayMethod(final boolean arg[]) {
183        final boolean[] res = new boolean[arg.length];
184        for (int i = 0; i < arg.length; i++) {
185            res[i] = !arg[i];
186        }
187        return res;
188    }
189
190    public int intMethod(final int arg) {
191        return arg + arg;
192    }
193
194    public Integer intBoxingMethod(final Integer arg) {
195        return arg + arg;
196    }
197
198    public int[] intArrayMethod(final int arg[]) {
199        final int[] res = new int[arg.length];
200        for (int i = 0; i < arg.length; i++) {
201            res[i] = arg[i] * 2;
202        }
203        return res;
204    }
205
206    public long longMethod(final long arg) {
207        return arg + arg;
208    }
209
210    public Long longBoxingMethod(final Long arg) {
211        return arg + arg;
212    }
213
214    public long[] longArrayMethod(final long[] arg) {
215        final long[] res = new long[arg.length];
216        for (int i = 0; i < arg.length; i++) {
217            res[i] = arg[i] * 2;
218        }
219        return res;
220    }
221
222    public byte byteMethod(final byte arg) {
223        return (byte)(arg + arg);
224    }
225
226    public Byte byteBoxingMethod(final Byte arg) {
227        return (byte)(arg + arg);
228    }
229
230    public byte[] byteArrayMethod(final byte[] arg) {
231        final byte[] res = new byte[arg.length];
232        for (int i = 0; i < arg.length; i++) {
233            res[i] = (byte)(arg[i] * 2);
234        }
235        return res;
236    }
237
238    public char charMethod(final char arg) {
239        return Character.toUpperCase(arg);
240    }
241
242    public Character charBoxingMethod(final Character arg) {
243        return Character.toUpperCase(arg);
244    }
245
246    public char[] charArrayMethod(final char[] arg) {
247        final char[] res = new char[arg.length];
248        for (int i = 0; i < arg.length; i++) {
249            res[i] = Character.toUpperCase(arg[i]);
250        }
251        return res;
252    }
253
254    public short shortMethod(final short arg) {
255        return (short)(arg + arg);
256    }
257
258    public Short shortBoxingMethod(final Short arg) {
259        return (short)(arg + arg);
260    }
261
262    public short[] shortArrayMethod(final short[] arg) {
263        final short[] res = new short[arg.length];
264        for (int i = 0; i < arg.length; i++) {
265            res[i] = (short)(arg[i] * 2);
266        }
267        return res;
268    }
269
270    public float floatMethod(final float arg) {
271        return arg + arg;
272    }
273
274    public Float floatBoxingMethod(final Float arg) {
275        return arg + arg;
276    }
277
278    public float[] floatArrayMethod(final float[] arg) {
279        final float[] res = new float[arg.length];
280        for (int i = 0; i < arg.length; i++) {
281            res[i] = arg[i] * 2;
282        }
283        return res;
284    }
285
286    public double doubleMethod(final double arg) {
287        return arg + arg;
288    }
289
290    public Double doubleBoxingMethod(final Double arg) {
291        return arg + arg;
292    }
293
294    public double[] doubleArrayMethod(final double[] arg) {
295        final double[] res = new double[arg.length];
296        for (int i = 0; i < arg.length; i++) {
297            res[i] = arg[i] * 2;
298        }
299        return res;
300    }
301
302    public String stringMethod(final String str) {
303        return str + str;
304    }
305
306    public String[] stringArrayMethod(final String[] arr) {
307        final int l = arr.length;
308        final String[] res = new String[l];
309        for (int i = 0; i < l; i++) {
310            res[i] = arr[l - i - 1];
311        }
312        return res;
313    }
314
315    public Person[] objectArrayMethod(final Person[] arr) {
316        final Person[] res = new Person[arr.length];
317        for (int i = 0; i < arr.length; i++) {
318            res[i] = new Person(i + 100);
319        }
320        return res;
321    }
322
323    public Person objectMethod(final Person t) {
324        t.id *= 2;
325        return t;
326    }
327
328    public int twoParamMethod(final long l, final double d) {
329        return (int)(l + d);
330    }
331
332    public int threeParamMethod(final short s, final long l, final char c) {
333        return (int)(s + l + c);
334    }
335
336    public Person[] twoObjectParamMethod(final Person arg1, final Person arg2) {
337        return new Person[] { arg2, arg1 };
338    }
339
340    public Person[] threeObjectParamMethod(final Person arg1, final Person arg2, final Person arg3) {
341        return new Person[] { arg3, arg2, arg1 };
342    }
343
344    public Person[] eightObjectParamMethod(final Person arg1, final Person arg2, final Person arg3, final Person arg4, final Person arg5, final Person arg6, final Person arg7, final Person arg8) {
345        return new Person[] { arg8, arg7, arg6, arg5, arg4, arg3, arg2, arg1 };
346    }
347
348    public Person[] nineObjectParamMethod(final Person arg1, final Person arg2, final Person arg3, final Person arg4, final Person arg5, final Person arg6, final Person arg7, final Person arg8, final Person arg9) {
349        return new Person[] { arg9, arg8, arg7, arg6, arg5, arg4, arg3, arg2, arg1 };
350    }
351
352    public Person[] methodObjectEllipsis(final Person... args) {
353        final int l = args.length;
354        final Person[] res = new Person[l];
355        for (int i = 0; i < l; i++) {
356            res[i] = args[l - i - 1];
357        }
358        return res;
359    }
360
361    public Person[] methodPrimitiveEllipsis(final int... args) {
362        final int l = args.length;
363        final Person[] res = new Person[l];
364        for (int i = 0; i < l; i++) {
365            res[i] = new Person(args[i]);
366        }
367        return res;
368    }
369
370    public Object[] methodMixedEllipsis(final Object... args) {
371        return args;
372    }
373
374    public Object[] methodObjectWithEllipsis(final String arg, final int... args) {
375        final Object[] res = new Object[args.length + 1];
376        res[0] = arg;
377        for (int i = 0; i < args.length; i++) {
378            res[i + 1] = args[i];
379        }
380        return res;
381    }
382
383    public Object[] methodPrimitiveWithEllipsis(final int arg, final long... args) {
384        final Object[] res = new Object[args.length + 1];
385        res[0] = arg;
386        for (int i = 0; i < args.length; i++) {
387            res[i + 1] = args[i];
388        }
389        return res;
390    }
391
392    public Object[] methodMixedWithEllipsis(final String arg1, final int arg2, final Object... args) {
393        final Object[] res = new Object[args.length + 2];
394        res[0] = arg1;
395        res[1] = arg2;
396        System.arraycopy(args, 0, res, 2, args.length);
397        return res;
398    }
399
400    public void methodStartsThread() {
401        isFinished = false;
402
403        final Thread t = new Thread(new Runnable() {
404            @Override
405            public void run() {
406                try {
407                    Thread.sleep(1000);
408                    isFinished = true;
409                } catch (final InterruptedException e) {
410                    e.printStackTrace();
411                }
412            }
413        });
414
415        t.start();
416    }
417
418    public String overloadedMethodDoubleVSint(final int arg) {
419        return "int";
420    }
421
422    public String overloadedMethodDoubleVSint(final double arg) {
423        return "double";
424    }
425
426    public int overloadedMethod(final int arg) {
427        return arg*2;
428    }
429
430    public int overloadedMethod(final String arg) {
431        return arg.length();
432    }
433
434    public int overloadedMethod(final boolean arg) {
435        return (arg) ? 1 : 0;
436    }
437
438    public int overloadedMethod(final Person arg) {
439        return arg.id*2;
440    }
441
442    public int firstLevelMethodInt(final int arg) throws ScriptException, NoSuchMethodException {
443        return (int) ((Invocable)engine).invokeFunction("secondLevelMethodInt", arg);
444    }
445
446    public int thirdLevelMethodInt(final int arg) {
447        return arg*5;
448    }
449
450    public int firstLevelMethodInteger(final Integer arg) throws ScriptException, NoSuchMethodException {
451        return (int) ((Invocable)engine).invokeFunction("secondLevelMethodInteger", arg);
452    }
453
454    public int thirdLevelMethodInteger(final Integer arg) {
455        return arg*10;
456    }
457
458    public Person firstLevelMethodObject(final Person p) throws ScriptException, NoSuchMethodException {
459        return (Person) ((Invocable)engine).invokeFunction("secondLevelMethodObject", p);
460    }
461
462    public Person thirdLevelMethodObject(final Person p) {
463        p.id *= 10;
464        return p;
465    }
466
467}
468