|
14 | 14 | import java.util.logging.Logger; |
15 | 15 | import edu.umd.cs.findbugs.annotations.CheckForNull; |
16 | 16 | import edu.umd.cs.findbugs.annotations.NonNull; |
| 17 | +import java.io.File; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.net.MalformedURLException; |
| 20 | +import java.net.URI; |
| 21 | +import java.net.URLConnection; |
| 22 | +import java.net.URLStreamHandler; |
| 23 | +import java.util.regex.Matcher; |
| 24 | +import java.util.regex.Pattern; |
17 | 25 | import org.codehaus.groovy.control.CompilationFailedException; |
18 | 26 | import org.codehaus.groovy.control.CompilationUnit; |
19 | 27 | import org.codehaus.groovy.control.CompilerConfiguration; |
@@ -76,6 +84,46 @@ private static final class CleanGroovyClassLoader extends GroovyClassLoader { |
76 | 84 | return new CleanClassCollector(unit, su); |
77 | 85 | } |
78 | 86 |
|
| 87 | + private static final Pattern JAR_URL = Pattern.compile("jar:(file:/.+[.]jar)!/.+"); |
| 88 | + |
| 89 | + // Avoid expensive and (JDK-6956385) leaky implementations of certain JarURLConnection methods: |
| 90 | + @Override public URL findResource(String name) { |
| 91 | + URL url = super.findResource(name); |
| 92 | + if (url != null && url.getProtocol().equals("jar")) { |
| 93 | + try { |
| 94 | + return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile(), new URLStreamHandler() { |
| 95 | + @Override protected URLConnection openConnection(URL url2) throws IOException { |
| 96 | + URLConnection delegate = url.openConnection(); |
| 97 | + return new URLConnection(url2) { |
| 98 | + @Override public void connect() throws IOException { |
| 99 | + delegate.connect(); |
| 100 | + } |
| 101 | + @Override public InputStream getInputStream() throws IOException { |
| 102 | + return delegate.getInputStream(); |
| 103 | + } |
| 104 | + @Override public String getHeaderField(String name) { |
| 105 | + return delegate.getHeaderField(name); |
| 106 | + } |
| 107 | + @Override public long getLastModified() { |
| 108 | + Matcher m = JAR_URL.matcher(url.toString()); |
| 109 | + if (m.matches()) { |
| 110 | + return new File(URI.create(m.group(1))).lastModified(); |
| 111 | + } |
| 112 | + return delegate.getLastModified(); |
| 113 | + } |
| 114 | + @Override public String getContentEncoding() { |
| 115 | + return null; |
| 116 | + } |
| 117 | + }; |
| 118 | + } |
| 119 | + }); |
| 120 | + } catch (MalformedURLException x) { |
| 121 | + LOGGER.log(Level.WARNING, null, x); |
| 122 | + } |
| 123 | + } |
| 124 | + return url; |
| 125 | + } |
| 126 | + |
79 | 127 | private final class CleanClassCollector extends ClassCollector { |
80 | 128 |
|
81 | 129 | CleanClassCollector(CompilationUnit unit, SourceUnit su) { |
|
0 commit comments