|
19 | 19 | import static de.thetaphi.forbiddenapis.Checker.Option.*; |
20 | 20 |
|
21 | 21 | import java.io.File; |
| 22 | +import java.io.FileInputStream; |
22 | 23 | import java.io.IOException; |
23 | | -import java.io.InputStream; |
24 | | -import java.io.OutputStream; |
25 | | -import java.nio.file.Files; |
26 | | -import java.nio.file.Path; |
27 | | -import java.util.*; |
28 | 24 | import java.net.JarURLConnection; |
29 | | -import java.net.URLConnection; |
30 | | -import java.net.URLClassLoader; |
| 25 | +import java.net.MalformedURLException; |
31 | 26 | import java.net.URISyntaxException; |
32 | 27 | import java.net.URL; |
33 | | -import java.net.MalformedURLException; |
34 | | -import java.util.jar.JarEntry; |
35 | | -import java.util.jar.JarFile; |
| 28 | +import java.net.URLClassLoader; |
| 29 | +import java.net.URLConnection; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.EnumSet; |
| 32 | +import java.util.LinkedHashSet; |
| 33 | +import java.util.Locale; |
| 34 | +import java.util.zip.ZipEntry; |
| 35 | +import java.util.zip.ZipInputStream; |
36 | 36 |
|
37 | 37 | import org.apache.commons.cli.CommandLine; |
38 | 38 | import org.apache.commons.cli.DefaultParser; |
|
41 | 41 | import org.apache.commons.cli.OptionGroup; |
42 | 42 | import org.apache.commons.cli.Options; |
43 | 43 | import org.codehaus.plexus.util.DirectoryScanner; |
| 44 | +import org.codehaus.plexus.util.SelectorUtils; |
44 | 45 |
|
45 | 46 | import de.thetaphi.forbiddenapis.AsmUtils; |
46 | 47 | import de.thetaphi.forbiddenapis.Checker; |
@@ -217,50 +218,11 @@ private void printHelp(Options options) { |
217 | 218 | } |
218 | 219 |
|
219 | 220 | public void run() throws ExitException { |
220 | | - File firstClassesDirectory = new File(cmd.getOptionValue(dirOpt.getLongOpt())).getAbsoluteFile(); |
221 | | - |
222 | | - if (!firstClassesDirectory.exists()) { |
223 | | - throw new ExitException(EXIT_ERR_OTHER, "Directory with class files does not exist: " + firstClassesDirectory); |
| 221 | + final File classesDirectory = new File(cmd.getOptionValue(dirOpt.getLongOpt())).getAbsoluteFile(); |
| 222 | + if (!classesDirectory.exists()) { |
| 223 | + throw new ExitException(EXIT_ERR_OTHER, "Directory with class files does not exist: " + classesDirectory); |
224 | 224 | } |
225 | 225 |
|
226 | | - try { |
227 | | - if (!firstClassesDirectory.isDirectory() && firstClassesDirectory.getName().endsWith(".jar")) { |
228 | | - // Create a temporary directory |
229 | | - Path tempDir = Files.createTempDirectory("jar_extract_"); |
230 | | - tempDir.toFile().deleteOnExit(); |
231 | | - System.out.println("Directory is a jar - temporary extracting to " + tempDir); |
232 | | - |
233 | | - // Extract JAR contents |
234 | | - try (JarFile jarFile = new JarFile(firstClassesDirectory)) { |
235 | | - Enumeration<JarEntry> entries = jarFile.entries(); |
236 | | - while (entries.hasMoreElements()) { |
237 | | - JarEntry entry = entries.nextElement(); |
238 | | - Path entryDestination = tempDir.resolve(entry.getName()); |
239 | | - |
240 | | - if (entry.isDirectory()) { |
241 | | - Files.createDirectories(entryDestination); |
242 | | - } else { |
243 | | - Files.createDirectories(entryDestination.getParent()); |
244 | | - try (InputStream in = jarFile.getInputStream(entry); |
245 | | - OutputStream out = Files.newOutputStream(entryDestination)) { |
246 | | - byte[] buffer = new byte[8192]; |
247 | | - int len; |
248 | | - while ((len = in.read(buffer)) > 0) { |
249 | | - out.write(buffer, 0, len); |
250 | | - } |
251 | | - } |
252 | | - } |
253 | | - entryDestination.toFile().deleteOnExit(); |
254 | | - } |
255 | | - } |
256 | | - firstClassesDirectory = tempDir.toFile(); |
257 | | - } |
258 | | - } catch (IOException e) { |
259 | | - throw new ExitException(EXIT_ERR_OTHER, "Could not unpack jar file: " + e); |
260 | | - } |
261 | | - |
262 | | - final File classesDirectory = firstClassesDirectory; |
263 | | - |
264 | 226 | // parse classpath given as argument; add -d to classpath, too |
265 | 227 | final String[] classpath = cmd.getOptionValues(classpathOpt.getLongOpt()); |
266 | 228 | final URL[] urls; |
@@ -305,27 +267,6 @@ public void run() throws ExitException { |
305 | 267 | checker.addSuppressAnnotation(a); |
306 | 268 | } |
307 | 269 |
|
308 | | - logger.info("Scanning for classes to check..."); |
309 | | - |
310 | | - String[] includes = cmd.getOptionValues(includesOpt.getLongOpt()); |
311 | | - if (includes == null || includes.length == 0) { |
312 | | - includes = new String[] { "**/*.class" }; |
313 | | - } |
314 | | - final String[] excludes = cmd.getOptionValues(excludesOpt.getLongOpt()); |
315 | | - final DirectoryScanner ds = new DirectoryScanner(); |
316 | | - ds.setBasedir(classesDirectory); |
317 | | - ds.setCaseSensitive(true); |
318 | | - ds.setIncludes(includes); |
319 | | - ds.setExcludes(excludes); |
320 | | - ds.addDefaultExcludes(); |
321 | | - ds.scan(); |
322 | | - final String[] files = ds.getIncludedFiles(); |
323 | | - if (files.length == 0) { |
324 | | - throw new ExitException(EXIT_ERR_OTHER, String.format(Locale.ENGLISH, |
325 | | - "No classes found in directory %s (includes=%s, excludes=%s).", |
326 | | - classesDirectory, Arrays.toString(includes), Arrays.toString(excludes))); |
327 | | - } |
328 | | - |
329 | 270 | try { |
330 | 271 | final String[] bundledSignatures = cmd.getOptionValues(bundledsignaturesOpt.getLongOpt()); |
331 | 272 | if (bundledSignatures != null) for (String bs : new LinkedHashSet<>(Arrays.asList(bundledSignatures))) { |
@@ -362,13 +303,73 @@ public void run() throws ExitException { |
362 | 303 | return; |
363 | 304 | } |
364 | 305 | } |
| 306 | + |
| 307 | + logger.info("Scanning for classes to check..."); |
365 | 308 |
|
366 | | - try { |
367 | | - checker.addClassesToCheck(classesDirectory, files); |
368 | | - } catch (IOException ioe) { |
369 | | - throw new ExitException(EXIT_ERR_OTHER, "Failed to load one of the given class files: " + ioe); |
| 309 | + String[] includes = cmd.getOptionValues(includesOpt.getLongOpt()); |
| 310 | + if (includes == null || includes.length == 0) { |
| 311 | + includes = new String[] { "**/*.class" }; |
370 | 312 | } |
| 313 | + final String[] excludes = cmd.getOptionValues(excludesOpt.getLongOpt()); |
| 314 | + |
| 315 | + if (classesDirectory.isDirectory()) { |
| 316 | + final DirectoryScanner ds = new DirectoryScanner(); |
| 317 | + ds.setBasedir(classesDirectory); |
| 318 | + ds.setCaseSensitive(true); |
| 319 | + ds.setIncludes(includes); |
| 320 | + ds.setExcludes(excludes); |
| 321 | + ds.addDefaultExcludes(); |
| 322 | + ds.scan(); |
| 323 | + final String[] files = ds.getIncludedFiles(); |
| 324 | + if (files.length == 0) { |
| 325 | + throw new ExitException(EXIT_ERR_OTHER, String.format(Locale.ENGLISH, |
| 326 | + "No classes found in directory %s (includes=%s, excludes=%s).", |
| 327 | + classesDirectory, Arrays.toString(includes), Arrays.toString(excludes))); |
| 328 | + } |
| 329 | + try { |
| 330 | + checker.addClassesToCheck(classesDirectory, files); |
| 331 | + } catch (IOException ioe) { |
| 332 | + throw new ExitException(EXIT_ERR_OTHER, "Failed to load one of the given class files: " + ioe); |
| 333 | + } |
| 334 | + } else if (classesDirectory.getName().endsWith(".jar") || classesDirectory.getName().endsWith(".zip")) { |
| 335 | + int filesFound = 0; |
| 336 | + try (final ZipInputStream zipin = new ZipInputStream(new FileInputStream(classesDirectory))) { |
| 337 | + ZipEntry entry; |
| 338 | + while ((entry = zipin.getNextEntry()) != null) { |
| 339 | + if (entry.isDirectory()) continue; |
| 340 | + // cleanup name in the zip file (fix trailing slash and windows separators): |
| 341 | + final String name = entry.getName().replace('\\', '/').replaceFirst("^/+", ""); |
| 342 | + next: |
| 343 | + for (String ipattern : includes) { |
| 344 | + if (SelectorUtils.match(ipattern, name)) { |
| 345 | + if (excludes != null) { |
| 346 | + for (String epattern : excludes) { |
| 347 | + if (SelectorUtils.match(epattern, name)) { |
| 348 | + break next; |
| 349 | + } |
| 350 | + } |
| 351 | + } |
| 352 | + try { |
| 353 | + checker.streamReadClassToCheck(zipin, name); |
| 354 | + filesFound++; |
| 355 | + } catch (IOException ioe) { |
| 356 | + throw new ExitException(EXIT_ERR_OTHER, String.format(Locale.ENGLISH, "Failed to load class file '%s' from jar/zip: %s", name, ioe)); |
| 357 | + } |
| 358 | + break next; |
| 359 | + } |
| 360 | + } |
| 361 | + } |
| 362 | + } |
| 363 | + if (filesFound == 0) { |
| 364 | + throw new ExitException(EXIT_ERR_OTHER, String.format(Locale.ENGLISH, |
| 365 | + "No classes found in jar/zip file %s (includes=%s, excludes=%s).", |
| 366 | + classesDirectory, Arrays.toString(includes), Arrays.toString(excludes))); |
| 367 | + } |
371 | 368 |
|
| 369 | + } else { |
| 370 | + throw new ExitException(EXIT_ERR_OTHER, "Classes directory parameter is neither a directory or a jar/zip file."); |
| 371 | + } |
| 372 | + |
372 | 373 | try { |
373 | 374 | checker.run(); |
374 | 375 | } catch (ForbiddenApiException fae) { |
|
0 commit comments