1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.codebox.builders;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import javassist.CannotCompileException;
21 import javassist.ClassPool;
22 import javassist.CtClass;
23 import javassist.CtField;
24 import javassist.CtMethod;
25 import javassist.NotFoundException;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31
32
33
34
35
36 public class ExtensionBuilder<T> {
37
38
39 private static final Logger LOGGER = LoggerFactory.getLogger(ExtensionBuilder.class);
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public Class<?> generate(final Class<T> clazz) throws NotFoundException, CannotCompileException {
55 try {
56
57 return Class.forName(clazz.getName() + "Extension");
58 } catch (final ClassNotFoundException e) {
59
60 ExtensionBuilder.LOGGER.trace("No extension exists, so create it", e);
61 }
62
63 final ClassPool pool = ClassPool.getDefault();
64 final CtClass cc = pool.makeClass(clazz.getName() + "Extension");
65
66
67 cc.setSuperclass(ExtensionBuilder.resolveCtClass(clazz));
68
69 final Map<String, Class<?>> properties = new HashMap<>();
70 properties.put("jbExtension1", String.class);
71 properties.put("jbExtension2", String.class);
72 properties.put("jbExtension3", String.class);
73 properties.put("jbExtension4", String.class);
74
75 for (final Entry<String, Class<?>> entry : properties.entrySet()) {
76
77
78 cc.addField(new CtField(ExtensionBuilder.resolveCtClass(entry.getValue()), entry.getKey(), cc));
79
80
81 cc.addMethod(ExtensionBuilder.generateGetter(cc, entry.getKey(), entry.getValue()));
82
83
84 cc.addMethod(ExtensionBuilder.generateSetter(cc, entry.getKey(), entry.getValue()));
85 }
86
87 return cc.toClass();
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 private static CtMethod generateGetter(final CtClass declaringClass, final String fieldName,
106 final Class<?> fieldClass) throws CannotCompileException {
107 String methodSrc = """
108 public %s get%s() {
109 return this.%s;
110 }
111 """.formatted(fieldClass.getName(), fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1),
112 fieldName);
113 return CtMethod.make(methodSrc, declaringClass);
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 private static CtMethod generateSetter(final CtClass declaringClass, final String fieldName,
132 final Class<?> fieldClass) throws CannotCompileException {
133 String methodSrc = """
134 public void set%s(%s %s) {
135 this.%s = %s;
136 }
137 """.formatted(fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), fieldClass.getName(),
138 fieldName, fieldName, fieldName);
139 return CtMethod.make(methodSrc, declaringClass);
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153 private static CtClass resolveCtClass(final Class<?> clazz) throws NotFoundException {
154 return ClassPool.getDefault().get(clazz.getName());
155 }
156
157 }