1 /*
   2  * Copyright 2004 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 5040740
  27  * @summary annotations cause memory leak
  28  * @author gafter
  29  *
  30  * @run shell LoaderLeak.sh
  31  */
  32 
  33 import java.net.*;
  34 import java.lang.ref.*;
  35 import java.util.*;
  36 import java.io.*;
  37 
  38 public class Main {
  39     public static void main(String[] args) throws Exception {
  40         for (int i=0; i<100; i++)
  41             doTest(args.length != 0);
  42     }
  43 
  44     static void doTest(boolean readAnn) throws Exception {
  45         // URL classes = new URL("file://" + System.getProperty("user.dir") + "/classes");
  46         // URL[] path = { classes };
  47         // URLClassLoader loader = new URLClassLoader(path);
  48         ClassLoader loader = new SimpleClassLoader();
  49         WeakReference<Class<?>> c = new WeakReference(loader.loadClass("C"));
  50         if (c.get() == null) throw new AssertionError();
  51         if (c.get().getClassLoader() != loader) throw new AssertionError();
  52         if (readAnn) System.out.println(c.get().getAnnotations()[0]);
  53         if (c.get() == null) throw new AssertionError();
  54         System.gc();
  55         System.gc();
  56         if (c.get() == null) throw new AssertionError();
  57         System.gc();
  58         System.gc();
  59         loader = null;
  60         System.gc();
  61         System.gc();
  62         if (c.get() != null) throw new AssertionError();
  63     }
  64 }
  65 
  66 class SimpleClassLoader extends ClassLoader {
  67     private Hashtable classes = new Hashtable();
  68 
  69     public SimpleClassLoader() {
  70     }
  71     private byte getClassImplFromDataBase(String className)[] {
  72         byte result[];
  73         try {
  74             FileInputStream fi = new FileInputStream("classes/"+className+".class");
  75             result = new byte[fi.available()];
  76             fi.read(result);
  77             return result;
  78         } catch (Exception e) {
  79 
  80             /*
  81              * If we caught an exception, either the class wasnt found or it
  82              * was unreadable by our process.
  83              */
  84             return null;
  85         }
  86     }
  87     public Class loadClass(String className) throws ClassNotFoundException {
  88         return (loadClass(className, true));
  89     }
  90     public synchronized Class loadClass(String className, boolean resolveIt)
  91         throws ClassNotFoundException {
  92         Class result;
  93         byte  classData[];
  94 
  95         /* Check our local cache of classes */
  96         result = (Class)classes.get(className);
  97         if (result != null) {
  98             return result;
  99         }
 100 
 101         /* Check with the primordial class loader */
 102         try {
 103             result = super.findSystemClass(className);
 104             return result;
 105         } catch (ClassNotFoundException e) {
 106         }
 107 
 108         /* Try to load it from our repository */
 109         classData = getClassImplFromDataBase(className);
 110         if (classData == null) {
 111             throw new ClassNotFoundException();
 112         }
 113 
 114         /* Define it (parse the class file) */
 115         result = defineClass(classData, 0, classData.length);
 116         if (result == null) {
 117             throw new ClassFormatError();
 118         }
 119 
 120         if (resolveIt) {
 121             resolveClass(result);
 122         }
 123 
 124         classes.put(className, result);
 125         return result;
 126     }
 127 }