Hello,

Your problem then isn't with Guice but probably with your logger. As proof,
here's a working example. Feel free to directly adapt it.

com/example/logged/Echoer.java

package com.example.logged;

public class Echoer {
  public String echo(String s) { return String.format("echo %s", s);  }
}

com/example/notlogged/Printer.java

package com.example.notlogged;

public class Printer {
  public String print(String s) { return String.format("print %s", s); }
}

com/example/Main.java

package com.example;

import com.example.logged.Echoer;
import com.example.notlogged.Printer;
import com.google.inject.*;
import com.google.inject.matcher.Matchers;
import org.aopalliance.intercept.*;
import java.util.Arrays;

public class Main {
  @Inject Echoer echoer;
  @Inject Printer printer;
  public static void main(String[] args) {
    Main app = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
        bindInterceptor(Matchers.inSubpackage("com.example.logged"),
Matchers.any(), new MethodInterceptor() {
          @Override
          public Object invoke(MethodInvocation mi) throws Throwable {
            System.out.printf("Calling method %s with arguments %s%n",
mi.getMethod().getName(), Arrays.toString(mi.getArguments()));
            try {
              Object returnValue = mi.proceed();
              System.out.printf("Method %s returned %s%n",
mi.getMethod().getName(), returnValue);
              return returnValue;
            } catch (Throwable t) {
              System.out.printf("Method %s threw an exception %s%n",
mi.getMethod().getName(), t);
              throw t;
            }
          }
        });
      }
    }).getInstance(Main.class);
    System.out.println(app.echoer.echo("foo"));
    System.out.println(app.printer.print("bar"));
  }
}

The result is :

Calling method echo with arguments [foo]
Method echo returned echo foo
echo foo
print bar

It is expected that this is the correct result, since we only wrap around
the package "com.example.logged" which contains the class Echoer, not the
class Printer.


Le lun. 25 juil. 2016 à 13:44, Ankur Mahajan <[email protected]>
a écrit :

> *Hi Olivier,*
>
> Thanks for the reply, I've tried what you suggested but No LUCK :(
> Could you please suggest something else which would work like charm :).
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/google-guice.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/google-guice/912f0de3-45a9-45f5-b04d-d5f603d9e21c%40googlegroups.com
> <https://groups.google.com/d/msgid/google-guice/912f0de3-45a9-45f5-b04d-d5f603d9e21c%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/CAORw%3DcMwYP%3DLkVgqAiwW4kYLbpVtv1gwt4rZY3FW7Qr9J3j25Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to