guice-aop named parameters

315 views
Skip to first unread message

Carlos Alexandro Becker

unread,
May 7, 2012, 12:09:44 PM5/7/12
to google...@googlegroups.com
I'm taking a look at guice aop integration... and wonder something:

If I have a named parameter like this:

@RequiresPermission(permission = Permission.WRITE)
public void needsWrite(@Named("test") User u, @Named("test2") String t) {
System.out.println("I write something :) " + t);
}

Can I get these parameters by Named annotations in my method interceptor?

Thanks

Fred Faber

unread,
May 7, 2012, 1:11:54 PM5/7/12
to google...@googlegroups.com
Yes, something like this:

  void findAnnotatedArguments(MethodInvocation methodInvocation) {    Method method = methodInvocation.getMethod();
    checkArgument(
        method.isAnnotationPresent(RequiresPermission.class),
        "Unexpected method passed to the Interceptor: %s",
        method);

    Object[] methodArguments = methodInvocation.getArguments();
    Type[] genericParameterTypes = method.getGenericParameterTypes();
    Annotation[][] annotationsOfParameters = method.getParameterAnnotations();
    for (int i = 0; i < annotationsOfParameters.length; i++) {
      Annotation[] annotationsOfParameter = annotationsOfParameters[i];
for (Annotation annotation : annotationsOfParameter) {
if (Named.class.isInstance(annotation)) {
... do whatever you want
}
}
} }

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/FjI8kHjOB9oJ.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Carlos Alexandro Becker

unread,
May 8, 2012, 12:59:40 PM5/8/12
to google...@googlegroups.com, ffa...@faiser.com
hmm.. but this way, how I get the value of the field?

thanks.
Yes, something like this:

To unsubscribe from this group, send email to google-guice+unsubscribe@googlegroups.com.

Fred Faber

unread,
May 8, 2012, 1:06:44 PM5/8/12
to Carlos Alexandro Becker, google...@googlegroups.com

Fred Faber

unread,
May 8, 2012, 1:09:38 PM5/8/12
to Carlos Alexandro Becker, google...@googlegroups.com
Not the helpful link I wanted.

In any event, you can call value() on an instance of Named to get its value.

Carlos Alexandro Becker

unread,
May 8, 2012, 1:12:33 PM5/8/12
to google...@googlegroups.com, Carlos Alexandro Becker, ffa...@faiser.com
hmm

I still don't understanding how can I get the value of the parameter..

Fred Faber

unread,
May 8, 2012, 1:20:34 PM5/8/12
to Carlos Alexandro Becker, google...@googlegroups.com
do you mean value of the argument?

methodArguments[i]?

Carlos Alexandro Becker

unread,
May 8, 2012, 1:35:20 PM5/8/12
to google...@googlegroups.com, Carlos Alexandro Becker, ffa...@faiser.com
Ooww yeah 
My mistake.

It worked (methodArguments[i]), thanks man :P
Reply all
Reply to author
Forward
0 new messages