comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/LazyClassLoadingTest.java @ 15478:dd95dff835f9

TruffleDSL: add class loading test
author Andreas Woess <andreas.woess@jku.at>
date Fri, 02 May 2014 15:51:11 +0200
parents
children a665483c3881
comparison
equal deleted inserted replaced
15477:fd18fa50a8e0 15478:dd95dff835f9
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.ValueNode;
32
33 public class LazyClassLoadingTest {
34 @Test
35 public void test() {
36 String testClassName = getClass().getName();
37 String factoryClassName = testClassName + "Factory";
38 String nodeFactoryClassName = factoryClassName + "$TestNodeFactory";
39
40 Assert.assertFalse(isLoaded(factoryClassName + "$TestNode"));
41 Assert.assertFalse(isLoaded(nodeFactoryClassName));
42
43 NodeFactory<TestNode> factory = TestNodeFactory.getInstance();
44
45 Assert.assertTrue(isLoaded(nodeFactoryClassName));
46 Assert.assertFalse(isLoaded(nodeFactoryClassName + "$TestBaseNode"));
47
48 TestHelper.createRoot(factory);
49
50 Assert.assertTrue(isLoaded(nodeFactoryClassName + "$TestBaseNode"));
51 Assert.assertTrue(isLoaded(nodeFactoryClassName + "$TestUninitializedNode"));
52 Assert.assertFalse(isLoaded(nodeFactoryClassName + "$TestGenericNode"));
53 }
54
55 private boolean isLoaded(String className) {
56 ClassLoader classLoader = getClass().getClassLoader();
57 Method m;
58 try {
59 m = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
60 m.setAccessible(true);
61 return m.invoke(classLoader, className) != null;
62 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
63 throw new RuntimeException(e);
64 }
65 }
66
67 @SuppressWarnings("unused")
68 @NodeChildren({@NodeChild("left"), @NodeChild("right")})
69 abstract static class TestNode extends ValueNode {
70 @Specialization(order = 1)
71 int add(int left, int right) {
72 return 42;
73 }
74
75 @Specialization(order = 2)
76 int add(boolean left, boolean right) {
77 return 21;
78 }
79
80 @Specialization(order = 4)
81 String add(boolean left, int right) {
82 return "(boolean,int)";
83 }
84 }
85 }