Skip to content

Commit

Permalink
[MCHECKSTYLE-453] Convert injection to Sisu Guice (#160)
Browse files Browse the repository at this point in the history
* sisu plugin
  • Loading branch information
elharo authored Nov 24, 2024
1 parent d97687d commit 6c1c802
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 49 deletions.
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 @@ -61,8 +60,6 @@

/**
* Base abstract class for Checkstyle reports.
*
*
*/
public abstract class AbstractCheckstyleReport extends AbstractMavenReport {
protected static final String JAVA_FILES = "**\\/*.java";
Expand Down Expand Up @@ -440,25 +437,27 @@ 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) {
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 @@ -487,6 +483,16 @@ 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 {
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.checkstyle.exec;

import org.apache.maven.plugin.testing.AbstractMojoTestCase;

public class CheckstyleExecutorTest extends AbstractMojoTestCase {
public void testDefaultConfig() throws Exception {
CheckstyleExecutor executor = (CheckstyleExecutor) lookup(CheckstyleExecutor.class);
assertNotNull("project null.", executor);
}
}

0 comments on commit 6c1c802

Please sign in to comment.