Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@ public interface Location {
* access restrictions.
*/
ClassLoader getDependencyClassLoader();

/**
* Loads a configuration setting from the environment, such as when configured by a Maven plugin.
*/
/*@CheckForNull*/ String getProperty(String key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is @CheckForNull commented out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this annotation library is not a dependency.


}
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to add support of some info/justification text to DoNotUse so that error/warning messages can provide more details. Not a part for this PR I'd guess

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just cover that in Javadoc.


@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");
}

}
17 changes: 17 additions & 0 deletions access-modifier-checker/src/it/beta-fail/api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>test</groupId>
<artifactId>beta-fail</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>api</artifactId>
<dependencies>
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>access-modifier-annotation</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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
}

}
33 changes: 33 additions & 0 deletions access-modifier-checker/src/it/beta-fail/caller/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>test</groupId>
<artifactId>beta-fail</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>caller</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.kohsuke</groupId>
<artifactId>access-modifier-checker</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package caller;

import api.Api;

public class Caller {

public Caller() {
Api.experimental(); // illegal
}

}
2 changes: 2 additions & 0 deletions access-modifier-checker/src/it/beta-fail/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals=clean package
invoker.buildResult = failure
17 changes: 17 additions & 0 deletions access-modifier-checker/src/it/beta-fail/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>beta-fail</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modules>
<module>api</module>
<module>caller</module>
</modules>
</project>
1 change: 1 addition & 0 deletions access-modifier-checker/src/it/beta-fail/postbuild.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert new File(basedir, 'build.log').text.contains('[ERROR] caller/Caller:8 api/Api.experimental()V is still in beta')
17 changes: 17 additions & 0 deletions access-modifier-checker/src/it/beta-pass/api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>test</groupId>
<artifactId>beta-pass</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>api</artifactId>
<dependencies>
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>access-modifier-annotation</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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
}

}
41 changes: 41 additions & 0 deletions access-modifier-checker/src/it/beta-pass/caller/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>test</groupId>
<artifactId>beta-pass</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>caller</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.kohsuke</groupId>
<artifactId>access-modifier-checker</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<properties>
<property>
<name>useBeta</name>
<value>true</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package caller;

import api.Api;

public class Caller {

public Caller() {
Api.experimental(); // OK
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=clean package
17 changes: 17 additions & 0 deletions access-modifier-checker/src/it/beta-pass/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>beta-pass</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modules>
<module>api</module>
<module>caller</module>
</modules>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -68,6 +69,8 @@ public class Checker {
*/
private final ErrorListener errorListener;

private final Properties properties;

/**
* Restrictions found from dependencies.
* <p>
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down