Skip to content

Commit

Permalink
Format code with Spotless (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Feb 29, 2024
1 parent cb5eef4 commit b0123d6
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 260 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<changelist>-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/lib-${project.artifactId}</gitHubRepo>
<asm.version>9.6</asm.version>
<spotless.check.skip>false</spotless.check.skip>
</properties>

<dependencies>
Expand Down
15 changes: 4 additions & 11 deletions src/main/java/org/kohsuke/file_leak_detector/ActivityListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@ public abstract class ActivityListener {
* @param file
* File being opened.
*/
public void open(Object obj, File file) {
}
public void open(Object obj, File file) {}

/**
* Called when a new socket is opened.
*
* @param obj
* {@link Socket}, {@link ServerSocket} or {@link SocketChannel}
*/
public void openSocket(Object obj) {
}
public void openSocket(Object obj) {}

/**
* Called when a file is closed.
Expand All @@ -50,17 +48,12 @@ public void openSocket(Object obj) {
* @param obj
* {@link FileInputStream}, {@link FileOutputStream}, {@link RandomAccessFile}, {@link Socket}, {@link ServerSocket}, or {@link ZipFile}.
*/
public void close(Object obj) {
}
public void close(Object obj) {}

public void fd_open(Object obj) {

}
public void fd_open(Object obj) {}

/**
* These listeners get called.
*/
public static final List<ActivityListener> LIST = new CopyOnWriteArrayList<>();


}
241 changes: 124 additions & 117 deletions src/main/java/org/kohsuke/file_leak_detector/AgentMain.java

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions src/main/java/org/kohsuke/file_leak_detector/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ public void dump(String prefix, PrintWriter pw) {
}
}


/**
* Record of opened socket.
*/
Expand Down Expand Up @@ -427,9 +426,6 @@ public static synchronized void close(Object _this) {
}
}




/**
* Dumps all files that are currently open.
*/
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/org/kohsuke/file_leak_detector/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package org.kohsuke.file_leak_detector;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -17,17 +13,20 @@
import java.security.ProtectionDomain;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

