Questions tagged [javassist]

Javassist is a class library for editing bytecode in java.

Javassist is one of several tools that exist for manipulation of Java Bytecode but, it includes a simple Java Compiler for processing source text. It receives source text written in Java and compiles it into Java bytecode, which will be inlined into a method body.

This feature allows first comers to start modifying class' bytecode to some extent without deep knowledge regarding the class format or actual bytecode instruction set and OP Codes.

611 questions
65
votes
11 answers

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

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…
slattery
  • 1,379
  • 3
  • 10
  • 19
42
votes
3 answers

Dynamic Java Bytecode Manipulation Framework Comparison

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…
35
votes
6 answers

Reflections - Java 8 - invalid constant type

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…
Lukasz
  • 517
  • 1
  • 8
  • 20
34
votes
4 answers

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

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?
user471011
  • 7,104
  • 17
  • 69
  • 97
26
votes
3 answers

What does "()V" mean in a class signature?

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…
Evils
  • 857
  • 3
  • 12
  • 31
23
votes
1 answer

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

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 =…
Idan K
  • 20,443
  • 10
  • 63
  • 83
22
votes
3 answers

Bad method call from inside of a branch

After upgrading JDK to java7u65 several unit-tests utilizing Mockito and PowerMock started to fail with following causes: 15:15:23,807 INFO - Tests in error: 15:15:23,810 INFO - initializationError(com.your.ClassHere): Bad method call…
Lauri
  • 1,859
  • 2
  • 14
  • 17
22
votes
2 answers

Replace content of some methods at runtime

I would like to replace the content of some methods at runtime. I know I can use javassist for this but it does not work because the classes I would like to enhance are already loaded by the system classLoader. How can I do, to replace the content…
Fabien Henon
  • 783
  • 15
  • 37
21
votes
4 answers

PowerMock and Java 8 issue: InterfaceMethodrefInfo cannot be cast to MethodrefInfo

I´m having issues while trying to execute a unit test using PowerMock with Mockito. I need PowerMockito to mock an static method. These are the versions I´m using: PowerMock 1.6.2 Mockito 1.10.19 JUnit 4.12 Java 8 When I add the annotation…
KurroGB
  • 431
  • 2
  • 4
  • 8
17
votes
1 answer

Is there Scala aware high level byte-code manipulation tool like Javassist?

I am looking for a high level bytecode manipulation tool like Javassist, but that understands some of Scala peculiarities. Lower level bytecode manipulation tools should be relatively agnostic, but for my use cases something at the level of…
Daniel Mahler
  • 7,653
  • 5
  • 51
  • 90
15
votes
1 answer

javassist loading a class file in the pre-main method (java instrumentation)

I'm trying to load a specific class using javassist, I'm doing this inside a pre-main method as the follwoing: public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain…
ManKeer
  • 543
  • 2
  • 6
  • 27
13
votes
3 answers

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

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…
Peter Hart
  • 4,955
  • 2
  • 25
  • 30
11
votes
2 answers

Removing instructions from Java bytecode

I have been using Javassist to dynamically manipulate classes as they are loaded. While adding code to a method is relatively easy using Javassist, I have not been able to find a way to remove code. At this time I am simulating the removal of the…
thkala
  • 84,049
  • 23
  • 157
  • 201
11
votes
4 answers

Post-compilation removal of annotations from byte code

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…
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
10
votes
4 answers

Hibernate javassist proxies and `Object#equals`

When providing #equals implementation for a UDT in Java one of the conditions is that the passed argument object must be an instance of the current class otherwise we fail-fast return false see Effective Java (EJ2). However, while using Hibernate 4…
SkyWalker
  • 13,729
  • 18
  • 91
  • 187
1
2 3
40 41