Skip to content

Instantly share code, notes, and snippets.

@gissuebot
Created July 7, 2014 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gissuebot/be925b89200c7741e6b5 to your computer and use it in GitHub Desktop.
Save gissuebot/be925b89200c7741e6b5 to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 101, comment 0
package guiceissue;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target( { ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnon {
}
package guiceissue;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.matcher.Matchers;
@TypeAnnon
public class Test {
final public static void main(String args[]) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
final MethodInterceptor interceptor = new MethodInterceptor() {
public Object invoke(MethodInvocation method) throws Throwable {
return method.proceed();
}
};
Module moduleWithInterceptor = new AbstractModule() {
@Override
protected void configure() {
bindInterceptor(Matchers.annotatedWith(TypeAnnon.class),
Matchers.any(), interceptor);
}
};
Module moduleWithoutInterceptor = new AbstractModule() {
@Override
protected void configure() {
}
};
Injector injectorWithInterceptor = Guice
.createInjector(moduleWithInterceptor);
Injector injectorWithoutInterceptor = Guice
.createInjector(moduleWithoutInterceptor);
Test test = injectorWithoutInterceptor.getInstance(Test.class);
Method method = test.getClass().getMethod("setTest",
new Class[] { String.class });
System.out
.println("#:" + method.isAnnotationPresent(MethodAnnon.class));
test = injectorWithInterceptor.getInstance(Test.class);
method = test.getClass().getMethod("setTest",
new Class[] { String.class });
System.out
.println("#:" + method.isAnnotationPresent(MethodAnnon.class));
}
@MethodAnnon
public void setTest(String t) {
}
}
package guiceissue;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target( { ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@interface TypeAnnon {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment