OSDN Git Service

fixed warning.
authorHaruaki Tamada <tamada@cc.kyoto-su.ac.jp>
Sat, 1 Sep 2012 03:49:03 +0000 (12:49 +0900)
committerHaruaki Tamada <tamada@cc.kyoto-su.ac.jp>
Sat, 1 Sep 2012 03:49:03 +0000 (12:49 +0900)
src/main/java/jp/sourceforge/stigmata/digger/ClassFileArchive.java
src/main/java/jp/sourceforge/stigmata/digger/ClassFileEntry.java
src/main/java/jp/sourceforge/stigmata/digger/ClasspathContext.java
src/main/java/jp/sourceforge/stigmata/digger/JarClassFileArchive.java
src/main/java/jp/sourceforge/stigmata/digger/WarClassFileArchive.java
src/main/java/jp/sourceforge/stigmata/digger/WarClassLoader.java

index 5188785..67a265a 100644 (file)
@@ -12,33 +12,35 @@ import java.util.Iterator;
  * @author Haruaki TAMADA
  */
 public interface ClassFileArchive extends Iterable<ClassFileEntry>{
+    String CLASS_FILE_EXTENSION = ".class";
+
     /**
      * returns the location of this archive.
      */
-    public URL getLocation();
+    URL getLocation();
 
     /**
      * returns the InputStream object from given entry.
      */
-    public InputStream getInputStream(ClassFileEntry entry) throws IOException;
+    InputStream getInputStream(ClassFileEntry entry) throws IOException;
 
     /**
      * returns an entries of this archive.
      */
-    public Iterator<ClassFileEntry> iterator();
+    Iterator<ClassFileEntry> iterator();
 
     /**
      * returns this archive has given class entry or not.
      */
-    public boolean hasEntry(String className);
+    boolean hasEntry(String className);
 
     /**
      * returns an entry of given class name.
      */
-    public ClassFileEntry getEntry(String className) throws ClassNotFoundException;
+    ClassFileEntry getEntry(String className) throws ClassNotFoundException;
 
     /**
      * returns the name of this archive.
      */
-    public String getName();
+    String getName();
 }
index b1a9eb8..bdbab3a 100644 (file)
@@ -22,7 +22,7 @@ public class ClassFileEntry{
         return className;
     }
 
-    public void setLocation(URL location){
+    public final void setLocation(URL location){
         this.location = location;
     }
 
index f03d66f..1011d76 100644 (file)
@@ -127,33 +127,10 @@ public class ClasspathContext implements Iterable<URL>{
             return classpath.iterator();
         }
         else{
-            final Iterator<URL> parentIterator = parent.iterator();
-            final Iterator<URL> thisIterator = classpath.iterator();
-            return new Iterator<URL>(){
-                public boolean hasNext(){
-                    boolean next = parentIterator.hasNext();
-                    if(!next){
-                        next = thisIterator.hasNext();
-                    }
-                    return next;
-                }
-                public URL next(){
-                    URL nextObject = null;
-                    if(parentIterator.hasNext()){
-                        nextObject = parentIterator.next();
-                    }
-                    else{
-                        nextObject = thisIterator.next();
-                    }
-                    return nextObject;
-                }
-                public void remove(){
-                }
-            };
+            return new ParentIterator(parent.iterator(), classpath.iterator());
         }
     }
 
-
     /**
      * construct and returns a ClassLoader object which loads from classpath list.
      */
@@ -176,10 +153,10 @@ public class ClasspathContext implements Iterable<URL>{
                     parentClassLoader = null;
                 }
             }
-            final ClassLoader parent = parentClassLoader;
+            final ClassLoader parentLoader = parentClassLoader;
             loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
                 public ClassLoader run(){
-                    return new WarClassLoader(list.toArray(new URL[list.size()]), parent);
+                    return new WarClassLoader(list.toArray(new URL[list.size()]), parentLoader);
                 }
             });
         }
