Mercurial > hg > openjdk > lambda > jdk
changeset 9513:cd0ea5563523
7184826: (reflect) Add support for Project Lambda concepts in core reflection
Reviewed-by: darcy, jfranck
Contributed-by: Amy Lu <amy.lu@oracle.com>
author | jfranck |
---|---|
date | Tue, 06 Aug 2013 18:54:02 +0200 |
parents | ba634b53f53a |
children | 98643f3ddf40 |
files | test/java/lang/reflect/DefaultStaticTest/DefaultStaticInvokeTest.java test/java/lang/reflect/DefaultStaticTest/DefaultStaticTestData.java test/java/lang/reflect/DefaultStaticTest/helper/Declared.java test/java/lang/reflect/DefaultStaticTest/helper/Mod.java test/java/lang/reflect/Method/DefaultMethodModeling.java test/java/lang/reflect/Method/IsDefaultTest.java |
diffstat | 6 files changed, 876 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/lang/reflect/DefaultStaticTest/DefaultStaticInvokeTest.java Tue Aug 06 18:54:02 2013 +0200 @@ -0,0 +1,307 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary Test locating and invoking default/static method that defined + * in interfaces and/or in inheritance + * @bug 7184826 + * @build helper.Mod helper.Declared DefaultStaticTestData + * @run testng DefaultStaticInvokeTest + * @author Yong Lu + */ + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; +import org.testng.annotations.Test; + +import static helper.Mod.*; +import static helper.Declared.*; +import helper.Mod; + +public class DefaultStaticInvokeTest { + + @Test(dataProvider = "testCasesAll", + dataProviderClass = DefaultStaticTestData.class) + public void testGetMethods(String testTarget, Object param) + throws Exception { + // test the methods retrieved by getMethods() + testMethods(ALL_METHODS, testTarget, param); + } + + @Test(dataProvider = "testCasesAll", + dataProviderClass = DefaultStaticTestData.class) + public void testGetDeclaredMethods(String testTarget, Object param) + throws Exception { + // test the methods retrieved by getDeclaredMethods() + testMethods(DECLARED_ONLY, testTarget, param); + } + + @Test(dataProvider = "testCasesAll", + dataProviderClass = DefaultStaticTestData.class) + public void testMethodInvoke(String testTarget, Object param) + throws Exception { + Class<?> typeUnderTest = Class.forName(testTarget); + MethodDesc[] expectedMethods = typeUnderTest.getAnnotationsByType(MethodDesc.class); + + // test the method retrieved by Class.getMethod(String, Object[]) + for (MethodDesc toTest : expectedMethods) { + String name = toTest.name(); + Method m = getTestMethod(typeUnderTest, name, param); + testThisMethod(toTest, m, typeUnderTest, param); + } + } + + @Test(dataProvider = "testCasesAll", + dataProviderClass = DefaultStaticTestData.class) + public void testMethodHandleInvoke(String testTarget, Object param) + throws Throwable { + Class<?> typeUnderTest = Class.forName(testTarget); + MethodDesc[] expectedMethods = typeUnderTest.getAnnotationsByType(MethodDesc.class); + + for (MethodDesc toTest : expectedMethods) { + String mName = toTest.name(); + Mod mod = toTest.mod(); + if (mod != STATIC && typeUnderTest.isInterface()) { + return; + } + + String result = null; + String expectedReturn = toTest.retval(); + + MethodHandle methodHandle = getTestMH(typeUnderTest, mName, param); + if (mName.equals("staticMethod")) { + result = (param == null) + ? (String) methodHandle.invoke() + : (String) methodHandle.invoke(param); + } else { + result = (param == null) + ? (String) methodHandle.invoke(typeUnderTest.newInstance()) + : (String) methodHandle.invoke(typeUnderTest.newInstance(), param); + } + + assertEquals(result, expectedReturn); + } + + } + + @Test(dataProvider = "testClasses", + dataProviderClass = DefaultStaticTestData.class) + public void testIAE(String testTarget, Object param) + throws ClassNotFoundException { + + Class<?> typeUnderTest = Class.forName(testTarget); + MethodDesc[] expectedMethods = typeUnderTest.getAnnotationsByType(MethodDesc.class); + + for (MethodDesc toTest : expectedMethods) { + String mName = toTest.name(); + Mod mod = toTest.mod(); + if (mod != STATIC && typeUnderTest.isInterface()) { + return; + } + Exception caught = null; + try { + getTestMH(typeUnderTest, mName, param, true); + } catch (Exception e) { + caught = e; + } + assertTrue(caught != null); + assertEquals(caught.getClass(), IllegalAccessException.class); + } + } + private static final String[] OBJECT_METHOD_NAMES = { + "equals", + "hashCode", + "getClass", + "notify", + "notifyAll", + "toString", + "wait", + "wait", + "wait",}; + private static final String LAMBDA_METHOD_NAMES = "lambda$"; + private static final HashSet<String> OBJECT_NAMES = new HashSet<>(Arrays.asList(OBJECT_METHOD_NAMES)); + private static final boolean DECLARED_ONLY = true; + private static final boolean ALL_METHODS = false; + + private void testMethods(boolean declaredOnly, String testTarget, Object param) + throws Exception { + Class<?> typeUnderTest = Class.forName(testTarget); + Method[] methods = declaredOnly + ? typeUnderTest.getDeclaredMethods() + : typeUnderTest.getMethods(); + + MethodDesc[] baseExpectedMethods = typeUnderTest.getAnnotationsByType(MethodDesc.class); + MethodDesc[] expectedMethods; + + // If only declared filter out non-declared from expected result + if (declaredOnly) { + int nonDeclared = 0; + for (MethodDesc desc : baseExpectedMethods) { + if (desc.declared() == NO) { + nonDeclared++; + } + } + expectedMethods = new MethodDesc[baseExpectedMethods.length - nonDeclared]; + int i = 0; + for (MethodDesc desc : baseExpectedMethods) { + if (desc.declared() == YES) { + expectedMethods[i++] = desc; + } + } + } else { + expectedMethods = baseExpectedMethods; + } + + HashMap<String, Method> myMethods = new HashMap<>(methods.length); + for (Method m : methods) { + String mName = m.getName(); + // don't add Object methods and method created from lambda expression + if ((!OBJECT_NAMES.contains(mName)) && (!mName.contains(LAMBDA_METHOD_NAMES))) { + myMethods.put(mName, m); + } + } + assertEquals(expectedMethods.length, myMethods.size()); + + for (MethodDesc toTest : expectedMethods) { + + String name = toTest.name(); + Method candidate = myMethods.get(name); + + assertNotNull(candidate); + myMethods.remove(name); + + testThisMethod(toTest, candidate, typeUnderTest, param); + + } + + // Should be no methods left since we remove all we expect to see + assertTrue(myMethods.isEmpty()); + } + + private void testThisMethod(MethodDesc toTest, Method method, + Class<?> typeUnderTest, Object param) throws Exception { + // Test modifiers, and invoke + Mod mod = toTest.mod(); + String expectedReturn = toTest.retval(); + switch (mod) { + case STATIC: + //assert candidate is static + assertTrue(Modifier.isStatic(method.getModifiers())); + assertFalse(method.isDefault()); + + // Test invoke it + assertEquals(tryInvoke(method, null, param), expectedReturn); + break; + case DEFAULT: + // if typeUnderTest is a class then instantiate and invoke + if (!typeUnderTest.isInterface()) { + assertEquals(tryInvoke( + method, + typeUnderTest, + param), + expectedReturn); + } + + //assert candidate is default + assertFalse(Modifier.isStatic(method.getModifiers())); + assertTrue(method.isDefault()); + break; + case REGULAR: + // if typeUnderTest must be a class + assertEquals(tryInvoke( + method, + typeUnderTest, + param), + expectedReturn); + + //assert candidate is neither default nor static + assertFalse(Modifier.isStatic(method.getModifiers())); + assertFalse(method.isDefault()); + break; + case ABSTRACT: + //assert candidate is neither default nor static + assertFalse(Modifier.isStatic(method.getModifiers())); + assertFalse(method.isDefault()); + break; + default: + assertFalse(true); //this should never happen + break; + } + + } + + private Object tryInvoke(Method m, Class<?> receiverType, Object param) + throws Exception { + Object receiver = receiverType == null ? null : receiverType.newInstance(); + Object result = null; + if (param == null) { + result = m.invoke(receiver); + } else { + result = m.invoke(receiver, param); + } + return result; + } + + private Method getTestMethod(Class clazz, String methodName, Object param) + throws NoSuchMethodException { + Class[] paramsType = (param != null) + ? new Class[]{Object.class} + : new Class[]{}; + return clazz.getMethod(methodName, paramsType); + } + + private MethodHandle getTestMH(Class clazz, String methodName, Object param) + throws Exception { + return getTestMH(clazz, methodName, param, false); + } + + private MethodHandle getTestMH(Class clazz, String methodName, + Object param, boolean isNegativeTest) + throws Exception { + MethodType mType = (param != null) + ? MethodType.genericMethodType(1) + : MethodType.methodType(String.class); + MethodHandles.Lookup lookup = MethodHandles.lookup(); + if (!isNegativeTest) { + return methodName.equals("staticMethod") + ? lookup.findStatic(clazz, methodName, mType) + : lookup.findVirtual(clazz, methodName, mType); + } else { + return methodName.equals("staticMethod") + ? lookup.findVirtual(clazz, methodName, mType) + : lookup.findStatic(clazz, methodName, mType); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/lang/reflect/DefaultStaticTest/DefaultStaticTestData.java Tue Aug 06 18:54:02 2013 +0200 @@ -0,0 +1,401 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * Test Data used for testing default/static method + * + * @author Yong Lu + */ + +import java.util.Arrays; +import java.util.List; + +import org.testng.annotations.DataProvider; +import org.testng.collections.Lists; + +import static helper.Mod.*; +import static helper.Declared.*; +import helper.Mod; +import helper.Declared; +import java.lang.annotation.Repeatable; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@MethodDesc(name = "defaultMethod", retval = "TestIF1.defaultMethod", mod = DEFAULT, declared = YES) +interface TestIF1 { + + default String defaultMethod() { + return "TestIF1.defaultMethod"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF1.defaultMethod", mod = DEFAULT, declared = NO) +class TestClass1 implements TestIF1 { +} + +@MethodDesc(name = "staticMethod", retval = "TestIF2.staticMethod", mod = STATIC, declared = YES) +interface TestIF2 { + + static String staticMethod() { + return "TestIF2.staticMethod"; + } +} + +@MethodDesc(name = "method", retval = "TestIF2.staticMethod", mod = REGULAR, declared = YES) +class TestClass2 implements TestIF2 { + + public String method() { + return TestIF2.staticMethod(); + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF3.defaultMethod", mod = DEFAULT, declared = YES) +@MethodDesc(name = "method", retval = "", mod = ABSTRACT, declared = YES) +interface TestIF3 { + + String method(); + + default String defaultMethod() { + return "TestIF3.defaultMethod"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF3.defaultMethod", mod = DEFAULT, declared = NO) +@MethodDesc(name = "method", retval = "TestClass3.method", mod = REGULAR, declared = YES) +class TestClass3 implements TestIF3 { + + public String method() { + return "TestClass3.method"; + } +} + +@MethodDesc(name = "staticMethod", retval = "TestIF4.staticMethod", mod = STATIC, declared = YES) +@MethodDesc(name = "method", retval = "", mod = ABSTRACT, declared = YES) +interface TestIF4 { + + String method(); + + static String staticMethod() { + return "TestIF4.staticMethod"; + } +} + +@MethodDesc(name = "method", retval = "TestClass4.method", mod = REGULAR, declared = YES) +class TestClass4 implements TestIF4 { + + public String method() { + return "TestClass4.method"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF5.defaultMethod", mod = DEFAULT, declared = YES) +@MethodDesc(name = "staticMethod", retval = "TestIF5.staticMethod", mod = STATIC, declared = YES) +interface TestIF5 { + + default String defaultMethod() { + return "TestIF5.defaultMethod"; + } + + static String staticMethod() { + return "TestIF5.staticMethod"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF5.defaultMethod", mod = DEFAULT, declared = NO) +class TestClass5 implements TestIF5 { +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF6.defaultMethod", mod = DEFAULT, declared = YES) +@MethodDesc(name = "staticMethod", retval = "TestIF6.staticMethod", mod = STATIC, declared = YES) +@MethodDesc(name = "method", retval = "", mod = ABSTRACT, declared = YES) +interface TestIF6 { + + String method(); + + default String defaultMethod() { + return "TestIF6.defaultMethod"; + } + + static String staticMethod() { + return "TestIF6.staticMethod"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF6.defaultMethod", mod = DEFAULT, declared = NO) +@MethodDesc(name = "method", retval = "TestClass6.method", mod = REGULAR, declared = YES) +class TestClass6 implements TestIF6 { + + public String method() { + return "TestClass6.method"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF7.TestClass7", mod = DEFAULT, declared = YES) +interface TestIF7<T> { + + default T defaultMethod(T t) { + return t; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF7.TestClass7", mod = DEFAULT, declared = NO) +class TestClass7<T> implements TestIF7<T> { +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF8.TestClass8", mod = DEFAULT, declared = YES) +interface TestIF8<E> { + + default <E> E defaultMethod(E e) { + return e; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF8.TestClass8", mod = DEFAULT, declared = NO) +class TestClass8<T> implements TestIF8<T> { +}; + +@MethodDesc(name = "defaultMethod", retval = "TestIF9.defaultMethod", mod = DEFAULT, declared = YES) +interface TestIF9 extends TestIF1 { + + default String defaultMethod() { + return "TestIF9.defaultMethod"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF9.defaultMethod", mod = DEFAULT, declared = NO) +class TestClass9 implements TestIF9 { +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF9.defaultMethod", mod = DEFAULT, declared = NO) +@MethodDesc(name = "method", retval = "TestIF9.defaultMethod", mod = REGULAR, declared = YES) +class TestClass91 implements TestIF9, TestIF1 { + + public String method() { + return defaultMethod(); + } +} + +@MethodDesc(name = "staticMethod", retval = "TestIF10.staticMethod", mod = STATIC, declared = YES) +interface TestIF10 extends TestIF2 { + + static String staticMethod() { + + return "TestIF10.staticMethod"; + } +} + +@MethodDesc(name = "staticMethod", retval = "TestIF11.staticMethod", mod = STATIC, declared = YES) +@MethodDesc(name = "defaultMethod", retval = "TestIF1.defaultMethod", mod = DEFAULT, declared = NO) +interface TestIF11 extends TestIF1 { + + static String staticMethod() { + return "TestIF11.staticMethod"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF1.defaultMethod", mod = DEFAULT, declared = NO) +class TestClass11 implements TestIF11 { +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF12.defaultMethod", mod = DEFAULT, declared = YES) +@MethodDesc(name = "staticMethod", retval = "TestIF2.staticMethod", mod = STATIC, declared = NO) +interface TestIF12 extends TestIF2 { + + default String defaultMethod() { + return "TestIF12.defaultMethod"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF12.defaultMethod", mod = DEFAULT, declared = NO) +class TestClass12 implements TestIF12 { +} + +//Diamond Case +@MethodDesc(name = "defaultMethod", retval = "TestIF1.defaultMethod", mod = DEFAULT, declared = NO) +interface TestIF1A extends TestIF1 { +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF1.defaultMethod", mod = DEFAULT, declared = NO) +interface TestIF1B extends TestIF1 { +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF1.defaultMethod", mod = DEFAULT, declared = NO) +class TestClass13 implements TestIF1A, TestIF1B { +} + +//Diamond Override Case +@MethodDesc(name = "defaultMethod", retval = "TestIF1C.defaultMethod", mod = DEFAULT, declared = YES) +interface TestIF1C extends TestIF1 { + + default String defaultMethod() { + return "TestIF1C.defaultMethod"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF1D.defaultMethod", mod = DEFAULT, declared = YES) +interface TestIF1D extends TestIF1 { + + default String defaultMethod() { + return "TestIF1D.defaultMethod"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestClass14.defaultMethod", mod = REGULAR, declared = YES) +class TestClass14 implements TestIF1C, TestIF1D { + + public String defaultMethod() { + return "TestClass14.defaultMethod"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "", mod = ABSTRACT, declared = YES) +interface TestIF15 extends TestIF1 { + + String defaultMethod(); +} + +@MethodDesc(name = "defaultMethod", retval = "TestClass15.defaultMethod", mod = REGULAR, declared = YES) +class TestClass15 implements TestIF15 { + + public String defaultMethod() { + return "TestClass15.defaultMethod"; + } +} + +interface FuncInterface<T> { + + String test(T t); +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF16.defaultMethod", mod = DEFAULT, declared = YES) +interface TestIF16 { + + default String defaultMethod() { + FuncInterface<Object> fi = o -> o.toString(); + Object o = "TestIF16.defaultMethod"; + return fi.test(o); + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF16.defaultMethod", mod = DEFAULT, declared = NO) +class TestClass16 implements TestIF16 { +}; + +@MethodDesc(name = "defaultMethod", retval = "TestIF17.defaultMethod", mod = DEFAULT, declared = YES) +@MethodDesc(name = "staticMethod", retval = "TestIF17.staticMethod", mod = STATIC, declared = YES) +interface TestIF17 { + + default String defaultMethod() { + return staticMethod().replace("staticMethod", "defaultMethod"); + } + + public static String staticMethod() { + return "TestIF17.staticMethod"; + } +} + +@MethodDesc(name = "defaultMethod", retval = "TestIF17.defaultMethod", mod = DEFAULT, declared = NO) +class TestClass17 implements TestIF17 { +} + +@Retention(RetentionPolicy.RUNTIME) +@Repeatable(MethodDescs.class) +@interface MethodDesc { + String name(); + String retval(); + Mod mod(); + Declared declared(); +} + +@Retention(RetentionPolicy.RUNTIME) +@interface MethodDescs { + MethodDesc[] value(); +} + +public class DefaultStaticTestData { + + /** + * Test data for DefaultStaticInvokeTest The format of inner array is: First + * data is the name of the class under test Second data used in test as the + * arguments used for the method call. + */ + @DataProvider + static Object[][] testClasses() { + return new Object[][]{ + {"TestClass1", null}, + //{"TestClass2", null}, @ignore due to JDK-8009411 + {"TestClass3", null}, + //{"TestClass4", null}, @ignore due to JDK-8009411 + //{"TestClass5", null}, @ignore due to JDK-8009411 + //{"TestClass6", null}, @ignore due to JDK-8009411 + {"TestClass7", "TestIF7.TestClass7"}, + {"TestClass8", "TestIF8.TestClass8"}, + {"TestClass9", null}, + {"TestClass91", null}, + //{"TestClass11", null}, @ignore due to JDK-8009411 + //{"TestClass12", null}, @ignore due to JDK-8009411 + {"TestClass13", null}, + {"TestClass14", null}, + {"TestClass15", null}, + {"TestClass16", null} + //{"TestClass17", null} @ignore due to JDK-8009411 + }; + } + + /** + * Test data for DefaultStaticInvokeTest The format of inner array is: First + * data is the name of the interface under test Second data used in test as + * the arguments used for the method call. + */ + @DataProvider + static Object[][] testInterfaces() { + return new Object[][]{ + {"TestIF1", null}, + {"TestIF2", null}, + {"TestIF3", null}, + {"TestIF4", null}, + {"TestIF5", null}, + {"TestIF6", null}, + {"TestIF7", "TestIF7.TestClass7"}, + {"TestIF8", "TestIF8.TestClass8"}, + {"TestIF9", null}, + {"TestIF10", null}, + {"TestIF11", null}, + {"TestIF12", null}, + {"TestIF1A", null}, + {"TestIF1B", null}, + {"TestIF1C", null}, + {"TestIF1D", null}, + {"TestIF15", null}, + {"TestIF16", null}, + {"TestIF17", null},}; + } + + @DataProvider + static Object[][] testCasesAll() { + List<Object[]> result = Lists.newArrayList(); + result.addAll(Arrays.asList(testClasses())); + result.addAll(Arrays.asList(testInterfaces())); + return result.toArray(new Object[result.size()][]); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/lang/reflect/DefaultStaticTest/helper/Declared.java Tue Aug 06 18:54:02 2013 +0200 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * Helper class used for testing default/static method + * + * @author Yong Lu + */ + +package helper; + +public enum Declared { + YES, + NO +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/lang/reflect/DefaultStaticTest/helper/Mod.java Tue Aug 06 18:54:02 2013 +0200 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * Helper class used for testing default/static method + * + * @author Yong Lu + */ + +package helper; + +public enum Mod { + DEFAULT, + STATIC, + REGULAR, + ABSTRACT +}
--- a/test/java/lang/reflect/Method/DefaultMethodModeling.java Tue Aug 06 08:34:28 2013 -0700 +++ b/test/java/lang/reflect/Method/DefaultMethodModeling.java Tue Aug 06 18:54:02 2013 +0200 @@ -43,7 +43,7 @@ SuperIwithDefault.class, SuperIwithDefaultChild.class, Base.class, Combo1.class, Combo2.class, SonSuperIwithDefault.class, DaughterSuperIwithDefault.class, GrandchildSuperIwithDefault.class, D.class, - B.class, C.class + B.class, C.class, B1.class, D1.class }; for(Class<?> clazz : classes) { @@ -202,6 +202,17 @@ public void quux(){} } +class D1 implements SonSuperIwithDefault, DaughterSuperIwithDefault { + @ExpectedModel(declaringClass=D1.class) + public void foo(){} + + @ExpectedModel(declaringClass=D1.class) + public void baz(){} + + @ExpectedModel(declaringClass=D1.class) + public void quux(){} +} + // -=-=-=- // What does re-abstraction look like? @@ -222,3 +233,21 @@ @ExpectedModel(declaringClass=C.class) public void bar(){} } + +abstract class A1 implements SonSuperIwithDefault { + @ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=A1.class) + public abstract void baz(); + + @ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=A1.class) + public abstract void foo(); +} + +class B1 extends A1 { + @ExpectedModel(declaringClass=B1.class) + @Override + public void foo(){;} + + @ExpectedModel(declaringClass=B1.class) + @Override + public void baz(){} +}
--- a/test/java/lang/reflect/Method/IsDefaultTest.java Tue Aug 06 08:34:28 2013 -0700 +++ b/test/java/lang/reflect/Method/IsDefaultTest.java Tue Aug 06 18:54:02 2013 +0200 @@ -42,6 +42,12 @@ classList.add(TestType2.class); classList.add(TestType3.class); classList.add(TestType4.class); + classList.add(TestType2.nestedTestType2.class); + classList.add(TestType5.class); + classList.add(TestType5.nestedTestType5.class); + classList.add(TestType6.class); + classList.add(TestType6.nestedTestType6.class); + classList.add(TestType7.class); for(Class<?> clazz: classList) { for(Method method: clazz.getDeclaredMethods()) { @@ -78,11 +84,22 @@ @ExpectedIsDefault(true) default void bar() {}; // Default method + + @ExpectedIsDefault(true) + default void bar(int i) {}; // Default method + + @ExpectedIsDefault(true) + default void bar(String i) {}; // Default method } class TestType2 { @ExpectedIsDefault(false) void bar() {}; + + interface nestedTestType2 { + @ExpectedIsDefault(true) + default void nestedBar() {}; + } } class TestType3 implements TestType1 { @@ -92,6 +109,10 @@ @ExpectedIsDefault(false) @Override public void bar() {}; + + @ExpectedIsDefault(false) + @Override + public void bar(int i) {}; } @interface TestType4 { @@ -102,6 +123,51 @@ String anotherValue() default ""; } +interface TestType5 { + @ExpectedIsDefault(false) + abstract void aFoo(); + + @ExpectedIsDefault(false) + static void sFoo() {}; + + @ExpectedIsDefault(true) + public default void pBar() {}; + + @ExpectedIsDefault(true) + public default String sBar() {return "";}; + + interface nestedTestType5{ + @ExpectedIsDefault(false) + void nestedFoo(); + + @ExpectedIsDefault(true) + default void nestedBar() {}; + } +} + +class TestType6{ + interface nestedTestType6 { + @ExpectedIsDefault(true) + default void nestedBar() {}; + + @ExpectedIsDefault(false) + void nestedFoo(); + } + + @ExpectedIsDefault(false) + void foo(nestedTestType6 n) {} +} + +class TestType7 implements TestType6.nestedTestType6 { + + @ExpectedIsDefault(false) + public void nestedFoo() {} + + @ExpectedIsDefault(false) + @Override + public void nestedBar() {}; +} + @Retention(RetentionPolicy.RUNTIME) @interface ExpectedIsDefault { boolean value();