/**
* Entry point for externally attaching agent into another local process.
*
* @author Kohsuke Kawaguchi
*/
public class Main {
@Argument(index=0,metaVar="PID",usage="Process ID to activate file leak detector",required=true)
@Argument(index = 0, metaVar = "PID", usage = "Process ID to activate file leak detector", required = true)
public String pid;

@Argument(index=1,metaVar="OPTSTR",usage="Packed option string of the form key1[=value1],key2[=value2],...")
@Argument(index = 1, metaVar = "OPTSTR", usage = "Packed option string of the form key1[=value1],key2[=value2],...")
public String options;

public static void main(String[] args) {
Expand Down Expand Up @@ -71,7 +70,8 @@ public void run() throws IOException, ReflectiveOperationException {
System.out.println("Activating file leak detector at " + agentJar);
// load a specified agent onto the JVM
// pass the hidden option to prevent this from killing the target JVM if the options were wrong
api.getMethod("loadAgent",String.class,String.class).invoke(vm, agentJar.getPath(), options == null ? "noexit" : "noexit,"+options);
api.getMethod("loadAgent", String.class, String.class)
.invoke(vm, agentJar.getPath(), options == null ? "noexit" : "noexit," + options);
} finally {
api.getMethod("detach").invoke(vm);
}
Expand All @@ -87,7 +87,8 @@ private Class<?> loadAttachApi() throws MalformedURLException {
try {
return cl.loadClass("com.sun.tools.attach.VirtualMachine");
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Unable to find tools.jar at "+toolsJar+" --- you need to run this tool with a JDK",e);
throw new IllegalStateException(
"Unable to find tools.jar at " + toolsJar + " --- you need to run this tool with a JDK", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ public void invokeVirtual(String owner, String name, String desc) {
* This is used for instrumenting classes in the bootstrap classloader,
* which cannot see the classes in the system classloader.
*/
// public void invokeAppStatic(String userClassName, String userMethodName, Class[] argTypes, int[] localIndex) {
// visitMethodInsn(INVOKESTATIC,"java/lang/ClassLoader","getSystemClassLoader","()Ljava/lang/ClassLoader;");
// ldc(userClassName);
// invokeVirtual("java/lang/ClassLoader","loadClass","(Ljava/lang/String;)Ljava/lang/Class;");
// ldc(userMethodName);
// newArray("java/lang/Class",0);
//// for (int i = 0; i < argTypes.length; i++)
//// storeConst(i, argTypes[i]);
//
// invokeVirtual("java/lang/Class","getDeclaredMethod","(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");
// pop();
// }

// public void invokeAppStatic(String userClassName, String userMethodName, Class[] argTypes, int[] localIndex) {
// visitMethodInsn(INVOKESTATIC,"java/lang/ClassLoader","getSystemClassLoader","()Ljava/lang/ClassLoader;");
// ldc(userClassName);
// invokeVirtual("java/lang/ClassLoader","loadClass","(Ljava/lang/String;)Ljava/lang/Class;");
// ldc(userMethodName);
// newArray("java/lang/Class",0);
//// for (int i = 0; i < argTypes.length; i++)
//// storeConst(i, argTypes[i]);
//
//
// invokeVirtual("java/lang/Class","getDeclaredMethod","(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");
// pop();
// }

public void invokeAppStatic(Class<?> userClass, String userMethodName, Class<?>[] argTypes, int[] localIndex) {
invokeAppStatic(userClass.getName(),userMethodName,argTypes,localIndex);
invokeAppStatic(userClass.getName(), userMethodName, argTypes, localIndex);
}

public void invokeAppStatic(String userClassName, String userMethodName, Class<?>[] argTypes, int[] localIndex) {
Expand All @@ -115,17 +115,22 @@ public void invokeAppStatic(String userClassName, String userMethodName, Class<?
Label tail = new Label();
visitTryCatchBlock(s, e, h, "java/lang/Exception");
visitLabel(s);
// [RESULT] m = ClassLoader.getSystemClassLoadeR().loadClass($userClassName).getDeclaredMethod($userMethodName,[...]);
visitMethodInsn(INVOKESTATIC,"java/lang/ClassLoader","getSystemClassLoader","()Ljava/lang/ClassLoader;", false);
// [RESULT] m =
// ClassLoader.getSystemClassLoadeR().loadClass($userClassName).getDeclaredMethod($userMethodName,[...]);
visitMethodInsn(
INVOKESTATIC, "java/lang/ClassLoader", "getSystemClassLoader", "()Ljava/lang/ClassLoader;", false);
ldc(userClassName);
invokeVirtual("java/lang/ClassLoader","loadClass","(Ljava/lang/String;)Ljava/lang/Class;");
invokeVirtual("java/lang/ClassLoader", "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
ldc(userMethodName);
newArray("java/lang/Class", argTypes.length);
for (int i = 0; i < argTypes.length; i++) {
storeConst(i, argTypes[i]);
}

invokeVirtual("java/lang/Class","getDeclaredMethod","(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");
invokeVirtual(
"java/lang/Class",
"getDeclaredMethod",
"(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");

// [RESULT] m.invoke(null,new Object[]{this,file})
_null();
Expand All @@ -138,7 +143,8 @@ public void invokeAppStatic(String userClassName, String userMethodName, Class<?
aastore();
}

invokeVirtual("java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");
invokeVirtual(
"java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");
pop();
_goto(tail);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ public MethodAppender(String name, String desc) {
protected abstract void append(CodeGenerator g);

@Override
public MethodVisitor newAdapter(MethodVisitor base, int access, String name, String desc, String signature, String[] exceptions) {
public MethodVisitor newAdapter(
MethodVisitor base, int access, String name, String desc, String signature, String[] exceptions) {
final CodeGenerator cg = new CodeGenerator(base);
return new MethodVisitor(Opcodes.ASM9, base) {
@Override
public void visitInsn(int opcode) {
switch (opcode) {
case Opcodes.RETURN:
case Opcodes.ARETURN:
append(cg);
break;
default:
// ignored
case Opcodes.RETURN:
case Opcodes.ARETURN:
append(cg);
break;
default:
// ignored
}
super.visitInsn(opcode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ public MethodTransformSpec(String name, String desc) {
* Creates a visitor that receives the original method definition and writes
* the transformed method to the given base.
*/
public abstract MethodVisitor newAdapter(MethodVisitor base, int access, String name, String desc, String signature, String[] exceptions);
public abstract MethodVisitor newAdapter(
MethodVisitor base, int access, String name, String desc, String signature, String[] exceptions);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.kohsuke.file_leak_detector.transform;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.lang.instrument.ClassFileTransformer;
import java.security.ProtectionDomain;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
* @author Kohsuke Kawaguchi
Expand All @@ -26,8 +25,13 @@ public TransformerImpl(Collection<ClassTransformSpec> specs) {
}

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
return transform(className,classfileBuffer);
public byte[] transform(
ClassLoader loader,
String className,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) {
return transform(className, classfileBuffer);
}

public byte[] transform(String className, byte[] classfileBuffer) {
Expand All @@ -38,24 +42,27 @@ public byte[] transform(String className, byte[] classfileBuffer) {

ClassReader cr = new ClassReader(classfileBuffer);
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
cr.accept(new ClassVisitor(Opcodes.ASM9, cw) {
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
MethodVisitor base = super.visitMethod(access, name, desc, signature, exceptions);

MethodTransformSpec ms = cs.methodSpecs.get(name + desc);
if (ms == null) {
ms = cs.methodSpecs.get(name + "*");
}
if (ms == null) {
return base;
}

return ms.newAdapter(base, access, name, desc, signature, exceptions);
}
}, ClassReader.SKIP_FRAMES);

// System.out.println("Transforming "+className);
cr.accept(
new ClassVisitor(Opcodes.ASM9, cw) {
@Override
public MethodVisitor visitMethod(
int access, String name, String desc, String signature, String[] exceptions) {
MethodVisitor base = super.visitMethod(access, name, desc, signature, exceptions);

MethodTransformSpec ms = cs.methodSpecs.get(name + desc);
if (ms == null) {
ms = cs.methodSpecs.get(name + "*");
}
if (ms == null) {
return base;
}

return ms.newAdapter(base, access, name, desc, signature, exceptions);
}
},
ClassReader.SKIP_FRAMES);

// System.out.println("Transforming "+className);
return cw.toByteArray();
}
}
34 changes: 17 additions & 17 deletions src/test/java/org/kohsuke/file_leak_detector/AgentMainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@ public void testPreMain() throws Exception {

Instrumentation instrumentation = mock(Instrumentation.class);
doAnswer((Answer<Object>) invocationOnMock -> {
for (Object obj : invocationOnMock.getArguments()) {
Class<?> clazz = (Class<?>) obj;
String name = clazz.getName().replace(".", "/");
assertTrue(
"Tried to transform a class which is not contained in the specs: "
+ name
+ " ("
+ clazz
+ "), having remaining classes: "
+ seenClasses,
seenClasses.remove(name));
}
return null;
}).when(instrumentation).retransformClasses(any(Class[].class));
for (Object obj : invocationOnMock.getArguments()) {
Class<?> clazz = (Class<?>) obj;
String name = clazz.getName().replace(".", "/");
assertTrue(
"Tried to transform a class which is not contained in the specs: "
+ name
+ " ("
+ clazz
+ "), having remaining classes: "
+ seenClasses,
seenClasses.remove(name));
}
return null;
})
.when(instrumentation)
.retransformClasses(any(Class[].class));

AgentMain.premain(null, instrumentation);

Expand All @@ -67,8 +69,6 @@ public void testPreMain() throws Exception {
seenClasses.remove("java/net/PlainSocketImpl");
}

assertTrue(
"Had classes in the spec which were not instrumented: " + seenClasses,
seenClasses.isEmpty());
assertTrue("Had classes in the spec which were not instrumented: " + seenClasses, seenClasses.isEmpty());
}
}
8 changes: 4 additions & 4 deletions src/test/java/org/kohsuke/file_leak_detector/Bar.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ void foo() {
bar();
try {
bar();
} catch(IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
bar();
}
bar();
}
}

void bar() {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public void testInstrumentations() throws Exception {
}
byte[] data2 = t.transform(name, data);

// File classFile = new File("/tmp/" + name + ".class");
// classFile.getParentFile().mkdirs();
// FileOutputStream o = new FileOutputStream(classFile);
// o.write(data2);
// o.close();
// File classFile = new File("/tmp/" + name + ".class");
// classFile.getParentFile().mkdirs();
// FileOutputStream o = new FileOutputStream(classFile);
// o.write(data2);
// o.close();

final String errors;
ClassReader classReader = new ClassReader(data2);
Expand Down
Loading

0 comments on commit b0123d6

Please sign in to comment.