|
| 1 | +package me.threedr3am.log.agent; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.lang.instrument.ClassFileTransformer; |
| 5 | +import java.lang.instrument.IllegalClassFormatException; |
| 6 | +import java.security.ProtectionDomain; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.Set; |
| 9 | +import java.util.regex.Pattern; |
| 10 | +import javassist.ClassClassPath; |
| 11 | +import javassist.ClassPool; |
| 12 | +import javassist.CtClass; |
| 13 | +import javassist.CtMethod; |
| 14 | +import javassist.LoaderClassPath; |
| 15 | +import javassist.Modifier; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author threedr3am |
| 19 | + */ |
| 20 | +public class CatClassFileTransformer implements ClassFileTransformer { |
| 21 | + |
| 22 | + private String pkgPattern = "."; |
| 23 | + private Pattern pattern; |
| 24 | + |
| 25 | + public String getPkgPattern() { |
| 26 | + return pkgPattern; |
| 27 | + } |
| 28 | + |
| 29 | + public void setPkgPattern(String pkgPattern) { |
| 30 | + this.pkgPattern = pkgPattern; |
| 31 | + } |
| 32 | + |
| 33 | + public Pattern getPattern() { |
| 34 | + return pattern; |
| 35 | + } |
| 36 | + |
| 37 | + public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { |
| 38 | + if (pattern == null) |
| 39 | + pattern = Pattern.compile(pkgPattern); |
| 40 | + className = className.replace("/","."); |
| 41 | + if (pattern.matcher(className).find()) { |
| 42 | + System.out.println("[LOG-AGENT] --------- modify class: " + className); |
| 43 | + CtClass ctClass = null; |
| 44 | + try { |
| 45 | + ClassPool classPool = ClassPool.getDefault(); |
| 46 | + addLoader(classPool, loader); |
| 47 | + ctClass = classPool.makeClass(new ByteArrayInputStream(classfileBuffer)); |
| 48 | + Set<String> cache = new HashSet(); |
| 49 | + CtMethod[] ctMethods = ctClass.getMethods(); |
| 50 | + if (ctMethods != null) { |
| 51 | + inject(ctMethods, cache); |
| 52 | + } |
| 53 | + ctMethods = ctClass.getDeclaredMethods(); |
| 54 | + if (ctMethods != null) { |
| 55 | + inject(ctMethods, cache); |
| 56 | + } |
| 57 | + return ctClass.toBytecode(); |
| 58 | + } catch (Throwable e) { |
| 59 | + e.printStackTrace(); |
| 60 | + } finally { |
| 61 | + if (ctClass != null) { |
| 62 | + ctClass.detach(); |
| 63 | + } |
| 64 | + } |
| 65 | + System.out.println("[LOG-AGENT] --------- modify class end."); |
| 66 | + } |
| 67 | + return classfileBuffer; |
| 68 | + } |
| 69 | + |
| 70 | + private void inject(CtMethod[] ctMethods, Set<String> cache) { |
| 71 | + for (int i = 0; i < ctMethods.length; i++) { |
| 72 | + CtMethod ctMethod = ctMethods[i]; |
| 73 | + if (ctMethod.isEmpty() || Modifier.isNative(ctMethod.getModifiers())) |
| 74 | + continue; |
| 75 | + String methodName = ctMethod.getLongName(); |
| 76 | + if (cache.contains(methodName)) |
| 77 | + continue; |
| 78 | + try { |
| 79 | + System.out.println("[LOG-AGENT] method: " + methodName + " " + cache.size()); |
| 80 | + StringBuilder stringBuilder = new StringBuilder() |
| 81 | + .append("{") |
| 82 | + .append(String.format(" if (me.threedr3am.log.agent.CatContext.check(\"%s\"))", methodName)) |
| 83 | + .append(String.format(" System.out.println(\"%s %s\");", "[LOG-AGENT] ", methodName)) |
| 84 | + .append("}"); |
| 85 | + ctMethod.insertBefore(stringBuilder.toString()); |
| 86 | + cache.add(methodName); |
| 87 | + } catch (Throwable e) { |
| 88 | + System.err.println(String.format("[LOG-AGENT] inject code into method:%s fail!", methodName)); |
| 89 | + e.printStackTrace(); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private void addLoader(ClassPool classPool, ClassLoader loader) { |
| 95 | + classPool.appendSystemPath(); |
| 96 | + classPool.appendClassPath(new ClassClassPath(CatClassFileTransformer.class)); |
| 97 | + if (loader != null) { |
| 98 | + classPool.appendClassPath(new LoaderClassPath(loader)); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments