Mercurial > hg > truffle
annotate truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/impl/AccessorTest.java @ 22429:5692953272eb
Replacing reflection in AccessorTest by package private access
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Thu, 03 Dec 2015 09:14:20 +0100 |
parents | truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/vm/AccessorTest.java@b39b603e2a1e |
children | 1f7fe9f4207a |
rev | line source |
---|---|
22012
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
1 /* |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
2 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
4 * |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
8 * |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
13 * accompanied this code). |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
14 * |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
18 * |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
21 * questions. |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
22 */ |
22429
5692953272eb
Replacing reflection in AccessorTest by package private access
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
22425
diff
changeset
|
23 package com.oracle.truffle.api.impl; |
22012
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
24 |
22425
b39b603e2a1e
Organize imports after rename.
Christian Humer <christian.humer@oracle.com>
parents:
22424
diff
changeset
|
25 import static org.junit.Assert.assertNotNull; |
b39b603e2a1e
Organize imports after rename.
Christian Humer <christian.humer@oracle.com>
parents:
22424
diff
changeset
|
26 import static org.junit.Assert.assertNull; |
22424
fec62e298245
Rename package truffle.api.test to truffle.api to enable package-protected API access for testing.
Christian Humer <christian.humer@oracle.com>
parents:
22335
diff
changeset
|
27 |
22012
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
28 import java.lang.reflect.Field; |
22121
d045a505c2b3
Asynchronous TruffleVM can be created by providing own Executor when configuring the TruffleVM.Builder
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
22104
diff
changeset
|
29 import java.util.concurrent.Executors; |
22424
fec62e298245
Rename package truffle.api.test to truffle.api to enable package-protected API access for testing.
Christian Humer <christian.humer@oracle.com>
parents:
22335
diff
changeset
|
30 |
22012
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
31 import org.junit.BeforeClass; |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
32 import org.junit.Test; |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
33 |
22425
b39b603e2a1e
Organize imports after rename.
Christian Humer <christian.humer@oracle.com>
parents:
22424
diff
changeset
|
34 import com.oracle.truffle.api.source.Source; |
b39b603e2a1e
Organize imports after rename.
Christian Humer <christian.humer@oracle.com>
parents:
22424
diff
changeset
|
35 import com.oracle.truffle.api.vm.ImplicitExplicitExportTest.ExportImportLanguage1; |
22429
5692953272eb
Replacing reflection in AccessorTest by package private access
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
22425
diff
changeset
|
36 import static com.oracle.truffle.api.vm.ImplicitExplicitExportTest.L1; |
5692953272eb
Replacing reflection in AccessorTest by package private access
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
22425
diff
changeset
|
37 import com.oracle.truffle.api.vm.PolyglotEngine; |
22425
b39b603e2a1e
Organize imports after rename.
Christian Humer <christian.humer@oracle.com>
parents:
22424
diff
changeset
|
38 |
22012
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
39 public class AccessorTest { |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
40 public static Accessor API; |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
41 |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
42 @BeforeClass |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
43 public static void initAccessors() throws Exception { |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
44 Field f = Accessor.class.getDeclaredField("API"); |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
45 f.setAccessible(true); |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
46 API = (Accessor) f.get(null); |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
47 } |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
48 |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
49 @Test |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
50 public void canGetAccessToOwnLanguageInstance() throws Exception { |
22335
906a5f6e07cc
Giving the PolyglotEngine builder factory method more sensible name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
22278
diff
changeset
|
51 PolyglotEngine vm = PolyglotEngine.newBuilder().executor(Executors.newSingleThreadExecutor()).build(); |
22170
796f0fef110e
Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
22168
diff
changeset
|
52 PolyglotEngine.Language language = vm.getLanguages().get(L1); |
22012
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
53 assertNotNull("L1 language is defined", language); |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
54 |
22278
85a6db6624ab
Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
22173
diff
changeset
|
55 Source s = Source.fromText("return nothing", "nothing"); |
85a6db6624ab
Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
22173
diff
changeset
|
56 Object ret = language.eval(s).get(); |
22012
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
57 assertNull("nothing is returned", ret); |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
58 |
22429
5692953272eb
Replacing reflection in AccessorTest by package private access
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
22425
diff
changeset
|
59 ExportImportLanguage1 afterInitialization = Accessor.findLanguageByClass(vm, ExportImportLanguage1.class); |
22012
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
60 assertNotNull("Language found", afterInitialization); |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
61 } |
09531c471176
Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff
changeset
|
62 } |