Created
July 7, 2014 17:58
-
-
Save gissuebot/be925b89200c7741e6b5 to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 101, comment 0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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