From 0e715b1ee519551118cfa1632f19d557c90f0a2e Mon Sep 17 00:00:00 2001 From: "vishal.jangid" Date: Fri, 20 Dec 2024 03:58:07 +0530 Subject: [PATCH 1/3] Fix for Guice Wrapped Class --- pom.xml | 2 +- .../tef/bizlogics/DataAdapterBizlogic.java | 7 ++ .../bizlogics/DataAdapterBizlogicTest.java | 96 +++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 tef-impl/src/test/java/flipkart/tef/bizlogics/DataAdapterBizlogicTest.java diff --git a/pom.xml b/pom.xml index cf569e6..f869087 100644 --- a/pom.xml +++ b/pom.xml @@ -107,7 +107,7 @@ - 0.1.6 + 0.1.7 19.0 5.1.0 diff --git a/tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java b/tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java index 91e6833..3f4021f 100644 --- a/tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java +++ b/tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java @@ -16,6 +16,7 @@ package flipkart.tef.bizlogics; +import com.google.inject.internal.BytecodeGen; import flipkart.tef.annotations.EmitData; import flipkart.tef.annotations.InjectData; import flipkart.tef.exception.TefExecutionException; @@ -23,6 +24,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -80,6 +82,11 @@ private Map, Field> buildCacheOfMutableFields() { @SuppressWarnings("rawtypes") public static String getEmittedDataName(Class clazz) { + if (clazz.getName().contains(BytecodeGen.ENHANCER_BY_GUICE_MARKER)) { + // If clazz is a guice proxy clazz , then Its super class will always be of type Class + clazz = (Class) clazz.getSuperclass(); + } + EmitData emitData = clazz.getAnnotation(EmitData.class); if (emitData != null) { return emitData.name(); diff --git a/tef-impl/src/test/java/flipkart/tef/bizlogics/DataAdapterBizlogicTest.java b/tef-impl/src/test/java/flipkart/tef/bizlogics/DataAdapterBizlogicTest.java new file mode 100644 index 0000000..ce95bfb --- /dev/null +++ b/tef-impl/src/test/java/flipkart/tef/bizlogics/DataAdapterBizlogicTest.java @@ -0,0 +1,96 @@ +package flipkart.tef.bizlogics; + +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.internal.BytecodeGen; +import com.google.inject.matcher.Matchers; +import flipkart.tef.annotations.EmitData; +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class DataAdapterBizlogicTest { + + @EmitData(name = "testData") + static class TestDataAdapterBizlogic extends DataAdapterBizlogic { + @Override + public Object adapt(TefContext tefContext) { + return null; + } + } + + static class TestDataAdapterBizlogic1 extends DataAdapterBizlogic { + @Override + public Object adapt(TefContext tefContext) { + return null; + } + } + + @Test + public void testGetEmittedDataName() { + //setup + Class clazz = TestDataAdapterBizlogic.class; + + //test + String emittedDataName = DataAdapterBizlogic.getEmittedDataName(clazz); + + //validate + assertEquals("testData", emittedDataName); + } + + @Test + public void testGetEmittedDataNameForAnnotationAbsence() { + //setup + Class clazz = TestDataAdapterBizlogic1.class; + + //test + String emittedDataName = DataAdapterBizlogic.getEmittedDataName(clazz); + + //validate + assertEquals("", emittedDataName); + } + + @Test + public void testGetEmittedDataNameWithGuiceProxy() { + // setup + Injector injector = Guice.createInjector(new GuiceModule()); + TestDataAdapterBizlogic dataAdapterBizlogic = injector.getInstance(TestDataAdapterBizlogic.class); + + // test + String emittedDataName = DataAdapterBizlogic.getEmittedDataName(dataAdapterBizlogic.getClass()); + + // validate + assertTrue(dataAdapterBizlogic.getClass().getName().contains(BytecodeGen.ENHANCER_BY_GUICE_MARKER)); + assertEquals("testData", emittedDataName); + } + + class GuiceModule extends AbstractModule { + @Override + protected void configure() { + bindInterceptor( + Matchers.subclassesOf(TestDataAdapterBizlogic.class), + Matchers.any(), + new CustomInterceptor() + ); + } + } + + public class CustomInterceptor implements MethodInterceptor { + @Override + public Object invoke(MethodInvocation invocation) throws Throwable { + // Log method details before invocation + System.out.println("Before test method invocation"); + + // Proceed with the actual method invocation + Object result = invocation.proceed(); + + // Log method details after invocation + System.out.println("After test method invocation"); + return result; + } + } +} \ No newline at end of file From 34c1950e03a25bcd743e1024ed44b4f5d4cb996f Mon Sep 17 00:00:00 2001 From: vsgamb <57193494+vsgamb@users.noreply.github.com> Date: Fri, 20 Dec 2024 04:20:09 +0530 Subject: [PATCH 2/3] Removed Unused Import --- .../main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java | 1 - 1 file changed, 1 deletion(-) diff --git a/tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java b/tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java index 3f4021f..9e4d55c 100644 --- a/tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java +++ b/tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java @@ -24,7 +24,6 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.lang.reflect.Proxy; import java.util.HashMap; import java.util.Map; import java.util.Optional; From 3260c94dc420f8699f45c81d8bca7211c39787d6 Mon Sep 17 00:00:00 2001 From: vsgamb Date: Fri, 20 Dec 2024 13:24:28 +0530 Subject: [PATCH 3/3] Comments Addressing --- .../tef/bizlogics/DataAdapterBizlogic.java | 2 ++ .../bizlogics/DataAdapterBizlogicTest.java | 25 +++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java b/tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java index 3f4021f..afaf982 100644 --- a/tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java +++ b/tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java @@ -82,6 +82,8 @@ private Map, Field> buildCacheOfMutableFields() { @SuppressWarnings("rawtypes") public static String getEmittedDataName(Class clazz) { + // If method interceptor is applied via guice AOP , then guice creates an instance wrapped by EnhancerByGuice + // and then it hinders any annotation present on the superclass. So extracting superclass to find the annotations. if (clazz.getName().contains(BytecodeGen.ENHANCER_BY_GUICE_MARKER)) { // If clazz is a guice proxy clazz , then Its super class will always be of type Class clazz = (Class) clazz.getSuperclass(); diff --git a/tef-impl/src/test/java/flipkart/tef/bizlogics/DataAdapterBizlogicTest.java b/tef-impl/src/test/java/flipkart/tef/bizlogics/DataAdapterBizlogicTest.java index ce95bfb..eb436c9 100644 --- a/tef-impl/src/test/java/flipkart/tef/bizlogics/DataAdapterBizlogicTest.java +++ b/tef-impl/src/test/java/flipkart/tef/bizlogics/DataAdapterBizlogicTest.java @@ -1,3 +1,19 @@ +/* + *Copyright [2024] [The Original Author] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package flipkart.tef.bizlogics; import com.google.inject.AbstractModule; @@ -82,15 +98,8 @@ protected void configure() { public class CustomInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { - // Log method details before invocation - System.out.println("Before test method invocation"); - // Proceed with the actual method invocation - Object result = invocation.proceed(); - - // Log method details after invocation - System.out.println("After test method invocation"); - return result; + return invocation.proceed(); } } } \ No newline at end of file