diff --git a/build.xml b/build.xml index d0f0eca4..65495124 100644 --- a/build.xml +++ b/build.xml @@ -26,9 +26,9 @@ - + - + @@ -54,7 +54,6 @@ --> - @@ -68,9 +67,8 @@ - + - @@ -79,7 +77,7 @@ - + @@ -88,19 +86,19 @@ - + - + - + - + @@ -126,7 +124,7 @@ - + @@ -148,7 +146,6 @@ - @@ -156,6 +153,7 @@ + @@ -195,10 +193,10 @@ - + @@ -687,9 +685,6 @@ - - - @@ -699,20 +694,13 @@ + nowarn="true" source="1.7" target="1.7" debug="true" deprecation="false" encoding="${build.encoding}"> - - - - - - - + - diff --git a/ivy-settings.xml b/ivy-settings.xml index abb46451..bd8d2641 100644 --- a/ivy-settings.xml +++ b/ivy-settings.xml @@ -26,7 +26,7 @@ - + diff --git a/src/main/docs/bundled-signatures.html b/src/main/docs/bundled-signatures.html index a93b59d4..73580e63 100644 --- a/src/main/docs/bundled-signatures.html +++ b/src/main/docs/bundled-signatures.html @@ -29,14 +29,14 @@

Bundled Signatures Documentation

  • jdk-unsafe-*: Signatures of "unsafe" methods that use default charset, default locale, or default timezone. For server applications it is very stupid to call those methods, as the results will definitely not what the user wants -(for Java * = 1.6, 1.7, 1.8, 9, 10, 11, 12, 13, 14; Ant / Maven / Gradle automatically add the compile Java version).
  • +(for Java * = 1.7, 1.8, 9, 10, 11, 12, 13, 14; Ant / Maven / Gradle automatically add the compile Java version).
  • jdk-deprecated-*: This disallows all deprecated -methods from the JDK (for Java * = 1.6, 1.7, 1.8, 9, 10, 11, 12, 13, 14; Ant / Maven / Gradle automatically add the compile Java version).
  • +methods from the JDK (for Java * = 1.7, 1.8, 9, 10, 11, 12, 13, 14; Ant / Maven / Gradle automatically add the compile Java version).
  • jdk-internal-*: Lists all internal packages of the JDK as of Security.getProperty("package.access"). Calling those methods will always trigger security manager and is completely forbidden from Java 9 on -(for Java * = 1.6, 1.7, 1.8, 9, 10, 11, 12, 13, 14; Ant / Maven / Gradle automatically add the compile Java version, since forbiddenapis v2.1).
  • +(for Java * = 1.7, 1.8, 9, 10, 11, 12, 13, 14; Ant / Maven / Gradle automatically add the compile Java version, since forbiddenapis v2.1).
  • jdk-non-portable: Signatures of all non-portable (like com.sun.management.HotSpotDiagnosticMXBean) or internal runtime APIs (like sun.misc.Unsafe). This is a superset of jdk-internal.
    diff --git a/src/main/java/de/thetaphi/forbiddenapis/AsmUtils.java b/src/main/java/de/thetaphi/forbiddenapis/AsmUtils.java index f2505c1a..e7f0fb79 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/AsmUtils.java +++ b/src/main/java/de/thetaphi/forbiddenapis/AsmUtils.java @@ -162,8 +162,7 @@ private static void patchClassMajorVersion(byte[] bytecode, int versionFrom, int } } - /** This method is used to read the whole stream into byte array. This allows patching. - * It also works around a bug in ASM 6.1 (https://gitlab.ow2.org/asm/asm/issues/317816). */ + /** This method is used to read the whole stream into byte array. This allows patching. */ private static byte[] readStream(final InputStream in) throws IOException { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final byte[] data = new byte[4096]; diff --git a/src/main/java/de/thetaphi/forbiddenapis/Checker.java b/src/main/java/de/thetaphi/forbiddenapis/Checker.java index 4b97ffeb..1a928757 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/Checker.java +++ b/src/main/java/de/thetaphi/forbiddenapis/Checker.java @@ -69,14 +69,14 @@ public static enum Option { final EnumSet
  • classes: key is the internal name (slashed) * */ - final Map signatures = new HashMap(); + final Map signatures = new HashMap<>(); /** set of patterns of forbidden classes */ - final Set classPatterns = new LinkedHashSet(); + final Set classPatterns = new LinkedHashSet<>(); /** if enabled, the bundled signature to enable heuristics for detection of non-portable runtime calls is used */ private boolean forbidNonPortableRuntime = false; @@ -261,8 +261,7 @@ private void parseSignaturesStream(InputStream in, boolean allowBundled, Set missingClasses) throws IOException,ParseException { - final BufferedReader r = new BufferedReader(reader); - try { + try (final BufferedReader r = new BufferedReader(reader)) { String line, defaultMessage = null; UnresolvableReporting reporter = failOnUnresolvableSignatures ? UnresolvableReporting.FAIL : UnresolvableReporting.WARNING; while ((line = r.readLine()) != null) { @@ -285,14 +284,12 @@ private void parseSignaturesFile(Reader reader, boolean isBundled, Set m addSignature(line, defaultMessage, reporter, missingClasses); } } - } finally { - r.close(); } } /** Reads a list of bundled API signatures from classpath. */ public void addBundledSignatures(String name, String jdkTargetVersion) throws IOException,ParseException { - final Set missingClasses = new TreeSet(); + final Set missingClasses = new TreeSet<>(); addBundledSignatures(name, jdkTargetVersion, true, missingClasses); reportMissingSignatureClasses(missingClasses); } @@ -300,7 +297,7 @@ public void addBundledSignatures(String name, String jdkTargetVersion) throws IO /** Reads a list of API signatures. Closes the Reader when done (on Exception, too)! */ public void parseSignaturesStream(InputStream in, String name) throws IOException,ParseException { logger.info("Reading API signatures: " + name); - final Set missingClasses = new TreeSet(); + final Set missingClasses = new TreeSet<>(); parseSignaturesStream(in, false, missingClasses); reportMissingSignatureClasses(missingClasses); } @@ -308,7 +305,7 @@ public void parseSignaturesStream(InputStream in, String name) throws IOExceptio /** Reads a list of API signatures from a String. */ public void parseSignaturesString(String signatures) throws IOException,ParseException { logger.info("Reading inline API signatures..."); - final Set missingClasses = new TreeSet(); + final Set missingClasses = new TreeSet<>(); parseSignaturesFile(new StringReader(signatures), false, missingClasses); reportMissingSignatureClasses(missingClasses); } diff --git a/src/main/java/de/thetaphi/forbiddenapis/ant/AntTask.java b/src/main/java/de/thetaphi/forbiddenapis/ant/AntTask.java index a415c356..9b160768 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/ant/AntTask.java +++ b/src/main/java/de/thetaphi/forbiddenapis/ant/AntTask.java @@ -59,8 +59,8 @@ public class AntTask extends Task implements Constants { private final Union classFiles = new Union(); private final Union apiSignatures = new Union(); - private final Collection bundledSignatures = new LinkedHashSet(); - private final Collection suppressAnnotations = new LinkedHashSet(); + private final Collection bundledSignatures = new LinkedHashSet<>(); + private final Collection suppressAnnotations = new LinkedHashSet<>(); private Path classpath = null; private boolean failOnUnsupportedJava = false; diff --git a/src/main/java/de/thetaphi/forbiddenapis/cli/CliMain.java b/src/main/java/de/thetaphi/forbiddenapis/cli/CliMain.java index b4c94f89..27bb7c1b 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/cli/CliMain.java +++ b/src/main/java/de/thetaphi/forbiddenapis/cli/CliMain.java @@ -18,7 +18,6 @@ import static de.thetaphi.forbiddenapis.Checker.Option.*; -import java.io.Closeable; import java.io.File; import java.io.IOException; import java.util.Arrays; @@ -214,8 +213,7 @@ public void run() throws ExitException { } // System.err.println("Classpath: " + Arrays.toString(urls)); - final URLClassLoader loader = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader()); - try { + try (final URLClassLoader loader = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader())) { final EnumSet options = EnumSet.of(FAIL_ON_VIOLATION); if (!cmd.hasOption(allowmissingclassesOpt.getLongOpt())) options.add(FAIL_ON_MISSING_CLASSES); if (!cmd.hasOption(allowunresolvablesignaturesOpt.getLongOpt())) options.add(FAIL_ON_UNRESOLVABLE_SIGNATURES); @@ -257,7 +255,7 @@ public void run() throws ExitException { try { final String[] bundledSignatures = cmd.getOptionValues(bundledsignaturesOpt.getLongOpt()); - if (bundledSignatures != null) for (String bs : new LinkedHashSet(Arrays.asList(bundledSignatures))) { + if (bundledSignatures != null) for (String bs : new LinkedHashSet<>(Arrays.asList(bundledSignatures))) { checker.addBundledSignatures(bs, null); } if (cmd.hasOption(internalruntimeforbiddenOpt.getLongOpt())) { @@ -266,7 +264,7 @@ public void run() throws ExitException { } final String[] signaturesFiles = cmd.getOptionValues(signaturesfileOpt.getLongOpt()); - if (signaturesFiles != null) for (String sf : new LinkedHashSet(Arrays.asList(signaturesFiles))) { + if (signaturesFiles != null) for (String sf : new LinkedHashSet<>(Arrays.asList(signaturesFiles))) { final File f = new File(sf).getAbsoluteFile(); checker.parseSignaturesFile(f); } @@ -294,13 +292,8 @@ public void run() throws ExitException { } catch (ForbiddenApiException fae) { throw new ExitException(EXIT_VIOLATION, fae.getMessage()); } - } finally { - // Java 7 supports closing URLClassLoader, so check for Closeable interface: - if (loader instanceof Closeable) try { - ((Closeable) loader).close(); - } catch (IOException ioe) { - // ignore - } + } catch (IOException ioe) { + throw new ExitException(EXIT_ERR_OTHER, "General IO problem: " + ioe); } } diff --git a/src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApis.java b/src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApis.java index 94880a39..e1629f34 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApis.java +++ b/src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApis.java @@ -19,7 +19,6 @@ import static de.thetaphi.forbiddenapis.Checker.Option.*; import groovy.lang.Closure; -import java.io.Closeable; import java.io.File; import java.io.IOException; import java.lang.annotation.RetentionPolicy; @@ -515,7 +514,7 @@ public void info(String msg) { } }; - final Set cpElements = new LinkedHashSet(); + final Set cpElements = new LinkedHashSet<>(); cpElements.addAll(classpath.getFiles()); cpElements.addAll(classesDirs.getFiles()); final URL[] urls = new URL[cpElements.size()]; @@ -618,9 +617,9 @@ public void info(String msg) { checker.run(); } finally { - // Java 7 supports closing URLClassLoader, so check for Closeable interface: - if (urlLoader instanceof Closeable) try { - ((Closeable) urlLoader).close(); + // Close the classloader to free resources: + try { + if (urlLoader != null) urlLoader.close(); } catch (IOException ioe) { // ignore } diff --git a/src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApisExtension.java b/src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApisExtension.java index 9cf3c0d8..ddc944d7 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApisExtension.java +++ b/src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApisExtension.java @@ -33,10 +33,10 @@ public class CheckForbiddenApisExtension { public FileCollection signaturesFiles; // initialized by plugin-init.groovy - public Set signaturesURLs = new LinkedHashSet(); - public List signatures = new ArrayList(); - public Set bundledSignatures = new LinkedHashSet(), - suppressAnnotations = new LinkedHashSet(); + public Set signaturesURLs = new LinkedHashSet<>(); + public List signatures = new ArrayList<>(); + public Set bundledSignatures = new LinkedHashSet<>(), + suppressAnnotations = new LinkedHashSet<>(); @Deprecated public boolean internalRuntimeForbidden = false; public boolean failOnUnsupportedJava = false, failOnMissingClasses = true, diff --git a/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java b/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java index 1b064a9e..70811047 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java +++ b/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java @@ -70,7 +70,9 @@ public class ForbiddenApisPlugin implements Plugin { @Override public Class run() { try { - final GroovyClassLoader loader = new GroovyClassLoader(ForbiddenApisPlugin.class.getClassLoader(), configuration); + // We don't close the classloader, as we may need it later when loading other classes from inside script: + @SuppressWarnings("resource") final GroovyClassLoader loader = + new GroovyClassLoader(ForbiddenApisPlugin.class.getClassLoader(), configuration); final GroovyCodeSource csrc = new GroovyCodeSource(scriptUrl); @SuppressWarnings("unchecked") final Class clazz = loader.parseClass(csrc, false).asSubclass(DelegatingScript.class); diff --git a/src/main/java/de/thetaphi/forbiddenapis/maven/AbstractCheckMojo.java b/src/main/java/de/thetaphi/forbiddenapis/maven/AbstractCheckMojo.java index 9a8f5d55..a93e9b26 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/maven/AbstractCheckMojo.java +++ b/src/main/java/de/thetaphi/forbiddenapis/maven/AbstractCheckMojo.java @@ -36,7 +36,6 @@ import de.thetaphi.forbiddenapis.Logger; import de.thetaphi.forbiddenapis.ParseException; -import java.io.Closeable; import java.io.File; import java.io.IOException; import java.lang.annotation.RetentionPolicy; @@ -374,7 +373,7 @@ public void info(String msg) { "Trying to read bundled JDK signatures without compiler target. " + "You have to explicitly specify the version in the resource name."); } - for (String bs : new LinkedHashSet(Arrays.asList(bundledSignatures))) { + for (String bs : new LinkedHashSet<>(Arrays.asList(bundledSignatures))) { checker.addBundledSignatures(bs, targetVersion); } } @@ -383,8 +382,8 @@ public void info(String msg) { checker.addBundledSignatures(BS_JDK_NONPORTABLE, null); } - final Set sigFiles = new LinkedHashSet(); - final Set sigUrls = new LinkedHashSet(); + final Set sigFiles = new LinkedHashSet<>(); + final Set sigUrls = new LinkedHashSet<>(); if (signaturesFiles != null) { sigFiles.addAll(Arrays.asList(signaturesFiles)); } @@ -446,9 +445,9 @@ public void info(String msg) { throw new MojoExecutionException(fae.getMessage(), fae.getCause()); } } finally { - // Java 7 supports closing URLClassLoader, so check for Closeable interface: - if (urlLoader instanceof Closeable) try { - ((Closeable) urlLoader).close(); + // Close the classloader to free resources: + try { + if (urlLoader != null) urlLoader.close(); } catch (IOException ioe) { // ignore } diff --git a/src/main/maven/settings.xml.template b/src/main/maven/settings.xml.template index cf3ce983..f40fce8d 100644 --- a/src/main/maven/settings.xml.template +++ b/src/main/maven/settings.xml.template @@ -20,7 +20,7 @@