use VMOptions instead of System

This commit is contained in:
novice.li 2024-02-25 09:03:30 +08:00
parent 29131379bc
commit eeac095187
3 changed files with 22 additions and 34 deletions

View File

@ -25,10 +25,10 @@ public class AgentMain {
.on(ElementMatchers.named("openServer").and(ElementMatchers.takesArgument(0, String.class))))) .on(ElementMatchers.named("openServer").and(ElementMatchers.takesArgument(0, String.class)))))
.asTerminalTransformation() .asTerminalTransformation()
.type(ElementMatchers.named("java.lang.System")) .type(ElementMatchers.named("com.intellij.diagnostic.VMOptions"))
.transform((builder, typeDescription, classLoader, module, protectionDomain) -> builder .transform((builder, typeDescription, classLoader, module, protectionDomain) -> builder
.visit(Advice.to(SystemAdvice.class) .visit(Advice.to(VMOptionsAdvice.class)
.on(ElementMatchers.named("getProperty")))) .on(ElementMatchers.named("getUserOptionsFile"))))
.asTerminalTransformation() .asTerminalTransformation()
.installOn(inst); .installOn(inst);

View File

@ -1,31 +0,0 @@
package win.novice.li;
import net.bytebuddy.asm.Advice;
import java.util.Objects;
public class SystemAdvice {
// System.getProperty
@Advice.OnMethodExit
public static void intercept(@Advice.Argument(0) Object x, @Advice.Return(readOnly = false) String r) throws Exception {
if (Objects.equals(x, "jb.vmOptionsFile")) {
RuntimeException exception = new RuntimeException();
int nullCnt = 0;
boolean hasReflect = false;
for (StackTraceElement element : exception.getStackTrace()) {
if (element.getFileName() == null) {
nullCnt += 1;
continue;
}
if (element.getFileName().equals("Method.java")) {
hasReflect = true;
}
}
if (nullCnt >= 3 && hasReflect) {
r = null;
}
}
}
}

View File

@ -0,0 +1,19 @@
package win.novice.li;
import net.bytebuddy.asm.Advice;
import java.nio.file.Path;
public class VMOptionsAdvice {
@Advice.OnMethodExit
public static void intercept(@Advice.Return(readOnly = false) Path r) throws Exception {
RuntimeException e = new RuntimeException();
for (StackTraceElement stackTraceElement : e.getStackTrace()) {
if (stackTraceElement.getFileName() == null){
r = null;
break;
}
}
}
}