bineanzhou / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

matcher for annotation on class (not method) #165

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
It would be useful to match an annotation on the class, this comes in handy
when intercepting the constructor (see issue #163) and finalize method (see
issue #164).

This would allow a single annotation on the class level, and with the help
of a method name matcher, allow one to intercept both the construct and
finalize method (without dedicated annotations per method/construct).

Something along the lines of..

binder.bindInterceptor(
classAnnotatedWith(XAnnotation.class), // Match class
defaultConstructor(), // Match method (in this case construct)
new MyConstructInterceptor() // The interceptor.
);

and

binder.bindInterceptor(
classAnnotatedWith(XAnnotation.class), // Match class
methodCalled("finalize"), // Match method
new MyFinalizeInterceptor() // The interceptor.
);

Or is this crazy talk?

Original issue reported on code.google.com by AaronJWh...@gmail.com on 14 Nov 2007 at 2:20

GoogleCodeExporter commented 9 years ago
public class ClassAnnotatedWithMatcher
extends AbstractMatcher<Class>
{
    public static Matcher<Class> classAnnotatedWith( Class< ? extends Annotation>
annotation )
    {
        return new ClassAnnotatedWithMatcher(annotation);
    }

    private Class< ? extends Annotation> annotation;

    public ClassAnnotatedWithMatcher(Class< ? extends Annotation> annotation)
    {
        Objects.nonNull(annotation, "annotation");
        this.annotation = annotation;
    }

    @Override
    public boolean matches(Class clazz)
    {
        return clazz.isAnnotationPresent(annotation);
    }

    @Override
    public String toString() {
        return "classAnnotatedWith(" + annotation.getSimpleName() + ")";
    }
}

Original comment by AaronJWh...@gmail.com on 19 Nov 2007 at 3:48

GoogleCodeExporter commented 9 years ago
Matchers.annotatedWith() supports classes.

Original comment by limpbizkit on 5 Jun 2008 at 7:54