1/*
2 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4508780
27 * @summary Tests shared access to the Introspector cache
28 * @author Mark Davidson
29 */
30
31import java.beans.IntrospectionException;
32import java.beans.Introspector;
33import java.beans.PropertyDescriptor;
34
35import java.lang.reflect.Method;
36
37/**
38 * Multiple classloader test to ensure that methods
39 * returned by the BeanInfo classes are unique to the classloader.
40 */
41public class Test4508780 implements Runnable {
42    /**
43     * This is here to force the bean classes to be compiled
44     */
45    private static final Class[] COMPILE = {
46            Bean.class,
47            Bean2.class,
48            Bean3.class,
49            Bean4.class,
50    };
51
52    public static void main(String[] args) {
53        for (int i = 0; i < 10; i++) test();
54    }
55
56    private static void test() {
57        test("Bean", "Bean2", "Bean3", "Bean4");
58        test("Bean4", "Bean", "Bean2", "Bean3");
59        test("Bean3", "Bean4", "Bean", "Bean2");
60        test("Bean2", "Bean3", "Bean4", "Bean");
61        Introspector.flushCaches();
62    }
63
64    private static void test(String... names) {
65        new Thread(new Test4508780(names)).start();
66    }
67
68    private final ClassLoader loader = new SimpleClassLoader();
69    private final String[] names;
70
71    private Test4508780(String... names) {
72        this.names = names;
73    }
74
75    public void run() {
76        for (String name : this.names) {
77            Object bean;
78            try {
79                bean = this.loader.loadClass(name).newInstance();
80            } catch (Exception exception) {
81                throw new Error("could not instantiate bean: " + name, exception);
82            }
83            if (this.loader != bean.getClass().getClassLoader()) {
84                throw new Error("bean class loader is not equal to default one");
85            }
86            PropertyDescriptor[] pds = getPropertyDescriptors(bean);
87            for (PropertyDescriptor pd : pds) {
88                Class type = pd.getPropertyType();
89                Method setter = pd.getWriteMethod();
90                Method getter = pd.getReadMethod();
91
92                if (type.equals(String.class)) {
93                    executeMethod(setter, bean, "Foo");
94                } else if (type.equals(int.class)) {
95                    executeMethod(setter, bean, Integer.valueOf(1));
96                }
97                executeMethod(getter, bean);
98            }
99        }
100    }
101
102    private static void executeMethod(Method method, Object bean, Object... args) {
103        if (method == null) {
104            throw new Error("method is null");
105        }
106        if (bean == null) {
107            throw new Error("target bean is null");
108        }
109        try {
110            method.invoke(bean, args);
111        } catch (Exception exception) {
112            throw new Error("could not execute method: " + method, exception);
113        }
114    }
115
116    private static PropertyDescriptor[] getPropertyDescriptors(Object object) {
117        Class type = object.getClass();
118        synchronized (System.out) {
119            System.out.println(type);
120            ClassLoader loader = type.getClassLoader();
121            while (loader != null) {
122                System.out.println(" - loader: " + loader);
123                loader = loader.getParent();
124            }
125        }
126        try {
127            return Introspector.getBeanInfo(type).getPropertyDescriptors();
128        } catch (IntrospectionException exception) {
129            throw new Error("unexpected exception", exception);
130        }
131    }
132}
133