@@ -193,9 +170,11 @@ public class ClasspathContext implements Iterable<URL>{
      * @return ClassFileEntry object, if not found given class, then returns null.
      */
     public synchronized ClassFileEntry findEntry(String className){
-        ClassLoader loader = createClassLoader();
+        ClassLoader localLoader = createClassLoader();
 
-        URL resource = loader.getResource(className.replace('.', '/') + ".class");
+        URL resource = localLoader.getResource(
+            className.replace('.', '/') + ClassFileArchive.CLASS_FILE_EXTENSION
+        );
 
         if(resource != null){
             return new ClassFileEntry(className, resource);
@@ -207,8 +186,10 @@ public class ClasspathContext implements Iterable<URL>{
      * returns this context has given class entry or not.
      */
     public synchronized boolean hasEntry(String className){
-        ClassLoader loader = createClassLoader();
-        return loader.getResource(className.replace('.', '/') + ".class") != null;
+        ClassLoader localLoader = createClassLoader();
+        return localLoader.getResource(
+            className.replace('.', '/') + ClassFileArchive.CLASS_FILE_EXTENSION
+        ) != null;
     }
 
     /**
@@ -217,13 +198,43 @@ public class ClasspathContext implements Iterable<URL>{
      */
     public synchronized Class<?> findClass(String className) throws ClassNotFoundException{
         try{
-            ClassLoader loader = createClassLoader();
+            ClassLoader localLoader = createClassLoader();
 
-            Class<?> clazz = loader.loadClass(className);
+            Class<?> clazz = localLoader.loadClass(className);
 
             return clazz;
         } catch(NoClassDefFoundError e){
             throw new ClassNotFoundException(e.getMessage(), e);
         }
     }
+
+    private static class ParentIterator implements Iterator<URL>{
+        private Iterator<URL> parentIterator;
+        private Iterator<URL> thisIterator;
+    
+        public ParentIterator(Iterator<URL> parentIterator, Iterator<URL> thisIterator){
+            this.parentIterator = parentIterator;
+            this.thisIterator = thisIterator;
+        }
+    
+        public boolean hasNext(){
+            boolean next = parentIterator.hasNext();
+            if(!next){
+                next = thisIterator.hasNext();
+            }
+            return next;
+        }
+        public URL next(){
+            URL nextObject = null;
+            if(parentIterator.hasNext()){
+                nextObject = parentIterator.next();
+            }
+            else{
+                nextObject = thisIterator.next();
+            }
+            return nextObject;
+        }
+        public void remove(){
+        }
+    }
 }
index 765dc28..8d23d03 100644 (file)
@@ -37,7 +37,9 @@ public class JarClassFileArchive implements ClassFileArchive{
 
     public InputStream getInputStream(ClassFileEntry entry) throws IOException{
         if(hasEntry(entry.getClassName())){
-            return jarfile.getInputStream(jarfile.getEntry(entry.getClassName().replace('.', '/') + ".class"));
+            return jarfile.getInputStream(jarfile.getEntry(
+                entry.getClassName().replace('.', '/') + CLASS_FILE_EXTENSION
+            ));
         }
         return null;
     }
@@ -47,17 +49,17 @@ public class JarClassFileArchive implements ClassFileArchive{
         
         for(Enumeration<JarEntry> e = jarfile.entries(); e.hasMoreElements(); ){
             JarEntry entry = e.nextElement();
-            if(entry.getName().endsWith(".class")){
+            if(entry.getName().endsWith(CLASS_FILE_EXTENSION)){
                 URL location = null;
                 try {
                     location = new URL("jar:" + getLocation() + "!/" + entry.getName());
                     String className = entry.getName();
-                    className = className.substring(0, className.length() - ".class".length());
+                    className = className.substring(0, className.length() - CLASS_FILE_EXTENSION.length());
                     className = className.replace('/', '.');
                     
                     list.add(new ClassFileEntry(className, location));
                 } catch (MalformedURLException ex) {
-                    throw new InternalError(ex.getMessage());
+                    throw new IllegalStateException(ex);
                 }
             }
         }
@@ -65,18 +67,18 @@ public class JarClassFileArchive implements ClassFileArchive{
     }
 
     public boolean hasEntry(String className){
-        return jarfile.getEntry(className.replace('.', '/') + ".class") != null;
+        return jarfile.getEntry(className.replace('.', '/') + CLASS_FILE_EXTENSION) != null;
     }
 
     public ClassFileEntry getEntry(String className) throws ClassNotFoundException{
         if(hasEntry(className)){
-            String entryName = className.replace('.', '/') + ".class";
+            String entryName = className.replace('.', '/') + CLASS_FILE_EXTENSION;
             try{
                 URL location = new URL("jar:" + jarfile.getName() + "!/" + entryName);
                 
                 return new ClassFileEntry(className, location);
             } catch(MalformedURLException e){
-                throw new InternalError(e.getMessage());
+                throw new IllegalStateException(e);
             }
         }
         return null;
index 32e8cb2..762fb57 100644 (file)
@@ -17,6 +17,8 @@ import java.util.jar.JarEntry;
  * @author Haruaki TAMADA
  */
 public class WarClassFileArchive extends JarClassFileArchive{
+    private static final String WAR_FILE_CLASSPATH = "WEB-INF/classes/";
+
     public WarClassFileArchive(String jarfile) throws IOException{
         super(jarfile);
     }
@@ -33,17 +35,19 @@ public class WarClassFileArchive extends JarClassFileArchive{
 
         for(Enumeration<JarEntry> e = jarEntries(); e.hasMoreElements(); ){
             JarEntry entry = e.nextElement();
-            if(entry.getName().endsWith(".class")){
+            if(entry.getName().endsWith(CLASS_FILE_EXTENSION)){
                 URL location = null;
                 try {
                     location = new URL("jar:" + getLocation() + "!/" + entry.getName());
                     String className = entry.getName();
-                    className = className.substring("WEB-INF/classes/".length(), className.length() - ".class".length());
+                    className = className.substring(
+                        WAR_FILE_CLASSPATH.length(), className.length() - CLASS_FILE_EXTENSION.length()
+                    );
                     className = className.replace('/', '.');
 
                     list.add(new ClassFileEntry(className, location));
-                } catch (MalformedURLException ex) {
-                    throw new InternalError(ex.getMessage());
+                } catch(MalformedURLException ex){
+                    throw new IllegalStateException(ex);
                 }
             }
         }
@@ -51,18 +55,20 @@ public class WarClassFileArchive extends JarClassFileArchive{
     }
 
     public boolean hasEntry(String className){
-        return hasJarEntry("WEB-INF/classes/" + className.replace('.', '/') + ".class");
+        return hasJarEntry(
+            WAR_FILE_CLASSPATH + className.replace('.', '/') + CLASS_FILE_EXTENSION
+        );
     }
 
     public ClassFileEntry getEntry(String className) throws ClassNotFoundException{
         if(hasEntry(className)){
-            String entryName = className.replace('.', '/') + ".class";
+            String entryName = className.replace('.', '/') + CLASS_FILE_EXTENSION;
             try{
-                URL location = new URL("jar:" + getLocation() + "!/WEB-INF/classes/" + entryName);
+                URL location = new URL("jar:" + getLocation() + "!/" + WAR_FILE_CLASSPATH + entryName);
 
                 return new ClassFileEntry(className, location);
             } catch(MalformedURLException e){
-                throw new InternalError(e.getMessage());
+                throw new IllegalStateException(e);
             }
         }
         return null;
index 8baf2d1..dd0eb6d 100644 (file)
@@ -17,6 +17,8 @@ import java.net.URLStreamHandlerFactory;
  * @author Haruaki Tamada
  */
 public class WarClassLoader extends URLClassLoader{
+    private static final int BUFFER_SIZE = 256;
+
     public WarClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory){
         super(urls, parent, factory);
     }
@@ -43,7 +45,10 @@ public class WarClassLoader extends URLClassLoader{
         try{
             clazz = super.findClass(name);
         } catch(ClassNotFoundException e){
-            String path = "WEB-INF/classes/" + name.replace('.', '/') + ".class";
+        }
+        if(clazz == null){
+            String path = "WEB-INF/classes/" + name.replace('.', '/')
+                + ClassFileArchive.CLASS_FILE_EXTENSION;
             for(URL url: getURLs()){
                 if(url.toString().endsWith(".war")){
                     InputStream in = null;
@@ -51,7 +56,7 @@ public class WarClassLoader extends URLClassLoader{
                     try{
                         URL newurl = new URL("jar:" + url + "!/" + path);
                         in = newurl.openStream();
-                        byte[] data = new byte[256];
+                        byte[] data = new byte[BUFFER_SIZE];
                         int read = 0;
                         while((read = in.read(data, 0, data.length)) != -1){
                             out.write(data, 0, read);
@@ -59,18 +64,18 @@ public class WarClassLoader extends URLClassLoader{
                         byte[] classdata = out.toByteArray();
                         clazz = defineClass(name, classdata, 0, classdata.length);
                     } catch(IOException exp){
-                        throw e;
+                        throw new ClassNotFoundException(name, exp);
                     } finally{
                         if(in != null){
                             try{ in.close(); }
                             catch(IOException exception){
-                                throw new InternalError(exception.getMessage());
+                                throw new IllegalStateException(exception);
                             }
                         }
                         try{ 
                             out.close();
                         } catch(IOException exception){
-                            throw new InternalError(exception.getMessage());
+                            throw new IllegalStateException(exception);
                         }
                     }
                     break;