javassist

post-compilation removal of annotations from byte code

有些话、适合烂在心里 提交于 2019-12-04 12:09:51
问题 we are using a library that contains beans that are annotated with JAXB annotations. nothing in the way we use these classes depends on JAXB. in other words, we don't need JAXB and do not depend on the annotations. however, because the annotations exist, they end up being referenced by other classes that process annotations. this requires me to bundle JAXB in our application, which isn't allowed, because JAXB is in the javax.* package (android doesn't allow "core libraries" to be included in

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

こ雲淡風輕ζ 提交于 2019-12-04 11:38:51
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 correctly replace the body of my getMessage() method? Am I doing it right? A ClassPool contains CtClass

custom class loader for android?

删除回忆录丶 提交于 2019-12-04 09:41:40
问题 I'm writing an instrumentation library that I'd like to work on both desktop and mobile (Android). It functions by: Exposing a main which takes a single parameter, the main of the target class Installing a class loader which intercepts all classes as they are loaded and instruments them Like so: // Expects args[0] to contain the name of the INNER main public static void main(String[] args) throws Throwable { String className = args[0]; String [] newArgs = new String[0]; if(args.length > 1) {

How can I create a dynamic proxy in java that retains parameter annotations on methods?

心已入冬 提交于 2019-12-04 09:20:45
问题 I currently am trying to proxy some existing JAX/RS resources, in order to allow me to use the Hibernate Validator's method validation support. However, when I proxy my class (currently using cglib 2.2), the FormParam annotation is not present on parameters in the proxy class, and so the JAX/RS runtime (apache wink) is not populating parameters. Here's some test code that shows this: import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.*; import

What does “()V” mean in a class signature?

不想你离开。 提交于 2019-12-04 07:48:33
问题 I created a constructor with Javassist which has no real method CtConstructor c = CtNewConstructor.make ( argTypes, null, newClass ); When I'm trying to put out the signature of this class c.getSignature(); I get public Echo ()V I'm confused what "V" means? I expected either public Echo (); or something similar... 回答1: The JVM uses a compact way of storing method signatures, of which constructors are considered a special case. For your example: () indicates a method taking no arguments V

javassist使用中遇到的技术细节记录

风格不统一 提交于 2019-12-04 06:10:49
1.java -jar 运行jar包时,javassist.ClassPool 报ClassNotFound 在完成 基于javassist技术和RequestMappingHandlerMapping实现自定义|动态注册spring mvc HandlerMapping的迭代记录 的开发后,部署到线上环境中时,运行提示异常: javassist.NotFoundException: cn.yuyizyk.compent.Handler at javassist.ClassPool.get(ClassPool.java:422) ~[javassist-3.23.1-GA.jar!/:na] at cn.yuyizyk.compent.SrcActionAnalysis.lambda$3(SrcActionAnalysis.java:194) ~[classes!/:0.0.1-SNAPSHOT] 定位错误位置: // cn.yuyizyk.compent.SrcActionAnalysis CtClass handleCtClz = pool.makeClass(Handler.class.getName() + "$" + c.getSimpleName() + "$" + m.getName()+ "R" + (new Random().nextInt(1024)),pool

Cglib、Javassist、JDK动态代理

白昼怎懂夜的黑 提交于 2019-12-04 05:32:01
一、简介 Java的动态代理真的非常重要,特别是想要了解一些框架的原理的时候,如果对Java动态代理不熟悉,甚至不了解Java动态代理,那基本上就只能说一句“我太难了”。 比如想要知道MyBatis为什么不用实现方法,只需要一个接口和一个对应的xml文件就能实现数据库操作。 Spring中为什么我们使用一个@Transactional注解就能实现事务自动回滚。 当然了解了Java的动态代理,我们自己也可以实现一些无侵入,对用户透明友好的附加功能。 所以想要了解这些原理,就先来了解一下Java动态代理吧。 二、JDK动态代理 首先我们来看一些JDK的动态代理怎样实现。 要理解JDK的动态代理,只需要理解Proxy类和InvocationHandler接口就可以了。 不过在这之前我们先介绍2个概念: 目标对象(target):被代理的对象 代理对象(proxy):Proxy动态生成并加载的对象 2.1 InvocationHandler 首先我们来看InvocationHandler,InvocationHandler接口非常简单就一个方法,如下所示: public Object invoke(Object proxy, Method method, Object[] args) 这个方法就是实现JDK代理逻辑的地方,proxy是代理对象,这个是在Proxy类中创建的。

Javassist failure in hibernate: invalid constant type: 60

空扰寡人 提交于 2019-12-04 05:23:11
I'm creating a cli tool to manage an existing application. Both the application and the tests build fine and run fine but despite that I receive a javassist failure when running my cli tool that exists within the jar: INFO: Bytecode provider name : javassist ... INFO: Hibernate EntityManager 3.5.1-Final Exception in thread "main" javax.persistence.PersistenceException: Unable to configure EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:371) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55) at javax

Adding an annotation to a runtime generated class using Javassist

安稳与你 提交于 2019-12-04 03:54:29
问题 I'm using Javassist(Java 1.7) to add an annotation to the class ClassA, but i get the exception. What am i doing wrong? The code I tried looks like this: ClassA.java public class ClassA { } add method public static <T> Class<T> addXmlRootAnnotationDynamicly(Class<T> declaredTyp) throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException { //pool creation ClassPool pool = ClassPool.getDefault(); //extracting the class CtClass cc = pool.getCtClass

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

早过忘川 提交于 2019-12-03 08:58:48
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, defrost() it, remove all members etc, and re-create it. Any other ideas? Or is there some reliable way