Skip to content

Commit 9901742

Browse files
author
Xiang Zhong
committed
PathString bug fix
1 parent 4756479 commit 9901742

File tree

2 files changed

+39
-126
lines changed

2 files changed

+39
-126
lines changed

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

Lines changed: 35 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919
import java.util.Calendar;
2020
import java.util.Date;
2121

22+
import com.intuit.ipp.data.Customer;
2223
import net.bytebuddy.ByteBuddy;
2324
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
2425
import net.bytebuddy.implementation.MethodDelegation;
25-
import net.bytebuddy.implementation.SuperMethodCall;
2626
import net.bytebuddy.matcher.ElementMatchers;
27-
import net.sf.cglib.proxy.Callback;
28-
import net.sf.cglib.proxy.CallbackFilter;
29-
import net.sf.cglib.proxy.Enhancer;
3027

3128
import com.intuit.ipp.core.IEntity;
3229
import com.intuit.ipp.query.expr.BooleanPath;
@@ -35,7 +32,6 @@
3532
import com.intuit.ipp.query.expr.NumberPath;
3633
import com.intuit.ipp.query.expr.StringPath;
3734
import com.intuit.ipp.util.Logger;
38-
import net.sf.cglib.proxy.NoOp;
3935

4036
/**
4137
* Class used to generate the query string
@@ -69,49 +65,6 @@ public final class GenerateQuery {
6965
private GenerateQuery() {
7066
}
7167

72-
// /**
73-
// * Method to create the query entity for the given class
74-
// *
75-
// * @param cl the class
76-
// * @return the proxified object
77-
// */
78-
// @SuppressWarnings("unchecked")
79-
// public static <T> T createQueryEntity(Class<T> cl) {
80-
// Enhancer enhancer = new Enhancer();
81-
// if (cl.isInterface()) {
82-
// LOG.debug("The given class is interface");
83-
// //enhancer.setInterfaces(new Class[] { cl });
84-
// //enhancer.setCallback(new MyMethodInterceptor());
85-
// } else {
86-
// enhancer.setSuperclass(cl);
87-
// }
88-
// enhancer.setCallbackFilter(CALLBACK_FILTER);
89-
// enhancer.setCallbacks(new Callback[] {NoOp.INSTANCE, new MyMethodInterceptor()});
90-
// return (T) enhancer.create();
91-
// }
92-
//
93-
// /**
94-
// * Method to create the query for the given entity
95-
// *
96-
// * @param entity the entity
97-
// * @return the proxified object
98-
// */
99-
// @SuppressWarnings("unchecked")
100-
// public static <T> T createQueryEntity(T entity) {
101-
// Class<?> cl = entity.getClass();
102-
// Enhancer enhancer = new Enhancer();
103-
// if (cl.isInterface()) {
104-
// LOG.debug("The given entity is interface");
105-
// //enhancer.setInterfaces(new Class[] { cl });
106-
// //enhancer.setCallback(new MyMethodInterceptor());
107-
// } else {
108-
// enhancer.setSuperclass(cl);
109-
// enhancer.setCallbackFilter(CALLBACK_FILTER);
110-
// enhancer.setCallbacks(new Callback[] {NoOp.INSTANCE, new MyMethodInterceptor()});
111-
// }
112-
// return (T) enhancer.create();
113-
// }
114-
11568

