doresolveandclinit.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,937 行 · 第 1/5 页

JAVA
1,937
字号
/* * @(#)DoResolveAndClinit.java	1.8 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program 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 program 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 at /legal/license.txt).    *    * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * *//*    NOTE: This module compiles some methods which are specifically designed    to operate on some bytecodes which require runtime class initializations,    and exercises lazy resolution.  This module will test each of those cases    to see if the desired behavior is executed.    To run, use:    cvm -Xjit:compile=none -cp testclasses.zip DoResolveAndClinit    NOTE: compile=none is important because this testsuite needs to control    which and when methods get compiled.  It also uses the CompilerTest class    in testclasses.zip to do the controlled JIT compilation.*//*    Cases to test:    =============    opc_checkcast:    opc_instanceof:    opc_new:    opc_anewarray:    opc_multianewarray:    opc_invokestatic:    opc_invokevirtual:    opc_invokespecial:    opc_invokeinterface:    opc_putstatic:    opc_getstatic:    opc_putfield:    opc_getfield:*/public class DoResolveAndClinit{    static int totalTests = 0;    static int totalFailures = 0;    static String usageStr[] = {	"=========================================================================",	"TestSuite: DoResolveAndClinit",	"",	"To run, use:",	"cvm -Xjit:compile=none -cp testclasses.zip DoResolveAndClinit",	"",	"NOTE: compile=none is important because this testsuite needs to control",	"which and when methods get compiled.  It also uses the CompilerTest class",	"in testclasses.zip to do the controlled JIT compilation.",	"",	"If run successfully, there should be 0 failures reported at the end of",	"the run.",	"=========================================================================",	"",    };    static void printUsage() {	for (int i = 0; i < usageStr.length; i++) {	    System.out.println(usageStr[i]);	}    }    public static void main(String[] args) {	printUsage();        totalTests = 0;        totalFailures = 0;        // Do setup:        DoCheckCast.doSetup();        DoInstanceOf.doSetup();        DoNew.doSetup();        DoANewArray.doSetup();        DoMultiANewArray.doSetup();        DoInvokeStatic.doSetup();        DoInvokeVirtual.doSetup();//        DoInvokeSpecial.doSetup();        DoInvokeInterface.doSetup();        DoPutStatic.doSetup();        DoGetStatic.doSetup();        DoPutField.doSetup();        DoGetField.doSetup();        // Do tests:        DoCheckCast.doTest();        DoInstanceOf.doTest();        DoNew.doTest();        DoANewArray.doTest();        DoMultiANewArray.doTest();        DoInvokeStatic.doTest();        DoInvokeVirtual.doTest();//        DoInvokeSpecial.doTest();        DoInvokeInterface.doTest();        DoPutStatic.doTest();        DoGetStatic.doTest();        DoPutField.doTest();        DoGetField.doTest();        // Report the total number of failures:        System.out.println("Tests ran: " + totalTests + ", failures: " +                           totalFailures);    }    public static void reportPassIf(String testName, boolean success) {        System.out.println((success ? "PASSED" : "FAILED") + " Test " +                           testName);        totalTests++;        if (!success) {            totalFailures++;        }    }    public static void reportPassIf(String testName, boolean actual, boolean expected) {	boolean success = (actual == expected);        System.out.println((success ? "PASSED" : "FAILED") + " Test " +                           testName);        totalTests++;        if (!success) {	    System.out.println("  expecting = " + expected +			       ", actual = " + actual);            totalFailures++;        }    }    public static void reportPassIf(String testName, byte actual, byte expected) {	boolean success = (actual == expected);        System.out.println((success ? "PASSED" : "FAILED") + " Test " +                           testName);        totalTests++;        if (!success) {	    System.out.println("  expecting = " + expected +			       ", actual = " + actual);            totalFailures++;        }    }    public static void reportPassIf(String testName, char actual, char expected) {	boolean success = (actual == expected);        System.out.println((success ? "PASSED" : "FAILED") + " Test " +                           testName);        totalTests++;        if (!success) {	    System.out.println("  expecting = " + expected +			       ", actual = " + actual);            totalFailures++;        }    }    public static void reportPassIf(String testName, short actual, short expected) {	boolean success = (actual == expected);        System.out.println((success ? "PASSED" : "FAILED") + " Test " +                           testName);        totalTests++;        if (!success) {	    System.out.println("  expecting = " + expected +			       ", actual = " + actual);            totalFailures++;        }    }    public static void reportPassIf(String testName, int actual, int expected) {	boolean success = (actual == expected);        System.out.println((success ? "PASSED" : "FAILED") + " Test " +                           testName);        totalTests++;        if (!success) {	    System.out.println("  expecting = " + expected +			       ", actual = " + actual);            totalFailures++;        }    }    public static void reportPassIf(String testName, float actual, float expected) {	boolean success = (actual == expected);        System.out.println((success ? "PASSED" : "FAILED") + " Test " +                           testName);        totalTests++;        if (!success) {	    System.out.println("  expecting = " + expected +			       ", actual = " + actual);            totalFailures++;        }    }    public static void reportPassIf(String testName, long actual, long expected) {	boolean success = (actual == expected);        System.out.println((success ? "PASSED" : "FAILED") + " Test " +                           testName);        totalTests++;        if (!success) {	    System.out.println("  expecting = " + expected +			       ", actual = " + actual);            totalFailures++;        }    }    public static void reportPassIf(String testName, double actual, double expected) {	boolean success = (actual == expected);        System.out.println((success ? "PASSED" : "FAILED") + " Test " +                           testName);        totalTests++;        if (!success) {	    System.out.println("  expecting = " + expected +			       ", actual = " + actual);            totalFailures++;        }    }    public static void reportPassIf(String testName, Object actual, Object expected) {	boolean success = (actual == expected);        System.out.println((success ? "PASSED" : "FAILED") + " Test " +                           testName);        totalTests++;        if (!success) {	    System.out.println("  expecting = " + expected +			       ", actual = " + actual);            totalFailures++;        }    }}///////////////////////////////////////////////////////////////////////////////// Testing opcode checkcast:// Note: Resolves to a CB pointer.class DoCheckCastResolved {}class DoCheckCastUnresolved {}class DoCheckCastUnresolvedNull {}class DoCheckCastResolved2 extends DoCheckCastResolved {}class DoCheckCastUnresolved2 extends DoCheckCastUnresolved {}class DoCheckCast{    static final String[] compileItems = {        "DoCheckCast.doResolved(Ljava/lang/Object;)LDoCheckCastResolved;",        "DoCheckCast.doUnresolved(Ljava/lang/Object;)LDoCheckCastUnresolved;",        "DoCheckCast.doUnresolvedNull(Ljava/lang/Object;)LDoCheckCastUnresolved;",    };    public static void doSetup() {        // Setup initial conditions:        DoCheckCastResolved r = new DoCheckCastResolved();  // Resolve it.        // Do compilation:        CompilerTest.main(compileItems);    }    public static void doTest() {        Object o = new Object();        boolean success;        // 1. Resolved CheckCast against a NULL object.        try {            doResolved(null);            success = true;  // Should pass this checkcast.        } catch (ClassCastException cce) {            success = false;        }        DoResolveAndClinit.reportPassIf("CheckCastResolved(NULL)", success);        // 2. Resolved CheckCast against an invalid object.        try {            doResolved(o);            success = false;        } catch (ClassCastException cce) {            success = true; // Should fail this checkcast.        }        DoResolveAndClinit.reportPassIf("CheckCastResolved(Invalid)", success);        // 3. Resolved CheckCast against a valid object.        try {            doResolved(new DoCheckCastResolved());            success = true; // Should pass this checkcast.        } catch (ClassCastException cce) {            success = false;        }        DoResolveAndClinit.reportPassIf("CheckCastResolved(Valid)", success);        // 4. Resolved CheckCast against a valid object w/ guess.        //    Guess initialized in 3.        try {            doResolved(new DoCheckCastResolved());            success = true; // Should pass this checkcast.        } catch (ClassCastException cce) {            success = false;        }        DoResolveAndClinit.reportPassIf(            "CheckCastResolved(Valid + Guess)", success);        // 5. Resolved CheckCast against a valid sub-object w/o guess.        //    Guess initialized in 3.        try {            doResolved(new DoCheckCastResolved2());            success = true; // Should pass this checkcast.        } catch (ClassCastException cce) {            success = false;        }        DoResolveAndClinit.reportPassIf(            "CheckCastResolved(ValidSubClass)", success);        // 6. Resolved CheckCast against a valid sub-object w/ guess.        //    Guess initialized in 5.        try {            doResolved(new DoCheckCastResolved2());            success = true; // Should pass this checkcast.        } catch (ClassCastException cce) {            success = false;        }        DoResolveAndClinit.reportPassIf(            "CheckCastResolved(ValidSubClass + Guess)", success);        // 7. Unresolved CheckCast against a NULL object.        //    Not yet resolved ... 1st use.        try {            doUnresolvedNull(null);            success = true;        } catch (ClassCastException cce) {            success = false;        }        DoResolveAndClinit.reportPassIf(            "CheckCastUnresolvedNull(NULL, unresolved)", success);        // 8. Unresolved CheckCast against an invalid object.        //    Resolved by step 7.        try {            doUnresolvedNull(o);            success = false;        } catch (ClassCastException cce) {            success = true;        }        DoResolveAndClinit.reportPassIf(            "CheckCastUnresolvedNull(Invalid, unresolved)", success);        // 9. Unresolved CheckCast against an invalid object.        //    Not yet resolved because NULL is check inline in step 7.        try {            doUnresolved(o);            success = false;        } catch (ClassCastException cce) {            success = true;        }        DoResolveAndClinit.reportPassIf(            "CheckCastUnresolved(Invalid, unresolved)", success);        // 10. Unresolved CheckCast against a NULL object.        //     Resolved by step 9.        try {            doUnresolved(null);            success = true;        } catch (ClassCastException cce) {            success = false;        }        DoResolveAndClinit.reportPassIf(            "CheckCastUnresolved(NULL, resolved)", success);        // 11. Unresolved CheckCast against an invalid object.        //     Resolved by step 9.        try {            doUnresolved(o);            success = false;        } catch (ClassCastException cce) {            success = true;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?