/* * Copyright (c) 2014, 2016, 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.
*/
protectedvoid runTests() throws Exception { /* * Get to the top of main() * to determine targetClass and mainThread
*/
BreakpointEvent bpe = startToMain(TARGET_CLASS_NAME);
/* * resume the target listening for events
*/
listenUntilVMDisconnect();
/* * deal with results of test * if anything has called failure("foo") testFailed will be true
*/ if (!testFailed) {
println("InterfaceMethodsTest: passed");
} else { thrownew Exception("InterfaceMethodsTest: failed");
}
}
// invoke the InterfaceA's "defaultMethodA"
testInvokePos(ifaceClass, ref, "defaultMethodA", "()I", vm().mirrorOf(RESULT_A));
// invoke the InterfaceA's "defaultMethodB"
testInvokePos(ifaceClass, ref, "defaultMethodB", "()I", vm().mirrorOf(RESULT_A));
// invoke the InterfaceA's "defaultMethodC"
testInvokePos(ifaceClass, ref, "defaultMethodC", "()I", vm().mirrorOf(RESULT_A));
// "defaultMethodD" from InterfaceB is not accessible from here
testInvokeNeg(ifaceClass, ref, "defaultMethodD", "()I", vm().mirrorOf(RESULT_B), "Attempted to invoke non-existing method");
// non-virtual invoke of the abstract method "implementedMethod" fails
testInvokeNeg(ifaceClass, ref, "implementedMethod", "()I", vm().mirrorOf(TARGET_CLASS_NAME), "Invocation of abstract methods is not supported");
// invoking static method A on the instance fails because static method A is // not inherited by TargetClass.
testInvokeNeg(ifaceClass, ref, "staticMethodA", "()I", vm().mirrorOf(RESULT_A), "Invalid MethodID");
// invoking static method B on the instance fails because static method B is // not inherited by TargetClass.
testInvokeNeg(ifaceClass, ref, "staticMethodB", "()I", vm().mirrorOf(RESULT_A), "Invalid MethodID");
// try to invoke a virtual method
testInvokePos(ifaceClass, ref, "implementedMethod", "()I", vm().mirrorOf(RESULT_TARGET), true);
}
privatevoid testInterfaceB(ObjectReference ref) { // Test non-virtual calls on InterfaceB
ReferenceType ifaceClass = (ReferenceType)vm().classesByName(INTERFACEB_NAME).get(0);
/* private method calls */
/* These should fail but won't because of JDK-8167416 testLookup(ifaceClass, "privateMethodA", "()I", true, NoSuchMethodError.class); // should fail testLookup(ifaceClass, "privateMethodA", "()I", false, NoSuchMethodError.class); // should fail
*/
Method m = testLookup(ifaceClass, "privateMethodB", "()I", true, null); // should succeed
testInvokePos(m, ref, vm().mirrorOf(RESULT_B), false);
testInvokePos(m, ref, vm().mirrorOf(RESULT_B), true);
/* Default method calls */
// invoke the inherited "defaultMethodA"
testInvokePos(ifaceClass, ref, "defaultMethodA", "()I", vm().mirrorOf(RESULT_A));
// invoke the inherited "defaultMethodB"
testInvokePos(ifaceClass, ref, "defaultMethodB", "()I", vm().mirrorOf(RESULT_A));
// invoke the inherited and overridden "defaultMethodC"
testInvokePos(ifaceClass, ref, "defaultMethodC", "()I", vm().mirrorOf(RESULT_B));
// invoke InterfaceB only "defaultMethodD"
testInvokePos(ifaceClass, ref, "defaultMethodD", "()I", vm().mirrorOf(RESULT_B));
// "implementedMethod" is not present in InterfaceB
testInvokeNeg(ifaceClass, ref, "implementedMethod", "()I", vm().mirrorOf(RESULT_TARGET), "Invocation of non-default methods is not supported");
/* Static method calls*/
// "staticMethodA" must not be inherited by InterfaceB
testInvokeNeg(ifaceClass, null, "staticMethodA", "()I", vm().mirrorOf(RESULT_A), "Static interface methods are not inheritable");
// "staticMethodA" is not inherited by InterfaceB even from an actual instance
testInvokeNeg(ifaceClass, ref, "staticMethodA", "()I", vm().mirrorOf(RESULT_A), "Static interface methods are not inheritable");
// "staticMethodB" is re-defined in InterfaceB
testInvokePos(ifaceClass, null, "staticMethodB", "()I", vm().mirrorOf(RESULT_B));
// the instance fails to invoke the re-defined form of "staticMethodB" from // InterfaceB because staticMethodB is not inherited by TargetClass
testInvokeNeg(ifaceClass, ref, "staticMethodB", "()I", vm().mirrorOf(RESULT_B), "Invalid MethodID");
// "staticMethodC" is present only in InterfaceB
testInvokePos(ifaceClass, null, "staticMethodC", "()I", vm().mirrorOf(RESULT_B));
// "staticMethodC" is not reachable from the instance because staticMethodC // is not inherited by TargetClass.
testInvokeNeg(ifaceClass, ref, "staticMethodC", "()I", vm().mirrorOf(RESULT_B), "Invalid MethodID");
}
privatevoid testImplementationClass(ReferenceType targetClass, ObjectReference thisObject) { // Test invocations on the implementation object
// Note: private interface calls have already been tested
/* Default method calls */
// "defaultMethodA" is accessible and not overridden
testInvokePos(targetClass, thisObject, "defaultMethodA", "()I", vm().mirrorOf(RESULT_A));
// "defaultMethodB" is accessible and overridden in TargetClass
testInvokePos(targetClass, thisObject, "defaultMethodB", "()I", vm().mirrorOf(RESULT_TARGET));
// "defaultMethodC" is accessible and overridden in InterfaceB
testInvokePos(targetClass, thisObject, "defaultMethodC", "()I", vm().mirrorOf(RESULT_B));
// "defaultMethodD" is accessible
testInvokePos(targetClass, thisObject, "defaultMethodD", "()I", vm().mirrorOf(RESULT_B));
/* Non-default instance method calls */
// "classMethod" declared in TargetClass is accessible
testInvokePos(targetClass, thisObject, "classMethod", "()I", vm().mirrorOf(RESULT_TARGET));
// the abstract "implementedMethod" has been implemented in TargetClass
testInvokePos(targetClass, thisObject, "implementedMethod", "()I", vm().mirrorOf(RESULT_TARGET));
/* Static method calls */
// All the static methods declared by the interfaces are not reachable from the instance of the implementor class
testInvokeNeg(targetClass, thisObject, "staticMethodA", "()I", vm().mirrorOf(RESULT_A), "Static interface methods are not inheritable");
testInvokeNeg(targetClass, thisObject, "staticMethodB", "()I", vm().mirrorOf(RESULT_B), "Static interface methods are not inheritable");
testInvokeNeg(targetClass, thisObject, "staticMethodC", "()I", vm().mirrorOf(RESULT_B), "Static interface methods are not inheritable");
// All the static methods declared by the interfaces are not reachable through the implementor class
testInvokeNeg(targetClass, null, "staticMethodA", "()I", vm().mirrorOf(RESULT_A), "Static interface methods are not inheritable");
testInvokeNeg(targetClass, null, "staticMethodB", "()I", vm().mirrorOf(RESULT_B), "Static interface methods are not inheritable");
testInvokeNeg(targetClass, null, "staticMethodC", "()I", vm().mirrorOf(RESULT_B), "Static interface methods are not inheritable");
}
println(" return val = " + returnValue); // It has to be the same value as what we passed in! if (returnValue.equals(value)) {
println(" " + method.name() + " return value matches: "
+ value);
} else { if (value != null) { thrownew Exception(method.name() + " returned: " + returnValue + " expected: " + value );
} else {
println(" " + method.name() + " return value : " + returnValue);
}
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 und die Messung sind noch experimentell.