Guice injection failing with guice annotation module









up vote
0
down vote

favorite












I'm using google guice injection most of my code and it has been working fine until I made a google guice annotation.



The code itself is a jax-rs web service where I pass 2 header parameters userId and adminId and then I retrieve data from an Oracle Database. The idea with the annotation is that I would time how long certain segments of code take. The userId is passed to the annotation and is logged out along with the class and the amount of time it takes for the code to complete.



The problem is that when I added this code it caused one of the guice modules to fail making every other guice module fail in the project.



Very Basic Code



Lowest Class called for database calls



public class lowestClass 
public static lowestClass create(final String userId)
final Injector injector = Guice.createInjector(new LowestClassModule(userId));
return injector.getInstance(lowectClass.class);


@LogTimerMeasurement
public DataInfo getDataInfo()
//populates a DataInfo object with info from database




Lowest Class Module



public class lowestClassModule 
public final String adminId;
public final String userId;

public lowestClassModule(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;


public lowestClassModule(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;


@Override
protected void configure()
install(new TimerBindingModule(userId));




UpperClass



public class upperClass 
@LogTimerMeasurement
public DataResponse getDataResponse()
//Calls lower class gets DataInfo object and returns DataResponse object


//Guice module for endpoint
public static class Module extends AbstractModule
private final String adminId;
private final String userId;

public Module(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;


public Module(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;


@Override
protected void configure()
install(new TimerBindingModule(userId));
bind(lowestClass.class).toInstance(lowestClass.create(userId));





Guice Annotation



Annotation Interface



@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogTimerMeasurement




Annotation Interceptor



public class LogTimerMeasurementInterceptor implements MethodInterceptor 
private final String userId;

public LogTimerMeasurementInterceptor(final String userId)
this.userId = userId;


@Override
public Object invoke(final MethodInvocation invocation) throws Throwable
final Method method = invocation.getMethod();
final String methodName = method.getName();
final Class<?> declaringClass = method.getDeclaringClass();

final I18N supplyI18n = I18NFactory.createI18N(this.getClass());

final long startTime = System.currentTimeMillis();
try
return invocation.proceed();
catch (final Exception e)
supplyI18n.log(getClass() + "timerError", "n0 : 1 :: Unable to log time due to 1",declaringClass + "." + methodName, userId, e);
return invocation.proceed();
finally
final long endTime = System.currentTimeMillis();
supplyI18n.log("timerMessage", "n0 : 1 :: Process completed in 2 milliseconds", declaringClass + "." + methodName, userIf, endTime - startTime);





Annotation Module



public class TimerBindingModule extends AbstractModule 
private final String userId;

public TimerBindingModule(final String userId)
this.userId = userId;


@Override
protected void configure()
bindInterceptor(Matchers.any(), Matchers.annotatedWith(LogTimerMeasurement.class), LogTimerMeasurementInterceptor.create(userId));











share|improve this question























  • What are the details of the exception you are getting?
    – Matthew Pope
    Nov 21 at 3:07










  • Also, you probably shouldn’t call invocation.proceed() in the catch block. Since you’re not trying to implement a retry interceptor, you should probably just rethrow the exception.
    – Matthew Pope
    Nov 24 at 5:19














up vote
0
down vote

favorite












I'm using google guice injection most of my code and it has been working fine until I made a google guice annotation.



The code itself is a jax-rs web service where I pass 2 header parameters userId and adminId and then I retrieve data from an Oracle Database. The idea with the annotation is that I would time how long certain segments of code take. The userId is passed to the annotation and is logged out along with the class and the amount of time it takes for the code to complete.



The problem is that when I added this code it caused one of the guice modules to fail making every other guice module fail in the project.



Very Basic Code



Lowest Class called for database calls



public class lowestClass 
public static lowestClass create(final String userId)
final Injector injector = Guice.createInjector(new LowestClassModule(userId));
return injector.getInstance(lowectClass.class);


@LogTimerMeasurement
public DataInfo getDataInfo()
//populates a DataInfo object with info from database




Lowest Class Module



public class lowestClassModule 
public final String adminId;
public final String userId;

public lowestClassModule(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;


public lowestClassModule(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;


@Override
protected void configure()
install(new TimerBindingModule(userId));




UpperClass



public class upperClass 
@LogTimerMeasurement
public DataResponse getDataResponse()
//Calls lower class gets DataInfo object and returns DataResponse object


//Guice module for endpoint
public static class Module extends AbstractModule
private final String adminId;
private final String userId;

public Module(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;


public Module(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;


@Override
protected void configure()
install(new TimerBindingModule(userId));
bind(lowestClass.class).toInstance(lowestClass.create(userId));





Guice Annotation



Annotation Interface



@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogTimerMeasurement




Annotation Interceptor



public class LogTimerMeasurementInterceptor implements MethodInterceptor 
private final String userId;

public LogTimerMeasurementInterceptor(final String userId)
this.userId = userId;


@Override
public Object invoke(final MethodInvocation invocation) throws Throwable
final Method method = invocation.getMethod();
final String methodName = method.getName();
final Class<?> declaringClass = method.getDeclaringClass();

final I18N supplyI18n = I18NFactory.createI18N(this.getClass());

final long startTime = System.currentTimeMillis();
try
return invocation.proceed();
catch (final Exception e)
supplyI18n.log(getClass() + "timerError", "n0 : 1 :: Unable to log time due to 1",declaringClass + "." + methodName, userId, e);
return invocation.proceed();
finally
final long endTime = System.currentTimeMillis();
supplyI18n.log("timerMessage", "n0 : 1 :: Process completed in 2 milliseconds", declaringClass + "." + methodName, userIf, endTime - startTime);





Annotation Module



public class TimerBindingModule extends AbstractModule 
private final String userId;

public TimerBindingModule(final String userId)
this.userId = userId;


@Override
protected void configure()
bindInterceptor(Matchers.any(), Matchers.annotatedWith(LogTimerMeasurement.class), LogTimerMeasurementInterceptor.create(userId));











share|improve this question























  • What are the details of the exception you are getting?
    – Matthew Pope
    Nov 21 at 3:07










  • Also, you probably shouldn’t call invocation.proceed() in the catch block. Since you’re not trying to implement a retry interceptor, you should probably just rethrow the exception.
    – Matthew Pope
    Nov 24 at 5:19












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm using google guice injection most of my code and it has been working fine until I made a google guice annotation.



The code itself is a jax-rs web service where I pass 2 header parameters userId and adminId and then I retrieve data from an Oracle Database. The idea with the annotation is that I would time how long certain segments of code take. The userId is passed to the annotation and is logged out along with the class and the amount of time it takes for the code to complete.



The problem is that when I added this code it caused one of the guice modules to fail making every other guice module fail in the project.



Very Basic Code



Lowest Class called for database calls



public class lowestClass 
public static lowestClass create(final String userId)
final Injector injector = Guice.createInjector(new LowestClassModule(userId));
return injector.getInstance(lowectClass.class);


@LogTimerMeasurement
public DataInfo getDataInfo()
//populates a DataInfo object with info from database




Lowest Class Module



public class lowestClassModule 
public final String adminId;
public final String userId;

public lowestClassModule(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;


public lowestClassModule(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;


@Override
protected void configure()
install(new TimerBindingModule(userId));




UpperClass



public class upperClass 
@LogTimerMeasurement
public DataResponse getDataResponse()
//Calls lower class gets DataInfo object and returns DataResponse object


//Guice module for endpoint
public static class Module extends AbstractModule
private final String adminId;
private final String userId;

public Module(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;


public Module(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;


@Override
protected void configure()
install(new TimerBindingModule(userId));
bind(lowestClass.class).toInstance(lowestClass.create(userId));





Guice Annotation



Annotation Interface



@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogTimerMeasurement




Annotation Interceptor



public class LogTimerMeasurementInterceptor implements MethodInterceptor 
private final String userId;

public LogTimerMeasurementInterceptor(final String userId)
this.userId = userId;


@Override
public Object invoke(final MethodInvocation invocation) throws Throwable
final Method method = invocation.getMethod();
final String methodName = method.getName();
final Class<?> declaringClass = method.getDeclaringClass();

final I18N supplyI18n = I18NFactory.createI18N(this.getClass());

final long startTime = System.currentTimeMillis();
try
return invocation.proceed();
catch (final Exception e)
supplyI18n.log(getClass() + "timerError", "n0 : 1 :: Unable to log time due to 1",declaringClass + "." + methodName, userId, e);
return invocation.proceed();
finally
final long endTime = System.currentTimeMillis();
supplyI18n.log("timerMessage", "n0 : 1 :: Process completed in 2 milliseconds", declaringClass + "." + methodName, userIf, endTime - startTime);





Annotation Module



public class TimerBindingModule extends AbstractModule 
private final String userId;

public TimerBindingModule(final String userId)
this.userId = userId;


@Override
protected void configure()
bindInterceptor(Matchers.any(), Matchers.annotatedWith(LogTimerMeasurement.class), LogTimerMeasurementInterceptor.create(userId));











share|improve this question















I'm using google guice injection most of my code and it has been working fine until I made a google guice annotation.



The code itself is a jax-rs web service where I pass 2 header parameters userId and adminId and then I retrieve data from an Oracle Database. The idea with the annotation is that I would time how long certain segments of code take. The userId is passed to the annotation and is logged out along with the class and the amount of time it takes for the code to complete.



The problem is that when I added this code it caused one of the guice modules to fail making every other guice module fail in the project.



Very Basic Code



Lowest Class called for database calls



public class lowestClass 
public static lowestClass create(final String userId)
final Injector injector = Guice.createInjector(new LowestClassModule(userId));
return injector.getInstance(lowectClass.class);


@LogTimerMeasurement
public DataInfo getDataInfo()
//populates a DataInfo object with info from database




Lowest Class Module



public class lowestClassModule 
public final String adminId;
public final String userId;

public lowestClassModule(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;


public lowestClassModule(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;


@Override
protected void configure()
install(new TimerBindingModule(userId));




UpperClass



public class upperClass 
@LogTimerMeasurement
public DataResponse getDataResponse()
//Calls lower class gets DataInfo object and returns DataResponse object


//Guice module for endpoint
public static class Module extends AbstractModule
private final String adminId;
private final String userId;

public Module(final String userId)
this.adminId = "NoAdminId";
this.userId = userId;


public Module(final String adminId, final String userId)
this.adminId = adminId;
this.userId = userId;


@Override
protected void configure()
install(new TimerBindingModule(userId));
bind(lowestClass.class).toInstance(lowestClass.create(userId));





Guice Annotation



Annotation Interface



@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogTimerMeasurement




Annotation Interceptor



public class LogTimerMeasurementInterceptor implements MethodInterceptor 
private final String userId;

public LogTimerMeasurementInterceptor(final String userId)
this.userId = userId;


@Override
public Object invoke(final MethodInvocation invocation) throws Throwable
final Method method = invocation.getMethod();
final String methodName = method.getName();
final Class<?> declaringClass = method.getDeclaringClass();

final I18N supplyI18n = I18NFactory.createI18N(this.getClass());

final long startTime = System.currentTimeMillis();
try
return invocation.proceed();
catch (final Exception e)
supplyI18n.log(getClass() + "timerError", "n0 : 1 :: Unable to log time due to 1",declaringClass + "." + methodName, userId, e);
return invocation.proceed();
finally
final long endTime = System.currentTimeMillis();
supplyI18n.log("timerMessage", "n0 : 1 :: Process completed in 2 milliseconds", declaringClass + "." + methodName, userIf, endTime - startTime);





Annotation Module



public class TimerBindingModule extends AbstractModule 
private final String userId;

public TimerBindingModule(final String userId)
this.userId = userId;


@Override
protected void configure()
bindInterceptor(Matchers.any(), Matchers.annotatedWith(LogTimerMeasurement.class), LogTimerMeasurementInterceptor.create(userId));








java timer annotations guice






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 16:42

























asked Nov 10 at 17:34









TOTOROCATBUS

387




387











  • What are the details of the exception you are getting?
    – Matthew Pope
    Nov 21 at 3:07










  • Also, you probably shouldn’t call invocation.proceed() in the catch block. Since you’re not trying to implement a retry interceptor, you should probably just rethrow the exception.
    – Matthew Pope
    Nov 24 at 5:19
















  • What are the details of the exception you are getting?
    – Matthew Pope
    Nov 21 at 3:07










  • Also, you probably shouldn’t call invocation.proceed() in the catch block. Since you’re not trying to implement a retry interceptor, you should probably just rethrow the exception.
    – Matthew Pope
    Nov 24 at 5:19















What are the details of the exception you are getting?
– Matthew Pope
Nov 21 at 3:07




What are the details of the exception you are getting?
– Matthew Pope
Nov 21 at 3:07












Also, you probably shouldn’t call invocation.proceed() in the catch block. Since you’re not trying to implement a retry interceptor, you should probably just rethrow the exception.
– Matthew Pope
Nov 24 at 5:19




Also, you probably shouldn’t call invocation.proceed() in the catch block. Since you’re not trying to implement a retry interceptor, you should probably just rethrow the exception.
– Matthew Pope
Nov 24 at 5:19

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241629%2fguice-injection-failing-with-guice-annotation-module%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241629%2fguice-injection-failing-with-guice-annotation-module%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Museum of Modern and Contemporary Art of Trento and Rovereto

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully