|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 | +import java.beans.IntrospectionException; |
| 25 | +import java.beans.Introspector; |
| 26 | +import java.lang.ref.Reference; |
| 27 | +import java.lang.ref.WeakReference; |
| 28 | +import java.net.URL; |
| 29 | +import java.net.URLClassLoader; |
| 30 | + |
| 31 | +/** |
| 32 | + * @test |
| 33 | + * @bug 8231454 |
| 34 | + * @summary Tests cache cleanup by the Introspector.flushXXX() |
| 35 | + */ |
| 36 | +public final class FlushClassInfoCache { |
| 37 | + |
| 38 | + public static void main(String[] args) throws Exception { |
| 39 | + verify(getLoader("testClass")); |
| 40 | + verify(getLoader("testAll")); |
| 41 | + Reference<ClassLoader> loader = getLoader("test"); |
| 42 | + // Clear the cache in com.sun.beans.introspect.ClassInfo::CACHE |
| 43 | + Introspector.flushCaches(); |
| 44 | + verify(loader); |
| 45 | + } |
| 46 | + |
| 47 | + private static void verify(Reference<?> loader) throws Exception { |
| 48 | + int attempt = 0; |
| 49 | + while (loader.get() != null) { |
| 50 | + if (++attempt > 10) { |
| 51 | + throw new RuntimeException("Too many attempts: " + attempt); |
| 52 | + } |
| 53 | + // Cannot generate OOM here, it will clear the CACHE as well |
| 54 | + System.gc(); |
| 55 | + Thread.sleep(1000); |
| 56 | + System.out.println("Not freed :("); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static void test() { |
| 61 | + try { |
| 62 | + Introspector.getBeanInfo(FlushClassInfoCache.class); |
| 63 | + } catch (IntrospectionException e) { |
| 64 | + throw new RuntimeException(e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static void testClass() { |
| 69 | + try { |
| 70 | + Introspector.getBeanInfo(FlushClassInfoCache.class); |
| 71 | + // Clear the cache in com.sun.beans.introspect.ClassInfo::CACHE |
| 72 | + Introspector.flushFromCaches(FlushClassInfoCache.class); |
| 73 | + } catch (IntrospectionException e) { |
| 74 | + throw new RuntimeException(e); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static void testAll() { |
| 79 | + try { |
| 80 | + Introspector.getBeanInfo(FlushClassInfoCache.class); |
| 81 | + // Clear the cache in com.sun.beans.introspect.ClassInfo::CACHE |
| 82 | + Introspector.flushCaches(); |
| 83 | + } catch (IntrospectionException e) { |
| 84 | + throw new RuntimeException(e); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static Reference<ClassLoader> getLoader(String m) throws Exception { |
| 89 | + URL url = FlushClassInfoCache.class.getProtectionDomain() |
| 90 | + .getCodeSource().getLocation(); |
| 91 | + URLClassLoader loader = new URLClassLoader(new URL[]{url}, null); |
| 92 | + Class<?> cls = Class.forName("FlushClassInfoCache", true, loader); |
| 93 | + cls.getDeclaredMethod(m).invoke(null); |
| 94 | + loader.close(); |
| 95 | + return new WeakReference<>(loader); |
| 96 | + } |
| 97 | +} |
0 commit comments