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

Miscellaneous code cleanup #32

Merged
merged 1 commit into from
Nov 17, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -38,7 +37,7 @@
*
* @author Kohsuke Kawaguchi
*/
@Retention(CLASS)
@Target(TYPE)
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface BridgeMethodsAdded {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -103,8 +102,8 @@
*
* @author Kohsuke Kawaguchi
*/
@Retention(CLASS)
@Target(METHOD)
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
@Documented
@Indexed
public @interface WithBridgeMethods {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'

Expand Down Expand Up @@ -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;
}

Expand All @@ -136,7 +135,7 @@ protected void emit() {
private static class WithBridgeMethodsAnnotationVisitor extends AnnotationVisitor {
protected boolean castRequired = false;
protected String adapterMethod = null;
protected List<Type> types = new ArrayList<Type>();
protected final List<Type> types = new ArrayList<>();

public WithBridgeMethodsAnnotationVisitor(AnnotationVisitor av) {
super(Opcodes.ASM9, av);
Expand All @@ -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
Expand All @@ -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<SyntheticMethod> syntheticMethods = new ArrayList<SyntheticMethod>();
private final List<SyntheticMethod> syntheticMethods = new ArrayList<>();

class SyntheticMethod {
final int access;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -278,7 +278,7 @@ private void insertAdapterMethod(GeneratorAdapter ga) {
Type.getType(Object.class), // return type
originalReturnType,
Type.getType(Class.class)
));
), false);
ga.unbox(returnType);
}
}
Expand All @@ -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);
}

Expand All @@ -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;
}
};
Expand All @@ -332,8 +335,9 @@ public void visitEnd() {
*/
@Override
public void visitEnd() {
for (SyntheticMethod m : syntheticMethods)
for (SyntheticMethod m : syntheticMethods) {
m.inject(cv);
}
super.visitEnd();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down