Skip to content

Commit ff9ab96

Browse files
authored
Merge pull request #184 from xzhong66/migrateCGLIB
Migrate CGLIB to ByteBuddy
2 parents f26fd0a + 048f7f2 commit ff9ab96

File tree

4 files changed

+101
-159
lines changed

4 files changed

+101
-159
lines changed

ipp-v3-java-devkit/pom.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@
4343
<artifactId>jaxb2-commons-lang</artifactId>
4444
</exclusion>
4545
</exclusions>
46-
</dependency>
47-
<dependency>
48-
<groupId>cglib</groupId>
49-
<artifactId>cglib</artifactId>
50-
<version>3.2.12</version>
51-
</dependency>
46+
</dependency>
5247
<dependency>
5348
<groupId>asm</groupId>
5449
<artifactId>asm-commons</artifactId>
@@ -119,7 +114,12 @@
119114
<artifactId>jmockit</artifactId>
120115
<version>1.25</version>
121116
<scope>test</scope>
122-
</dependency>
117+
</dependency>
118+
<dependency>
119+
<groupId>net.bytebuddy</groupId>
120+
<artifactId>byte-buddy</artifactId>
121+
<version>1.12.10</version>
122+
</dependency>
123123
</dependencies>
124124
<build>
125125
<testResources>

ipp-v3-java-devkit/src/main/java/com/intuit/ipp/query/GenerateQuery.java

Lines changed: 44 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
*******************************************************************************/
1616
package com.intuit.ipp.query;
1717

18-
import java.lang.reflect.Method;
1918
import java.util.Calendar;
2019
import java.util.Date;
2120

22-
import net.sf.cglib.proxy.Callback;
23-
import net.sf.cglib.proxy.CallbackFilter;
24-
import net.sf.cglib.proxy.Enhancer;
21+
import net.bytebuddy.ByteBuddy;
22+
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
23+
import net.bytebuddy.implementation.MethodDelegation;
24+
import net.bytebuddy.matcher.ElementMatchers;
2525

2626
import com.intuit.ipp.core.IEntity;
2727
import com.intuit.ipp.query.expr.BooleanPath;
@@ -30,87 +30,87 @@
3030
import com.intuit.ipp.query.expr.NumberPath;
3131
import com.intuit.ipp.query.expr.StringPath;
3232
import com.intuit.ipp.util.Logger;
33-
import net.sf.cglib.proxy.NoOp;
3433

