12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2006 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
165d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
175d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaoimport java.lang.reflect.*;
185d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaoimport java.io.IOException;
195d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaoimport java.util.Collections;
20741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.util.ArrayList;
21741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.util.List;
22741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.util.Map;
235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao/**
255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Reflection test.
265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */
275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaopublic class Main {
28741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    private static boolean FULL_ACCESS_CHECKS = false;  // b/5861201
29741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    public Main() {}
30741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    public Main(ArrayList<Integer> stuff) {}
31741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    void printMethodInfo(Method meth) {
335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        Class[] params, exceptions;
345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        int i;
355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
365d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("Method name is " + meth.getName());
375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println(" Declaring class is "
385d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            + meth.getDeclaringClass().getName());
395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        params = meth.getParameterTypes();
405d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        for (i = 0; i < params.length; i++)
415d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println(" Arg " + i + ": " + params[i].getName());
425d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        exceptions = meth.getExceptionTypes();
435d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        for (i = 0; i < exceptions.length; i++)
445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println(" Exc " + i + ": " + exceptions[i].getName());
455d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println(" Return type is " + meth.getReturnType().getName());
465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println(" Access flags are 0x"
475d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            + Integer.toHexString(meth.getModifiers()));
485d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        //System.out.println(" GenericStr is " + meth.toGenericString());
495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    void printFieldInfo(Field field) {
525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("Field name is " + field.getName());
535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println(" Declaring class is "
545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            + field.getDeclaringClass().getName());
555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println(" Field type is " + field.getType().getName());
565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println(" Access flags are 0x"
575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            + Integer.toHexString(field.getModifiers()));
585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    private void showStrings(Target instance)
615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        throws NoSuchFieldException, IllegalAccessException {
625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
635d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        Class target = Target.class;
645d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        String one, two, three, four;
655d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        Field field = null;
665d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
675d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        field = target.getField("string1");
685d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        one = (String) field.get(instance);
695d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
705d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        field = target.getField("string2");
715d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        two = (String) field.get(instance);
725d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
735d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        field = target.getField("string3");
745d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        three = (String) field.get(instance);
755d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
765d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("  ::: " + one + ":" + two + ":" + three);
775d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
785d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
79741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    public static void checkAccess() {
80741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        try {
81741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            Class target = otherpackage.Other.class;
82741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            Object instance = new otherpackage.Other();
83741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            Method meth;
84741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
85741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            meth = target.getMethod("publicMethod", (Class[]) null);
86741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            meth.invoke(instance);
87741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
88741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            try {
89741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                meth = target.getMethod("packageMethod", (Class[]) null);
90741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                System.err.println("succeeded on package-scope method");
91741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            } catch (NoSuchMethodException nsme) {
92741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                // good
93741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            }
94741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
95741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
96741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            instance = otherpackage.Other.getInnerClassInstance();
97741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            target = instance.getClass();
98741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            meth = target.getMethod("innerMethod", (Class[]) null);
99741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            try {
100741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                if (!FULL_ACCESS_CHECKS) { throw new IllegalAccessException(); }
101741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                meth.invoke(instance);
102741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                System.err.println("inner-method invoke unexpectedly worked");
103741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            } catch (IllegalAccessException iae) {
104741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                // good
105741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            }
106741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
107741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            Field field = target.getField("innerField");
108741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            try {
109741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                int x = field.getInt(instance);
110741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                if (!FULL_ACCESS_CHECKS) { throw new IllegalAccessException(); }
111741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                System.err.println("field get unexpectedly worked: " + x);
112741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            } catch (IllegalAccessException iae) {
113741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                // good
114741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            }
115741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        } catch (Exception ex) {
116741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            System.out.println("----- unexpected exception -----");
117741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            ex.printStackTrace();
118741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        }
119741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    }
120741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
1215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public void run() {
1225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        Class target = Target.class;
1235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        Method meth = null;
1245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        Field field = null;
1255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        boolean excep;
1265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        try {
1285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            meth = target.getMethod("myMethod", new Class[] { int.class });
1295d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            if (meth.getDeclaringClass() != target)
1315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                throw new RuntimeException();
1325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            printMethodInfo(meth);
1335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            meth = target.getMethod("myMethod", new Class[] { float.class });
1355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            printMethodInfo(meth);
1365d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            meth = target.getMethod("myNoargMethod", (Class[]) null);
1385d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            printMethodInfo(meth);
1395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1405d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            meth = target.getMethod("myMethod",
1415d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                new Class[] { String[].class, float.class, char.class });
1425d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            printMethodInfo(meth);
1435d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            Target instance = new Target();
1455d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            Object[] argList = new Object[] {
1465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                new String[] { "hi there" },
1475d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                new Float(3.1415926f),
1485d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                new Character('Q')
1495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            };
1505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("Before, float is "
1515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                + ((Float)argList[1]).floatValue());
1525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            Integer boxval;
1545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            boxval = (Integer) meth.invoke(instance, argList);
1555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("Result of invoke: " + boxval.intValue());
1565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("Calling no-arg void-return method");
1585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            meth = target.getMethod("myNoargMethod", (Class[]) null);
1595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            meth.invoke(instance, (Object[]) null);
1605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            /* try invoking a method that throws an exception */
1625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            meth = target.getMethod("throwingMethod", (Class[]) null);
1635d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
1645d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                meth.invoke(instance, (Object[]) null);
1655d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("GLITCH: didn't throw");
1665d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            } catch (InvocationTargetException ite) {
1675d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("Invoke got expected exception:");
1685d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println(ite.getClass().getName());
1695d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println(ite.getCause());
1705d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
1715d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            catch (Exception ex) {
1725d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("GLITCH: invoke got wrong exception:");
1735d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                ex.printStackTrace();
1745d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
1755d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("");
1765d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1775d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1785d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field = target.getField("string1");
1795d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            if (field.getDeclaringClass() != target)
1805d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                throw new RuntimeException();
1815d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            printFieldInfo(field);
1825d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            String strVal = (String) field.get(instance);
1835d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("  string1 value is '" + strVal + "'");
1845d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1855d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            showStrings(instance);
1865d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1875d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field.set(instance, new String("a new string"));
1885d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            strVal = (String) field.get(instance);
1895d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("  string1 value is now '" + strVal + "'");
1905d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1915d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            showStrings(instance);
1925d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
1935d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
1945d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                field.set(instance, new Object());
1955d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("WARNING: able to store Object into String");
1965d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
1975d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            catch (IllegalArgumentException iae) {
1985d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("  got expected illegal obj store exc");
1995d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2005d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
2015d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
2025d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
2035d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                String four;
2045d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                field = target.getField("string4");
2055d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                four = (String) field.get(instance);
2065d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("WARNING: able to access string4: "
2075d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                    + four);
2085d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2095d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            catch (IllegalAccessException iae) {
2105d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("  got expected access exc");
2115d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2125d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            catch (NoSuchFieldException nsfe) {
2135d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("  got the other expected access exc");
2145d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2155d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
2165d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                String three;
2175d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                field = target.getField("string3");
2185d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                three = (String) field.get(this);
2195d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("WARNING: able to get string3 in wrong obj: "
2205d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                    + three);
2215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            catch (IllegalArgumentException iae) {
2235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("  got expected arg exc");
2245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
2265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            /*
2275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao             * Try setting a field to null.
2285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao             */
2295d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            String four;
2305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field = target.getDeclaredField("string3");
2315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field.set(instance, null);
2325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
2335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            /*
2345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao             * Do some stuff with long.
2355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao             */
2365d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            long longVal;
2375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field = target.getField("pubLong");
2385d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            longVal = field.getLong(instance);
2395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("pubLong initial value is " +
2405d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                Long.toHexString(longVal));
2415d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field.setLong(instance, 0x9988776655443322L);
2425d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            longVal = field.getLong(instance);
2435d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("pubLong new value is " +
2445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                Long.toHexString(longVal));
2455d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
2465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
2475d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field = target.getField("superInt");
2485d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            if (field.getDeclaringClass() == target)
2495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                throw new RuntimeException();
2505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            printFieldInfo(field);
2515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            int intVal = field.getInt(instance);
2525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("  superInt value is " + intVal);
2535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            Integer boxedIntVal = (Integer) field.get(instance);
2545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("  superInt boxed is " + boxedIntVal);
2555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
2565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field.set(instance, new Integer(20202));
2575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            intVal = field.getInt(instance);
2585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("  superInt value is now " + intVal);
2595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field.setShort(instance, (short)30303);
2605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            intVal = field.getInt(instance);
2615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("  superInt value (from short) is now " +intVal);
2625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field.setInt(instance, 40404);
2635d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            intVal = field.getInt(instance);
2645d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("  superInt value is now " + intVal);
2655d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
2665d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                field.set(instance, new Long(123));
2675d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("FAIL: expected exception not thrown");
2685d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2695d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            catch (IllegalArgumentException iae) {
2705d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("  got expected long->int failure");
2715d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2725d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
2735d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                field.setLong(instance, 123);
2745d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("FAIL: expected exception not thrown");
2755d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2765d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            catch (IllegalArgumentException iae) {
2775d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("  got expected long->int failure");
2785d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2795d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
2805d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                field.set(instance, new String("abc"));
2815d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("FAIL: expected exception not thrown");
2825d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2835d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            catch (IllegalArgumentException iae) {
2845d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("  got expected string->int failure");
2855d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2865d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
2875d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
2885d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                field.getShort(instance);
2895d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("FAIL: expected exception not thrown");
2905d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2915d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            catch (IllegalArgumentException iae) {
2925d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("  got expected int->short failure");
2935d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
2945d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
2955d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field = target.getField("superClassInt");
2965d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            printFieldInfo(field);
2975d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            int superClassIntVal = field.getInt(instance);
2985d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("  superClassInt value is " + superClassIntVal);
2995d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
3005d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field = target.getField("staticDouble");
3015d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            printFieldInfo(field);
3025d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            double staticDoubleVal = field.getDouble(null);
3035d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("  staticDoubleVal value is " + staticDoubleVal);
3045d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
3055d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
3065d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                field.getLong(instance);
3075d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("FAIL: expected exception not thrown");
3085d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
3095d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            catch (IllegalArgumentException iae) {
3105d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("  got expected double->long failure");
3115d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
3125d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
3135d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            excep = false;
3145d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
3155d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                field = target.getField("aPrivateInt");
3165d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                printFieldInfo(field);
3175d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
3185d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            catch (NoSuchFieldException nsfe) {
3195d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("as expected: aPrivateInt not found");
3205d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                excep = true;
3215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
3225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            if (!excep)
3235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                System.out.println("BUG: got aPrivateInt");
3245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
3255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
3265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field = target.getField("constantString");
3275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            printFieldInfo(field);
3285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            String val = (String) field.get(instance);
3295d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("  Constant test value is " + val);
3305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
3315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
3325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field = target.getField("cantTouchThis");
3335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            printFieldInfo(field);
3345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            intVal = field.getInt(instance);
3355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("  cantTouchThis is " + intVal);
3365d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            try {
3375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                field.setInt(instance, 99);
33811d5d8fffe41cc7daadbfa2ca98ecb978f3029afJeff Hao                System.out.println("ERROR: set-final did not throw exception");
3395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            } catch (IllegalAccessException iae) {
34011d5d8fffe41cc7daadbfa2ca98ecb978f3029afJeff Hao                System.out.println("  as expected: set-final throws exception");
3415d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            }
3425d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            intVal = field.getInt(instance);
34311d5d8fffe41cc7daadbfa2ca98ecb978f3029afJeff Hao            System.out.println("  cantTouchThis is still " + intVal);
3445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
345ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            System.out.println("  " + field + " accessible=" + field.isAccessible());
3465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field.setAccessible(true);
347ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            System.out.println("  " + field + " accessible=" + field.isAccessible());
3485d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field.setInt(instance, 87);     // exercise int version
349582a7d16bb0db323d8bd730beb61578aa3765f43Elliott Hughes            intVal = field.getInt(instance);
350582a7d16bb0db323d8bd730beb61578aa3765f43Elliott Hughes            System.out.println("  cantTouchThis is now " + intVal);
3515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            field.set(instance, 88);        // exercise Object version
3525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            intVal = field.getInt(instance);
3535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("  cantTouchThis is now " + intVal);
3545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
3555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            Constructor<Target> cons;
3565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            Target targ;
3575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            Object[] args;
3585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
3595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            cons = target.getConstructor(new Class[] { int.class,float.class });
3605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            args = new Object[] { new Integer(7), new Float(3.3333) };
3615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("cons modifiers=" + cons.getModifiers());
3625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            targ = cons.newInstance(args);
3635d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            targ.myMethod(17);
3645d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
36563f5b9e8f660ae761901072821ece30d87891644Jeff Hao            try {
36663f5b9e8f660ae761901072821ece30d87891644Jeff Hao                Thrower thrower = Thrower.class.newInstance();
36763f5b9e8f660ae761901072821ece30d87891644Jeff Hao                System.out.println("ERROR: Class.newInstance did not throw exception");
36863f5b9e8f660ae761901072821ece30d87891644Jeff Hao            } catch (UnsupportedOperationException uoe) {
36963f5b9e8f660ae761901072821ece30d87891644Jeff Hao                System.out.println("got expected exception for Class.newInstance");
37063f5b9e8f660ae761901072821ece30d87891644Jeff Hao            } catch (Exception e) {
37163f5b9e8f660ae761901072821ece30d87891644Jeff Hao                System.out.println("ERROR: Class.newInstance got unexpected exception: " +
37263f5b9e8f660ae761901072821ece30d87891644Jeff Hao                                   e.getClass().getName());
37363f5b9e8f660ae761901072821ece30d87891644Jeff Hao            }
37463f5b9e8f660ae761901072821ece30d87891644Jeff Hao
37563f5b9e8f660ae761901072821ece30d87891644Jeff Hao            try {
37663f5b9e8f660ae761901072821ece30d87891644Jeff Hao                Constructor<Thrower> constructor = Thrower.class.getDeclaredConstructor();
37763f5b9e8f660ae761901072821ece30d87891644Jeff Hao                Thrower thrower = constructor.newInstance();
37863f5b9e8f660ae761901072821ece30d87891644Jeff Hao                System.out.println("ERROR: Constructor.newInstance did not throw exception");
37963f5b9e8f660ae761901072821ece30d87891644Jeff Hao            } catch (InvocationTargetException ite) {
38063f5b9e8f660ae761901072821ece30d87891644Jeff Hao                System.out.println("got expected exception for Constructor.newInstance");
38163f5b9e8f660ae761901072821ece30d87891644Jeff Hao            } catch (Exception e) {
38263f5b9e8f660ae761901072821ece30d87891644Jeff Hao                System.out.println("ERROR: Constructor.newInstance got unexpected exception: " +
38363f5b9e8f660ae761901072821ece30d87891644Jeff Hao                                   e.getClass().getName());
38463f5b9e8f660ae761901072821ece30d87891644Jeff Hao            }
38563f5b9e8f660ae761901072821ece30d87891644Jeff Hao
386741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        } catch (Exception ex) {
3875d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("----- unexpected exception -----");
3885d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            ex.printStackTrace();
3895d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        }
3905d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
3915d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("ReflectTest done!");
3925d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
3935d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
3945d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public static void checkType() {
3955d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        Method m;
3965d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
3975d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        try {
3985d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            m = Collections.class.getDeclaredMethod("checkType",
3995d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao                            Object.class, Class.class);
4005d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        } catch (NoSuchMethodException nsme) {
4015d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            nsme.printStackTrace();
4025d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            return;
4035d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        }
404ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        System.out.println(m + " accessible=" + m.isAccessible());
4055d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        m.setAccessible(true);
406ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        System.out.println(m + " accessible=" + m.isAccessible());
4075d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        try {
4085d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            m.invoke(null, new Object(), Object.class);
4097a549467e332398229724058c07d998d0acfb0bbJeff Hao        } catch (IllegalAccessException iae) {
4107a549467e332398229724058c07d998d0acfb0bbJeff Hao            iae.printStackTrace();
4117a549467e332398229724058c07d998d0acfb0bbJeff Hao            return;
4127a549467e332398229724058c07d998d0acfb0bbJeff Hao        } catch (InvocationTargetException ite) {
4137a549467e332398229724058c07d998d0acfb0bbJeff Hao            ite.printStackTrace();
4147a549467e332398229724058c07d998d0acfb0bbJeff Hao            return;
4157a549467e332398229724058c07d998d0acfb0bbJeff Hao        }
4167a549467e332398229724058c07d998d0acfb0bbJeff Hao
4177a549467e332398229724058c07d998d0acfb0bbJeff Hao        try {
4187a549467e332398229724058c07d998d0acfb0bbJeff Hao            String s = "Should be ignored";
4197a549467e332398229724058c07d998d0acfb0bbJeff Hao            m.invoke(s, new Object(), Object.class);
4205d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        } catch (IllegalAccessException iae) {
4215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            iae.printStackTrace();
4225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            return;
4235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        } catch (InvocationTargetException ite) {
4245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            ite.printStackTrace();
4255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            return;
4265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        }
4275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
4285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        try {
4295d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("checkType invoking null");
4305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            m.invoke(null, new Object(), int.class);
4315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("ERROR: should throw InvocationTargetException");
4325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        } catch (InvocationTargetException ite) {
4335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            System.out.println("checkType got expected exception");
4345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        } catch (IllegalAccessException iae) {
4355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            iae.printStackTrace();
4365d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            return;
4375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        }
4385d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
4395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
440923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes    public static void checkClinitForFields() throws Exception {
441923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      // Loading a class constant shouldn't run <clinit>.
442923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      System.out.println("calling const-class FieldNoisyInitUser.class");
443923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      Class niuClass = FieldNoisyInitUser.class;
444923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      System.out.println("called const-class FieldNoisyInitUser.class");
445923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes
446923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      // Getting the declared fields doesn't run <clinit>.
447923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      Field[] fields = niuClass.getDeclaredFields();
448923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      System.out.println("got fields");
449923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes
450923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      Field field = niuClass.getField("staticField");
451923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      System.out.println("got field");
452923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      field.get(null);
453923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      System.out.println("read field value");
454923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes
455923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      // FieldNoisyInitUser should now be initialized, but FieldNoisyInit shouldn't be initialized yet.
456923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      FieldNoisyInitUser niu = new FieldNoisyInitUser();
457923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      FieldNoisyInit ni = new FieldNoisyInit();
458923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes
459923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      System.out.println("");
460923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes    }
461923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes
462923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes    public static void checkClinitForMethods() throws Exception {
463923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      // Loading a class constant shouldn't run <clinit>.
464923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      System.out.println("calling const-class MethodNoisyInitUser.class");
465923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      Class niuClass = MethodNoisyInitUser.class;
466923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      System.out.println("called const-class MethodNoisyInitUser.class");
467923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes
468923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      // Getting the declared methods doesn't run <clinit>.
469923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      Method[] methods = niuClass.getDeclaredMethods();
470923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      System.out.println("got methods");
471923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes
472923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      Method method = niuClass.getMethod("staticMethod", (Class[]) null);
473923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      System.out.println("got method");
474923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      method.invoke(null);
475923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      System.out.println("invoked method");
476923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes
477923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      // MethodNoisyInitUser should now be initialized, but MethodNoisyInit shouldn't be initialized yet.
478923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      MethodNoisyInitUser niu = new MethodNoisyInitUser();
479923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      MethodNoisyInit ni = new MethodNoisyInit();
480923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes
481923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes      System.out.println("");
482741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    }
483741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
484741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
485741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    /*
486741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes     * Test some generic type stuff.
487741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes     */
488741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    public List<String> dummy;
489741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    public Map<Integer,String> fancyMethod(ArrayList<String> blah) { return null; }
490741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    public static void checkGeneric() {
491741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        Field field;
492741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        try {
493741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            field = Main.class.getField("dummy");
494741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        } catch (NoSuchFieldException nsfe) {
495741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            throw new RuntimeException(nsfe);
496741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        }
497741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        Type listType = field.getGenericType();
498741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        System.out.println("generic field: " + listType);
499741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
500741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        Method method;
501741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        try {
502741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            method = Main.class.getMethod("fancyMethod",
503741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                new Class[] { ArrayList.class });
504741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        } catch (NoSuchMethodException nsme) {
505741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            throw new RuntimeException(nsme);
506741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        }
507741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        Type[] parmTypes = method.getGenericParameterTypes();
508741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        Type ret = method.getGenericReturnType();
509741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        System.out.println("generic method " + method.getName() + " params='"
510741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            + stringifyTypeArray(parmTypes) + "' ret='" + ret + "'");
511741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
512741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        Constructor ctor;
513741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        try {
514741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            ctor = Main.class.getConstructor(new Class[] { ArrayList.class });
515741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        } catch (NoSuchMethodException nsme) {
516741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            throw new RuntimeException(nsme);
517741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        }
518741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        parmTypes = ctor.getGenericParameterTypes();
519741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        System.out.println("generic ctor " + ctor.getName() + " params='"
520741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            + stringifyTypeArray(parmTypes) + "'");
521741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    }
522741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
523741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    /*
524741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes     * Convert an array of Type into a string.  Start with an array count.
525741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes     */
526741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    private static String stringifyTypeArray(Type[] types) {
527741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        StringBuilder stb = new StringBuilder();
528741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        boolean first = true;
529741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
530741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        stb.append("[" + types.length + "]");
531741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
532741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        for (Type t: types) {
533741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            if (first) {
534741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                stb.append(" ");
535741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                first = false;
536741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            } else {
537741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes                stb.append(", ");
538741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            }
539741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            stb.append(t.toString());
540741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        }
541741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
542741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        return stb.toString();
5435d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
5445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
545ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    public static void checkUnique() {
546ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        Field field1, field2;
547ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        try {
548ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            field1 = Main.class.getField("dummy");
549ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            field2 = Main.class.getField("dummy");
550ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        } catch (NoSuchFieldException nsfe) {
551ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            throw new RuntimeException(nsfe);
552ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        }
553ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        if (field1 == field2) {
554ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            System.out.println("ERROR: fields shouldn't have reference equality");
555ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        } else {
556ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            System.out.println("fields are unique");
557ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        }
558ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        if (field1.hashCode() == field2.hashCode() && field1.equals(field2)) {
559ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            System.out.println("fields are .equals");
560ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        } else {
561ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            System.out.println("ERROR: fields fail equality");
562ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        }
563ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        Method method1, method2;
564ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        try {
565ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            method1 = Main.class.getMethod("fancyMethod", new Class[] { ArrayList.class });
566ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            method2 = Main.class.getMethod("fancyMethod", new Class[] { ArrayList.class });
567ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        } catch (NoSuchMethodException nsme) {
568ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            throw new RuntimeException(nsme);
569ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        }
570ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        if (method1 == method2) {
571ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            System.out.println("ERROR: methods shouldn't have reference equality");
572ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        } else {
573ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            System.out.println("methods are unique");
574ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        }
575ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        if (method1.hashCode() == method2.hashCode() && method1.equals(method2)) {
576ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            System.out.println("methods are .equals");
577ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        } else {
578ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom            System.out.println("ERROR: methods fail equality");
579ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        }
580ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    }
581741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
582923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes    public static void main(String[] args) throws Exception {
5835d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        Main test = new Main();
5845d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        test.run();
5855d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
586741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        checkAccess();
5875d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        checkType();
588923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes        checkClinitForFields();
589923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes        checkClinitForMethods();
590741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        checkGeneric();
591ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        checkUnique();
5925d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
5935d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao}
5945d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
5955d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
5965d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaoclass SuperTarget {
5975d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public SuperTarget() {
5985d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("SuperTarget constructor ()V");
5995d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        superInt = 1010101;
6005d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        superClassInt = 1010102;
6015d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
6025d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6035d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public int myMethod(float floatArg) {
6045d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("myMethod (F)I " + floatArg);
6055d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        return 6;
6065d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
6075d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6085d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public int superInt;
6095d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public static int superClassInt;
6105d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao}
6115d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6125d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaoclass Target extends SuperTarget {
6135d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public Target() {
6145d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("Target constructor ()V");
6155d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
6165d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6175d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public Target(int ii, float ff) {
6185d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("Target constructor (IF)V : ii="
6195d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao            + ii + " ff=" + ff);
6205d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        anInt = ii;
6215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
6225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public int myMethod(int intarg) throws NullPointerException, IOException {
6245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("myMethod (I)I");
6255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println(" arg=" + intarg + " anInt=" + anInt);
6265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        return 5;
6275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
6285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6295d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public int myMethod(String[] strarg, float f, char c) {
6305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("myMethod: " + strarg[0] + " " + f + " " + c + " !");
6315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        return 7;
6325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
6335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public static void myNoargMethod() {
6355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("myNoargMethod ()V");
6365d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
6375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6385d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public void throwingMethod() {
6395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("throwingMethod");
6405d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        throw new NullPointerException("gratuitous throw!");
6415d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
6425d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6435d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public void misc() {
6445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao        System.out.println("misc");
6455d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    }
6465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6475d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public int anInt;
6485d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public String string1 = "hey";
6495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public String string2 = "yo";
6505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public String string3 = "there";
6515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    private String string4 = "naughty";
6525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public static final String constantString = "a constant string";
6535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    private int aPrivateInt;
6545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public final int cantTouchThis = 77;
6565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public long pubLong = 0x1122334455667788L;
6585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
6595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao    public static double staticDouble = 3.3;
6605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao}
6615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
662923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughesclass FieldNoisyInit {
663923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes  static {
664923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes    System.out.println("FieldNoisyInit is initializing");
665923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes    //Throwable th = new Throwable();
666923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes    //th.printStackTrace();
667923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes  }
6685d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao}
6695d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao
670923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughesclass FieldNoisyInitUser {
671923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes  static {
672923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes    System.out.println("FieldNoisyInitUser is initializing");
673923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes  }
674923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes  public static int staticField;
675923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes  public static FieldNoisyInit noisy;
676923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes}
677923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes
678923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughesclass MethodNoisyInit {
679923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes  static {
680923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes    System.out.println("MethodNoisyInit is initializing");
681923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes    //Throwable th = new Throwable();
682923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes    //th.printStackTrace();
683923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes  }
684923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes}
685923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes
686923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughesclass MethodNoisyInitUser {
687923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes  static {
688923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes    System.out.println("MethodNoisyInitUser is initializing");
689923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes  }
690923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes  public static void staticMethod() {}
691923e8b8e936ead33408e111682aa9372e3a7ed43Elliott Hughes  public void createMethodNoisyInit(MethodNoisyInit ni) {}
6925d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao}
69363f5b9e8f660ae761901072821ece30d87891644Jeff Hao
69463f5b9e8f660ae761901072821ece30d87891644Jeff Haoclass Thrower {
69563f5b9e8f660ae761901072821ece30d87891644Jeff Hao  public Thrower() throws UnsupportedOperationException {
69663f5b9e8f660ae761901072821ece30d87891644Jeff Hao    throw new UnsupportedOperationException();
69763f5b9e8f660ae761901072821ece30d87891644Jeff Hao  }
69863f5b9e8f660ae761901072821ece30d87891644Jeff Hao}
699