summaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
authorChristian Melchior <christian@ilios.dk>2017-11-12 15:17:19 +0100
committerChristian Melchior <christian@ilios.dk>2017-11-12 15:17:19 +0100
commitf4769c7ad702fa529a790106ea5c318a7af5c7b7 (patch)
tree34306d7bb7bf328a9d99d4a23695fd05cb7ddf6d /src/main
parent457934016fc5a8ba2135196d795c66e1dd25cb2c (diff)
downloadjavassist-f4769c7ad702fa529a790106ea5c318a7af5c7b7.tar.gz
javassist-f4769c7ad702fa529a790106ea5c318a7af5c7b7.zip
Remove close method on ClassPath. Add unit tests for new behaviour.
Diffstat (limited to 'src/main')
-rw-r--r--src/main/javassist/ByteArrayClassPath.java5
-rw-r--r--src/main/javassist/ClassClassPath.java6
-rw-r--r--src/main/javassist/ClassPath.java7
-rw-r--r--src/main/javassist/ClassPoolTail.java71
-rw-r--r--src/main/javassist/LoaderClassPath.java7
-rw-r--r--src/main/javassist/URLClassPath.java5
6 files changed, 33 insertions, 68 deletions
diff --git a/src/main/javassist/ByteArrayClassPath.java b/src/main/javassist/ByteArrayClassPath.java
index d385eddc..00397470 100644
--- a/src/main/javassist/ByteArrayClassPath.java
+++ b/src/main/javassist/ByteArrayClassPath.java
@@ -62,11 +62,6 @@ public class ByteArrayClassPath implements ClassPath {
this.classfile = classfile;
}
- /**
- * Closes this class path.
- */
- public void close() {}
-
public String toString() {
return "byte[]:" + classname;
}
diff --git a/src/main/javassist/ClassClassPath.java b/src/main/javassist/ClassClassPath.java
index 3befbf4c..e1c44f08 100644
--- a/src/main/javassist/ClassClassPath.java
+++ b/src/main/javassist/ClassClassPath.java
@@ -91,12 +91,6 @@ public class ClassClassPath implements ClassPath {
return thisClass.getResource(filename);
}
- /**
- * Does nothing.
- */
- public void close() {
- }
-
public String toString() {
return thisClass.getName() + ".class";
}
diff --git a/src/main/javassist/ClassPath.java b/src/main/javassist/ClassPath.java
index 0c605de2..60fe80e9 100644
--- a/src/main/javassist/ClassPath.java
+++ b/src/main/javassist/ClassPath.java
@@ -58,11 +58,4 @@ public interface ClassPath {
* @return null if the specified class file could not be found.
*/
URL find(String classname);
-
- /**
- * This method is invoked when the <code>ClassPath</code> object is
- * detached from the search path. It will be an empty method in most of
- * classes.
- */
- void close();
}
diff --git a/src/main/javassist/ClassPoolTail.java b/src/main/javassist/ClassPoolTail.java
index 1b580855..8e03873c 100644
--- a/src/main/javassist/ClassPoolTail.java
+++ b/src/main/javassist/ClassPoolTail.java
@@ -25,6 +25,9 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
@@ -72,8 +75,6 @@ final class DirClassPath implements ClassPath {
return null;
}
- public void close() {}
-
public String toString() {
return directory;
}
@@ -118,67 +119,63 @@ final class JarDirClassPath implements ClassPath {
return null; // not found
}
-
- public void close() {
- if (jars != null)
- for (int i = 0; i < jars.length; i++)
- jars[i].close();
- }
}
final class JarClassPath implements ClassPath {
- JarFile jarfile;
+ List<String> jarfileEntries;
String jarfileURL;
JarClassPath(String pathname) throws NotFoundException {
+ JarFile jarfile = null;
try {
jarfile = new JarFile(pathname);
+ jarfileEntries = new ArrayList<String>();
+ for (JarEntry je:Collections.list(jarfile.entries()))
+ if (je.getName().endsWith(".class"))
+ jarfileEntries.add(je.getName());
jarfileURL = new File(pathname).getCanonicalFile()
- .toURI().toURL().toString();
+ .toURI().toURL().toString();
return;
+ } catch (IOException e) {}
+ finally {
+ if (null != jarfile)
+ try {
+ jarfile.close();
+ } catch (IOException e) {}
}
- catch (IOException e) {}
throw new NotFoundException(pathname);
}
+ @Override
public InputStream openClassfile(String classname)
- throws NotFoundException
+ throws NotFoundException
{
- try {
- String jarname = classname.replace('.', '/') + ".class";
- JarEntry je = jarfile.getJarEntry(jarname);
- if (je != null)
- return jarfile.getInputStream(je);
- else
- return null; // not found
- }
- catch (IOException e) {}
- throw new NotFoundException("broken jar file?: "
- + jarfile.getName());
+ URL jarURL = find(classname);
+ if (null != jarURL)
+ try {
+ return jarURL.openConnection().getInputStream();
+ }
+ catch (IOException e) {
+ throw new NotFoundException("broken jar file?: "
+ + classname);
+ }
+ return null;
}
+ @Override
public URL find(String classname) {
String jarname = classname.replace('.', '/') + ".class";
- JarEntry je = jarfile.getJarEntry(jarname);
- if (je != null)
+ if (jarfileEntries.contains(jarname))
try {
- return new URL("jar:" + jarfileURL + "!/" + jarname);
+ return new URL(String.format("jar:%s!/%s", jarfileURL, jarname));
}
catch (MalformedURLException e) {}
-
return null; // not found
}
- public void close() {
- try {
- jarfile.close();
- jarfile = null;
- }
- catch (IOException e) {}
- }
-
+ @Override
public String toString() {
- return jarfile == null ? "<null>" : jarfile.toString();
+ return jarfileURL == null ? "<null>" : jarfileURL.toString();
}
}
@@ -235,8 +232,6 @@ final class ClassPoolTail {
else
list = list.next;
}
-
- cp.close();
}
public ClassPath appendSystemPath() {
diff --git a/src/main/javassist/LoaderClassPath.java b/src/main/javassist/LoaderClassPath.java
index 6921ca6e..299fdb85 100644
--- a/src/main/javassist/LoaderClassPath.java
+++ b/src/main/javassist/LoaderClassPath.java
@@ -94,11 +94,4 @@ public class LoaderClassPath implements ClassPath {
return url;
}
}
-
- /**
- * Closes this class path.
- */
- public void close() {
- clref = null;
- }
}
diff --git a/src/main/javassist/URLClassPath.java b/src/main/javassist/URLClassPath.java
index bea0b3c2..5eeef3a9 100644
--- a/src/main/javassist/URLClassPath.java
+++ b/src/main/javassist/URLClassPath.java
@@ -113,11 +113,6 @@ public class URLClassPath implements ClassPath {
}
/**
- * Closes this class path.
- */
- public void close() {}
-
- /**
* Reads a class file on an http server.
*
* @param host host name