ResolvedJavaTypeResolveConcreteMethodTest.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2014, 2016, 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
24/**
25 * @test
26 * @requires vm.jvmci
27 * @modules jdk.internal.vm.ci/jdk.vm.ci.meta
28 *          jdk.internal.vm.ci/jdk.vm.ci.runtime
29 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.vm.ci.runtime.test.ResolvedJavaTypeResolveConcreteMethodTest
30 */
31
32package jdk.vm.ci.runtime.test;
33
34import jdk.vm.ci.meta.MetaAccessProvider;
35import jdk.vm.ci.meta.ResolvedJavaMethod;
36import jdk.vm.ci.meta.ResolvedJavaType;
37import jdk.vm.ci.runtime.JVMCI;
38import org.junit.Test;
39
40import static org.junit.Assert.assertEquals;
41import static org.junit.Assert.assertNull;
42
43public class ResolvedJavaTypeResolveConcreteMethodTest {
44    public final MetaAccessProvider metaAccess;
45
46    public ResolvedJavaTypeResolveConcreteMethodTest() {
47        metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess();
48    }
49
50    protected abstract static class A {
51        @SuppressWarnings("unused")
52        private void priv() {
53        }
54
55        public void v1() {
56        }
57
58        public void v2() {
59        }
60
61        public abstract void abs();
62    }
63
64    protected static class B extends A implements I {
65        public void i() {
66        }
67
68        @Override
69        public void v2() {
70        }
71
72        @Override
73        public void abs() {
74
75        }
76    }
77
78    protected static class C extends B {
79        public void d() {
80        }
81    }
82
83    protected abstract static class D extends A {
84
85    }
86
87    protected static class E extends D {
88        @Override
89        public void abs() {
90        }
91    }
92
93    protected interface I {
94        void i();
95
96        default void d() {
97        }
98    }
99
100    @Test
101    public void testDefaultMethod() {
102        ResolvedJavaType i = getType(I.class);
103        ResolvedJavaType b = getType(B.class);
104        ResolvedJavaType c = getType(C.class);
105        ResolvedJavaMethod di = getMethod(i, "d");
106        ResolvedJavaMethod dc = getMethod(c, "d");
107
108        assertEquals(null, i.resolveConcreteMethod(di, c));
109        assertEquals(di, b.resolveConcreteMethod(di, c));
110        assertEquals(dc, c.resolveConcreteMethod(di, c));
111    }
112
113    @Test
114    public void testPrivateMethod() {
115        ResolvedJavaType a = getType(A.class);
116        ResolvedJavaType b = getType(B.class);
117        ResolvedJavaType c = getType(C.class);
118        ResolvedJavaMethod priv = getMethod(a, "priv");
119
120        assertNull(a.resolveConcreteMethod(priv, c));
121        assertNull(b.resolveConcreteMethod(priv, c));
122    }
123
124    @Test
125    public void testAbstractMethod() {
126        ResolvedJavaType a = getType(A.class);
127        ResolvedJavaType b = getType(B.class);
128        ResolvedJavaType c = getType(C.class);
129        ResolvedJavaType d = getType(D.class);
130        ResolvedJavaType e = getType(E.class);
131        ResolvedJavaMethod absa = getMethod(a, "abs");
132        ResolvedJavaMethod absb = getMethod(b, "abs");
133        ResolvedJavaMethod abse = getMethod(e, "abs");
134
135        assertNull(a.resolveConcreteMethod(absa, c));
136        assertNull(d.resolveConcreteMethod(absa, c));
137
138        assertEquals(absb, b.resolveConcreteMethod(absa, c));
139        assertEquals(absb, b.resolveConcreteMethod(absb, c));
140        assertEquals(absb, c.resolveConcreteMethod(absa, c));
141        assertEquals(absb, c.resolveConcreteMethod(absb, c));
142        assertEquals(abse, e.resolveConcreteMethod(absa, c));
143        assertNull(e.resolveConcreteMethod(absb, c));
144        assertEquals(abse, e.resolveConcreteMethod(abse, c));
145    }
146
147    @Test
148    public void testVirtualMethod() {
149        ResolvedJavaType a = getType(A.class);
150        ResolvedJavaType b = getType(B.class);
151        ResolvedJavaType c = getType(C.class);
152        ResolvedJavaMethod v1a = getMethod(a, "v1");
153        ResolvedJavaMethod v2a = getMethod(a, "v2");
154        ResolvedJavaMethod v2b = getMethod(b, "v2");
155
156        assertEquals(v1a, a.resolveConcreteMethod(v1a, c));
157        assertEquals(v1a, b.resolveConcreteMethod(v1a, c));
158        assertEquals(v1a, c.resolveConcreteMethod(v1a, c));
159        assertEquals(v2a, a.resolveConcreteMethod(v2a, c));
160        assertEquals(v2b, b.resolveConcreteMethod(v2a, c));
161        assertEquals(v2b, b.resolveConcreteMethod(v2b, c));
162        assertEquals(v2b, c.resolveConcreteMethod(v2a, c));
163        assertEquals(v2b, c.resolveConcreteMethod(v2b, c));
164
165    }
166
167    static ResolvedJavaMethod getMethod(ResolvedJavaType type, String methodName) {
168        for (ResolvedJavaMethod method : type.getDeclaredMethods()) {
169            if (method.getName().equals(methodName)) {
170                return method;
171            }
172        }
173        throw new IllegalArgumentException();
174    }
175
176    protected ResolvedJavaType getType(Class<?> clazz) {
177        ResolvedJavaType type = metaAccess.lookupJavaType(clazz);
178        type.initialize();
179        return type;
180    }
181}
182