001/*
002 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.runtime.test;
024
025import static org.junit.Assert.*;
026import jdk.internal.jvmci.meta.*;
027import jdk.internal.jvmci.runtime.*;
028
029import org.junit.*;
030
031public class ResolvedJavaTypeResolveConcreteMethodTest {
032    public final MetaAccessProvider metaAccess;
033
034    public ResolvedJavaTypeResolveConcreteMethodTest() {
035        metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess();
036    }
037
038    protected abstract static class A {
039        @SuppressWarnings("unused")
040        private void priv() {
041        }
042
043        public void v1() {
044        }
045
046        public void v2() {
047        }
048
049        public abstract void abs();
050    }
051
052    protected static class B extends A implements I {
053        public void i() {
054        }
055
056        @Override
057        public void v2() {
058        }
059
060        @Override
061        public void abs() {
062
063        }
064    }
065
066    protected static class C extends B {
067        public void d() {
068        }
069    }
070
071    protected abstract static class D extends A {
072
073    }
074
075    protected static class E extends D {
076        @Override
077        public void abs() {
078        }
079    }
080
081    protected interface I {
082        void i();
083
084        default void d() {
085        }
086    }
087
088    @Test
089    public void testDefaultMethod() {
090        ResolvedJavaType i = getType(I.class);
091        ResolvedJavaType b = getType(B.class);
092        ResolvedJavaType c = getType(C.class);
093        ResolvedJavaMethod di = getMethod(i, "d");
094        ResolvedJavaMethod dc = getMethod(c, "d");
095
096        assertEquals(di, i.resolveConcreteMethod(di, c));
097        assertEquals(di, b.resolveConcreteMethod(di, c));
098        assertEquals(dc, c.resolveConcreteMethod(di, c));
099    }
100
101    @Test
102    public void testPrivateMethod() {
103        ResolvedJavaType a = getType(A.class);
104        ResolvedJavaType b = getType(B.class);
105        ResolvedJavaType c = getType(C.class);
106        ResolvedJavaMethod priv = getMethod(a, "priv");
107
108        assertNull(a.resolveConcreteMethod(priv, c));
109        assertNull(b.resolveConcreteMethod(priv, c));
110    }
111
112    @Test
113    public void testAbstractMethod() {
114        ResolvedJavaType a = getType(A.class);
115        ResolvedJavaType b = getType(B.class);
116        ResolvedJavaType c = getType(C.class);
117        ResolvedJavaType d = getType(D.class);
118        ResolvedJavaType e = getType(E.class);
119        ResolvedJavaMethod absa = getMethod(a, "abs");
120        ResolvedJavaMethod absb = getMethod(b, "abs");
121        ResolvedJavaMethod abse = getMethod(e, "abs");
122
123        assertNull(a.resolveConcreteMethod(absa, c));
124        assertNull(d.resolveConcreteMethod(absa, c));
125
126        assertEquals(absb, b.resolveConcreteMethod(absa, c));
127        assertEquals(absb, b.resolveConcreteMethod(absb, c));
128        assertEquals(absb, c.resolveConcreteMethod(absa, c));
129        assertEquals(absb, c.resolveConcreteMethod(absb, c));
130        assertEquals(abse, e.resolveConcreteMethod(absa, c));
131        assertNull(e.resolveConcreteMethod(absb, c));
132        assertEquals(abse, e.resolveConcreteMethod(abse, c));
133    }
134
135    @Test
136    public void testVirtualMethod() {
137        ResolvedJavaType a = getType(A.class);
138        ResolvedJavaType b = getType(B.class);
139        ResolvedJavaType c = getType(C.class);
140        ResolvedJavaMethod v1a = getMethod(a, "v1");
141        ResolvedJavaMethod v2a = getMethod(a, "v2");
142        ResolvedJavaMethod v2b = getMethod(b, "v2");
143
144        assertEquals(v1a, a.resolveConcreteMethod(v1a, c));
145        assertEquals(v1a, b.resolveConcreteMethod(v1a, c));
146        assertEquals(v1a, c.resolveConcreteMethod(v1a, c));
147        assertEquals(v2a, a.resolveConcreteMethod(v2a, c));
148        assertEquals(v2b, b.resolveConcreteMethod(v2a, c));
149        assertEquals(v2b, b.resolveConcreteMethod(v2b, c));
150        assertEquals(v2b, c.resolveConcreteMethod(v2a, c));
151        assertEquals(v2b, c.resolveConcreteMethod(v2b, c));
152
153    }
154
155    static ResolvedJavaMethod getMethod(ResolvedJavaType type, String methodName) {
156        for (ResolvedJavaMethod method : type.getDeclaredMethods()) {
157            if (method.getName().equals(methodName)) {
158                return method;
159            }
160        }
161        throw new IllegalArgumentException();
162    }
163
164    protected ResolvedJavaType getType(Class<?> clazz) {
165        ResolvedJavaType type = metaAccess.lookupJavaType(clazz);
166        type.initialize();
167        return type;
168    }
169}