1919import java .util .Calendar ;
2020import java .util .Date ;
2121
22+ import com .intuit .ipp .data .Customer ;
2223import net .bytebuddy .ByteBuddy ;
2324import net .bytebuddy .dynamic .loading .ClassLoadingStrategy ;
2425import net .bytebuddy .implementation .MethodDelegation ;
25- import net .bytebuddy .implementation .SuperMethodCall ;
2626import 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
3128import com .intuit .ipp .core .IEntity ;
3229import com .intuit .ipp .query .expr .BooleanPath ;
3532import com .intuit .ipp .query .expr .NumberPath ;
3633import com .intuit .ipp .query .expr .StringPath ;
3734import 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}
0 commit comments