test/src/jdk/nashorn/api/javaaccess/SharedObject.java

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

mercurial