javassist

Adding an annotation to a runtime generated method/class using Javassist

只愿长相守 提交于 2019-11-28 23:04:20
I'm using Javassist to generate a class foo , with method bar , but I can't seem to find a way to add an annotation (the annotation itself isn't runtime generated) to the method. The code I tried looks like this: ClassPool pool = ClassPool.getDefault(); // create the class CtClass cc = pool.makeClass("foo"); // create the method CtMethod mthd = CtNewMethod.make("public Integer getInteger() { return null; }", cc); cc.addMethod(mthd); ClassFile ccFile = cc.getClassFile(); ConstPool constpool = ccFile.getConstPool(); // create the annotation AnnotationsAttribute attr = new AnnotationsAttribute

Dynamic Java Bytecode Manipulation Framework Comparison

半城伤御伤魂 提交于 2019-11-28 16:20:09
问题 There are some frameworks out there for dynamic bytecode generation, manipulation and weaving (BCEL, CGLIB, javassist, ASM, MPS). I want to learn about them, but since I don't have much time to know all the details about all of them, I would like to see a sort of comparison chart saying the advantages and disadvantages of one versus the others and an explanation of why. Here in SO, I found a lot of questions asking something similar, and the answers normally said "you can use cglib or ASM",

Javassist. What is the main idea and where real use?

假装没事ソ 提交于 2019-11-28 16:00:18
问题 I know that Javassist is a Java library providing a means to manipulate the Java bytecode of an application. Ok, but why we need manipulate bytecode? Any real example? Any real app, where javassist used? 回答1: A common application is to generate proxy classes at runtime, i.e. to create a subclass at runtime that intercepts all method invocations. Examples: Hibernate uses Proxies to intercept method invocations on entities to implement lazy loading, i.e. fetching the object from the database

Adding an annotation to a runtime generated method/class using Javassist

倾然丶 夕夏残阳落幕 提交于 2019-11-27 14:29:04
问题 I'm using Javassist to generate a class foo , with method bar , but I can't seem to find a way to add an annotation (the annotation itself isn't runtime generated) to the method. The code I tried looks like this: ClassPool pool = ClassPool.getDefault(); // create the class CtClass cc = pool.makeClass("foo"); // create the method CtMethod mthd = CtNewMethod.make("public Integer getInteger() { return null; }", cc); cc.addMethod(mthd); ClassFile ccFile = cc.getClassFile(); ConstPool constpool =

Reflections - Java 8 - invalid constant type

白昼怎懂夜的黑 提交于 2019-11-27 13:11:10
I have a problem with Reflections library. I am trying to load dynamically all classes which implement specific interface. Everything works fine (all classes are loaded) as long as I do not use lambda expression in these classes (java 8). I tried upgrade lib version but effect was the same (java.io.IOException: invalid constant type: 18). Dependency and build in pom.xml <dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> <version>0.9.10</version> <exclusions> <exclusion> <groupId>javassist</groupId> <artifactId>javassist</artifactId> </exclusion> </exclusions>

秒懂Java动态编程(Javassist研究)

北城余情 提交于 2019-11-27 06:23:42
[toc] 概述 什么是动态编程?动态编程解决什么问题?Java中如何使用?什么原理?如何改进?(需要我们一起探索,由于自己也是比较菜,一般深入不到这个程度)。 什么是动态编程 动态编程是相对于静态编程而言的,平时我们讨论比较多的就是 静态编程语言 ,例如Java,与 动态编程语言 ,例如JavaScript。那二者有什么明显的区别呢?简单的说就是在静态编程中,类型检查是在 编译时 完成的,而动态编程中类型检查是在 运行时 完成的。所谓动态编程就是绕过编译过程在运行时进行操作的技术,在Java中有如下几种方式: 反射 这个搞Java的应该比较熟悉,原理也就是通过在运行时获得类型信息然后做相应的操作。 动态编译 动态编译是从 Java 6 开始支持的,主要是通过一个 JavaCompiler 接口来完成的。通过这种方式我们可以直接编译一个已经存在的java文件,也可以在内存中动态生成Java代码,动态编译执行。 调用JavaScript引擎 Java 6 加入了对 Script(JSR223) 的支持。这是一个脚本框架,提供了让脚本语言来访问Java内部的方法。你可以在运行的时候找到脚本引擎,然后调用这个引擎去执行脚本。这个脚本API允许你为脚本语言提供Java支持。 动态生成字节码 这种技术通过操作Java字节码的方式在 JVM 中生成新类或者对已经加载的类动态添加元素。

Dubbo - 技术选型

偶尔善良 提交于 2019-11-27 01:35:39
Zookeeper注册中心 Stable 支持基于网络的集群方式,有广泛周边开源产品,建议使用dubbo-2.3.3以上版本(推荐使用) 依赖于Zookeeper的稳定性 Simple监控中心 Stable 支持JFreeChart统计报表 没有集群支持,可能单点故障,但故障后不影响RPC运行 Dubbo协议 Stable 采用NIO复用单一长连接,并使用线程池并发处理请求,减少握手和加大并发效率,性能较好(推荐使用) 在大文件传输时,单一连接会成为瓶颈 Netty Transporter Stable JBoss的NIO框架,性能较好(推荐使用) 一次请求派发两种事件,需屏蔽无用事件 序列化方案 Hessian Serialization Stable 性能较好,多语言支持(推荐使用) Hessian的各版本兼容性不好,可能和应用使用的Hessian冲突,Dubbo内嵌了hessian3.2.1的源码 接口代理生成方案 Javassist ProxyFactory Stable 通过字节码生成代替反射,性能比较好(推荐使用) 依赖于javassist.jar包,占用JVM的Perm内存,Perm可能要设大一些:java -XX:PermSize=128m 集群容错 Failover Cluster Stable 失败自动切换,当出现失败,重试其它服务器,通常用于读操作(推荐使用)

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

空扰寡人 提交于 2019-11-26 14:39:50
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层入口方法过滤,配置入口运行前的方法进行监控,相当于监控特有的方法耗时,进行方法专题分析。 4、支持入口方法参数输出功能

Reflections - Java 8 - invalid constant type

让人想犯罪 __ 提交于 2019-11-26 13:57:53
问题 I have a problem with Reflections library. I am trying to load dynamically all classes which implement specific interface. Everything works fine (all classes are loaded) as long as I do not use lambda expression in these classes (java 8). I tried upgrade lib version but effect was the same (java.io.IOException: invalid constant type: 18). Dependency and build in pom.xml <dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> <version>0.9.10</version> <exclusions>

Can a Java class add a method to itself at runtime?

二次信任 提交于 2019-11-26 11:20:27
Can a class add a method to itself at runtime (like from a static block), so that if someone is performing reflection on this class, they'll see the new method, even though it wasn't defined at compile time? Background: A framework I'm using expects Action classes to be defined that have a doAction(...) method, by convention. The framework inspects these classes at runtime to see what type of parameters are available in their doAction() method. For example: doAction( String a, Integer b) I'd like each class to be able to programatically generate its doAction() method with various parameters,