-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
281 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
generator/src/main/java/com/reajason/javaweb/buddy/LogRemoveMethodVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.reajason.javaweb.buddy; | ||
|
||
import net.bytebuddy.asm.AsmVisitorWrapper; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.dynamic.DynamicType; | ||
import net.bytebuddy.implementation.Implementation; | ||
import net.bytebuddy.jar.asm.MethodVisitor; | ||
import net.bytebuddy.jar.asm.Opcodes; | ||
import net.bytebuddy.matcher.ElementMatchers; | ||
import net.bytebuddy.pool.TypePool; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import static net.bytebuddy.jar.asm.Opcodes.INVOKEVIRTUAL; | ||
import static net.bytebuddy.jar.asm.Opcodes.POP; | ||
|
||
/** | ||
* Debug 信息打印移除器,目前仅支持移除 System.out.println() - (printf 还不支持) 和 e.printStackTrace() | ||
* @author ReaJason | ||
*/ | ||
public class LogRemoveMethodVisitor implements AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper { | ||
public static final LogRemoveMethodVisitor INSTANCE = new LogRemoveMethodVisitor(); | ||
|
||
public static DynamicType.Builder<?> extend(DynamicType.Builder<?> builder) { | ||
return builder.visit( | ||
new AsmVisitorWrapper.ForDeclaredMethods() | ||
.method(ElementMatchers.any(), LogRemoveMethodVisitor.INSTANCE)); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public MethodVisitor wrap(@NotNull TypeDescription instrumentedType, | ||
@NotNull MethodDescription instrumentedMethod, | ||
@NotNull MethodVisitor methodVisitor, | ||
@NotNull Implementation.Context implementationContext, | ||
@NotNull TypePool typePool, | ||
int writerFlags, | ||
int readerFlags) { | ||
return new MethodVisitor(Opcodes.ASM9, methodVisitor) { | ||
@Override | ||
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) { | ||
if ((opcode == INVOKEVIRTUAL && owner.equals("java/io/PrintStream") && name.equals("println")) | ||
|| (opcode == INVOKEVIRTUAL && owner.endsWith("Exception") && name.equals("printStackTrace"))) { | ||
String[] args = descriptor.substring(1, descriptor.indexOf(')')).split(";"); | ||
for (String arg : args) { | ||
if (StringUtils.isNotBlank(arg)) { | ||
super.visitInsn(POP); | ||
} | ||
} | ||
super.visitInsn(POP); | ||
} else { | ||
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface); | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
generator/src/test/java/com/reajason/javaweb/buddy/ByPassJavaModuleInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.reajason.javaweb.buddy; | ||
|
||
import lombok.SneakyThrows; | ||
import net.bytebuddy.ByteBuddy; | ||
import net.bytebuddy.dynamic.DynamicType; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledOnJre; | ||
|
||
import java.lang.reflect.InaccessibleObjectException; | ||
import java.lang.reflect.Method; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.junit.jupiter.api.condition.JRE.JAVA_17; | ||
|
||
/** | ||
* @author ReaJason | ||
* @since 2024/12/7 | ||
*/ | ||
class ByPassJavaModuleInterceptorTest { | ||
static class TestClass { | ||
static { | ||
System.out.println("TestClass"); | ||
} | ||
|
||
public TestClass() { | ||
} | ||
|
||
|
||
public String hello() { | ||
return "hello"; | ||
} | ||
} | ||
|
||
@Test | ||
@SneakyThrows | ||
@EnabledOnJre(JAVA_17) | ||
void testByPassModule() { | ||
Method defineClass = ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, int.class, int.class); | ||
assertThrows(InaccessibleObjectException.class, () -> { | ||
defineClass.setAccessible(true); | ||
}); | ||
ByPassJavaModuleInterceptor.enter(this.getClass()); | ||
assertDoesNotThrow(() -> { | ||
defineClass.setAccessible(true); | ||
}); | ||
} | ||
|
||
@Test | ||
@SneakyThrows | ||
void test() { | ||
DynamicType.Builder<?> builder = new ByteBuddy() | ||
.redefine(TestClass.class) | ||
.name("com.reajason.javaweb.buddy.ByPassJdkModuleInterceptorTest$TestClass1"); | ||
builder = ByPassJavaModuleInterceptor.extend(builder); | ||
try (DynamicType.Unloaded<?> make = builder.make()) { | ||
byte[] bytes = make.getBytes(); | ||
Files.write(Paths.get("build", "classes", "TestClass1.class"), bytes); | ||
Class<?> loaded = make.load(Thread.currentThread().getContextClassLoader()).getLoaded(); | ||
assertNotNull(loaded.getDeclaredMethod("byPassJdkModule")); | ||
} | ||
} | ||
|
||
} |
44 changes: 0 additions & 44 deletions
44
generator/src/test/java/com/reajason/javaweb/buddy/ByPassJdkModuleInterceptorTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.