comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/LazyClassLoadingTest.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/LazyClassLoadingTest.java@b1530a6cce8c
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
1 /*
2 * Copyright (c) 2014, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.api.dsl.test;
24
25 import java.lang.reflect.*;
26
27 import org.junit.*;
28
29 import com.oracle.truffle.api.dsl.*;
30 import com.oracle.truffle.api.dsl.test.LazyClassLoadingTestFactory.TestNodeFactory;
31 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode;
32 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
33
34 public class LazyClassLoadingTest {
35 @Test
36 public void test() {
37 String factoryName = TestNodeFactory.class.getName();
38 String nodeName = factoryName + "$" + TestNode.class.getSimpleName() + "Gen";
39 Assert.assertTrue(isLoaded(factoryName));
40 Assert.assertFalse(isLoaded(nodeName));
41
42 NodeFactory<TestNode> factory = TestNodeFactory.getInstance();
43 Assert.assertTrue(isLoaded(factoryName));
44 Assert.assertTrue(isLoaded(nodeName));
45
46 Assert.assertFalse(isLoaded(nodeName + "$UninitializedNode_"));
47 Assert.assertFalse(isLoaded(nodeName + "$BaseNode_"));
48 Assert.assertFalse(isLoaded(nodeName + "$IntNode_"));
49 Assert.assertFalse(isLoaded(nodeName + "$BooleanNode_"));
50 Assert.assertFalse(isLoaded(nodeName + "$PolymorphicNode_"));
51
52 TestRootNode<TestNode> root = TestHelper.createRoot(factory);
53
54 Assert.assertTrue(isLoaded(nodeName + "$UninitializedNode_"));
55 Assert.assertTrue(isLoaded(nodeName + "$BaseNode_"));
56 Assert.assertFalse(isLoaded(nodeName + "$IntNode_"));
57 Assert.assertFalse(isLoaded(nodeName + "$BooleanNode_"));
58 Assert.assertFalse(isLoaded(nodeName + "$PolymorphicNode_"));
59
60 Assert.assertEquals(42, TestHelper.executeWith(root, 42));
61
62 Assert.assertTrue(isLoaded(nodeName + "$IntNode_"));
63 Assert.assertFalse(isLoaded(nodeName + "$BooleanNode_"));
64 Assert.assertFalse(isLoaded(nodeName + "$PolymorphicNode_"));
65
66 Assert.assertEquals(true, TestHelper.executeWith(root, true));
67
68 Assert.assertTrue(isLoaded(nodeName + "$IntNode_"));
69 Assert.assertTrue(isLoaded(nodeName + "$BooleanNode_"));
70 Assert.assertTrue(isLoaded(nodeName + "$PolymorphicNode_"));
71 }
72
73 private boolean isLoaded(String className) {
74 ClassLoader classLoader = getClass().getClassLoader();
75 Method m;
76 try {
77 m = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
78 m.setAccessible(true);
79 return m.invoke(classLoader, className) != null;
80 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
81 throw new RuntimeException(e);
82 }
83 }
84
85 @NodeChild("a")
86 abstract static class TestNode extends ValueNode {
87
88 @Specialization
89 int doInt(int a) {
90 return a;
91 }
92
93 @Specialization
94 boolean doBoolean(boolean a) {
95 return a;
96 }
97
98 }
99 }