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..133c1a7 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; @@ -80,6 +81,13 @@ 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(); + } + 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..eb436c9 --- /dev/null +++ b/tef-impl/src/test/java/flipkart/tef/bizlogics/DataAdapterBizlogicTest.java @@ -0,0 +1,105 @@ +/* + *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; +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 { + // Proceed with the actual method invocation + return invocation.proceed(); + } + } +} \ No newline at end of file