|
Java example source code file (DefaultMethodsTest.java)
The DefaultMethodsTest.java Java example source code/* * Copyright (c) 2012, 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. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * 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. */ package org.openjdk.tests.vm; import org.openjdk.tests.separate.Compiler; import org.openjdk.tests.separate.TestHarness; import org.testng.annotations.Test; import static org.openjdk.tests.separate.SourceModel.AbstractMethod; import static org.openjdk.tests.separate.SourceModel.AccessFlag; import static org.openjdk.tests.separate.SourceModel.Class; import static org.openjdk.tests.separate.SourceModel.ConcreteMethod; import static org.openjdk.tests.separate.SourceModel.DefaultMethod; import static org.openjdk.tests.separate.SourceModel.Extends; import static org.openjdk.tests.separate.SourceModel.Interface; import static org.openjdk.tests.separate.SourceModel.MethodParameter; import static org.openjdk.tests.separate.SourceModel.TypeParameter; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.fail; @Test(groups = "vm") public class DefaultMethodsTest extends TestHarness { public DefaultMethodsTest() { super(false, false); } /** * class C { public int m() { return 22; } } * * TEST: C c = new C(); c.m() == 22 */ public void testHarnessInvokeVirtual() { Class C = new Class("C", ConcreteMethod.std("22")); assertInvokeVirtualEquals(22, C); } /** * interface I { int m(); } * class C implements I { public int m() { return 33; } } * * TEST: I i = new C(); i.m() == 33; */ public void testHarnessInvokeInterface() { Interface I = new Interface("I", AbstractMethod.std()); Class C = new Class("C", I, ConcreteMethod.std("33")); assertInvokeInterfaceEquals(33, C, I); } /** * class C {} * * TEST: C c = new C(); c.m() throws NoSuchMethod */ public void testHarnessThrows() { Class C = new Class("C"); assertThrows(NoSuchMethodError.class, C); } /** * interface I { int m() default { return 44; } } * class C implements I {} * * TEST: C c = new C(); c.m() == 44; * TEST: I i = new C(); i.m() == 44; */ public void testBasicDefault() { Interface I = new Interface("I", DefaultMethod.std("44")); Class C = new Class("C", I); assertInvokeVirtualEquals(44, C); assertInvokeInterfaceEquals(44, C, I); } /** * interface I { default int m() { return 44; } } * interface J extends I {} * interface K extends J {} * class C implements K {} * * TEST: C c = new C(); c.m() == 44; * TEST: I i = new C(); i.m() == 44; */ public void testFarDefault() { Interface I = new Interface("I", DefaultMethod.std("44")); Interface J = new Interface("J", I); Interface K = new Interface("K", J); Class C = new Class("C", K); assertInvokeVirtualEquals(44, C); assertInvokeInterfaceEquals(44, C, K); } /** * interface I { int m(); } * interface J extends I { default int m() { return 44; } } * interface K extends J {} * class C implements K {} * * TEST: C c = new C(); c.m() == 44; * TEST: K k = new C(); k.m() == 44; */ public void testOverrideAbstract() { Interface I = new Interface("I", AbstractMethod.std()); Interface J = new Interface("J", I, DefaultMethod.std("44")); Interface K = new Interface("K", J); Class C = new Class("C", K); assertInvokeVirtualEquals(44, C); assertInvokeInterfaceEquals(44, C, K); } /** * interface I { int m() default { return 44; } } * class C implements I { public int m() { return 55; } } * * TEST: C c = new C(); c.m() == 55; * TEST: I i = new C(); i.m() == 55; */ public void testExisting() { Interface I = new Interface("I", DefaultMethod.std("44")); Class C = new Class("C", I, ConcreteMethod.std("55")); assertInvokeVirtualEquals(55, C); assertInvokeInterfaceEquals(55, C, I); } /** * interface I { default int m() { return 99; } } * class B implements I {} * class C extends B {} * * TEST: C c = new C(); c.m() == 99; * TEST: I i = new C(); i.m() == 99; */ public void testInherited() { Interface I = new Interface("I", DefaultMethod.std("99")); Class B = new Class("B", I); Class C = new Class("C", B); assertInvokeVirtualEquals(99, C); assertInvokeInterfaceEquals(99, C, I); } /** * interface I { default int m() { return 99; } } * class C { public int m() { return 11; } } * class D extends C implements I {} * * TEST: D d = new D(); d.m() == 11; * TEST: I i = new D(); i.m() == 11; */ public void testExistingInherited() { Interface I = new Interface("I", DefaultMethod.std("99")); Class C = new Class("C", ConcreteMethod.std("11")); Class D = new Class("D", C, I); assertInvokeVirtualEquals(11, D); assertInvokeInterfaceEquals(11, D, I); } /** * interface I { default int m() { return 44; } } * class C implements I { public int m() { return 11; } } * class D extends C { public int m() { return 22; } } * * TEST: D d = new D(); d.m() == 22; * TEST: I i = new D(); i.m() == 22; */ public void testExistingInheritedOverride() { Interface I = new Interface("I", DefaultMethod.std("99")); Class C = new Class("C", I, ConcreteMethod.std("11")); Class D = new Class("D", C, ConcreteMethod.std("22")); assertInvokeVirtualEquals(22, D); assertInvokeInterfaceEquals(22, D, I); } /** * interface I { default int m() { return 99; } } * interface J { defaultint m() { return 88; } } * class C implements I { public int m() { return 11; } } * class D extends C { public int m() { return 22; } } * class E extends D implements J {} * * TEST: E e = new E(); e.m() == 22; * TEST: J j = new E(); j.m() == 22; */ public void testExistingInheritedPlusDefault() { Interface I = new Interface("I", DefaultMethod.std("99")); Interface J = new Interface("J", DefaultMethod.std("88")); Class C = new Class("C", I, ConcreteMethod.std("11")); Class D = new Class("D", C, ConcreteMethod.std("22")); Class E = new Class("E", D, J); assertInvokeVirtualEquals(22, E); assertInvokeInterfaceEquals(22, E, J); } /** * interface I { default int m() { return 99; } } * class B implements I {} * class C extends B { public int m() { return 77; } } * * TEST: C c = new C(); c.m() == 77; * TEST: I i = new C(); i.m() == 77; */ public void testInheritedWithConcrete() { Interface I = new Interface("I", DefaultMethod.std("99")); Class B = new Class("B", I); Class C = new Class("C", B, ConcreteMethod.std("77")); assertInvokeVirtualEquals(77, C); assertInvokeInterfaceEquals(77, C, I); } /** * interface I { default int m() { return 99; } } * class B implements I {} * class C extends B implements I { public int m() { return 66; } } * * TEST: C c = new C(); c.m() == 66; * TEST: I i = new C(); i.m() == 66; */ public void testInheritedWithConcreteAndImpl() { Interface I = new Interface("I", DefaultMethod.std("99")); Class B = new Class("B", I); Class C = new Class("C", B, I, ConcreteMethod.std("66")); assertInvokeVirtualEquals(66, C); assertInvokeInterfaceEquals(66, C, I); } /** * interface I { default int m() { return 99; } } * interface J { default int m() { return 88; } } * class C implements I, J {} * * TEST: C c = new C(); c.m() throws ICCE */ public void testConflict() { Interface I = new Interface("I", DefaultMethod.std("99")); Interface J = new Interface("J", DefaultMethod.std("88")); Class C = new Class("C", I, J); assertThrows(IncompatibleClassChangeError.class, C); } /** * interface I { int m(); } * interface J { default int m() { return 88; } } * class C implements I, J {} * * TEST: C c = new C(); c.m() == 88 */ public void testAmbiguousReabstract() { Interface I = new Interface("I", AbstractMethod.std()); Interface J = new Interface("J", DefaultMethod.std("88")); Class C = new Class("C", I, J); assertInvokeVirtualEquals(88, C); } /** * interface I { default int m() { return 99; } } * interface J extends I { } * interface K extends I { } * class C implements J, K {} * * TEST: C c = new C(); c.m() == 99 * TEST: J j = new C(); j.m() == 99 * TEST: K k = new C(); k.m() == 99 * TEST: I i = new C(); i.m() == 99 */ public void testDiamond() { Interface I = new Interface("I", DefaultMethod.std("99")); Interface J = new Interface("J", I); Interface K = new Interface("K", I); Class C = new Class("C", J, K); assertInvokeVirtualEquals(99, C); assertInvokeInterfaceEquals(99, C, J); assertInvokeInterfaceEquals(99, C, K); assertInvokeInterfaceEquals(99, C, I); } /** * interface I { default int m() { return 99; } } * interface J extends I { } * interface K extends I { } * interface L extends I { } * interface M extends I { } * class C implements I, J, K, L, M {} * * TEST: C c = new C(); c.m() == 99 * TEST: J j = new C(); j.m() == 99 * TEST: K k = new C(); k.m() == 99 * TEST: I i = new C(); i.m() == 99 * TEST: L l = new C(); l.m() == 99 * TEST: M m = new C(); m.m() == 99 */ public void testExpandedDiamond() { Interface I = new Interface("I", DefaultMethod.std("99")); Interface J = new Interface("J", I); Interface K = new Interface("K", I); Interface L = new Interface("L", I); Interface M = new Interface("M", L); Class C = new Class("C", I, J, K, L, M); assertInvokeVirtualEquals(99, C); assertInvokeInterfaceEquals(99, C, J); assertInvokeInterfaceEquals(99, C, K); assertInvokeInterfaceEquals(99, C, I); assertInvokeInterfaceEquals(99, C, L); assertInvokeInterfaceEquals(99, C, M); } /** * interface I { int m() default { return 99; } } * interface J extends I { int m(); } * class C implements J {} * * TEST: C c = new C(); c.m() throws AME */ public void testReabstract() { Interface I = new Interface("I", DefaultMethod.std("99")); Interface J = new Interface("J", I, AbstractMethod.std()); Class C = new Class("C", J); assertThrows(AbstractMethodError.class, C); } /** * interface I { default int m() { return 88; } } * interface J extends I { default int m() { return 99; } } * class C implements J {} * * TEST: C c = new C(); c.m() == 99; * TEST: J j = new C(); j.m() == 99; * TEST: I i = new C(); i.m() == 99; */ public void testShadow() { Interface I = new Interface("I", DefaultMethod.std("88")); Interface J = new Interface("J", I, DefaultMethod.std("99")); Class C = new Class("C", J); assertInvokeVirtualEquals(99, C); assertInvokeInterfaceEquals(99, C, J); assertInvokeInterfaceEquals(99, C, I); } /** * interface I { default int m() { return 88; } } * interface J extends I { default int m() { return 99; } } * class C implements I, J {} * * TEST: C c = new C(); c.m() == 99; * TEST: J j = new C(); j.m() == 99; * TEST: I i = new C(); i.m() == 99; */ public void testDisqualified() { Interface I = new Interface("I", DefaultMethod.std("88")); Interface J = new Interface("J", I, DefaultMethod.std("99")); Class C = new Class("C", I, J); assertInvokeVirtualEquals(99, C); assertInvokeInterfaceEquals(99, C, J); assertInvokeInterfaceEquals(99, C, I); } /** * interface I<T> { default int m(T t) { return 99; } } * Class C implements I<String> { public int m(String s) { return 88; } } * * TEST: C c = new C(); c.m("string") == 88; * TEST: I i = new C(); i.m("string") == 88; */ public void testSelfFill() { // This test ensures that a concrete method overrides a default method // that matches at the language-level, but has a different method // signature due to erasure. DefaultMethod dm = new DefaultMethod( "int", "m", "return 99;", new MethodParameter("T", "t")); ConcreteMethod cm = new ConcreteMethod( "int", "m", "return 88;", AccessFlag.PUBLIC, new MethodParameter("String", "s")); Interface I = new Interface("I", new TypeParameter("T"), dm); Class C = new Class("C", I.with("String"), cm); AbstractMethod pm = new AbstractMethod( "int", "m", new MethodParameter("T", "t")); assertInvokeVirtualEquals(88, C, cm, "-1", "\"string\""); assertInvokeInterfaceEquals(99, C, I.with("String"), pm, "\"string\""); C.setFullCompilation(true); // Force full bridge generation assertInvokeInterfaceEquals(88, C, I.with("String"), pm, "\"string\""); } /** * interface I { default int m() { return 99; } } * class C implements I {} * * TEST: C.class.getMethod("m").invoke(new C()) == 99 */ public void testReflectCall() { Interface I = new Interface("I", DefaultMethod.std("99")); //workaround accessibility issue when loading C with DirectedClassLoader I.addAccessFlag(AccessFlag.PUBLIC); Class C = new Class("C", I); Compiler.Flags[] flags = this.verbose ? new Compiler.Flags[] { Compiler.Flags.VERBOSE } : new Compiler.Flags[] {}; Compiler compiler = new Compiler(flags); java.lang.Class<?> cls = null; try { cls = compiler.compileAndLoad(C); } catch (ClassNotFoundException e) { fail("Could not load class"); } java.lang.reflect.Method method = null; try { method = cls.getMethod(stdMethodName); } catch (NoSuchMethodException e) { fail("Could not find method in class"); } assertNotNull(method); Object c = null; try { c = cls.newInstance(); } catch (InstantiationException | IllegalAccessException e) { fail("Could not create instance of class"); } assertNotNull(c); Integer res = null; try { res = (Integer)method.invoke(c); } catch (IllegalAccessException | java.lang.reflect.InvocationTargetException e) { fail("Could not invoke default instance method"); } assertNotNull(res); assertEquals(res.intValue(), 99); compiler.cleanup(); } /** * interface I<T,V,W> { default int m(T t, V v, W w) { return 99; } } * interface J<T,V> extends I Other Java examples (source code examples)Here is a short list of links related to this Java DefaultMethodsTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.