From c82ea685c18bdf76b39098f1e3bebf7f1900063c Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Mon, 26 Mar 2018 17:33:04 -0400 Subject: [PATCH 1/2] @Restricted(Beta.class) --- .../org/kohsuke/accmod/impl/Location.java | 5 ++ .../org/kohsuke/accmod/restrictions/Beta.java | 49 +++++++++++++++++++ .../src/it/beta-fail/api/pom.xml | 17 +++++++ .../beta-fail/api/src/main/java/api/Api.java | 15 ++++++ .../src/it/beta-fail/caller/pom.xml | 33 +++++++++++++ .../caller/src/main/java/caller/Caller.java | 11 +++++ .../src/it/beta-fail/invoker.properties | 2 + .../src/it/beta-fail/pom.xml | 17 +++++++ .../src/it/beta-fail/postbuild.groovy | 1 + .../src/it/beta-pass/api/pom.xml | 17 +++++++ .../beta-pass/api/src/main/java/api/Api.java | 15 ++++++ .../src/it/beta-pass/caller/pom.xml | 41 ++++++++++++++++ .../caller/src/main/java/caller/Caller.java | 11 +++++ .../src/it/beta-pass/invoker.properties | 1 + .../src/it/beta-pass/pom.xml | 17 +++++++ .../java/org/kohsuke/accmod/impl/Checker.java | 12 +++-- .../org/kohsuke/accmod/impl/EnforcerMojo.java | 9 +++- pom.xml | 4 +- 18 files changed, 270 insertions(+), 7 deletions(-) create mode 100644 access-modifier-annotation/src/main/java/org/kohsuke/accmod/restrictions/Beta.java create mode 100644 access-modifier-checker/src/it/beta-fail/api/pom.xml create mode 100644 access-modifier-checker/src/it/beta-fail/api/src/main/java/api/Api.java create mode 100644 access-modifier-checker/src/it/beta-fail/caller/pom.xml create mode 100644 access-modifier-checker/src/it/beta-fail/caller/src/main/java/caller/Caller.java create mode 100644 access-modifier-checker/src/it/beta-fail/invoker.properties create mode 100644 access-modifier-checker/src/it/beta-fail/pom.xml create mode 100644 access-modifier-checker/src/it/beta-fail/postbuild.groovy create mode 100644 access-modifier-checker/src/it/beta-pass/api/pom.xml create mode 100644 access-modifier-checker/src/it/beta-pass/api/src/main/java/api/Api.java create mode 100644 access-modifier-checker/src/it/beta-pass/caller/pom.xml create mode 100644 access-modifier-checker/src/it/beta-pass/caller/src/main/java/caller/Caller.java create mode 100644 access-modifier-checker/src/it/beta-pass/invoker.properties create mode 100644 access-modifier-checker/src/it/beta-pass/pom.xml diff --git a/access-modifier-annotation/src/main/java/org/kohsuke/accmod/impl/Location.java b/access-modifier-annotation/src/main/java/org/kohsuke/accmod/impl/Location.java index 5f9a2e6..5898cac 100644 --- a/access-modifier-annotation/src/main/java/org/kohsuke/accmod/impl/Location.java +++ b/access-modifier-annotation/src/main/java/org/kohsuke/accmod/impl/Location.java @@ -74,4 +74,9 @@ public interface Location { * access restrictions. */ ClassLoader getDependencyClassLoader(); + + default /*@CheckForNull*/ String getProperty(String key) { + return null; + } + } diff --git a/access-modifier-annotation/src/main/java/org/kohsuke/accmod/restrictions/Beta.java b/access-modifier-annotation/src/main/java/org/kohsuke/accmod/restrictions/Beta.java new file mode 100644 index 0000000..83abfbc --- /dev/null +++ b/access-modifier-annotation/src/main/java/org/kohsuke/accmod/restrictions/Beta.java @@ -0,0 +1,49 @@ +/* + * The MIT License + * + * Copyright 2018 CloudBees, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.kohsuke.accmod.restrictions; + +import org.kohsuke.accmod.impl.ErrorListener; +import org.kohsuke.accmod.impl.Location; +import org.kohsuke.accmod.impl.RestrictedElement; + +/** + * References are only allowed within the same module, as in {@link NoExternalUse}, + * or when a special flag is set in the consuming module. + * This is the property {@code useBeta} with the value {@code true}. + */ +public class Beta extends DoNotUse { + + @Override + public void error(Location loc, RestrictedElement target, ErrorListener errorListener) { + if (target.isInTheInspectedModule()) { + return; + } + if ("true".equals(loc.getProperty("useBeta"))) { + return; + } + errorListener.onError(null, loc, target + " is still in beta"); + } + +} diff --git a/access-modifier-checker/src/it/beta-fail/api/pom.xml b/access-modifier-checker/src/it/beta-fail/api/pom.xml new file mode 100644 index 0000000..85e354f --- /dev/null +++ b/access-modifier-checker/src/it/beta-fail/api/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + test + beta-fail + 1.0-SNAPSHOT + + api + + + org.kohsuke + access-modifier-annotation + @project.version@ + + + \ No newline at end of file diff --git a/access-modifier-checker/src/it/beta-fail/api/src/main/java/api/Api.java b/access-modifier-checker/src/it/beta-fail/api/src/main/java/api/Api.java new file mode 100644 index 0000000..db6292c --- /dev/null +++ b/access-modifier-checker/src/it/beta-fail/api/src/main/java/api/Api.java @@ -0,0 +1,15 @@ +package api; + +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.Beta; + +public class Api { + + @Restricted(Beta.class) + public static void experimental() {} + + static { + experimental(); // OK + } + +} diff --git a/access-modifier-checker/src/it/beta-fail/caller/pom.xml b/access-modifier-checker/src/it/beta-fail/caller/pom.xml new file mode 100644 index 0000000..04e5b5a --- /dev/null +++ b/access-modifier-checker/src/it/beta-fail/caller/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + test + beta-fail + 1.0-SNAPSHOT + + caller + + + ${project.groupId} + api + ${project.version} + + + + + + org.kohsuke + access-modifier-checker + @project.version@ + + + + enforce + + + + + + + \ No newline at end of file diff --git a/access-modifier-checker/src/it/beta-fail/caller/src/main/java/caller/Caller.java b/access-modifier-checker/src/it/beta-fail/caller/src/main/java/caller/Caller.java new file mode 100644 index 0000000..34bb883 --- /dev/null +++ b/access-modifier-checker/src/it/beta-fail/caller/src/main/java/caller/Caller.java @@ -0,0 +1,11 @@ +package caller; + +import api.Api; + +public class Caller { + + public Caller() { + Api.experimental(); // illegal + } + +} diff --git a/access-modifier-checker/src/it/beta-fail/invoker.properties b/access-modifier-checker/src/it/beta-fail/invoker.properties new file mode 100644 index 0000000..8b87cc7 --- /dev/null +++ b/access-modifier-checker/src/it/beta-fail/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals=clean package +invoker.buildResult = failure diff --git a/access-modifier-checker/src/it/beta-fail/pom.xml b/access-modifier-checker/src/it/beta-fail/pom.xml new file mode 100644 index 0000000..25ccdc5 --- /dev/null +++ b/access-modifier-checker/src/it/beta-fail/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + test + beta-fail + 1.0-SNAPSHOT + pom + + UTF-8 + 1.8 + 1.8 + + + api + caller + + \ No newline at end of file diff --git a/access-modifier-checker/src/it/beta-fail/postbuild.groovy b/access-modifier-checker/src/it/beta-fail/postbuild.groovy new file mode 100644 index 0000000..985101e --- /dev/null +++ b/access-modifier-checker/src/it/beta-fail/postbuild.groovy @@ -0,0 +1 @@ +assert new File(basedir, 'build.log').text.contains('[ERROR] caller/Caller:8 api/Api.experimental()V is still in beta') diff --git a/access-modifier-checker/src/it/beta-pass/api/pom.xml b/access-modifier-checker/src/it/beta-pass/api/pom.xml new file mode 100644 index 0000000..ec7afcb --- /dev/null +++ b/access-modifier-checker/src/it/beta-pass/api/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + test + beta-pass + 1.0-SNAPSHOT + + api + + + org.kohsuke + access-modifier-annotation + @project.version@ + + + \ No newline at end of file diff --git a/access-modifier-checker/src/it/beta-pass/api/src/main/java/api/Api.java b/access-modifier-checker/src/it/beta-pass/api/src/main/java/api/Api.java new file mode 100644 index 0000000..db6292c --- /dev/null +++ b/access-modifier-checker/src/it/beta-pass/api/src/main/java/api/Api.java @@ -0,0 +1,15 @@ +package api; + +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.Beta; + +public class Api { + + @Restricted(Beta.class) + public static void experimental() {} + + static { + experimental(); // OK + } + +} diff --git a/access-modifier-checker/src/it/beta-pass/caller/pom.xml b/access-modifier-checker/src/it/beta-pass/caller/pom.xml new file mode 100644 index 0000000..5a1b8eb --- /dev/null +++ b/access-modifier-checker/src/it/beta-pass/caller/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + test + beta-pass + 1.0-SNAPSHOT + + caller + + + ${project.groupId} + api + ${project.version} + + + + + + org.kohsuke + access-modifier-checker + @project.version@ + + + + enforce + + + + + useBeta + true + + + + + + + + + \ No newline at end of file diff --git a/access-modifier-checker/src/it/beta-pass/caller/src/main/java/caller/Caller.java b/access-modifier-checker/src/it/beta-pass/caller/src/main/java/caller/Caller.java new file mode 100644 index 0000000..87b9d72 --- /dev/null +++ b/access-modifier-checker/src/it/beta-pass/caller/src/main/java/caller/Caller.java @@ -0,0 +1,11 @@ +package caller; + +import api.Api; + +public class Caller { + + public Caller() { + Api.experimental(); // OK + } + +} diff --git a/access-modifier-checker/src/it/beta-pass/invoker.properties b/access-modifier-checker/src/it/beta-pass/invoker.properties new file mode 100644 index 0000000..fff78ee --- /dev/null +++ b/access-modifier-checker/src/it/beta-pass/invoker.properties @@ -0,0 +1 @@ +invoker.goals=clean package diff --git a/access-modifier-checker/src/it/beta-pass/pom.xml b/access-modifier-checker/src/it/beta-pass/pom.xml new file mode 100644 index 0000000..a850f94 --- /dev/null +++ b/access-modifier-checker/src/it/beta-pass/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + test + beta-pass + 1.0-SNAPSHOT + pom + + UTF-8 + 1.8 + 1.8 + + + api + caller + + \ No newline at end of file diff --git a/access-modifier-checker/src/main/java/org/kohsuke/accmod/impl/Checker.java b/access-modifier-checker/src/main/java/org/kohsuke/accmod/impl/Checker.java index 514f619..4a6849f 100644 --- a/access-modifier-checker/src/main/java/org/kohsuke/accmod/impl/Checker.java +++ b/access-modifier-checker/src/main/java/org/kohsuke/accmod/impl/Checker.java @@ -48,6 +48,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import static org.objectweb.asm.ClassReader.SKIP_FRAMES; @@ -68,6 +69,8 @@ public class Checker { */ private final ErrorListener errorListener; + private final Properties properties; + /** * Restrictions found from dependencies. *

@@ -83,9 +86,10 @@ public class Checker { private final AccessRestrictionFactory factory; - public Checker(ClassLoader dependencies, ErrorListener errorListener) throws IOException { + Checker(ClassLoader dependencies, ErrorListener errorListener, Properties properties) throws IOException { this.dependencies = dependencies; this.errorListener = errorListener; + this.properties = properties; this.factory = new AccessRestrictionFactory(dependencies); // load access restrictions @@ -325,9 +329,9 @@ public ClassLoader getDependencyClassLoader() { return dependencies; } - public boolean isInTheSameModuleAs(RestrictedElement e) { - // TODO - throw new UnsupportedOperationException(); + @Override + public String getProperty(String key) { + return properties.getProperty(key); } }; }, SKIP_FRAMES); diff --git a/access-modifier-checker/src/main/java/org/kohsuke/accmod/impl/EnforcerMojo.java b/access-modifier-checker/src/main/java/org/kohsuke/accmod/impl/EnforcerMojo.java index 4e616e0..55e5c83 100644 --- a/access-modifier-checker/src/main/java/org/kohsuke/accmod/impl/EnforcerMojo.java +++ b/access-modifier-checker/src/main/java/org/kohsuke/accmod/impl/EnforcerMojo.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Properties; /** * Enforces the {@link Restricted} access modifier annotations. @@ -47,6 +48,12 @@ public class EnforcerMojo extends AbstractMojo { */ private boolean failOnError = true; + /** + * Optional properties to also make available to restriction checkers. + * @parameter + */ + private Properties properties; + public void execute() throws MojoExecutionException, MojoFailureException { if (skip) { getLog().info("Skipping access modifier checks"); @@ -77,7 +84,7 @@ public void onError(Throwable t, Location loc, String msg) { public void onWarning(Throwable t, Location loc, String msg) { getLog().warn(loc+" "+msg,t); } - }); + }, properties != null ? properties : new Properties()); {// if there's restriction list in the inspected module itself, load it as well InputStream self = null; diff --git a/pom.xml b/pom.xml index 55d6ae8..fc456dc 100644 --- a/pom.xml +++ b/pom.xml @@ -65,8 +65,8 @@ maven-compiler-plugin 2.3.2 - 1.7 - 1.7 + 1.8 + 1.8 From f54b26e70d2fa39ffd9e816eefc78725ca897459 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Mon, 26 Mar 2018 18:14:09 -0400 Subject: [PATCH 2/2] Stick to Java 7 bytecode for now. --- .../src/main/java/org/kohsuke/accmod/impl/Location.java | 7 ++++--- pom.xml | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/access-modifier-annotation/src/main/java/org/kohsuke/accmod/impl/Location.java b/access-modifier-annotation/src/main/java/org/kohsuke/accmod/impl/Location.java index 5898cac..7e1df5e 100644 --- a/access-modifier-annotation/src/main/java/org/kohsuke/accmod/impl/Location.java +++ b/access-modifier-annotation/src/main/java/org/kohsuke/accmod/impl/Location.java @@ -75,8 +75,9 @@ public interface Location { */ ClassLoader getDependencyClassLoader(); - default /*@CheckForNull*/ String getProperty(String key) { - return null; - } + /** + * Loads a configuration setting from the environment, such as when configured by a Maven plugin. + */ + /*@CheckForNull*/ String getProperty(String key); } diff --git a/pom.xml b/pom.xml index fc456dc..55d6ae8 100644 --- a/pom.xml +++ b/pom.xml @@ -65,8 +65,8 @@ maven-compiler-plugin 2.3.2 - 1.8 - 1.8 + 1.7 + 1.7