Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MCHECKSTYLE-453] Convert injection to Sisu Guice #160

Merged
merged 9 commits into from
Nov 24, 2024
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
18 changes: 5 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ under the License.

<!-- plexus -->
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>
<version>2.2.0</version>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.inject</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
Expand Down Expand Up @@ -325,16 +325,8 @@ under the License.
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>2.2.0</version>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
</goals>
</execution>
</executions>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.apache.maven.model.PluginManagement;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutor;
import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutorException;
Expand All @@ -62,8 +61,6 @@

/**
* Base abstract class for Checkstyle reports.
*
*
*/
public abstract class AbstractCheckstyleReport extends AbstractMavenReport {
protected static final String JAVA_FILES = "**\\/*.java";
Expand Down Expand Up @@ -441,31 +438,35 @@ public abstract class AbstractCheckstyleReport extends AbstractMavenReport {
@Parameter(property = "checkstyle.excludeGeneratedSources", defaultValue = "false")
private boolean excludeGeneratedSources;

/**
*/
@Component
protected ResourceManager locator;

/**
* @since 2.5
*/
@Component(role = CheckstyleExecutor.class, hint = "default")
protected CheckstyleExecutor checkstyleExecutor;
protected final CheckstyleExecutor checkstyleExecutor;

/**
* Internationalization component
*/
@Component
private I18N i18n;

protected ByteArrayOutputStream stringOutputStream;

public AbstractCheckstyleReport(
final ResourceManager locator, final CheckstyleExecutor checkstyleExecutor, final I18N i18n) {
this.locator = locator;
this.checkstyleExecutor = checkstyleExecutor;
this.i18n = i18n;
}

/** {@inheritDoc} */
@Override
public String getName(Locale locale) {
return getI18nString(locale, "name");
}

/** {@inheritDoc} */
@Override
public String getDescription(Locale locale) {
return getI18nString(locale, "description");
}
Expand All @@ -479,6 +480,7 @@ protected String getI18nString(Locale locale, String key) {
return i18n.getString("checkstyle-report", locale, "report.checkstyle." + key);
}

@Override
protected MavenProject getProject() {
return project;
}
Expand All @@ -488,6 +490,7 @@ protected List<MavenProject> getReactorProjects() {
}

/** {@inheritDoc} */
@Override
public void executeReport(Locale locale) throws MavenReportException {
checkDeprecatedParameterUsage(sourceDirectory, "sourceDirectory", "sourceDirectories");
checkDeprecatedParameterUsage(testSourceDirectory, "testSourceDirectory", "testSourceDirectories");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,39 @@
*/
package org.apache.maven.plugins.checkstyle;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutor;
import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutorRequest;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.resource.ResourceManager;

/**
* A reporting task that performs Checkstyle analysis and generates an aggregate
* HTML report on the violations that Checkstyle finds in a multi-module reactor
* build.
*
*
*/
@Mojo(
name = "checkstyle-aggregate",
aggregator = true,
requiresDependencyResolution = ResolutionScope.COMPILE,
threadSafe = true)
public class CheckstyleAggregateReport extends AbstractCheckstyleReport {

@Inject
public CheckstyleAggregateReport(
ResourceManager locator, @Named("default") CheckstyleExecutor checkstyleExecutor, I18N i18n) {
super(locator, checkstyleExecutor, i18n);
}

/**
* {@inheritDoc}
*/
@Override
protected CheckstyleExecutorRequest createRequest() throws MavenReportException {
CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
request.setAggregate(true)
Expand Down Expand Up @@ -70,11 +82,13 @@ protected CheckstyleExecutorRequest createRequest() throws MavenReportException
}

/** {@inheritDoc} */
@Override
public String getOutputName() {
return "checkstyle-aggregate";
}

/** {@inheritDoc} */
@Override
public boolean canGenerateReport() {
// TODO: would be good to scan the files here
return !skip && project.isExecutionRoot() && reactorProjects.size() > 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@
*/
package org.apache.maven.plugins.checkstyle;

import javax.inject.Inject;
import javax.inject.Named;

import java.io.File;
import java.util.List;

import org.apache.maven.model.Resource;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutor;
import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutorRequest;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.resource.ResourceManager;

/**
* A reporting task that performs Checkstyle analysis and generates an HTML
Expand All @@ -38,9 +44,19 @@
*/
@Mojo(name = "checkstyle", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
public class CheckstyleReport extends AbstractCheckstyleReport {

@Inject
public CheckstyleReport(
final ResourceManager locator,
final @Named("default") CheckstyleExecutor checkstyleExecutor,
final I18N i18n) {
super(locator, checkstyleExecutor, i18n);
}

/**
* {@inheritDoc}
*/
@Override
protected CheckstyleExecutorRequest createRequest() throws MavenReportException {
CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
request.setConsoleListener(getConsoleListener())
Expand Down Expand Up @@ -70,11 +86,13 @@ protected CheckstyleExecutorRequest createRequest() throws MavenReportException
}

/** {@inheritDoc} */
@Override
public String getOutputName() {
return "checkstyle";
}

/** {@inheritDoc} */
@Override
public boolean canGenerateReport() {
if (skip) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.maven.plugins.checkstyle;

import javax.inject.Inject;
import javax.inject.Named;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -47,7 +50,6 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -294,12 +296,6 @@ public class CheckstyleViolationCheckMojo extends AbstractMojo {
@Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
private String inputEncoding;

/**
* @since 2.5
*/
@Component(role = CheckstyleExecutor.class, hint = "default")
protected CheckstyleExecutor checkstyleExecutor;

/**
* Output errors to console.
*/
Expand Down Expand Up @@ -489,7 +485,18 @@ public class CheckstyleViolationCheckMojo extends AbstractMojo {

private File outputXmlFile;

/**
* @since 2.5
*/
protected final CheckstyleExecutor checkstyleExecutor;

@Inject
public CheckstyleViolationCheckMojo(final @Named("default") CheckstyleExecutor checkstyleExecutor) {
this.checkstyleExecutor = checkstyleExecutor;
}

/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
checkDeprecatedParameterUsage(sourceDirectory, "sourceDirectory", "sourceDirectories");
checkDeprecatedParameterUsage(testSourceDirectory, "testSourceDirectory", "testSourceDirectories");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
/**
* @author Olivier Lamy
* @since 2.5
*
*/
public interface CheckstyleExecutor {

/**
* @param request {@link CheckstyleExecutorRequest}
* @return {@link CheckstyleResults}
* @throws CheckstyleExecutorException in case of an error during plugin execution.
* @throws CheckstyleException in case of an error raised by Checkstyle.
* @throws CheckstyleExecutorException in case of an error during plugin execution
* @throws CheckstyleException in case of an error raised by Checkstyle
*/
CheckstyleResults executeCheckstyle(CheckstyleExecutorRequest request)
throws CheckstyleExecutorException, CheckstyleException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.maven.plugins.checkstyle.exec;

import javax.inject.Inject;
import javax.inject.Named;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -49,28 +52,33 @@
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.model.Resource;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.resource.ResourceManager;
import org.codehaus.plexus.resource.loader.FileResourceCreationException;
import org.codehaus.plexus.resource.loader.FileResourceLoader;
import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.sisu.Typed;

/**
* @author Olivier Lamy
* @since 2.5
*
*/
@Component(role = CheckstyleExecutor.class, hint = "default", instantiationStrategy = "per-lookup")
@Named
@Typed(CheckstyleExecutor.class)
public class DefaultCheckstyleExecutor extends AbstractLogEnabled implements CheckstyleExecutor {
@Requirement(hint = "default")
private ResourceManager locator;
private final ResourceManager locator;

@Requirement(hint = "license")
private ResourceManager licenseLocator;
private final ResourceManager licenseLocator;

@Inject
public DefaultCheckstyleExecutor(
final @Named("default") ResourceManager locator, final @Named("license") ResourceManager licenseLocator) {
this.locator = locator;
this.licenseLocator = licenseLocator;
}

@Override
public CheckstyleResults executeCheckstyle(CheckstyleExecutorRequest request)
throws CheckstyleExecutorException, CheckstyleException {
if (getLogger().isDebugEnabled()) {
Expand Down Expand Up @@ -255,6 +263,7 @@ protected void addSourceDirectory(
}
}

@Override
public Configuration getConfiguration(CheckstyleExecutorRequest request) throws CheckstyleExecutorException {
try {
// Checkstyle will always use the context classloader in order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
package org.apache.maven.plugins.checkstyle.resource;

import javax.inject.Inject;
import javax.inject.Named;

import java.util.Map;

import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.resource.DefaultResourceManager;
import org.codehaus.plexus.resource.PlexusResource;
import org.codehaus.plexus.resource.ResourceManager;
import org.codehaus.plexus.resource.loader.ResourceLoader;
import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
import org.codehaus.plexus.resource.loader.ThreadContextClasspathResourceLoader;
Expand All @@ -38,17 +36,17 @@
*
* @since 2.12
*/
@Component(role = ResourceManager.class, hint = "license", instantiationStrategy = "per-lookup")
@Named("license")
public class LicenseResourceManager extends DefaultResourceManager {

private static final Logger LOGGER = LoggerFactory.getLogger(LicenseResourceManager.class);

@Requirement(role = ResourceLoader.class)
private Map<String, ResourceLoader> resourceLoaders;
private final Map<String, ResourceLoader> resourceLoaders;

@Inject
public LicenseResourceManager(Map<String, ResourceLoader> resourceLoaders) {
super(resourceLoaders);
this.resourceLoaders = resourceLoaders;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
public class CheckstyleReportTest extends AbstractCheckstyleTestCase {
public void testNoSource() throws Exception {
File generatedReport = generateReport(getGoal(), "no-source-plugin-config.xml");
assertFalse(FileUtils.fileExists(generatedReport.getAbsolutePath()));
String absolutePath = generatedReport.getAbsolutePath();
assertFalse(absolutePath + " exists", FileUtils.fileExists(absolutePath));
}

public void testMinConfiguration() throws Exception {
Expand Down
Loading