KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > bytecode > JClassLoader


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.bytecode;
31
32 import java.lang.ref.SoftReference JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 /**
37  * Manages an introspected java classes.
38  */

39 abstract public class JClassLoader {
40   protected static final Logger JavaDoc log =
41     Logger.getLogger(JClassLoader.class.getName());
42   
43   private static JClassLoaderWrapper _staticClassLoader;
44
45   private static final HashMap JavaDoc<String JavaDoc,JClass> _staticClassMap =
46     new HashMap JavaDoc<String JavaDoc,JClass>();
47   
48   private final HashMap JavaDoc<String JavaDoc,SoftReference JavaDoc<JClass>> _classMap =
49     new HashMap JavaDoc<String JavaDoc,SoftReference JavaDoc<JClass>>();
50
51   /**
52    * Returns the matching JClass.
53    */

54   public JClass forName(String JavaDoc name)
55   {
56     SoftReference JavaDoc<JClass> jClassRef = _classMap.get(name);
57
58     JClass jClass = jClassRef != null ? jClassRef.get() : null;
59
60     if (jClass == null) {
61       jClass = _staticClassMap.get(name);
62
63       if (jClass == null) {
64     if (name.startsWith("[")) {
65       JClass subClass = descriptorToClass(name, 1);
66       jClass = new JClassArray(subClass);
67     }
68     else
69       jClass = loadClass(name);
70       }
71       
72       _classMap.put(name, new SoftReference JavaDoc<JClass>(jClass));
73     }
74
75     return jClass;
76   }
77
78   /**
79    * Closes the class loader.
80    */

81   public void close()
82   {
83     _classMap.clear();
84   }
85
86   /**
87    * Returns the matching JClass.
88    */

89   public static JClass systemForName(String JavaDoc name)
90   {
91     return getSystemClassLoader().forName(name);
92   }
93
94   /**
95    * Returns the wrapped system class loader.
96    */

97   static JClassLoader getSystemClassLoader()
98   {
99     if (_staticClassLoader == null)
100       _staticClassLoader = JClassLoaderWrapper.create(ClassLoader.getSystemClassLoader());
101
102     return _staticClassLoader;
103   }
104
105   /**
106    * Returns the matching JClass.
107    */

108   public static JClass localForName(String JavaDoc name)
109   {
110     JClassLoaderWrapper jLoader = JClassLoaderWrapper.create();
111     
112     return jLoader.forName(name);
113   }
114
115   /**
116    * Loads the class.
117    */

118   abstract protected JClass loadClass(String JavaDoc name);
119
120   /**
121    * Returns the static class loader.
122    */

123   public static JClassLoader getStaticClassLoader()
124   {
125     return getSystemClassLoader();
126   }
127
128   public JClass descriptorToClass(String JavaDoc name, int i)
129   {
130     switch (name.charAt(i)) {
131     case 'V': return forName("void");
132     case 'Z': return forName("boolean");
133     case 'C': return forName("char");
134     case 'B': return forName("byte");
135     case 'S': return forName("short");
136     case 'I': return forName("int");
137     case 'J': return forName("long");
138     case 'F': return forName("float");
139     case 'D': return forName("double");
140     case '[':
141       return forName(name.substring(i));
142       
143     case 'L':
144       {
145     int tail = name.indexOf(';', i);
146
147     if (tail < 0)
148       throw new IllegalStateException JavaDoc(name);
149     
150     String JavaDoc className = name.substring(i + 1, tail).replace('/', '.');
151
152     return forName(className);
153       }
154       
155     default:
156       throw new UnsupportedOperationException JavaDoc(name.substring(i));
157     }
158   }
159
160   public String JavaDoc toString()
161   {
162     return "JClassLoader[]";
163   }
164
165   static {
166     _staticClassMap.put("void", JClass.VOID);
167     _staticClassMap.put("boolean", JClass.BOOLEAN);
168     _staticClassMap.put("byte", JClass.BYTE);
169     _staticClassMap.put("short", JClass.SHORT);
170     _staticClassMap.put("int", JClass.INT);
171     _staticClassMap.put("long", JClass.LONG);
172     _staticClassMap.put("float", JClass.FLOAT);
173     _staticClassMap.put("double", JClass.DOUBLE);
174     _staticClassMap.put("char", JClass.CHAR);
175     _staticClassMap.put("java.lang.String", JClass.STRING);
176     _staticClassMap.put("java.lang.Object", JClass.OBJECT);
177   }
178 }
179
Popular Tags