Path: blob/master/test/jdk/java/beans/Introspector/FlushClassInfoCache.java
41149 views
/*1* Copyright (c) 2020, 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*/2223import java.beans.IntrospectionException;24import java.beans.Introspector;25import java.lang.ref.Reference;26import java.lang.ref.WeakReference;27import java.net.URL;28import java.net.URLClassLoader;2930/**31* @test32* @bug 823145433* @summary Tests cache cleanup by the Introspector.flushXXX()34*/35public final class FlushClassInfoCache {3637public static void main(String[] args) throws Exception {38verify(getLoader("testClass"));39verify(getLoader("testAll"));40Reference<ClassLoader> loader = getLoader("test");41// Clear the cache in com.sun.beans.introspect.ClassInfo::CACHE42Introspector.flushCaches();43verify(loader);44}4546private static void verify(Reference<?> loader) throws Exception {47int attempt = 0;48while (loader.get() != null) {49if (++attempt > 10) {50throw new RuntimeException("Too many attempts: " + attempt);51}52// Cannot generate OOM here, it will clear the CACHE as well53System.gc();54Thread.sleep(1000);55System.out.println("Not freed :(");56}57}5859public static void test() {60try {61Introspector.getBeanInfo(FlushClassInfoCache.class);62} catch (IntrospectionException e) {63throw new RuntimeException(e);64}65}6667public static void testClass() {68try {69Introspector.getBeanInfo(FlushClassInfoCache.class);70// Clear the cache in com.sun.beans.introspect.ClassInfo::CACHE71Introspector.flushFromCaches(FlushClassInfoCache.class);72} catch (IntrospectionException e) {73throw new RuntimeException(e);74}75}7677public static void testAll() {78try {79Introspector.getBeanInfo(FlushClassInfoCache.class);80// Clear the cache in com.sun.beans.introspect.ClassInfo::CACHE81Introspector.flushCaches();82} catch (IntrospectionException e) {83throw new RuntimeException(e);84}85}8687private static Reference<ClassLoader> getLoader(String m) throws Exception {88URL url = FlushClassInfoCache.class.getProtectionDomain()89.getCodeSource().getLocation();90URLClassLoader loader = new URLClassLoader(new URL[]{url}, null);91Class<?> cls = Class.forName("FlushClassInfoCache", true, loader);92cls.getDeclaredMethod(m).invoke(null);93loader.close();94return new WeakReference<>(loader);95}96}979899