annotate truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/AccessorTest.java @ 22066:78c3d3d8d86e

Clearly separating the TruffleLanguage definition from context used during its execution. TruffleLanguage now has to have public static field INSTANCE and override createContext method.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Thu, 06 Aug 2015 08:31:49 +0200
parents 09531c471176
children cf19259edf87
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 */
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
23 package com.oracle.truffle.api.test.vm;
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
24
22066
78c3d3d8d86e Clearly separating the TruffleLanguage definition from context used during its execution. TruffleLanguage now has to have public static field INSTANCE and override createContext method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22012
diff changeset
25 import com.oracle.truffle.api.TruffleLanguage;
22012
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
26 import com.oracle.truffle.api.impl.Accessor;
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
27 import com.oracle.truffle.api.test.vm.ImplicitExplicitExportTest.ExportImportLanguage1;
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 static com.oracle.truffle.api.test.vm.ImplicitExplicitExportTest.L1;
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
29 import com.oracle.truffle.api.vm.TruffleVM;
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
30 import java.lang.reflect.Field;
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 java.lang.reflect.InvocationTargetException;
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 java.lang.reflect.Method;
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
33 import static org.junit.Assert.*;
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
34 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
35 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
36
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
37 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
38 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
39
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
40 @BeforeClass
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
41 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
42 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
43 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
44 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
45 }
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
46
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
47 @Test
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
48 public void canGetAccessToOwnLanguageInstance() 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
49 TruffleVM vm = TruffleVM.newVM().build();
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
50 TruffleVM.Language language = vm.getLanguages().get(L1);
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
51 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
52
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
53 Object ret = vm.eval(L1, "return nothing");
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
54 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
55
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
56 Object afterInitialization = findLanguageByClass(vm);
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
57 assertNotNull("Language found", afterInitialization);
22066
78c3d3d8d86e Clearly separating the TruffleLanguage definition from context used during its execution. TruffleLanguage now has to have public static field INSTANCE and override createContext method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22012
diff changeset
58 assertTrue("Right instance: " + afterInitialization, afterInitialization instanceof ExportImportLanguage1);
22012
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
59 }
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
60
22066
78c3d3d8d86e Clearly separating the TruffleLanguage definition from context used during its execution. TruffleLanguage now has to have public static field INSTANCE and override createContext method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22012
diff changeset
61 Object findLanguageByClass(TruffleVM vm) throws IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
22012
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
62 Method find = Accessor.class.getDeclaredMethod("findLanguage", TruffleVM.class, Class.class);
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
63 find.setAccessible(true);
22066
78c3d3d8d86e Clearly separating the TruffleLanguage definition from context used during its execution. TruffleLanguage now has to have public static field INSTANCE and override createContext method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22012
diff changeset
64 TruffleLanguage.Env env = (TruffleLanguage.Env) find.invoke(API, vm, ExportImportLanguage1.class);
78c3d3d8d86e Clearly separating the TruffleLanguage definition from context used during its execution. TruffleLanguage now has to have public static field INSTANCE and override createContext method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22012
diff changeset
65 Field f = env.getClass().getDeclaredField("langCtx");
78c3d3d8d86e Clearly separating the TruffleLanguage definition from context used during its execution. TruffleLanguage now has to have public static field INSTANCE and override createContext method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22012
diff changeset
66 f.setAccessible(true);
78c3d3d8d86e Clearly separating the TruffleLanguage definition from context used during its execution. TruffleLanguage now has to have public static field INSTANCE and override createContext method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22012
diff changeset
67 Object langCtx = f.get(env);
78c3d3d8d86e Clearly separating the TruffleLanguage definition from context used during its execution. TruffleLanguage now has to have public static field INSTANCE and override createContext method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22012
diff changeset
68 f = langCtx.getClass().getDeclaredField("lang");
78c3d3d8d86e Clearly separating the TruffleLanguage definition from context used during its execution. TruffleLanguage now has to have public static field INSTANCE and override createContext method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22012
diff changeset
69 f.setAccessible(true);
78c3d3d8d86e Clearly separating the TruffleLanguage definition from context used during its execution. TruffleLanguage now has to have public static field INSTANCE and override createContext method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22012
diff changeset
70 return f.get(langCtx);
22012
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
71 }
09531c471176 Making sure the accessor can really find the TruffleLanguage instance by its class
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
72 }