/* * Copyright (c) 2020, 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.
*/
// a default method is declared in one of the proxy interfaces
@DataProvider(name = "defaultMethods") private Object[][] defaultMethods() { returnnew Object[][]{ new Object[]{newClass<?>[]{I1.class, I2.class}, true, 10}, new Object[]{newClass<?>[]{I1.class, I3.class}, true, 10}, new Object[]{newClass<?>[]{I1.class, I12.class}, true, 10}, new Object[]{newClass<?>[]{I2.class, I12.class}, true, 20}, new Object[]{newClass<?>[]{I4.class}, true, 40}, new Object[]{newClass<?>[]{I4.class, I3.class}, true, 40}, new Object[]{newClass<?>[]{I12.class}, false, -1}, new Object[]{newClass<?>[]{I12.class, I1.class, I2.class}, false, -1}
};
}
Object proxy = Proxy.newProxyInstance(DefaultMethods.class.getClassLoader(), intfs, ih);
Method m = proxy.getClass().getMethod("m"); int result = (int)m.invoke(proxy);
assertEquals(result, expected);
}
// a default method may be declared in a proxy interface or // inherited from a superinterface of a proxy interface
@DataProvider(name = "supers") private Object[][] supers() { returnnew Object[][]{ // invoke "m" implemented in the first proxy interface // same as the method passed to InvocationHandler::invoke new Object[]{newClass<?>[]{I1.class}, I1.class, 10}, new Object[]{newClass<?>[]{I2.class}, I2.class, 20}, new Object[]{newClass<?>[]{I1.class, I2.class}, I1.class, 10}, // "m" is implemented in I2, an indirect superinterface of I3 new Object[]{newClass<?>[]{I3.class}, I3.class, 20}, // "m" is implemented in I1, I2 and overridden in I4 new Object[]{newClass<?>[]{I4.class}, I4.class, 40}, // invoke "m" implemented in the second proxy interface // different from the method passed to InvocationHandler::invoke new Object[]{newClass<?>[]{I1.class, I2.class}, I2.class, 20}, new Object[]{newClass<?>[]{I1.class, I3.class}, I3.class, 20}, // I2::m is implemented in more than one proxy interface directly or indirectly // I3::m resolves to I2::m (indirect superinterface) // I2 is the superinterface of I4 and I4 overrides m // the proxy class can invoke I4::m and I2::m new Object[]{newClass<?>[]{I3.class, I4.class}, I3.class, 20}, new Object[]{newClass<?>[]{I3.class, I4.class}, I4.class, 40}, new Object[]{newClass<?>[]{I4.class, I3.class}, I3.class, 20}, new Object[]{newClass<?>[]{I4.class, I3.class}, I4.class, 40}
};
}
@DataProvider(name = "throwables") private Object[][] throwables() { returnnew Object[][] { new Object[] { new IOException() }, new Object[] { new IllegalArgumentException() }, new Object[] { new ClassCastException() }, new Object[] { new NullPointerException() }, new Object[] { new AssertionError() }, new Object[] { new Throwable() }
};
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.