From 1ba563caae003c557d6fff5d9ad0e9c2cef7865c Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 11 Dec 2023 15:15:09 -0800 Subject: [PATCH] Enable Spotless (#78) --- .../BridgeMethodsAdded.java | 3 +- .../WithBridgeMethods.java | 3 +- injector/pom.xml | 109 ++++++------ .../MethodInjector.java | 157 ++++++++++-------- .../bridge_method_injector/ProcessMojo.java | 28 ++-- injector/src/test/client/Main.java | 3 +- pom.xml | 47 +++--- 7 files changed, 187 insertions(+), 163 deletions(-) diff --git a/annotation/src/main/java/com/infradna/tool/bridge_method_injector/BridgeMethodsAdded.java b/annotation/src/main/java/com/infradna/tool/bridge_method_injector/BridgeMethodsAdded.java index b2af714..9122215 100644 --- a/annotation/src/main/java/com/infradna/tool/bridge_method_injector/BridgeMethodsAdded.java +++ b/annotation/src/main/java/com/infradna/tool/bridge_method_injector/BridgeMethodsAdded.java @@ -39,5 +39,4 @@ */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.TYPE) -public @interface BridgeMethodsAdded { -} +public @interface BridgeMethodsAdded {} diff --git a/annotation/src/main/java/com/infradna/tool/bridge_method_injector/WithBridgeMethods.java b/annotation/src/main/java/com/infradna/tool/bridge_method_injector/WithBridgeMethods.java index 082118f..ba2b445 100644 --- a/annotation/src/main/java/com/infradna/tool/bridge_method_injector/WithBridgeMethods.java +++ b/annotation/src/main/java/com/infradna/tool/bridge_method_injector/WithBridgeMethods.java @@ -23,13 +23,12 @@ */ package com.infradna.tool.bridge_method_injector; -import org.jvnet.hudson.annotation_indexer.Indexed; - import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.jvnet.hudson.annotation_indexer.Indexed; /** * Request that bridge methods of the same name and same arguments be generated diff --git a/injector/pom.xml b/injector/pom.xml index 4ca8192..42c9d03 100644 --- a/injector/pom.xml +++ b/injector/pom.xml @@ -6,21 +6,67 @@ bridge-method-injector-parent ${revision}${changelist} + bridge-method-injector maven-plugin - bridge-method-injector Evolve your classes without breaking compatibility + + ${maven.version} + + 9.6 3.10.2 3.8.1 - - ${maven.version} - + + + ${project.groupId} + bridge-method-annotation + ${project.version} + + + com.github.spotbugs + spotbugs-annotations + true + + + com.google.code.findbugs + jsr305 + + + + + org.ow2.asm + asm + ${asm.version} + + + org.ow2.asm + asm-commons + ${asm.version} + + + org.apache.maven + maven-plugin-api + ${maven.version} + provided + + + org.apache.maven.plugin-tools + maven-plugin-annotations + ${maven-plugin-tools.version} + provided + + + junit + junit + test + + @@ -46,8 +92,8 @@ - - + + @@ -57,7 +103,7 @@ - + @@ -81,7 +127,7 @@ - + @@ -118,51 +164,4 @@ - - - - ${project.groupId} - bridge-method-annotation - ${project.version} - - - com.github.spotbugs - spotbugs-annotations - true - - - com.google.code.findbugs - jsr305 - - - - - org.ow2.asm - asm - ${asm.version} - - - org.ow2.asm - asm-commons - ${asm.version} - - - org.apache.maven - maven-plugin-api - ${maven.version} - provided - - - org.apache.maven.plugin-tools - maven-plugin-annotations - ${maven-plugin-tools.version} - provided - - - junit - junit - test - - - diff --git a/injector/src/main/java/com/infradna/tool/bridge_method_injector/MethodInjector.java b/injector/src/main/java/com/infradna/tool/bridge_method_injector/MethodInjector.java index ba7105d..a30ffa4 100644 --- a/injector/src/main/java/com/infradna/tool/bridge_method_injector/MethodInjector.java +++ b/injector/src/main/java/com/infradna/tool/bridge_method_injector/MethodInjector.java @@ -35,6 +35,7 @@ import static org.objectweb.asm.Opcodes.POP; import static org.objectweb.asm.Opcodes.POP2; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; @@ -42,9 +43,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; @@ -75,10 +73,11 @@ public void handleRecursively(File f) throws IOException { public void handle(File classFile) throws IOException { byte[] image; - try (FileInputStream in = new FileInputStream(classFile); BufferedInputStream bis = new BufferedInputStream(in)) { + try (FileInputStream in = new FileInputStream(classFile); + BufferedInputStream bis = new BufferedInputStream(in)) { ClassReader cr = new ClassReader(bis); - ClassWriter cw = new ClassWriter(cr,COMPUTE_MAXS); - cr.accept(new Transformer(new ClassAnnotationInjectorImpl(cw)),0); + ClassWriter cw = new ClassWriter(cr, COMPUTE_MAXS); + cr.accept(new Transformer(new ClassAnnotationInjectorImpl(cw)), 0); image = cw.toByteArray(); } catch (AlreadyUpToDate unused) { // no need to process this class. it's already up-to-date. @@ -97,7 +96,7 @@ public void handle(File classFile) throws IOException { * Thrown to indicate that there's no need to re-process this class file. */ static class AlreadyUpToDate extends RuntimeException { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; } static class ClassAnnotationInjectorImpl extends ClassAnnotationInjector { @@ -113,40 +112,39 @@ protected void emit() { } private static class WithBridgeMethodsAnnotationVisitor extends AnnotationVisitor { - protected boolean castRequired = false; - protected String adapterMethod = null; - protected final List types = new ArrayList<>(); - - public WithBridgeMethodsAnnotationVisitor(AnnotationVisitor av) { - super(Opcodes.ASM9, av); - } - - @Override - public AnnotationVisitor visitArray(String name) { - return new AnnotationVisitor(Opcodes.ASM9, super.visitArray(name)) { - - @Override - public void visit(String name, Object value) { - if (value instanceof Type) { - // assume this is a member of the array of classes named "value" in WithBridgeMethods - types.add((Type) value); - } - super.visit(name, value); - } + protected boolean castRequired = false; + protected String adapterMethod = null; + protected final List types = new ArrayList<>(); + + public WithBridgeMethodsAnnotationVisitor(AnnotationVisitor av) { + super(Opcodes.ASM9, av); + } - }; - } + @Override + public AnnotationVisitor visitArray(String name) { + return new AnnotationVisitor(Opcodes.ASM9, super.visitArray(name)) { - @Override - public void visit(String name, Object value) { - if ("castRequired".equals(name) && value instanceof Boolean) { - castRequired = (Boolean) value; + @Override + public void visit(String name, Object value) { + if (value instanceof Type) { + // assume this is a member of the array of classes named "value" in WithBridgeMethods + types.add((Type) value); + } + super.visit(name, value); + } + }; } - if ("adapterMethod".equals(name) && value instanceof String) { - adapterMethod = (String) value; + + @Override + public void visit(String name, Object value) { + if ("castRequired".equals(name) && value instanceof Boolean) { + castRequired = (Boolean) value; + } + if ("adapterMethod".equals(name) && value instanceof String) { + adapterMethod = (String) value; + } + super.visit(name, value); } - super.visit(name, value); - } } static class Transformer extends ClassVisitor { @@ -174,7 +172,15 @@ class SyntheticMethod { */ final Type originalReturnType; - SyntheticMethod(int access, String name, String desc, String originalSignature, String[] exceptions, Type returnType, boolean castRequired, String adapterMethod) { + SyntheticMethod( + int access, + String name, + String desc, + String originalSignature, + String[] exceptions, + Type returnType, + boolean castRequired, + String adapterMethod) { this.access = access; this.name = name; this.desc = desc; @@ -194,9 +200,9 @@ public void inject(ClassVisitor cv) { int access = this.access | ACC_SYNTHETIC | ACC_BRIDGE; String methodDescriptor = Type.getMethodDescriptor(returnType, paramTypes); - MethodVisitor mv = cv.visitMethod(access, name, - methodDescriptor, null/*TODO:is this really correct?*/, exceptions); - if ((access&ACC_ABSTRACT)==0) { + MethodVisitor mv = cv.visitMethod( + access, name, methodDescriptor, null /*TODO:is this really correct?*/, exceptions); + if ((access & ACC_ABSTRACT) == 0) { GeneratorAdapter ga = new GeneratorAdapter(mv, access, name, methodDescriptor); mv.visitCode(); @@ -221,49 +227,52 @@ public void inject(ClassVisitor cv) { } sz += argpos; - mv.visitMethodInsn( - isStatic ? INVOKESTATIC : INVOKEVIRTUAL,internalClassName,name,desc,false); + mv.visitMethodInsn(isStatic ? INVOKESTATIC : INVOKEVIRTUAL, internalClassName, name, desc, false); if (hasAdapterMethod()) { insertAdapterMethod(ga); - } else - if (castRequired || returnType.equals(Type.VOID_TYPE)) { + } else if (castRequired || returnType.equals(Type.VOID_TYPE)) { ga.unbox(returnType); } else { ga.box(originalReturnType); } - if (returnType.equals(Type.VOID_TYPE) || returnType.getClassName().equals("java.lang.Void")) { + if (returnType.equals(Type.VOID_TYPE) + || returnType.getClassName().equals("java.lang.Void")) { // bridge to void, which means disregard the return value from the original method switch (originalReturnType.getSize()) { - case 0: - throw new IllegalArgumentException("Cannot bridge " + name + " from void to void; did you mean to use a different type?"); - case 1: - mv.visitInsn(POP); - break; - case 2: - mv.visitInsn(POP2); - break; - default: - throw new AssertionError("Unexpected operand size: "+originalReturnType); + case 0: + throw new IllegalArgumentException("Cannot bridge " + name + + " from void to void; did you mean to use a different type?"); + case 1: + mv.visitInsn(POP); + break; + case 2: + mv.visitInsn(POP2); + break; + default: + throw new AssertionError("Unexpected operand size: " + originalReturnType); } } mv.visitInsn(returnType.getOpcode(IRETURN)); - mv.visitMaxs(sz,0); + mv.visitMaxs(sz, 0); } mv.visitEnd(); } private boolean hasAdapterMethod() { - return adapterMethod!=null && adapterMethod.length()>0; + return adapterMethod != null && adapterMethod.length() > 0; } private void insertAdapterMethod(GeneratorAdapter ga) { ga.push(returnType); - ga.visitMethodInsn(INVOKEVIRTUAL, internalClassName, adapterMethod, + ga.visitMethodInsn( + INVOKEVIRTUAL, + internalClassName, + adapterMethod, Type.getMethodDescriptor( Type.getType(Object.class), // return type originalReturnType, - Type.getType(Class.class) - ), false); + Type.getType(Class.class)), + false); ga.unbox(returnType); } } @@ -273,7 +282,8 @@ private void insertAdapterMethod(GeneratorAdapter ga) { } @Override - public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { + public void visit( + int version, int access, String name, String signature, String superName, String[] interfaces) { this.internalClassName = name; super.visit(version, access, name, signature, superName, interfaces); } @@ -281,7 +291,7 @@ public void visit(int version, int access, String name, String signature, String @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { if (desc.equals(SYNTHETIC_METHODS_ADDED)) { - throw new AlreadyUpToDate(); // no need to process this class + throw new AlreadyUpToDate(); // no need to process this class } return super.visitAnnotation(desc, visible); } @@ -290,7 +300,12 @@ public AnnotationVisitor visitAnnotation(String desc, boolean visible) { * Look for methods annotated with {@link WithBridgeMethods}. */ @Override - public MethodVisitor visitMethod(final int access, final String name, final String mdesc, final String signature, final String[] exceptions) { + public MethodVisitor visitMethod( + final int access, + final String name, + final String mdesc, + final String signature, + final String[] exceptions) { MethodVisitor mv = super.visitMethod(access, name, mdesc, signature, exceptions); return new MethodVisitor(Opcodes.ASM9, mv) { @Override @@ -298,14 +313,20 @@ public AnnotationVisitor visitAnnotation(String adesc, boolean visible) { AnnotationVisitor av = super.visitAnnotation(adesc, visible); if (adesc.equals(WITH_SYNTHETIC_METHODS) && (access & ACC_SYNTHETIC) == 0) { return new WithBridgeMethodsAnnotationVisitor(av) { - + @Override public void visitEnd() { - super.visitEnd(); + super.visitEnd(); for (Type type : this.types) { syntheticMethods.add(new SyntheticMethod( - access,name,mdesc,signature,exceptions,type, this.castRequired, this.adapterMethod - )); + access, + name, + mdesc, + signature, + exceptions, + type, + this.castRequired, + this.adapterMethod)); } } }; diff --git a/injector/src/main/java/com/infradna/tool/bridge_method_injector/ProcessMojo.java b/injector/src/main/java/com/infradna/tool/bridge_method_injector/ProcessMojo.java index 6cc9e45..55965d0 100644 --- a/injector/src/main/java/com/infradna/tool/bridge_method_injector/ProcessMojo.java +++ b/injector/src/main/java/com/infradna/tool/bridge_method_injector/ProcessMojo.java @@ -24,24 +24,26 @@ package com.infradna.tool.bridge_method_injector; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; - -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.net.URLClassLoader; import org.jvnet.hudson.annotation_indexer.Index; /** * @author Kohsuke Kawaguchi */ -@Mojo(name = "process", requiresDependencyResolution = ResolutionScope.RUNTIME, defaultPhase = LifecyclePhase.PROCESS_CLASSES, threadSafe = true) +@Mojo( + name = "process", + requiresDependencyResolution = ResolutionScope.RUNTIME, + defaultPhase = LifecyclePhase.PROCESS_CLASSES, + threadSafe = true) public class ProcessMojo extends AbstractMojo { /** * The directory containing generated classes. @@ -55,13 +57,17 @@ public class ProcessMojo extends AbstractMojo { justification = "irrelevant without SecurityManager; user-provided value for running the program") public void execute() throws MojoExecutionException { try { - for (String line : Index.listClassNames(WithBridgeMethods.class, new URLClassLoader(new URL[] {classesDirectory.toURI().toURL()}, ClassLoader.getSystemClassLoader().getParent()))) { - File classFile = new File(classesDirectory,line.replace('.','/')+".class"); - getLog().debug("Processing "+line); + for (String line : Index.listClassNames( + WithBridgeMethods.class, + new URLClassLoader( + new URL[] {classesDirectory.toURI().toURL()}, + ClassLoader.getSystemClassLoader().getParent()))) { + File classFile = new File(classesDirectory, line.replace('.', '/') + ".class"); + getLog().debug("Processing " + line); new MethodInjector().handle(classFile); } } catch (IOException e) { - throw new MojoExecutionException("Failed to process @WithBridgeMethods",e); + throw new MojoExecutionException("Failed to process @WithBridgeMethods", e); } } } diff --git a/injector/src/test/client/Main.java b/injector/src/test/client/Main.java index ceaac50..4eeec5a 100644 --- a/injector/src/test/client/Main.java +++ b/injector/src/test/client/Main.java @@ -54,8 +54,9 @@ public static void main(String[] args) throws Exception { private static void assertEquals(Object expected, Object actual) { System.out.println("We got "+actual+", expecting "+expected); - if (!actual.equals(expected)) + if (!actual.equals(expected)) { System.exit(1); + } } private static void check(IFoo f, String expected) { diff --git a/pom.xml b/pom.xml index af3c578..9f20f2f 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - org.jenkins-ci jenkins @@ -13,20 +12,42 @@ bridge-method-injector-parent ${revision}${changelist} pom - Bridge Method Injection Parent POM Evolve your classes without breaking compatibility https://github.com/jenkinsci/bridge-method-injector + + + MIT License + https://opensource.org/licenses/MIT + repository + + + + + + kohsuke + Kohsuke Kawaguchi + + + annotation injector + + scm:git:https://github.com/${gitHubRepo}.git + scm:git:git@github.com:${gitHubRepo}.git + ${scmTag} + https://github.com/${gitHubRepo} + + 1.30 -SNAPSHOT jenkinsci/bridge-method-injector + false @@ -42,26 +63,4 @@ https://repo.jenkins-ci.org/public/ - - - scm:git:https://github.com/${gitHubRepo}.git - scm:git:git@github.com:${gitHubRepo}.git - https://github.com/${gitHubRepo} - ${scmTag} - - - - - kohsuke - Kohsuke Kawaguchi - - - - - - MIT License - repository - https://opensource.org/licenses/MIT - -