Skip to content

Commit 0f6461e

Browse files
authored
Merge pull request #18 from vsgamb/GuiceProxyClass
Fix for Guice Wrapped Class
2 parents f6777bd + 7dfa667 commit 0f6461e

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
</build>
108108

109109
<properties>
110-
<revision>0.1.6</revision>
110+
<revision>0.1.7</revision>
111111
<guava.version>19.0</guava.version>
112112
<guice.version>5.1.0</guice.version>
113113
</properties>

tef-impl/src/main/java/flipkart/tef/bizlogics/DataAdapterBizlogic.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package flipkart.tef.bizlogics;
1818

19+
import com.google.inject.internal.BytecodeGen;
1920
import flipkart.tef.annotations.EmitData;
2021
import flipkart.tef.annotations.InjectData;
2122
import flipkart.tef.exception.TefExecutionException;
@@ -80,6 +81,13 @@ private Map<DataAdapterKey<?>, Field> buildCacheOfMutableFields() {
8081

8182
@SuppressWarnings("rawtypes")
8283
public static String getEmittedDataName(Class<? extends DataAdapterBizlogic> clazz) {
84+
// If method interceptor is applied via guice AOP , then guice creates an instance wrapped by EnhancerByGuice
85+
// and then it hinders any annotation present on the superclass. So extracting superclass to find the annotations.
86+
if (clazz.getName().contains(BytecodeGen.ENHANCER_BY_GUICE_MARKER)) {
87+
// If clazz is a guice proxy clazz , then Its super class will always be of type Class<? extends DataAdapterBizlogic>
88+
clazz = (Class<? extends DataAdapterBizlogic>) clazz.getSuperclass();
89+
}
90+
8391
EmitData emitData = clazz.getAnnotation(EmitData.class);
8492
if (emitData != null) {
8593
return emitData.name();
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
*Copyright [2024] [The Original Author]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package flipkart.tef.bizlogics;
18+
19+
import com.google.inject.AbstractModule;
20+
import com.google.inject.Guice;
21+
import com.google.inject.Injector;
22+
import com.google.inject.internal.BytecodeGen;
23+
import com.google.inject.matcher.Matchers;
24+
import flipkart.tef.annotations.EmitData;
25+
import org.aopalliance.intercept.MethodInterceptor;
26+
import org.aopalliance.intercept.MethodInvocation;
27+
import org.junit.Test;
28+
29+
import static org.junit.Assert.assertEquals;
30+
import static org.junit.Assert.assertTrue;
31+
32+
public class DataAdapterBizlogicTest {
33+
34+
@EmitData(name = "testData")
35+
static class TestDataAdapterBizlogic extends DataAdapterBizlogic<Object> {
36+
@Override
37+
public Object adapt(TefContext tefContext) {
38+
return null;
39+
}
40+
}
41+
42+
static class TestDataAdapterBizlogic1 extends DataAdapterBizlogic<Object> {
43+
@Override
44+
public Object adapt(TefContext tefContext) {
45+
return null;
46+
}
47+
}
48+
49+
@Test
50+
public void testGetEmittedDataName() {
51+
//setup
52+
Class<? extends DataAdapterBizlogic> clazz = TestDataAdapterBizlogic.class;
53+
54+
//test
55+
String emittedDataName = DataAdapterBizlogic.getEmittedDataName(clazz);
56+
57+
//validate
58+
assertEquals("testData", emittedDataName);
59+
}
60+
61+
@Test
62+
public void testGetEmittedDataNameForAnnotationAbsence() {
63+
//setup
64+
Class<? extends DataAdapterBizlogic> clazz = TestDataAdapterBizlogic1.class;
65+
66+
//test
67+
String emittedDataName = DataAdapterBizlogic.getEmittedDataName(clazz);
68+
69+
//validate
70+
assertEquals("", emittedDataName);
71+
}
72+
73+
@Test
74+
public void testGetEmittedDataNameWithGuiceProxy() {
75+
// setup
76+
Injector injector = Guice.createInjector(new GuiceModule());
77+
TestDataAdapterBizlogic dataAdapterBizlogic = injector.getInstance(TestDataAdapterBizlogic.class);
78+
79+
// test
80+
String emittedDataName = DataAdapterBizlogic.getEmittedDataName(dataAdapterBizlogic.getClass());
81+
82+
// validate
83+
assertTrue(dataAdapterBizlogic.getClass().getName().contains(BytecodeGen.ENHANCER_BY_GUICE_MARKER));
84+
assertEquals("testData", emittedDataName);
85+
}
86+
87+
class GuiceModule extends AbstractModule {
88+
@Override
89+
protected void configure() {
90+
bindInterceptor(
91+
Matchers.subclassesOf(TestDataAdapterBizlogic.class),
92+
Matchers.any(),
93+
new CustomInterceptor()
94+
);
95+
}
96+
}
97+
98+
public class CustomInterceptor implements MethodInterceptor {
99+
@Override
100+
public Object invoke(MethodInvocation invocation) throws Throwable {
101+
// Proceed with the actual method invocation
102+
return invocation.proceed();
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)