javassist

Can I instrument outgoing method/constructor calls with ByteBuddy?

℡╲_俬逩灬. 提交于 2020-01-07 04:00:51
问题 I have a project where I was using Javassist to log outgoing method/constructor calls with code like this: CtMethod cm = ... ; cm.instrument( new ExprEditor() { public void edit(MethodCall m) throws CannotCompileException { if (m.getClassName().equals("Point") && m.getMethodName().equals("move")) m.replace("{ $1 = 0; $_ = $proceed($$); }"); } }); which assigns '0' to the first argument of the called method and then proceeds with the original call, that is, if cm represents the method

关于javassist.NotFoundException

假如想象 提交于 2020-01-06 17:06:03
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> javassist可以用于反射获取方法参数名称,项目中用到,测试用例通过,当发布到tomcat时报错 javassist.NotFoundException: xxx.xxx.xxxService ClassPool pool = ClassPool.getDefault(); CtClass cls = pool.get(clazz.getName()); // 此处报异常 javassist官网对此有如下说明: The default ClassPool returned by a static method ClassPool.getDefault() searches the same path that the underlying JVM (Java virtual machine) has. If a program is running on a web application server such as JBoss and Tomcat, the ClassPoolobject may not be able to find user classes since such a web application server uses multiple class loaders as well

【JAVA基础☞探针技术】Java探针-Java Agent技术

*爱你&永不变心* 提交于 2020-01-06 14:33:09
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 1、原理:基于javaAgent和Java字节码注入技术的java探针工具技术原理 2、原理分析 动态代理功能实现说明,我们利用javaAgent和ASM字节码技术开发java探针工具,实现原理如下: jdk1.5以后引入了javaAgent技术,javaAgent是运行方法之前的拦截器。我们利用javaAgent和ASM字节码技术,在JVM加载class二进制文件的时候,利用ASM动态的修改加载的class文件,在监控的方法前后添加计时器功能,用于计算监控方法耗时,同时将方法耗时及内部调用情况放入处理器,处理器利用栈先进后出的特点对方法调用先后顺序做处理,当一个请求处理结束后,将耗时方法轨迹和入参map输出到文件中,然后根据map中相应参数或耗时方法轨迹中的关键代码区分出我们要抓取的耗时业务。最后将相应耗时轨迹文件取下来,转化为xml格式并进行解析,通过浏览器将代码分层结构展示出来,方便耗时分析,如图下图所示。 Java探针工具功能点: 1、支持方法执行耗时范围抓取设置,根据耗时范围抓取系统运行时出现在设置耗时范围的代码运行轨迹。 2、支持抓取特定的代码配置,方便对配置的特定方法进行抓取,过滤出关系的代码执行耗时情况。 3、支持APP层入口方法过滤,配置入口运行前的方法进行监控,相当于监控特有的方法耗时

Adding a statement using Javassist

隐身守侯 提交于 2020-01-02 08:55:12
问题 System.out.println("A read operation on a field is encountered "); How can I add a statement, lets say , the above statement , whenever a read operation has been performed on a non-local field ? and also I need to know the details of the field which is read and the set of details should correspond to the uniqueness of the field Example (to remove abstraction in the question): public class Greet{ int knowncount; public Greet() { System.out.println("Hello"); knowncount++; } public Greet(String

Using Javassist to log method calls and argument values, how to make a logger class visible in every instrumented class?

一世执手 提交于 2020-01-02 06:26:30
问题 The tool (in this repo) comprises 3 classes (given below). The problem is how to make my ParaTracer.Logger class visible in every class I instrument (such as java.util.Random shown below). The statement cp.importPackage( "ParaTracer.Logger"); doesn't seem to work and I am getting this error: java.lang.NoClassDefFoundError: ParaTracer/Logger at java.util.Random.nextLong(Random.java) I tried dynamically loading the Logger class inside every instrumented class. But it seems I was using Class

Why do I have to call .toClass() after changing a method body with Javassist?

被刻印的时光 ゝ 提交于 2020-01-01 15:51:08
问题 I modify getMessage() method body of my TestClass by Javassist like this: ClassPool cp = new ClassPool(true); CtClass ctClass = cp.get("my.test.javassist.TestClass"); CtMethod ctMethod = ctClass.getDeclaredMethod("getMessage"); ctMethod.setBody("{ return \"Hello from javassist\"; }"); ctClass.toClass(); TestClass c = new TestClass(); System.out.println(c.getMessage()); It works well. However, if I remove the ctClass.toClass() method call, the body substitution doesn't work. Why? How should I

Why do I have to call .toClass() after changing a method body with Javassist?

我与影子孤独终老i 提交于 2020-01-01 15:51:05
问题 I modify getMessage() method body of my TestClass by Javassist like this: ClassPool cp = new ClassPool(true); CtClass ctClass = cp.get("my.test.javassist.TestClass"); CtMethod ctMethod = ctClass.getDeclaredMethod("getMessage"); ctMethod.setBody("{ return \"Hello from javassist\"; }"); ctClass.toClass(); TestClass c = new TestClass(); System.out.println(c.getMessage()); It works well. However, if I remove the ctClass.toClass() method call, the body substitution doesn't work. Why? How should I

Javassist: re-creating a class - delete first, or defrost() and modify?

∥☆過路亽.° 提交于 2020-01-01 03:24:07
问题 I use Javassist to create a class. And in a test suite, when a second test tries to create the same class, it fails at pool.makeClass( ... ) because the class is frozen (i.e. already created via toClass() . What's the way to overcome this? Ideally, the first test should delete the class somehow - perhaps unload from the classloader - but as I read in JLS, the unload operation is not reliable. So perhaps the workaround is to check in the class creating code whether it exists, and if it does,

java.io.IOException: invalid constant type: 19 at 5

拈花ヽ惹草 提交于 2019-12-30 06:30:12
问题 I have a project. It uses spring boot 2 , java 9 and maven. It can be build sucessfully using mvn clean package . To run spring boot application I used the command java -jar java-cloud-rest-api/target/java-cloud-rest-api-0.0.1-SNAPSHOT.jar But it failed with error org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]:

ProxyFactory$Adaptive

笑着哭i 提交于 2019-12-25 19:11:37
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> package com.alibaba.dubbo.rpc; import com.alibaba.dubbo.common.Node; import com.alibaba.dubbo.common.URL; import com.alibaba.dubbo.common.extension.ExtensionLoader; public class ProxyFactory$Adaptive implements ProxyFactory { public Object getProxy(Invoker paramInvoker) throws RpcException { if (paramInvoker == null) { throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument == null"); } if (paramInvoker.getUrl() == null) { throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument getUrl() == null"); } URL localURL =