Skip to content

Commit

Permalink
Merge pull request #18 from vsgamb/GuiceProxyClass
Browse files Browse the repository at this point in the history
Fix for Guice Wrapped Class
  • Loading branch information
bageshwar authored Dec 20, 2024
2 parents f6777bd + 7dfa667 commit 0f6461e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
</build>

<properties>
<revision>0.1.6</revision>
<revision>0.1.7</revision>
<guava.version>19.0</guava.version>
<guice.version>5.1.0</guice.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,6 +81,13 @@ private Map<DataAdapterKey<?>, Field> buildCacheOfMutableFields() {

@SuppressWarnings("rawtypes")
public static String getEmittedDataName(Class<? extends DataAdapterBizlogic> 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<? extends DataAdapterBizlogic>
clazz = (Class<? extends DataAdapterBizlogic>) clazz.getSuperclass();
}

EmitData emitData = clazz.getAnnotation(EmitData.class);
if (emitData != null) {
return emitData.name();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> {
@Override
public Object adapt(TefContext tefContext) {
return null;
}
}

static class TestDataAdapterBizlogic1 extends DataAdapterBizlogic<Object> {
@Override
public Object adapt(TefContext tefContext) {
return null;
}
}

@Test
public void testGetEmittedDataName() {
//setup
Class<? extends DataAdapterBizlogic> clazz = TestDataAdapterBizlogic.class;

//test
String emittedDataName = DataAdapterBizlogic.getEmittedDataName(clazz);

//validate
assertEquals("testData", emittedDataName);
}

@Test
public void testGetEmittedDataNameForAnnotationAbsence() {
//setup
Class<? extends DataAdapterBizlogic> 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();
}
}
}

0 comments on commit 0f6461e

Please sign in to comment.