Issue #1797 - JEP 238 - Multi-Release JAR files break bytecode scanning. (#1951)

Made MultiReleaseJarFile closeable and using try-with-resources in
AnnotationParser to avoid leaking file descriptors.

Made few code simplifications.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2017-11-08 21:38:26 +01:00 committed by GitHub
parent 7a6d7bd137
commit b1d5fea96c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 16 deletions

View File

@ -916,18 +916,20 @@ public class AnnotationParser
LOG.debug("Scanning jar {}", jarResource);
MultiException me = new MultiException();
MultiReleaseJarFile jarFile = new MultiReleaseJarFile(jarResource.getFile(),_javaPlatform,false);
jarFile.stream().forEach(e->
try (MultiReleaseJarFile jarFile = new MultiReleaseJarFile(jarResource.getFile(),_javaPlatform,false))
{
try
jarFile.stream().forEach(e->
{
parseJarEntry(handlers, jarResource, e, resolver);
}
catch (Exception ex)
{
me.add(new RuntimeException("Error scanning entry " + e.getName() + " from jar " + jarResource, ex));
}
});
try
{
parseJarEntry(handlers, jarResource, e, resolver);
}
catch (Exception ex)
{
me.add(new RuntimeException("Error scanning entry " + e.getName() + " from jar " + jarResource, ex));
}
});
}
me.ifExceptionThrow();
}
}

View File

@ -18,11 +18,13 @@
package org.eclipse.jetty.util;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.jar.JarEntry;
@ -33,7 +35,7 @@ import java.util.stream.Stream;
/**
* <p>Utility class to handle a Multi Release Jar file</p>
*/
public class MultiReleaseJarFile
public class MultiReleaseJarFile implements Closeable
{
private static final String META_INF_VERSIONS = "META-INF/versions/";
@ -84,12 +86,10 @@ public class MultiReleaseJarFile
{
Map.Entry<String,VersionedJarEntry> e = i.next();
VersionedJarEntry entry = e.getValue();
if (entry.inner)
{
VersionedJarEntry outer = entry.outer==null?null:map.get(entry.outer);
if (entry.outer==null || outer==null || outer.version!= entry.version)
if (outer==null || outer.version!=entry.version)
i.remove();
}
}
@ -130,6 +130,13 @@ public class MultiReleaseJarFile
return entries.get(name);
}
@Override
public void close() throws IOException
{
if (jarFile!=null)
jarFile.close();
}
@Override
public String toString()
{
@ -172,8 +179,8 @@ public class MultiReleaseJarFile
this.entry = entry;
this.name = name;
this.version = v;
this.inner = name.contains("$") && name.toLowerCase().endsWith(".class");
this.outer = inner ? name.substring(0, name.indexOf('$')) + name.substring(name.length() - 6, name.length()) : null;
this.inner = name.contains("$") && name.toLowerCase(Locale.ENGLISH).endsWith(".class");
this.outer = inner ? name.substring(0, name.indexOf('$')) + ".class" : null;
}
/**