From 075125960bd1303e9de1f7864845f9763060d7c8 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 17 Nov 2022 10:34:08 -0800 Subject: [PATCH] Miscellaneous code cleanup --- .../BridgeMethodsAdded.java | 9 ++- .../WithBridgeMethods.java | 9 ++- .../MethodInjector.java | 56 ++++++++++--------- .../bridge_method_injector/ProcessMojo.java | 4 +- 4 files changed, 40 insertions(+), 38 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 e3a9231..b2af714 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 @@ -23,12 +23,11 @@ */ package com.infradna.tool.bridge_method_injector; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import static java.lang.annotation.ElementType.*; -import static java.lang.annotation.RetentionPolicy.*; - /** * This annotation is added after the class transformation to indicate that * the class has already been processed by bridge method injector. @@ -38,7 +37,7 @@ * * @author Kohsuke Kawaguchi */ -@Retention(CLASS) -@Target(TYPE) +@Retention(RetentionPolicy.CLASS) +@Target(ElementType.TYPE) 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 00852cf..082118f 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 @@ -26,12 +26,11 @@ 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 static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.RetentionPolicy.CLASS; - /** * Request that bridge methods of the same name and same arguments be generated * with each specified type as the return type. This helps you maintain binary compatibility @@ -103,8 +102,8 @@ * * @author Kohsuke Kawaguchi */ -@Retention(CLASS) -@Target(METHOD) +@Retention(RetentionPolicy.CLASS) +@Target(ElementType.METHOD) @Documented @Indexed public @interface WithBridgeMethods { 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 40dcecf..9de191f 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 @@ -28,7 +28,6 @@ import static org.objectweb.asm.Opcodes.ACC_BRIDGE; import static org.objectweb.asm.Opcodes.ACC_STATIC; import static org.objectweb.asm.Opcodes.ACC_SYNTHETIC; -import static org.objectweb.asm.Opcodes.ALOAD; import static org.objectweb.asm.Opcodes.ILOAD; import static org.objectweb.asm.Opcodes.INVOKESTATIC; import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL; @@ -61,17 +60,21 @@ public class MethodInjector { public void handleRecursively(File f) throws IOException { if (f.isDirectory()) { - for (File c : f.listFiles()) - handleRecursively(c); - } else if (f.getName().endsWith(".class")) + File[] files = f.listFiles(); + if (files != null) { + for (File c : files) { + handleRecursively(c); + } + } + } else if (f.getName().endsWith(".class")) { handle(f); + } } public void handle(File classFile) throws IOException { - FileInputStream in = new FileInputStream(classFile); byte[] image; - try { - ClassReader cr = new ClassReader(new BufferedInputStream(in)); + try (FileInputStream in = new FileInputStream(classFile); BufferedInputStream bis = new BufferedInputStream(in)) { + ClassReader cr = new ClassReader(bis); /* working around JENKINS-22525 by not passing in 'cr' @@ -100,24 +103,20 @@ it is happy with the original class file. } catch (AlreadyUpToDate _) { // no need to process this class. it's already up-to-date. return; - } catch (IOException e) { - throw (IOException)new IOException("Failed to process "+classFile).initCause(e); - } catch (RuntimeException e) { - throw (IOException)new IOException("Failed to process "+classFile).initCause(e); - } finally { - in.close(); + } catch (IOException | RuntimeException e) { + throw new IOException("Failed to process " + classFile, e); } // write it back - FileOutputStream out = new FileOutputStream(classFile); - out.write(image); - out.close(); + try (FileOutputStream out = new FileOutputStream(classFile)) { + out.write(image); + } } /** * Thrown to indicate that there's no need to re-process this class file. */ - class AlreadyUpToDate extends RuntimeException { + static class AlreadyUpToDate extends RuntimeException { private static final long serialVersionUID = 1L; } @@ -136,7 +135,7 @@ protected void emit() { private static class WithBridgeMethodsAnnotationVisitor extends AnnotationVisitor { protected boolean castRequired = false; protected String adapterMethod = null; - protected List types = new ArrayList(); + protected final List types = new ArrayList<>(); public WithBridgeMethodsAnnotationVisitor(AnnotationVisitor av) { super(Opcodes.ASM9, av); @@ -146,6 +145,7 @@ public WithBridgeMethodsAnnotationVisitor(AnnotationVisitor av) { 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 @@ -169,12 +169,12 @@ public void visit(String name, Object value) { } } - class Transformer extends ClassVisitor { + static class Transformer extends ClassVisitor { private String internalClassName; /** * Synthetic methods to be generated. */ - private final List syntheticMethods = new ArrayList(); + private final List syntheticMethods = new ArrayList<>(); class SyntheticMethod { final int access; @@ -239,7 +239,7 @@ public void inject(ClassVisitor cv) { sz += p.getSize(); } mv.visitMethodInsn( - isStatic ? INVOKESTATIC : INVOKEVIRTUAL,internalClassName,name,desc); + isStatic ? INVOKESTATIC : INVOKEVIRTUAL,internalClassName,name,desc,false); if (hasAdapterMethod()) { insertAdapterMethod(ga); } else @@ -278,7 +278,7 @@ private void insertAdapterMethod(GeneratorAdapter ga) { Type.getType(Object.class), // return type originalReturnType, Type.getType(Class.class) - )); + ), false); ga.unbox(returnType); } } @@ -295,8 +295,9 @@ 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)) + if (desc.equals(SYNTHETIC_METHODS_ADDED)) { throw new AlreadyUpToDate(); // no need to process this class + } return super.visitAnnotation(desc, visible); } @@ -310,18 +311,20 @@ public MethodVisitor visitMethod(final int access, final String name, final Stri @Override public AnnotationVisitor visitAnnotation(String adesc, boolean visible) { AnnotationVisitor av = super.visitAnnotation(adesc, visible); - if (adesc.equals(WITH_SYNTHETIC_METHODS) && (access & ACC_SYNTHETIC) == 0) + if (adesc.equals(WITH_SYNTHETIC_METHODS) && (access & ACC_SYNTHETIC) == 0) { return new WithBridgeMethodsAnnotationVisitor(av) { @Override public void visitEnd() { super.visitEnd(); - for (Type type : this.types) + for (Type type : this.types) { syntheticMethods.add(new SyntheticMethod( access,name,mdesc,signature,exceptions,type, this.castRequired, this.adapterMethod )); + } } }; + } return av; } }; @@ -332,8 +335,9 @@ public void visitEnd() { */ @Override public void visitEnd() { - for (SyntheticMethod m : syntheticMethods) + for (SyntheticMethod m : syntheticMethods) { m.inject(cv); + } super.visitEnd(); } } 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 4525531..84fd076 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 @@ -25,7 +25,6 @@ import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import java.io.File; import java.io.IOException; @@ -49,7 +48,8 @@ public class ProcessMojo extends AbstractMojo { */ private File classesDirectory; - public void execute() throws MojoExecutionException, MojoFailureException { + @Override + 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");