11669
@SuppressWarnings("unchecked")
11770
public static <T> T createQueryEntity(Class<T> cl) {
@@ -121,15 +74,18 @@ public static <T> T createQueryEntity(Class<T> cl) {
12174
} else {
12275
proxied = new ByteBuddy()
12376
.subclass(cl)
124-
.method(ElementMatchers.any())
77+
.method(ElementMatchers.not(ElementMatchers.isClone().or(ElementMatchers.isFinalizer()).or(ElementMatchers.isEquals()).or(ElementMatchers.isHashCode()).or(ElementMatchers.isToString())))
12578
.intercept(MethodDelegation.to(new MyMethodInterceptor()))
126-
.method(ElementMatchers.isClone().or(ElementMatchers.isFinalizer()).or(ElementMatchers.isEquals()).or(ElementMatchers.isHashCode()).or(ElementMatchers.isToString()))
127-
.intercept(SuperMethodCall.INSTANCE)
12879
.make()
129-
.load(cl.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
80+
// .load(cl.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
81+
.load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
13082
.getLoaded();
13183
}
132-
return (T) proxied;
84+
try {
85+
return (T) proxied.newInstance();
86+
} catch(Exception exception) {
87+
throw new RuntimeException(exception);
88+
}
13389
}
13490

13591
@SuppressWarnings("unchecked")
@@ -141,15 +97,22 @@ public static <T> T createQueryEntity(T entity) {
14197
} else {
14298
proxied = new ByteBuddy()
14399
.subclass(cl)
144-
.method(ElementMatchers.any())
100+
// .method(ElementMatchers.any())
101+
// .intercept(MethodDelegation.to(new MyMethodInterceptor()))
102+
// .method(ElementMatchers.isClone().or(ElementMatchers.isFinalizer()).or(ElementMatchers.isEquals()).or(ElementMatchers.isHashCode()).or(ElementMatchers.isToString()))
103+
// .intercept(SuperMethodCall.INSTANCE)
104+
.method(ElementMatchers.not(ElementMatchers.isClone().or(ElementMatchers.isFinalizer()).or(ElementMatchers.isEquals()).or(ElementMatchers.isHashCode()).or(ElementMatchers.isToString())))
145105
.intercept(MethodDelegation.to(new MyMethodInterceptor()))
146-
.method(ElementMatchers.isClone().or(ElementMatchers.isFinalizer()).or(ElementMatchers.isEquals()).or(ElementMatchers.isHashCode()).or(ElementMatchers.isToString()))
147-
.intercept(SuperMethodCall.INSTANCE)
148106
.make()
149-
.load(cl.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
107+
// .load(cl.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
108+
.load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
150109
.getLoaded();
151110
}
152-
return (T) proxied;
111+
try {
112+
return (T) proxied.newInstance();
113+
} catch(Exception exception) {
114+
throw new RuntimeException(exception);
115+
}
153116
}
154117

155118

@@ -167,7 +130,7 @@ public static <T> T createQueryEntity(T entity) {
167130
return new Path<Object>(currentPath.getPathString().concat(".*"), currentPath.getEntity());
168131
} else {
169132
String name = ret.getClass().getSimpleName();
170-
String[] extracted = name.split("\\$\\$");
133+
String[] extracted = name.split("\\$");
171134
return new Path<Object>("*", extracted[0]);
172135
}
173136
}
@@ -276,7 +239,7 @@ public static <T extends IEntity> OptionalSyntax selectCount(T entity) {
276239
resetQueryMessage();
277240
getMessage().setSQL("SELECT");
278241
String name = entity.getClass().getSimpleName();
279-
String extracted[] = name.split("\\$\\$");
242+
String extracted[] = name.split("\\$");
280243
getMessage().setCount(true);
281244
if (extracted.length == LEN_3) {
282245
getMessage().setEntity(extracted[0]);
@@ -309,65 +272,17 @@ public static void resetQueryMessage() {
309272
setMessage(new QueryMessage());
310273
}
311274

312-
/**
313-
* Callback filter which will filter out callback triggers for several {@link Object} methods.
314-
*/
315-
private static final CallbackFilter CALLBACK_FILTER = new CallbackFilter() {
316-
317-
@Override
318-
public int accept(Method method) {
319-
if (isFinalizeMethod(method) || isCloneMethod(method) || isEqualsMethod(method)
320-
|| isHashCodeMethod(method) || isToStringMethod(method)) {
321-
return 0;
322-
}
323-
return 1;
324-
}
325-
326-
/**
327-
* Determine whether the given method is a "finalize" method.
328-
* @see java.lang.Object#finalize()
329-
*/
330-
private boolean isFinalizeMethod(Method method) {
331-
return (method != null && method.getName().equals("finalize") &&
332-
method.getParameterTypes().length == 0);
333-
}
334-
335-
/**
336-
* Determine whether the given method is a "finalize" method.
337-
* @see java.lang.Object#finalize()
338-
*/
339-
private boolean isCloneMethod(Method method) {
340-
return (method != null && method.getName().equals("clone") &&
341-
method.getParameterTypes().length == 0);
342-
}
343-
344-
/**
345-
* Determine whether the given method is an "equals" method.
346-
* @see java.lang.Object#equals(Object)
347-
*/
348-
private boolean isEqualsMethod(Method method) {
349-
if (method == null || !method.getName().equals("equals")) {
350-
return false;
351-
}
352-
Class<?>[] paramTypes = method.getParameterTypes();
353-
return (paramTypes.length == 1 && paramTypes[0] == Object.class);
354-
}
355-
356-
/**
357-
* Determine whether the given method is a "hashCode" method.
358-
* @see java.lang.Object#hashCode()
359-
*/
360-
private boolean isHashCodeMethod(Method method) {
361-
return (method != null && method.getName().equals("hashCode") && method.getParameterTypes().length == 0);
362-
}
363-
364-
/**
365-
* Determine whether the given method is a "toString" method.
366-
* @see java.lang.Object#toString()
367-
*/
368-
private boolean isToStringMethod(Method method) {
369-
return (method != null && method.getName().equals("toString") && method.getParameterTypes().length == 0);
370-
}
371-
};
372275

276+
public static void main(String[] args) {
277+
Customer customer = createQueryEntity(Customer.class);
278+
String query = select($(customer.getId()), $(customer.getDisplayName())).where($(customer.getId()).eq("10")).generate();
279+
System.out.println(query);
280+
// Class<?> proxied = new ByteBuddy()
281+
// .subclass(Customer.class)
282+
// .method(ElementMatchers.not(ElementMatchers.isClone().or(ElementMatchers.isFinalizer()).or(ElementMatchers.isEquals()).or(ElementMatchers.isHashCode()).or(ElementMatchers.isToString())))
283+
// .intercept(MethodDelegation.to(new MyMethodInterceptor()))
284+
// .make()
285+
// .load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
286+
// .getLoaded();
287+
}
373288
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import com.intuit.ipp.util.Logger;
3030

3131
import net.bytebuddy.implementation.bind.annotation.*;
32-
import net.sf.cglib.proxy.MethodInterceptor;
33-
import net.sf.cglib.proxy.MethodProxy;
3432

3533
/**
3634
* For intercepting method and adding the name of method called in threadlocal stringbuilder.
@@ -63,7 +61,7 @@ public MyMethodInterceptor() {
6361
}
6462

6563
@RuntimeType
66-
public Object intercept(@This Object arg0, @Origin Method arg1, @AllArguments Object[] arg2, @SuperMethod Method arg3) throws FMSException {
64+
public Object intercept(@This Object arg0, @Origin Method arg1, @AllArguments Object[] arg2, @SuperMethod(nullIfImpossible = true) Method arg3) throws FMSException {
6765

6866
if (GenerateQuery.path.get() == null) {
6967
GenerateQuery.path.set(new Path<Object>(extractPropertyName(arg1), extractEntity(arg0)));
@@ -81,7 +79,7 @@ public Object intercept(@This Object arg0, @Origin Method arg1, @AllArguments Ob
8179
*/
8280
private String extractEntity(Object obj) {
8381
String name = obj.getClass().getSimpleName();
84-
String[] extracted = name.split("\\$\\$");
82+
String[] extracted = name.split("\\$");
8583
if (extracted.length == NUM_3) {
8684
return extracted[0];
8785
}
@@ -115,9 +113,9 @@ public <T> T createInstance(Object arg0, Method arg1, Object[] arg2, Method arg3
115113
if (String.class.equals(type)) {
116114
obj = null;
117115
} else if (Integer.class.equals(type) || int.class.equals(type)) {
118-
obj = Integer.valueOf(0);
116+
obj = 0;
119117
} else if (Byte.class.equals(type) || byte.class.equals(type)) {
120-
obj = Integer.valueOf(0);
118+
obj = (byte) 0;
121119
} else if (java.util.Date.class.equals(type)) {
122120
obj = new Date();
123121
} else if (java.sql.Timestamp.class.equals(type)) {

0 commit comments

Comments
 (0)