Skip to content

Commit 5cd552a

Browse files
tomekl007pedja4
authored andcommitted
BAEL-634 javassist (eugenp#1349)
* BEEL-634 javassist dependency * BEEL-634 code for javassist article * BEEL-634 test refinement * BEEL-634 increment lib to newest version * add test that uses reflection to verify * add field * add bytecode to different class
1 parent 61660f5 commit 5cd552a

4 files changed

Lines changed: 162 additions & 1 deletion

File tree

‎libraries/pom.xml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</plugins>
2424
</build>
2525

26-
<dependencies>
26+
<dependencies>
2727
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
2828
<dependency>
2929
<groupId>cglib</groupId>
@@ -51,6 +51,11 @@
5151
<artifactId>javatuples</artifactId>
5252
<version>${javatuples.version}</version>
5353
</dependency>
54+
<dependency>
55+
<groupId>org.javassist</groupId>
56+
<artifactId>javassist</artifactId>
57+
<version>${javaassist.version}</version>
58+
</dependency>
5459
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
5560
<dependency>
5661
<groupId>org.assertj</groupId>
@@ -65,6 +70,7 @@
6570
<junit.version>4.12</junit.version>
6671
<jasypt.version>1.9.2</jasypt.version>
6772
<javatuples.version>1.2</javatuples.version>
73+
<javaassist.version>3.21.0-GA</javaassist.version>
6874
<assertj.version>3.6.2</assertj.version>
6975
</properties>
7076

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.javasisst;
2+
3+
4+
public class Point {
5+
public int x = 0;
6+
public int y = 0;
7+
8+
public Point(int x, int y) {
9+
this.x = x;
10+
this.y = y;
11+
}
12+
13+
public void move(int x, int y) {
14+
this.x = x;
15+
this.y = y;
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.javasisst;
2+
3+
4+
public class ThreeDimensionalPoint {
5+
public int x = 0;
6+
public int y = 0;
7+
public int z = 0;
8+
9+
public ThreeDimensionalPoint(int x, int y, int z) {
10+
this.x = x;
11+
this.y = y;
12+
this.z = z;
13+
}
14+
15+
public void move(int x, int y) {
16+
this.x = x;
17+
this.y = y;
18+
}
19+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.baeldung.javassist;
2+
3+
4+
import javassist.CannotCompileException;
5+
import javassist.ClassPool;
6+
import javassist.NotFoundException;
7+
import javassist.bytecode.*;
8+
import org.junit.Test;
9+
10+
import java.io.DataOutputStream;
11+
import java.io.FileOutputStream;
12+
import java.io.IOException;
13+
import java.lang.reflect.Field;
14+
import java.nio.file.Files;
15+
import java.nio.file.Paths;
16+
import java.util.Arrays;
17+
import java.util.LinkedList;
18+
import java.util.List;
19+
import java.util.stream.Collectors;
20+
import java.util.stream.Stream;
21+
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertTrue;
24+
25+
public class JavasisstTest {
26+
@Test
27+
public void givenJavasisstAPI_whenConstructClass_thenGenerateAClassFile() throws CannotCompileException, IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
28+
//given
29+
String classNameWithPackage = "com.baeldung.JavassistGeneratedClass";
30+
ClassFile cf = new ClassFile(false, classNameWithPackage, null);
31+
cf.setInterfaces(new String[]{"java.lang.Cloneable"});
32+
33+
FieldInfo f = new FieldInfo(cf.getConstPool(), "id", "I");
34+
f.setAccessFlags(AccessFlag.PUBLIC);
35+
cf.addField(f);
36+
37+
//when
38+
String className = "JavassistGeneratedClass.class";
39+
cf.write(new DataOutputStream(new FileOutputStream(className)));
40+
41+
//then
42+
ClassPool classPool = ClassPool.getDefault();
43+
Field[] fields = classPool.makeClass(cf).toClass().getFields();
44+
assertEquals(fields[0].getName(), "id");
45+
46+
String classContent = new String(Files.readAllBytes(Paths.get(className)));
47+
assertTrue(classContent.contains("java/lang/Cloneable"));
48+
}
49+
50+
@Test
51+
public void givenJavaClass_whenLoadAtByJavassist_thenTraversWholeClass() throws NotFoundException, CannotCompileException, BadBytecode {
52+
//given
53+
ClassPool cp = ClassPool.getDefault();
54+
ClassFile cf = cp.get("com.baeldung.javasisst.Point").getClassFile();
55+
MethodInfo minfo = cf.getMethod("move");
56+
CodeAttribute ca = minfo.getCodeAttribute();
57+
CodeIterator ci = ca.iterator();
58+
59+
//when
60+
List<String> operations = new LinkedList<>();
61+
while (ci.hasNext()) {
62+
int index = ci.next();
63+
int op = ci.byteAt(index);
64+
operations.add(Mnemonic.OPCODE[op]);
65+
}
66+
67+
//then
68+
assertEquals(operations,
69+
Arrays.asList("aload_0", "iload_1", "putfield", "aload_0", "iload_2", "putfield", "return"));
70+
71+
}
72+
73+
@Test
74+
public void givenTableOfInstructions_whenAddNewInstruction_thenShouldConstructProperSequence() throws NotFoundException, BadBytecode, CannotCompileException, IllegalAccessException, InstantiationException {
75+
//given
76+
ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.ThreeDimensionalPoint").getClassFile();
77+
78+
//when
79+
FieldInfo f = new FieldInfo(cf.getConstPool(), "id", "I");
80+
f.setAccessFlags(AccessFlag.PUBLIC);
81+
cf.addField(f);
82+
83+
84+
ClassPool classPool = ClassPool.getDefault();
85+
Field[] fields = classPool.makeClass(cf).toClass().getFields();
86+
List<String> fieldsList = Stream.of(fields).map(Field::getName).collect(Collectors.toList());
87+
assertTrue(fieldsList.contains("id"));
88+
89+
}
90+
91+
@Test
92+
public void givenLoadedClass_whenAddConstructorToClass_shouldCreateClassWithConstructor() throws NotFoundException, CannotCompileException, BadBytecode {
93+
//given
94+
ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.Point").getClassFile();
95+
Bytecode code = new Bytecode(cf.getConstPool());
96+
code.addAload(0);
97+
code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V");
98+
code.addReturn(null);
99+
100+
//when
101+
MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V");
102+
minfo.setCodeAttribute(code.toCodeAttribute());
103+
cf.addMethod(minfo);
104+
105+
//then
106+
CodeIterator ci = code.toCodeAttribute().iterator();
107+
List<String> operations = new LinkedList<>();
108+
while (ci.hasNext()) {
109+
int index = ci.next();
110+
int op = ci.byteAt(index);
111+
operations.add(Mnemonic.OPCODE[op]);
112+
}
113+
114+
assertEquals(operations,
115+
Arrays.asList("aload_0", "invokespecial", "return"));
116+
117+
118+
}
119+
}

0 commit comments

Comments
 (0)