reflectiontest.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 2,665 行 · 第 1/5 页
JAVA
2,665 行
/* * @(#)ReflectionTest.java 1.18 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. * */import java.lang.reflect.*;import cvmtest.ReflectionTestOtherPackageHelper;/** Fairly comprehensive test of Method.invoke() and java.lang.Field.{set,get} functionality. Tests most, if not all, widening conversions and their exception-throwing cases. Do not rename this class without modifying the using code below. @author Kenneth Russell */public class ReflectionTest { // Do not make any changes to these inner class hierarchies, // including class names and how many inner classes are declared, // without taking care to modify the using code below! public static class A { } public static class B extends A { } public ReflectionTest() { } public ReflectionTest(int i) { intField = i; } public ReflectionTest(double d, B b) { doubleField = d; bField = b; } public ReflectionTest(B b, double d) { doubleField = d; bField = b; } public static class D { public static class E { } } public static class F { } public static abstract class AbstractClass { int intField; public AbstractClass(int i) { intField = i; } public abstract void abstractMethod(); } public boolean booleanField = false; public byte byteField = 0; public char charField = 'a'; public short shortField = 0; public int intField = 0; public float floatField = 0; public double doubleField = 0; public long longField = 0; public B bField = null; public boolean voidMethodCalled = false; public void setBooleanField(boolean newVal) { System.out.println("setBooleanField(" + newVal + ")"); booleanField = newVal; } public void setByteField(byte newVal) { System.out.println("setByteField(" + newVal + ")"); byteField = newVal; } public void setCharField(char newVal) { System.out.println("setCharField(" + newVal + ")"); charField = newVal; } public void setShortField(short newVal) { System.out.println("setShortField(" + newVal + ")"); shortField = newVal; } public void setIntField(int newVal) { System.out.println("setIntField(" + newVal + ")"); intField = newVal; } public void setFloatField(float newVal) { System.out.println("setFloatField(" + newVal + ")"); floatField = newVal; } public void setDoubleField(double newVal) { System.out.println("setDoubleField(" + newVal + ")"); doubleField = newVal; } public void setLongField(long newVal) { System.out.println("setLongField(" + newVal + ")"); longField = newVal; } public boolean getBooleanField() { System.out.println("getBooleanField() returning " + booleanField); return booleanField; } public byte getByteField() { System.out.println("getByteField() returning " + byteField); return byteField; } public char getCharField() { System.out.println("getCharField() returning " + charField); return charField; } public short getShortField() { System.out.println("getShortField() returning " + shortField); return shortField; } public int getIntField() { System.out.println("getIntField() returning " + intField); return intField; } public float getFloatField() { System.out.println("getFloatField() returning " + floatField); return floatField; } public double getDoubleField() { System.out.println("getDoubleField() returning " + doubleField); return doubleField; } public long getLongField() { System.out.println("getLongField() returning " + longField); return longField; } public void voidMethod() { voidMethodCalled = true; } public static void check(String message, boolean condition) { System.out.print(message + ": "); if (condition) System.out.println("OK"); else System.out.println("**FAIL**"); } public static boolean find(Class[] classes, String name) { for (int i = 0; i < classes.length; i++) { if (classes[i].getName().equals(name)) return true; } return false; } public static void main(String[] args) { ReflectionTest test = new ReflectionTest(); Class myClass = test.getClass(); Method setMethod = null; Method getMethod = null; boolean booleanVal; byte byteVal; char charVal; short shortVal; int intVal; float floatVal; double doubleVal; long longVal; Object retVal; boolean success; boolean allTestsPassed = true; boolean warnAtEnd = false; int numWarnings = 0; boolean runMethodInvokeTests = true; boolean runAccessTests = true; boolean runFieldTests = true; Class[] setArgTypes = new Class[1]; setArgTypes[0] = myClass; Object[] setArgs = new Object[1]; Object[] getArgs = new Object[0]; if (runMethodInvokeTests) { // // Normal (non-exception) method tests // // System.out.println("1"); // Method.invoke() tests // set/getBooleanField setArgTypes[0] = Boolean.TYPE; success = false; try { //System.out.println("2"); setMethod = myClass.getMethod("setBooleanField", setArgTypes); //System.out.println("3"); success = true; } catch (Exception e) { e.printStackTrace(); success = false; } // System.out.println("4"); check("getMethod(\"setBooleanField\")", success); allTestsPassed &= success; // System.out.println("5"); try { getMethod = myClass.getMethod("getBooleanField", null); } catch (Exception e) { e.printStackTrace(); success = false; } check("getMethod(\"getBooleanField\")", success); allTestsPassed &= success; booleanVal = true; setArgs[0] = new Boolean(booleanVal); success = false; try { retVal = setMethod.invoke(test, setArgs); success = (retVal == null); } catch (Exception e) { e.printStackTrace(); success = false; } check("invoke of setBoolean", success); allTestsPassed &= success; success = false; try { retVal = getMethod.invoke(test, getArgs); // System.out.println("retval was a " + // retVal.getClass().getName()); success = (((Boolean) retVal).booleanValue() == booleanVal); } catch (Exception e) { e.printStackTrace(); success = false; } check("invoke of getBoolean", success); allTestsPassed &= success; // set/getByteField setArgTypes[0] = Byte.TYPE; success = false; try { setMethod = myClass.getMethod("setByteField", setArgTypes); success = true; } catch (Exception e) { e.printStackTrace(); success = false; } check("getMethod(\"setByteField\")", success); allTestsPassed &= success; success = false; try { getMethod = myClass.getMethod("getByteField", null); success = true; } catch (Exception e) { e.printStackTrace(); success = false; } check("getMethod(\"getByteField\")", success); allTestsPassed &= success; byteVal = 1; setArgs[0] = new Byte(byteVal); success = false; try { retVal = setMethod.invoke(test, setArgs); success = (retVal == null); } catch (Exception e) { e.printStackTrace(); success = false; } check("invoke of setByte", success); allTestsPassed &= success; success = false; try { retVal = getMethod.invoke(test, getArgs); success = (((Byte) retVal).byteValue() == byteVal); } catch (Exception e) { e.printStackTrace(); success = false; } check("invoke of getByte", success); allTestsPassed &= success; // set/getCharField setArgTypes[0] = Character.TYPE; success = false; try { setMethod = myClass.getMethod("setCharField", setArgTypes); success = true; } catch (Exception e) { e.printStackTrace(); success = false; } check("getMethod(\"setCharField\")", success); allTestsPassed &= success; success = false; try { getMethod = myClass.getMethod("getCharField", null); success = true; } catch (Exception e) { e.printStackTrace(); success = false; } check("getMethod(\"getCharField\")", success); allTestsPassed &= success; charVal = 'b'; setArgs[0] = new Character(charVal); success = false; try { retVal = setMethod.invoke(test, setArgs); success = (retVal == null); } catch (Exception e) { e.printStackTrace(); success = false; } check("invoke of setChar", success); allTestsPassed &= success; success = false; try { retVal = getMethod.invoke(test, getArgs); success = (((Character) retVal).charValue() == charVal); } catch (Exception e) { e.printStackTrace(); success = false; } check("invoke of getChar", success); allTestsPassed &= success; // set/getShortField setArgTypes[0] = Short.TYPE; success = false; try { setMethod = myClass.getMethod("setShortField", setArgTypes); success = true; } catch (Exception e) { e.printStackTrace(); success = false; } check("getMethod(\"setShortField\")", success); allTestsPassed &= success; success = false; try { getMethod = myClass.getMethod("getShortField", null); success = true; } catch (Exception e) { e.printStackTrace(); success = false; } check("getMethod(\"getShortField\")", success); allTestsPassed &= success; shortVal = 7; setArgs[0] = new Short(shortVal); success = false; try { retVal = setMethod.invoke(test, setArgs); success = (retVal == null); } catch (Exception e) { e.printStackTrace(); success = false; } check("invoke of setShort", success); allTestsPassed &= success; success = false; try { retVal = getMethod.invoke(test, getArgs); success = (((Short) retVal).shortValue() == shortVal); } catch (Exception e) { e.printStackTrace(); success = false; } check("invoke of getShort", success); allTestsPassed &= success; // set/getIntField setArgTypes[0] = Integer.TYPE; success = false; try { setMethod = myClass.getMethod("setIntField", setArgTypes); success = true; } catch (Exception e) { e.printStackTrace(); success = false; } check("getMethod(\"setIntField\")", success); allTestsPassed &= success; success = false; try { getMethod = myClass.getMethod("getIntField", null); success = true; } catch (Exception e) { e.printStackTrace(); success = false; } check("getMethod(\"getIntField\")", success); allTestsPassed &= success; intVal = 1; setArgs[0] = new Integer(intVal); success = false; try { retVal = setMethod.invoke(test, setArgs); success = (retVal == null); } catch (Exception e) { e.printStackTrace(); success = false; } check("invoke of setInt", success); allTestsPassed &= success; success = false; try { retVal = getMethod.invoke(test, getArgs); success = (((Integer) retVal).intValue() == intVal); } catch (Exception e) { e.printStackTrace(); success = false; } check("invoke of getInt", success); allTestsPassed &= success; // set/getFloatField setArgTypes[0] = Float.TYPE; success = false; try { setMethod = myClass.getMethod("setFloatField", setArgTypes); success = true; } catch (Exception e) { e.printStackTrace(); success = false; } check("getMethod(\"setFloatField\")", success); allTestsPassed &= success; success = false; try { getMethod = myClass.getMethod("getFloatField", null); success = true; } catch (Exception e) { e.printStackTrace(); success = false; } check("getMethod(\"getFloatField\")", success); allTestsPassed &= success; floatVal = 2.0f; setArgs[0] = new Float(floatVal); success = false; try { retVal = setMethod.invoke(test, setArgs); success = (retVal == null); } catch (Exception e) { e.printStackTrace(); success = false; } check("invoke of setFloat", success); allTestsPassed &= success; success = false; try { retVal = getMethod.invoke(test, getArgs); success = (((Float) retVal).floatValue() == floatVal); } catch (Exception e) { e.printStackTrace(); success = false; } check("invoke of getFloat", success); allTestsPassed &= success; // set/getDoubleField setArgTypes[0] = Double.TYPE; success = false; try { setMethod = myClass.getMethod("setDoubleField", setArgTypes); success = true; } catch (Exception e) { e.printStackTrace();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?