Path: blob/master/test/jdk/java/beans/Introspector/Test5102804.java
41149 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 510280426* @summary Tests memory leak27* @author Sergey Malenkov28* @run main/othervm -ms16m -mx16m Test510280429*/3031import java.beans.BeanInfo;32import java.beans.IntrospectionException;33import java.beans.Introspector;34import java.beans.PropertyDescriptor;35import java.beans.SimpleBeanInfo;36import java.lang.ref.Reference;37import java.lang.ref.WeakReference;38import java.net.URL;39import java.net.URLClassLoader;4041public class Test5102804 {42private static final String BEAN_NAME = "Test5102804$Example";43private static final String BEAN_INFO_NAME = BEAN_NAME + "BeanInfo";4445public static void main(String[] args) {46if (!isCollectible(getReference()))47throw new Error("Reference is not collected");48}4950private static Reference getReference() {51try {52ClassLoader loader = new Loader();53Class type = Class.forName(BEAN_NAME, true, loader);54if (!type.getClassLoader().equals(loader)) {55throw new Error("Wrong class loader");56}57BeanInfo info = Introspector.getBeanInfo(type);58if (0 != info.getDefaultPropertyIndex()) {59throw new Error("Wrong bean info found");60}61return new WeakReference<Class>(type);62}63catch (IntrospectionException exception) {64throw new Error("Introspection Error", exception);65}66catch (ClassNotFoundException exception) {67throw new Error("Class Not Found", exception);68}69}7071private static boolean isCollectible(Reference reference) {72int[] array = new int[10];73while (true) {74try {75array = new int[array.length + array.length / 3];76}77catch (OutOfMemoryError error) {78return null == reference.get();79}80}81}8283/**84* Custom class loader to load the Example class by itself.85* Could also load it from a different code source, but this is easier to set up.86*/87private static final class Loader extends URLClassLoader {88Loader() {89super(new URL[] {90Test5102804.class.getProtectionDomain().getCodeSource().getLocation()91});92}9394@Override95protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {96Class c = findLoadedClass(name);97if (c == null) {98if (BEAN_NAME.equals(name) || BEAN_INFO_NAME.equals(name)) {99c = findClass(name);100}101else try {102c = getParent().loadClass(name);103}104catch (ClassNotFoundException exception) {105c = findClass(name);106}107}108if (resolve) {109resolveClass(c);110}111return c;112}113}114115/**116* A simple bean to load from the Loader class, not main class loader.117*/118public static final class Example {119private int value;120121public int getValue() {122return value;123}124125public void setValue(int value) {126this.value = value;127}128}129130/**131* The BeanInfo for the Example class.132* It is also loaded from the Loader class.133*/134public static final class ExampleBeanInfo extends SimpleBeanInfo {135@Override136public int getDefaultPropertyIndex() {137return 0;138}139140@Override141public PropertyDescriptor[] getPropertyDescriptors() {142try {143return new PropertyDescriptor[] {144new PropertyDescriptor("value", Class.forName(BEAN_NAME))145};146}147catch (ClassNotFoundException exception) {148return null;149}150catch (IntrospectionException exception) {151return null;152}153}154}155}156157158