3534
/**
3635
* Class used to generate the query string
37-
*
36+
*
3837
*/
3938
public final class GenerateQuery {
4039

4140
/**
4241
* logger instance
4342
*/
4443
private static final org.slf4j.Logger LOG = Logger.getLogger();
45-
44+
4645
/**
4746
* variable LEN_3
4847
*/
4948
private static final int LEN_3 = 3;
50-
49+
5150
/**
5251
* variable path
5352
*/
5453
public static ThreadLocal<Path<?>> path = new ThreadLocal<Path<?>>();
55-
54+
5655
/**
5756
* varriable message
5857
*/
5958
private static QueryMessage message = new QueryMessage();
6059

60+
/**
61+
* variable CLASSNAME_SPLIT_PATTERN
62+
*/
63+
private static final String CLASSNAME_SPLIT_PATTERN = "\\$";
64+
6165
/**
6266
* Constructor to have private modifier as it has only static methods
6367
*/
6468
private GenerateQuery() {
6569
}
6670

6771
/**
68-
* Method to create the query entity for the given class
69-
*
72+
*
7073
* @param cl the class
7174
* @return the proxified object
7275
*/
7376
@SuppressWarnings("unchecked")
7477
public static <T> T createQueryEntity(Class<T> cl) {
75-
Enhancer enhancer = new Enhancer();
78+
Class<?> proxied = null;
7679
if (cl.isInterface()) {
7780
LOG.debug("The given class is interface");
78-
//enhancer.setInterfaces(new Class[] { cl });
79-
//enhancer.setCallback(new MyMethodInterceptor());
8081
} else {
81-
enhancer.setSuperclass(cl);
82+
proxied = new ByteBuddy()
83+
.subclass(cl)
84+
.method(ElementMatchers.not(ElementMatchers.isClone().or(ElementMatchers.isFinalizer()).or(ElementMatchers.isEquals()).or(ElementMatchers.isHashCode()).or(ElementMatchers.isToString())))
85+
.intercept(MethodDelegation.to(new MyMethodInterceptor()))
86+
.make()
87+
.load(GenerateQuery.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
88+
.getLoaded();
89+
}
90+
try {
91+
return (T) proxied.newInstance();
92+
} catch (InstantiationException | IllegalAccessException e) {
93+
LOG.error(e.getMessage());
94+
throw new RuntimeException(e);
8295
}
83-
enhancer.setCallbackFilter(CALLBACK_FILTER);
84-
enhancer.setCallbacks(new Callback[] {NoOp.INSTANCE, new MyMethodInterceptor()});
85-
return (T) enhancer.create();
8696
}
8797

8898
/**
89-
* Method to create the query for the given entity
90-
*
99+
*
91100
* @param entity the entity
92101
* @return the proxified object
93102
*/
94103
@SuppressWarnings("unchecked")
95104
public static <T> T createQueryEntity(T entity) {
96105
Class<?> cl = entity.getClass();
97-
Enhancer enhancer = new Enhancer();
98-
if (cl.isInterface()) {
99-
LOG.debug("The given entity is interface");
100-
//enhancer.setInterfaces(new Class[] { cl });
101-
//enhancer.setCallback(new MyMethodInterceptor());
102-
} else {
103-
enhancer.setSuperclass(cl);
104-
enhancer.setCallbackFilter(CALLBACK_FILTER);
105-
enhancer.setCallbacks(new Callback[] {NoOp.INSTANCE, new MyMethodInterceptor()});
106-
}
107-
return (T) enhancer.create();
106+
return (T) createQueryEntity(cl);
108107
}
109108

109+
110110
/**
111111
* when no handler for specific return type is defined which means properties of that type cannot be inserted in filter expression but can be
112112
* listed in select part, it will return Path
113-
*
113+
*
114114
* @param ret the object
115115
* @return path the path
116116
*/
@@ -121,15 +121,15 @@ public static <T> T createQueryEntity(T entity) {
121121
return new Path<Object>(currentPath.getPathString().concat(".*"), currentPath.getEntity());
122122
} else {
123123
String name = ret.getClass().getSimpleName();
124-
String[] extracted = name.split("\\$\\$");
124+
String[] extracted = name.split(CLASSNAME_SPLIT_PATTERN);
125125
return new Path<Object>("*", extracted[0]);
126126
}
127127
}
128128

129129
/**
130130
* When return type is Calendar, it will create CalendarPath which will expose filter methods accepting java.util.Calendar, java.util.Date and
131131
* java.sql.Date
132-
*
132+
*
133133
* @param ret
134134
* @return
135135
*/
@@ -141,7 +141,7 @@ public static <T> T createQueryEntity(T entity) {
141141

142142
/**
143143
* Method to get the calendar path
144-
*
144+
*
145145
* @param ret the date
146146
* @return CalendarPath the calendar path
147147
*/
@@ -153,7 +153,7 @@ public static <T> T createQueryEntity(T entity) {
153153

154154
/**
155155
* When return type is String, it will create StringPath which will expose filter methods accepting only String
156-
*
156+
*
157157
* @param ret the string
158158
* @return StringPath the string path
159159
*/
@@ -165,7 +165,7 @@ public static <T> T createQueryEntity(T entity) {
165165

166166
/**
167167
* Method to get the number path
168-
*
168+
*
169169
* @param ret the number
170170
* @return NumberPath the number path
171171
*/
@@ -177,7 +177,7 @@ public static <T> T createQueryEntity(T entity) {
177177

178178
/**
179179
* Method to get the boolean path
180-
*
180+
*
181181
* @param ret the boolean
182182
* @return BooleanPath the boolean path
183183
*/
@@ -189,7 +189,7 @@ public static <T> T createQueryEntity(T entity) {
189189

190190
/**
191191
* Method to get the Enum path
192-
*
192+
*
193193
* @param ret the enum
194194
* @return EnumPath the enum path
195195
*/
@@ -201,7 +201,7 @@ public static <T> T createQueryEntity(T entity) {
201201

202202
/**
203203
* Method to get the optional syntax
204-
*
204+
*
205205
* @param path the path
206206
* @param pathlist the path list
207207
* @return OptionalSyntax the optional syntax
@@ -222,15 +222,15 @@ public static <T extends IEntity> OptionalSyntax select(Path<?> path, Path<?>...
222222

223223
/**
224224
* Method to get the optional syntax for the given entity
225-
*
225+
*
226226
* @param entity the entity
227227
* @return OptionalSyntax the optional syntax
228228
*/
229229
public static <T extends IEntity> OptionalSyntax selectCount(T entity) {
230230
resetQueryMessage();
231231
getMessage().setSQL("SELECT");
232232
String name = entity.getClass().getSimpleName();
233-
String extracted[] = name.split("\\$\\$");
233+
String extracted[] = name.split(CLASSNAME_SPLIT_PATTERN);
234234
getMessage().setCount(true);
235235
if (extracted.length == LEN_3) {
236236
getMessage().setEntity(extracted[0]);
@@ -240,7 +240,7 @@ public static <T extends IEntity> OptionalSyntax selectCount(T entity) {
240240

241241
/**
242242
* Method to get the query message
243-
*
243+
*
244244
* @return QueryMessage the query message
245245
*/
246246
public static QueryMessage getMessage() {
@@ -249,7 +249,7 @@ public static QueryMessage getMessage() {
249249

250250
/**
251251
* Method to set the query message
252-
*
252+
*
253253
* @param mess the query message
254254
*/
255255
public static void setMessage(QueryMessage mess) {
@@ -262,66 +262,4 @@ public static void setMessage(QueryMessage mess) {
262262
public static void resetQueryMessage() {
263263
setMessage(new QueryMessage());
264264
}
265-
266-
/**
267-
* Callback filter which will filter out callback triggers for several {@link Object} methods.
268-
*/
269-
private static final CallbackFilter CALLBACK_FILTER = new CallbackFilter() {
270-
271-
@Override
272-
public int accept(Method method) {
273-
if (isFinalizeMethod(method) || isCloneMethod(method) || isEqualsMethod(method)
274-
|| isHashCodeMethod(method) || isToStringMethod(method)) {
275-
return 0;
276-
}
277-
return 1;
278-
}
279-
280-
/**
281-
* Determine whether the given method is a "finalize" method.
282-
* @see java.lang.Object#finalize()
283-
*/
284-
private boolean isFinalizeMethod(Method method) {
285-
return (method != null && method.getName().equals("finalize") &&
286-
method.getParameterTypes().length == 0);
287-
}
288-
289-
/**
290-
* Determine whether the given method is a "finalize" method.
291-
* @see java.lang.Object#finalize()
292-
*/
293-
private boolean isCloneMethod(Method method) {
294-
return (method != null && method.getName().equals("clone") &&
295-
method.getParameterTypes().length == 0);
296-
}
297-
298-
/**
299-
* Determine whether the given method is an "equals" method.
300-
* @see java.lang.Object#equals(Object)
301-
*/
302-
private boolean isEqualsMethod(Method method) {
303-
if (method == null || !method.getName().equals("equals")) {
304-
return false;
305-
}
306-
Class<?>[] paramTypes = method.getParameterTypes();
307-
return (paramTypes.length == 1 && paramTypes[0] == Object.class);
308-
}
309-
310-
/**
311-
* Determine whether the given method is a "hashCode" method.
312-
* @see java.lang.Object#hashCode()
313-
*/
314-
private boolean isHashCodeMethod(Method method) {
315-
return (method != null && method.getName().equals("hashCode") && method.getParameterTypes().length == 0);
316-
}
317-
318-
/**
319-
* Determine whether the given method is a "toString" method.
320-
* @see java.lang.Object#toString()
321-
*/
322-
private boolean isToStringMethod(Method method) {
323-
return (method != null && method.getName().equals("toString") && method.getParameterTypes().length == 0);
324-
}
325-
};
326-
327265
}

0 commit comments

Comments
 (0)