Skip to content

Commit

Permalink
refact: use var where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jun 12, 2024
1 parent 52b2744 commit 90ea3f8
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public int getAsmAPI() {

private void initClassVisitor() {

final AnnotationVisitor annotationVisitor = new AnnotationVisitor(asmAPI) {
final var annotationVisitor = new AnnotationVisitor(asmAPI) {
@Override
public void visit(final String name, final Object value) {
if (value instanceof org.objectweb.asm.Type) {
Expand All @@ -70,15 +70,15 @@ public AnnotationVisitor visitAnnotation(final String name, final String descrip
}
};

final FieldVisitor fieldVisitor = new FieldVisitor(asmAPI) {
final var fieldVisitor = new FieldVisitor(asmAPI) {
@Override
public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
parseSignature(descriptor);
return annotationVisitor;
}
};

final MethodVisitor methodVisitor = new MethodVisitor(asmAPI) {
final var methodVisitor = new MethodVisitor(asmAPI) {

@Override
public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
Expand All @@ -94,7 +94,7 @@ public void visitFieldInsn(final int opcode, final String owner, final String na

@Override
public AnnotationVisitor visitInsnAnnotation(final int typeRef, final TypePath typePath, final String descriptor,
final boolean visible) {
final boolean visible) {
parseSignature(descriptor);
return annotationVisitor;
}
Expand All @@ -108,15 +108,15 @@ public void visitLdcInsn(final Object cst) {

@Override
public void visitLocalVariable(final String name, final String descriptor, final String signature, final Label start,
final Label end, final int index) {
final Label end, final int index) {
if ("this".equals(name) || "super".equals(name))
return;
parseSignature(signature == null ? descriptor : signature);
}

@Override
public AnnotationVisitor visitLocalVariableAnnotation(final int typeRef, final TypePath typePath, final Label[] start,
final Label[] end, final int[] index, final String descriptor, final boolean visible) {
final Label[] end, final int[] index, final String descriptor, final boolean visible) {
parseSignature(descriptor);
return annotationVisitor;
}
Expand All @@ -140,7 +140,7 @@ public AnnotationVisitor visitParameterAnnotation(final int parameter, final Str

@Override
public AnnotationVisitor visitTryCatchAnnotation(final int typeRef, final TypePath typePath, final String descriptor,
final boolean visible) {
final boolean visible) {
parseSignature(descriptor);
return annotationVisitor;
}
Expand All @@ -152,7 +152,7 @@ public void visitTryCatchBlock(final Label start, final Label end, final Label h

@Override
public AnnotationVisitor visitTypeAnnotation(final int typeRef, final TypePath typePath, final String descriptor,
final boolean visible) {
final boolean visible) {
parseSignature(descriptor);
return annotationVisitor;
}
Expand All @@ -166,7 +166,7 @@ public void visitTypeInsn(final int opcode, final String internalClassName) {
classVisitor = new ClassVisitor(asmAPI) {
@Override
public void visit(final int version, final int access, final String name, final String signature, final String superName,
final String[] interfaces) {
final String[] interfaces) {
reportClassReference(superName);
if (interfaces != null) {
for (final String internalClassName : interfaces) {
Expand All @@ -186,7 +186,7 @@ public AnnotationVisitor visitAnnotation(final String descriptor, final boolean

@Override
public FieldVisitor visitField(final int access, final String name, final String descriptor, final String signature,
final Object value) {
final Object value) {
parseSignature(signature == null ? descriptor : signature);
if (value instanceof Type) {
reportClassReference(((Type) value).getInternalName());
Expand All @@ -196,7 +196,7 @@ public FieldVisitor visitField(final int access, final String name, final String

@Override
public MethodVisitor visitMethod(final int access, final String name, final String descriptor, final String signature,
final String[] exceptions) {
final String[] exceptions) {
parseSignature(signature == null ? descriptor : signature);
if (exceptions != null) {
for (final String internalClassName : exceptions) {
Expand All @@ -208,7 +208,7 @@ public MethodVisitor visitMethod(final int access, final String name, final Stri

@Override
public AnnotationVisitor visitTypeAnnotation(final int typeRef, final TypePath typePath, final String descriptor,
final boolean visible) {
final boolean visible) {
parseSignature(descriptor);
return annotationVisitor;
}
Expand Down Expand Up @@ -240,7 +240,7 @@ private void reportClassReference(final String internalClassName) {
}

public void scan(final InputStream classByteCode) throws IOException {
final ClassReader cr = new ClassReader(classByteCode);
final var cr = new ClassReader(classByteCode);
onClassName(Strings.replaceChars(cr.getClassName(), "/\\", "."));
cr.accept(classVisitor, ClassReader.SKIP_FRAMES);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
package com.vegardit.maven.plugin.depcheck;

import static net.sf.jstuff.core.Strings.*;
import static net.sf.jstuff.core.Strings.NEW_LINE;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -94,7 +94,7 @@ private void assertNoViolations(final List<DepsAnalyzer.ScanResult> results) thr
return;
}

final StringBuilder sb = new StringBuilder();
final var sb = new StringBuilder();

sb.append("The following violations have been detected:").append(NEW_LINE);
for (final DepsAnalyzer.ScanResult result : results) {
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/com/vegardit/maven/plugin/depcheck/DepsAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -85,7 +84,7 @@ public DepsAnalyzer(final AbstractMojo mojo) {
*/
private Map<String, Artifact> getClassesDeclaredByTransitiveDependencies() throws MojoExecutionException {

final Map<String, Artifact> classesDeclaredByTransitiveDependencies = new HashMap<>();
final var classesDeclaredByTransitiveDependencies = new HashMap<String, Artifact>();
for (final Artifact transDep : MavenUtils.withoutRuntimeAndTestScoped(mojo.getTransitiveDependencies())) {
if (!isArtifactWithClasses(transDep)) {
if (isVerbose) {
Expand Down Expand Up @@ -122,8 +121,8 @@ private Map<String, Artifact> getClassesDeclaredByTransitiveDependencies() throw
private MavenProject getReactorProject(final Artifact artifact) {
for (final MavenProject p : mojo.getReactorProjects()) {
if (p.getGroupId().equals(artifact.getGroupId()) //
&& p.getArtifactId().equals(artifact.getArtifactId()) //
&& p.getVersion().equals(artifact.getVersion()) //
&& p.getArtifactId().equals(artifact.getArtifactId()) //
&& p.getVersion().equals(artifact.getVersion()) //
)
return p;
}
Expand All @@ -143,14 +142,14 @@ boolean isArtifactWithClasses(final Artifact artifact) {
private boolean isReactorProject(final Artifact artifact) {
return mojo.getReactorProjects().stream().anyMatch(p -> //
p.getGroupId().equals(artifact.getGroupId()) //
&& p.getArtifactId().equals(artifact.getArtifactId())//
&& p.getVersion().equals(artifact.getVersion()) //
&& p.getArtifactId().equals(artifact.getArtifactId())//
&& p.getVersion().equals(artifact.getVersion()) //
);
}

public ScanResult scan(final boolean checkForUnusedDependencies, final boolean checkForUsedTransitiveDependencies)
throws MojoExecutionException {
final ScanResult result = new ScanResult(project);
throws MojoExecutionException {
final var result = new ScanResult(project);

if (!isArtifactWithClasses(project.getArtifact()))
return result;
Expand Down Expand Up @@ -218,7 +217,7 @@ public ScanResult scan(final boolean checkForUnusedDependencies, final boolean c
}
}
log.info(" => Found " + Pluralized.dependencies(directDeps.size(), "direct") + " with " + Pluralized.classes(directDepsClassCount)
+ ".");
+ ".");
if (checkForUnusedDependencies) {
if (result.hasUnusedDirectDependencies()) {
log.warn(" => " + Pluralized.dependencies(result.unusedDirectDependencies.size(), "potentially unused") + " found:");
Expand Down Expand Up @@ -285,8 +284,8 @@ private Set<String> scanArtifactForDeclaredClasses(final Artifact artifactWithJa
if (artifactWithJar.getFile() == null)
return Collections.emptySet();

try (JarFile jar = new JarFile(artifactWithJar.getFile())) {
final Set<String> classesDeclaredInJar = new HashSet<>();
try (var jar = new JarFile(artifactWithJar.getFile())) {
final var classesDeclaredInJar = new HashSet<String>();
// iterate over the jar's entries
for (final JarEntry jarEntry : Enumerations.toIterable(jar.entries())) {

Expand Down Expand Up @@ -338,8 +337,8 @@ private Tuple2<Set<String>, Set<String>> scanDirectoryForDeclaredAndReferencedCl
return Tuple2.create(Collections.emptySet(), Collections.emptySet());
}

final Set<String> declaredClasses = new HashSet<>(); // classes declared by current project
final Set<String> referencedClasses = new HashSet<>(); // classes referenced by current project
final var declaredClasses = new HashSet<String>(); // classes declared by current project
final var referencedClasses = new HashSet<String>(); // classes referenced by current project

Files.walkFileTree(classDirectory, new SimpleFileVisitor<Path>() {
@Override
Expand All @@ -352,7 +351,7 @@ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attr
log.info(" -> Analyzing class file: " + file);
}

try (InputStream classByteCode = new BufferedInputStream(Files.newInputStream(file))) {
try (var classByteCode = new BufferedInputStream(Files.newInputStream(file))) {
new AbstractClassAnalyzer(Opcodes.ASM9) {
@Override
protected void onClassName(final String nameOfReferencedClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public final class FixTransDepsMojo extends AbstractMojo {

private void backupPom(final File pomFile) throws MojoExecutionException {
final String backupPomFileName = (backupPomPrefix == null ? "" : backupPomPrefix) + pomFile.getName() + (backupPomSuffix == null ? ""
: backupPomSuffix);
final File backupPom = new File(pomFile.getParentFile(), backupPomFileName);
: backupPomSuffix);
final var backupPom = new File(pomFile.getParentFile(), backupPomFileName);
log.debug("Creating backup of " + pomFile + "...");
try {
Files.copy(pomFile, backupPom);
Expand Down Expand Up @@ -163,7 +163,7 @@ private void fixPOM(final DepsAnalyzer.ScanResult result) throws MojoExecutionEx
/*
* construct new pom content
*/
final StringBuilder pomContentNew = new StringBuilder() //
final var pomContentNew = new StringBuilder() //
.append(pomContent.substring(0, insertBefore)).append(newline);

if (insertDependenciesTag) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/vegardit/maven/util/AbstractMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public List<MavenProject> getReactorProjects() {
*/
public Set<Artifact> getTransitiveDependencies() {

final Set<Artifact> deps = new HashSet<>(mvnCurrentProject.getArtifacts());
final var deps = new HashSet<>(mvnCurrentProject.getArtifacts());
deps.removeAll(getDirectDependencies());

if (verbose) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
package com.vegardit.maven.plugin.depcheck;

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

Expand All @@ -18,7 +18,7 @@ public class DepsAnalyzerTest extends AbstractMavenTest {

@Test
public void testIsAnonymousInnerClass() throws Exception {
final DepsAnalyzer da = new DepsAnalyzer(getMojo(getSession("empty-project-with-check-deps"), CheckDepsMojo.MAVEN_GOAL));
final var da = new DepsAnalyzer(getMojo(getSession("empty-project-with-check-deps"), CheckDepsMojo.MAVEN_GOAL));

assertThat(da.isAnonymousInnerClass(null)).isFalse();
assertThat(da.isAnonymousInnerClass("")).isFalse();
Expand All @@ -30,7 +30,7 @@ public void testIsAnonymousInnerClass() throws Exception {

@Test
public void testIsArtifactWithClasses() throws Exception {
final DepsAnalyzer da = new DepsAnalyzer(getMojo(getSession("empty-project-with-check-deps"), CheckDepsMojo.MAVEN_GOAL));
final var da = new DepsAnalyzer(getMojo(getSession("empty-project-with-check-deps"), CheckDepsMojo.MAVEN_GOAL));

assertThat(da.isArtifactWithClasses(new DummyArtifact().withType("ejb"))).isTrue();
assertThat(da.isArtifactWithClasses(new DummyArtifact().withType("jar"))).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
package com.vegardit.maven.plugin.depcheck;

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -49,10 +49,10 @@ public void testUsedTransDeps() throws Exception {
mojo.backupPomPrefix = "foo";
mojo.backupPomSuffix = "bar";

final File pom = new File(sess.getCurrentProject().getBasedir(), "pom.xml");
final var pom = new File(sess.getCurrentProject().getBasedir(), "pom.xml");
final String pomOriginalContent = FileUtils.readFileToString(pom, Charset.defaultCharset());

final File pomBackup = new File(sess.getCurrentProject().getBasedir(), mojo.backupPomPrefix + "pom.xml" + mojo.backupPomSuffix);
final var pomBackup = new File(sess.getCurrentProject().getBasedir(), mojo.backupPomPrefix + "pom.xml" + mojo.backupPomSuffix);
assertThat(pomBackup).doesNotExist();

assertThat(pomOriginalContent).doesNotContain("<artifactId>commons-lang3</artifactId>");
Expand Down

0 comments on commit 90ea3f8

Please sign in to comment.