• R/O
  • HTTP
  • SSH
  • HTTPS

digger: Commit

クラスファイル読み込みのためのライブラリ.


Commit MetaInfo

Revision88563952d95aea857d100c051b71ae4979893e5c (tree)
Time2012-09-01 12:49:03
AuthorHaruaki Tamada <tamada@cc.k...>
CommiterHaruaki Tamada

Log Message

fixed warning.

Change Summary

Incremental Difference

--- a/src/main/java/jp/sourceforge/stigmata/digger/ClassFileArchive.java
+++ b/src/main/java/jp/sourceforge/stigmata/digger/ClassFileArchive.java
@@ -12,33 +12,35 @@ import java.util.Iterator;
1212 * @author Haruaki TAMADA
1313 */
1414 public interface ClassFileArchive extends Iterable<ClassFileEntry>{
15+ String CLASS_FILE_EXTENSION = ".class";
16+
1517 /**
1618 * returns the location of this archive.
1719 */
18- public URL getLocation();
20+ URL getLocation();
1921
2022 /**
2123 * returns the InputStream object from given entry.
2224 */
23- public InputStream getInputStream(ClassFileEntry entry) throws IOException;
25+ InputStream getInputStream(ClassFileEntry entry) throws IOException;
2426
2527 /**
2628 * returns an entries of this archive.
2729 */
28- public Iterator<ClassFileEntry> iterator();
30+ Iterator<ClassFileEntry> iterator();
2931
3032 /**
3133 * returns this archive has given class entry or not.
3234 */
33- public boolean hasEntry(String className);
35+ boolean hasEntry(String className);
3436
3537 /**
3638 * returns an entry of given class name.
3739 */
38- public ClassFileEntry getEntry(String className) throws ClassNotFoundException;
40+ ClassFileEntry getEntry(String className) throws ClassNotFoundException;
3941
4042 /**
4143 * returns the name of this archive.
4244 */
43- public String getName();
45+ String getName();
4446 }
--- a/src/main/java/jp/sourceforge/stigmata/digger/ClassFileEntry.java
+++ b/src/main/java/jp/sourceforge/stigmata/digger/ClassFileEntry.java
@@ -22,7 +22,7 @@ public class ClassFileEntry{
2222 return className;
2323 }
2424
25- public void setLocation(URL location){
25+ public final void setLocation(URL location){
2626 this.location = location;
2727 }
2828
--- a/src/main/java/jp/sourceforge/stigmata/digger/ClasspathContext.java
+++ b/src/main/java/jp/sourceforge/stigmata/digger/ClasspathContext.java
@@ -127,33 +127,10 @@ public class ClasspathContext implements Iterable<URL>{
127127 return classpath.iterator();
128128 }
129129 else{
130- final Iterator<URL> parentIterator = parent.iterator();
131- final Iterator<URL> thisIterator = classpath.iterator();
132- return new Iterator<URL>(){
133- public boolean hasNext(){
134- boolean next = parentIterator.hasNext();
135- if(!next){
136- next = thisIterator.hasNext();
137- }
138- return next;
139- }
140- public URL next(){
141- URL nextObject = null;
142- if(parentIterator.hasNext()){
143- nextObject = parentIterator.next();
144- }
145- else{
146- nextObject = thisIterator.next();
147- }
148- return nextObject;
149- }
150- public void remove(){
151- }
152- };
130+ return new ParentIterator(parent.iterator(), classpath.iterator());
153131 }
154132 }
155133
156-
157134 /**
158135 * construct and returns a ClassLoader object which loads from classpath list.
159136 */
@@ -176,10 +153,10 @@ public class ClasspathContext implements Iterable<URL>{
176153 parentClassLoader = null;
177154 }
178155 }
179- final ClassLoader parent = parentClassLoader;
156+ final ClassLoader parentLoader = parentClassLoader;
180157 loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
181158 public ClassLoader run(){
182- return new WarClassLoader(list.toArray(new URL[list.size()]), parent);
159+ return new WarClassLoader(list.toArray(new URL[list.size()]), parentLoader);
183160 }
184161 });
185162 }
@@ -193,9 +170,11 @@ public class ClasspathContext implements Iterable<URL>{
193170 * @return ClassFileEntry object, if not found given class, then returns null.
194171 */
195172 public synchronized ClassFileEntry findEntry(String className){
196- ClassLoader loader = createClassLoader();
173+ ClassLoader localLoader = createClassLoader();
197174
198- URL resource = loader.getResource(className.replace('.', '/') + ".class");
175+ URL resource = localLoader.getResource(
176+ className.replace('.', '/') + ClassFileArchive.CLASS_FILE_EXTENSION
177+ );
199178
200179 if(resource != null){
201180 return new ClassFileEntry(className, resource);
@@ -207,8 +186,10 @@ public class ClasspathContext implements Iterable<URL>{
207186 * returns this context has given class entry or not.
208187 */
209188 public synchronized boolean hasEntry(String className){
210- ClassLoader loader = createClassLoader();
211- return loader.getResource(className.replace('.', '/') + ".class") != null;
189+ ClassLoader localLoader = createClassLoader();
190+ return localLoader.getResource(
191+ className.replace('.', '/') + ClassFileArchive.CLASS_FILE_EXTENSION
192+ ) != null;
212193 }
213194
214195 /**
@@ -217,13 +198,43 @@ public class ClasspathContext implements Iterable<URL>{
217198 */
218199 public synchronized Class<?> findClass(String className) throws ClassNotFoundException{
219200 try{
220- ClassLoader loader = createClassLoader();
201+ ClassLoader localLoader = createClassLoader();
221202
222- Class<?> clazz = loader.loadClass(className);
203+ Class<?> clazz = localLoader.loadClass(className);
223204
224205 return clazz;
225206 } catch(NoClassDefFoundError e){
226207 throw new ClassNotFoundException(e.getMessage(), e);
227208 }
228209 }
210+
211+ private static class ParentIterator implements Iterator<URL>{
212+ private Iterator<URL> parentIterator;
213+ private Iterator<URL> thisIterator;
214+
215+ public ParentIterator(Iterator<URL> parentIterator, Iterator<URL> thisIterator){
216+ this.parentIterator = parentIterator;
217+ this.thisIterator = thisIterator;
218+ }
219+
220+ public boolean hasNext(){
221+ boolean next = parentIterator.hasNext();
222+ if(!next){
223+ next = thisIterator.hasNext();
224+ }
225+ return next;
226+ }
227+ public URL next(){
228+ URL nextObject = null;
229+ if(parentIterator.hasNext()){
230+ nextObject = parentIterator.next();
231+ }
232+ else{
233+ nextObject = thisIterator.next();
234+ }
235+ return nextObject;
236+ }
237+ public void remove(){
238+ }
239+ }
229240 }
--- a/src/main/java/jp/sourceforge/stigmata/digger/JarClassFileArchive.java
+++ b/src/main/java/jp/sourceforge/stigmata/digger/JarClassFileArchive.java
@@ -37,7 +37,9 @@ public class JarClassFileArchive implements ClassFileArchive{
3737
3838 public InputStream getInputStream(ClassFileEntry entry) throws IOException{
3939 if(hasEntry(entry.getClassName())){
40- return jarfile.getInputStream(jarfile.getEntry(entry.getClassName().replace('.', '/') + ".class"));
40+ return jarfile.getInputStream(jarfile.getEntry(
41+ entry.getClassName().replace('.', '/') + CLASS_FILE_EXTENSION
42+ ));
4143 }
4244 return null;
4345 }
@@ -47,17 +49,17 @@ public class JarClassFileArchive implements ClassFileArchive{
4749
4850 for(Enumeration<JarEntry> e = jarfile.entries(); e.hasMoreElements(); ){
4951 JarEntry entry = e.nextElement();
50- if(entry.getName().endsWith(".class")){
52+ if(entry.getName().endsWith(CLASS_FILE_EXTENSION)){
5153 URL location = null;
5254 try {
5355 location = new URL("jar:" + getLocation() + "!/" + entry.getName());
5456 String className = entry.getName();
55- className = className.substring(0, className.length() - ".class".length());
57+ className = className.substring(0, className.length() - CLASS_FILE_EXTENSION.length());
5658 className = className.replace('/', '.');
5759
5860 list.add(new ClassFileEntry(className, location));
5961 } catch (MalformedURLException ex) {
60- throw new InternalError(ex.getMessage());
62+ throw new IllegalStateException(ex);
6163 }
6264 }
6365 }
@@ -65,18 +67,18 @@ public class JarClassFileArchive implements ClassFileArchive{
6567 }
6668
6769 public boolean hasEntry(String className){
68- return jarfile.getEntry(className.replace('.', '/') + ".class") != null;
70+ return jarfile.getEntry(className.replace('.', '/') + CLASS_FILE_EXTENSION) != null;
6971 }
7072
7173 public ClassFileEntry getEntry(String className) throws ClassNotFoundException{
7274 if(hasEntry(className)){
73- String entryName = className.replace('.', '/') + ".class";
75+ String entryName = className.replace('.', '/') + CLASS_FILE_EXTENSION;
7476 try{
7577 URL location = new URL("jar:" + jarfile.getName() + "!/" + entryName);
7678
7779 return new ClassFileEntry(className, location);
7880 } catch(MalformedURLException e){
79- throw new InternalError(e.getMessage());
81+ throw new IllegalStateException(e);
8082 }
8183 }
8284 return null;
--- a/src/main/java/jp/sourceforge/stigmata/digger/WarClassFileArchive.java
+++ b/src/main/java/jp/sourceforge/stigmata/digger/WarClassFileArchive.java
@@ -17,6 +17,8 @@ import java.util.jar.JarEntry;
1717 * @author Haruaki TAMADA
1818 */
1919 public class WarClassFileArchive extends JarClassFileArchive{
20+ private static final String WAR_FILE_CLASSPATH = "WEB-INF/classes/";
21+
2022 public WarClassFileArchive(String jarfile) throws IOException{
2123 super(jarfile);
2224 }
@@ -33,17 +35,19 @@ public class WarClassFileArchive extends JarClassFileArchive{
3335
3436 for(Enumeration<JarEntry> e = jarEntries(); e.hasMoreElements(); ){
3537 JarEntry entry = e.nextElement();
36- if(entry.getName().endsWith(".class")){
38+ if(entry.getName().endsWith(CLASS_FILE_EXTENSION)){
3739 URL location = null;
3840 try {
3941 location = new URL("jar:" + getLocation() + "!/" + entry.getName());
4042 String className = entry.getName();
41- className = className.substring("WEB-INF/classes/".length(), className.length() - ".class".length());
43+ className = className.substring(
44+ WAR_FILE_CLASSPATH.length(), className.length() - CLASS_FILE_EXTENSION.length()
45+ );
4246 className = className.replace('/', '.');
4347
4448 list.add(new ClassFileEntry(className, location));
45- } catch (MalformedURLException ex) {
46- throw new InternalError(ex.getMessage());
49+ } catch(MalformedURLException ex){
50+ throw new IllegalStateException(ex);
4751 }
4852 }
4953 }
@@ -51,18 +55,20 @@ public class WarClassFileArchive extends JarClassFileArchive{
5155 }
5256
5357 public boolean hasEntry(String className){
54- return hasJarEntry("WEB-INF/classes/" + className.replace('.', '/') + ".class");
58+ return hasJarEntry(
59+ WAR_FILE_CLASSPATH + className.replace('.', '/') + CLASS_FILE_EXTENSION
60+ );
5561 }
5662
5763 public ClassFileEntry getEntry(String className) throws ClassNotFoundException{
5864 if(hasEntry(className)){
59- String entryName = className.replace('.', '/') + ".class";
65+ String entryName = className.replace('.', '/') + CLASS_FILE_EXTENSION;
6066 try{
61- URL location = new URL("jar:" + getLocation() + "!/WEB-INF/classes/" + entryName);
67+ URL location = new URL("jar:" + getLocation() + "!/" + WAR_FILE_CLASSPATH + entryName);
6268
6369 return new ClassFileEntry(className, location);
6470 } catch(MalformedURLException e){
65- throw new InternalError(e.getMessage());
71+ throw new IllegalStateException(e);
6672 }
6773 }
6874 return null;
--- a/src/main/java/jp/sourceforge/stigmata/digger/WarClassLoader.java
+++ b/src/main/java/jp/sourceforge/stigmata/digger/WarClassLoader.java
@@ -17,6 +17,8 @@ import java.net.URLStreamHandlerFactory;
1717 * @author Haruaki Tamada
1818 */
1919 public class WarClassLoader extends URLClassLoader{
20+ private static final int BUFFER_SIZE = 256;
21+
2022 public WarClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory){
2123 super(urls, parent, factory);
2224 }
@@ -43,7 +45,10 @@ public class WarClassLoader extends URLClassLoader{
4345 try{
4446 clazz = super.findClass(name);
4547 } catch(ClassNotFoundException e){
46- String path = "WEB-INF/classes/" + name.replace('.', '/') + ".class";
48+ }
49+ if(clazz == null){
50+ String path = "WEB-INF/classes/" + name.replace('.', '/')
51+ + ClassFileArchive.CLASS_FILE_EXTENSION;
4752 for(URL url: getURLs()){
4853 if(url.toString().endsWith(".war")){
4954 InputStream in = null;
@@ -51,7 +56,7 @@ public class WarClassLoader extends URLClassLoader{
5156 try{
5257 URL newurl = new URL("jar:" + url + "!/" + path);
5358 in = newurl.openStream();
54- byte[] data = new byte[256];
59+ byte[] data = new byte[BUFFER_SIZE];
5560 int read = 0;
5661 while((read = in.read(data, 0, data.length)) != -1){
5762 out.write(data, 0, read);
@@ -59,18 +64,18 @@ public class WarClassLoader extends URLClassLoader{
5964 byte[] classdata = out.toByteArray();
6065 clazz = defineClass(name, classdata, 0, classdata.length);
6166 } catch(IOException exp){
62- throw e;
67+ throw new ClassNotFoundException(name, exp);
6368 } finally{
6469 if(in != null){
6570 try{ in.close(); }
6671 catch(IOException exception){
67- throw new InternalError(exception.getMessage());
72+ throw new IllegalStateException(exception);
6873 }
6974 }
7075 try{
7176 out.close();
7277 } catch(IOException exception){
73- throw new InternalError(exception.getMessage());
78+ throw new IllegalStateException(exception);
7479 }
7580 }
7681 break;
Show on old repository browser