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

Configure Checkstyle (#38) #39

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=castor-software_depclean&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=castor-software_depclean)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=castor-software_depclean&metric=security_rating)](https://sonarcloud.io/dashboard?id=castor-software_depclean)
[![Maven Central](https://img.shields.io/maven-central/v/se.kth.castor/depclean-core.svg)](https://search.maven.org/search?q=g:se.kth.castor%20AND%20a:depclean*)
[![Licence](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/castor-software/depclean/blob/master/LICENSE.md)
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=castor-software_depclean&metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=castor-software_depclean)
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=castor-software_depclean&metric=bugs)](https://sonarcloud.io/dashboard?id=castor-software_depclean)
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=castor-software_depclean&metric=code_smells)](https://sonarcloud.io/dashboard?id=castor-software_depclean)
Expand Down
354 changes: 354 additions & 0 deletions checkstyle.xml

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions depclean-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,50 @@
<sonar.coverage.jacoco.xmlReportPaths>target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
</properties>

<build>
<plugins>
<!-- Checkstyle-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.41</version>
</dependency>
</dependencies>
<executions>
<execution>
<configuration>
<!-- Change to warning to enforce strong style rules -->
<violationSeverity>error</violationSeverity>
<failsOnError>true</failsOnError>
<!-- Google style is adopted, https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml -->
<configLocation>../checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<consoleOutput>true</consoleOutput>
</configuration>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
<execution>
<phase>verify</phase>
<goals>
<!-- Generate a Checkstyle report -->
<goal>checkstyle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<!-- Bytecode manipulation -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package se.kth.depclean.core.analysis;

import lombok.Data;

import java.util.Set;
import lombok.Data;

@Data
public class ArtifactTypes {

private Set<String> allTypes;
private Set<String> usedTypes;
private Set<String> allTypes;
private Set<String> usedTypes;

public ArtifactTypes(Set<String> allTypes, Set<String> usedTypes) {
this.allTypes = allTypes;
this.usedTypes = usedTypes;
}
public ArtifactTypes(Set<String> allTypes, Set<String> usedTypes) {
this.allTypes = allTypes;
this.usedTypes = usedTypes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
/**
* Gets the set of classes contained in a library given either as a jar file or an exploded directory.
*/
public interface ClassAnalyzer
{
public interface ClassAnalyzer {

// fields -----------------------------------------------------------------
// fields -----------------------------------------------------------------

String ROLE = ClassAnalyzer.class.getName();
String ROLE = ClassAnalyzer.class.getName();

// public methods ---------------------------------------------------------
// public methods ---------------------------------------------------------

Set<String> analyze(URL url)
Set<String> analyze(URL url)
throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import java.io.InputStream;

public interface ClassFileVisitor
{
void visitClass(String className, InputStream in);
public interface ClassFileVisitor {

void visitClass(String className, InputStream in);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
* under the License.
*/

import org.codehaus.plexus.util.DirectoryScanner;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -30,105 +28,100 @@
import java.net.URL;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import org.codehaus.plexus.util.DirectoryScanner;

/**
* Utility to visit classes in a library given either as a jar file or an exploded directory.
*/
public final class ClassFileVisitorUtils
{
// constants --------------------------------------------------------------

private static final String[] CLASS_INCLUDES = {"**/*.class"};
public static final String CLASS = ".class";

// constructors -----------------------------------------------------------

private ClassFileVisitorUtils()
{
// private constructor for utility class
}

// public methods ---------------------------------------------------------

public static void accept(URL url, ClassFileVisitor visitor)
throws IOException
{
if (url.getPath().endsWith(".jar")) {
acceptJar(url, visitor);
} else {
final String message = "Cannot accept visitor on URL: ";
if (url.getProtocol().equalsIgnoreCase("file")) {
try {
File file = new File(new URI(url.toString()));

if (file.isDirectory()) {
acceptDirectory(file, visitor);
} else if (file.exists()) {
throw new IllegalArgumentException(message + url);
}
} catch (URISyntaxException exception) {
throw new IllegalArgumentException(message + url, exception);
}
} else {
throw new IllegalArgumentException(message + url);
}
public final class ClassFileVisitorUtils {
// constants --------------------------------------------------------------

private static final String[] CLASS_INCLUDES = {"**/*.class"};
public static final String CLASS = ".class";

// constructors -----------------------------------------------------------

private ClassFileVisitorUtils() {
// private constructor for utility class
}

// public methods ---------------------------------------------------------

public static void accept(URL url, ClassFileVisitor visitor)
throws IOException {
if (url.getPath().endsWith(".jar")) {
acceptJar(url, visitor);
} else {
final String message = "Cannot accept visitor on URL: ";
if (url.getProtocol().equalsIgnoreCase("file")) {
try {
File file = new File(new URI(url.toString()));

if (file.isDirectory()) {
acceptDirectory(file, visitor);
} else if (file.exists()) {
throw new IllegalArgumentException(message + url);
}
} catch (URISyntaxException exception) {
throw new IllegalArgumentException(message + url, exception);
}
} else {
throw new IllegalArgumentException(message + url);
}
}

// private methods --------------------------------------------------------

private static void acceptJar(URL url, ClassFileVisitor visitor)
throws IOException
{
try (JarInputStream in = new JarInputStream(url.openStream())) {
JarEntry entry = null;
while ((entry = in.getNextJarEntry()) != null) {//NOSONAR
String name = entry.getName();
// ignore files like package-info.class and module-info.class
if (name.endsWith(CLASS) && name.indexOf('-') == -1) {
visitClass(name, in, visitor);
}
}
}

// private methods --------------------------------------------------------

private static void acceptJar(URL url, ClassFileVisitor visitor)
throws IOException {
try (JarInputStream in = new JarInputStream(url.openStream())) {
JarEntry entry = null;
while ((entry = in.getNextJarEntry()) != null) { //NOSONAR
String name = entry.getName();
// ignore files like package-info.class and module-info.class
if (name.endsWith(CLASS) && name.indexOf('-') == -1) {
visitClass(name, in, visitor);
}
}
}
}

private static void acceptDirectory(File directory, ClassFileVisitor visitor)
throws IOException
{
if (!directory.isDirectory()) {
throw new IllegalArgumentException("File is not a directory");
}
private static void acceptDirectory(File directory, ClassFileVisitor visitor)
throws IOException {
if (!directory.isDirectory()) {
throw new IllegalArgumentException("File is not a directory");
}

DirectoryScanner scanner = new DirectoryScanner();
DirectoryScanner scanner = new DirectoryScanner();

scanner.setBasedir(directory);
scanner.setIncludes(CLASS_INCLUDES);
scanner.setBasedir(directory);
scanner.setIncludes(CLASS_INCLUDES);

scanner.scan();
scanner.scan();

String[] paths = scanner.getIncludedFiles();
String[] paths = scanner.getIncludedFiles();

for (String path : paths) {
path = path.replace(File.separatorChar, '/');
for (String path : paths) {
path = path.replace(File.separatorChar, '/');

File file = new File(directory, path);
File file = new File(directory, path);

try (FileInputStream in = new FileInputStream(file)) {
visitClass(path, in, visitor);
}
}
try (FileInputStream in = new FileInputStream(file)) {
visitClass(path, in, visitor);
}
}
}

private static void visitClass(String path, InputStream in, ClassFileVisitor visitor)
{
if (!path.endsWith(CLASS)) {
throw new IllegalArgumentException("Path is not a class");
}
private static void visitClass(String path, InputStream in, ClassFileVisitor visitor) {
if (!path.endsWith(CLASS)) {
throw new IllegalArgumentException("Path is not a class");
}

String className = path.substring(0, path.length() - CLASS.length());
String className = path.substring(0, path.length() - CLASS.length());

className = className.replace('/', '.');
className = className.replace('/', '.');

visitor.visitClass(className, in);
}
visitor.visitClass(className, in);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,30 @@
* @see #getClasses()
*/
public class CollectorClassFileVisitor
implements ClassFileVisitor
{
// fields -----------------------------------------------------------------
implements ClassFileVisitor {
// fields -----------------------------------------------------------------

private final Set<String> classes;
private final Set<String> classes;

// constructors -----------------------------------------------------------
// constructors -----------------------------------------------------------

public CollectorClassFileVisitor()
{
classes = new HashSet<>();
}
public CollectorClassFileVisitor() {
classes = new HashSet<>();
}

// ClassFileVisitor methods -----------------------------------------------
// ClassFileVisitor methods -----------------------------------------------

/*
* @see org.apache.invoke.shared.dependency.analyzer.ClassFileVisitor#visitClass(java.lang.String,
* java.io.InputStream)
*/
public void visitClass(String className, InputStream in)
{
classes.add(className);
}
/*
* @see org.apache.invoke.shared.dependency.analyzer.ClassFileVisitor#visitClass(java.lang.String,
* java.io.InputStream)
*/
public void visitClass(String className, InputStream in) {
classes.add(className);
}

// public methods ---------------------------------------------------------
// public methods ---------------------------------------------------------

public Set<String> getClasses()
{
return classes;
}
public Set<String> getClasses() {
return classes;
}
}
Loading