The test case:
@Retention(RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface *MyAnno *{}
public interface *IToto *{}
public class *TotoImpl *implements IToto {}
@MyAnno
public interface *ITotoService *{
/**
* A great method which will do something
*
* @return
*/
IToto doSomething();
}
public class *TotoServiceImpl *implements ITotoService {
public TotoImpl doSomething() {
return null;
}
}
package tests.guice;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
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.matcher.AbstractMatcher;
import com.google.inject.matcher.Matchers;
public class Main {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new TotoModule());
ITotoService totoService = injector.getInstance(ITotoService.class);
// Method call
totoService.doSomething();
}
}
class TotoModule extends AbstractModule {
@Override
protected void configure() {
bind(ITotoService.class).to(TotoServiceImpl.class);
bindInterceptor(Matchers.any(), new
TypeAnnotatedMatcher(MyAnno.class), new TotoInterceptor());
}
}
class TotoInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation mi) throws Throwable {
System.out.println(mi.getMethod().getName() + " -> isSynthetic ? "
+ mi.getMethod().isSynthetic());
return mi.proceed();
}
}
class TypeAnnotatedMatcher extends AbstractMatcher<Method> {
private final Class<? extends Annotation> annClass;
/**
* Constructor.
*
* @param annClass
*/
public TypeAnnotatedMatcher(final Class<? extends Annotation> annClass)
{
this.annClass = annClass;
}
/**
* Search for the method signature into all the interface annotated with
a
* given annotation.
*/
public boolean matches(Method method) {
// Hook to avoid twice interceptions of the same method
if (method.isSynthetic()) {
//return false;
}
boolean matches = false;
Class<?> type = method.getDeclaringClass();
Class<?>[] iTypes = ReflectUtils.getInterfaces(type, annClass);
for (Class<?> iType : iTypes) {
Method iMethod = null;
try {
iMethod = iType.getDeclaredMethod(method.getName(), method
.getParameterTypes());
} catch (SecurityException e) {
// Should never happen
} catch (NoSuchMethodException e) {
// Should never happen
}
if (iMethod != null) {
matches = true;
break;
}
}
return matches;
}
}
class ReflectUtils {
/**
* Get type annoted with the given annotation
*
* @param type
* Type where we have to search for annoted interfaces
*
* @param annClass
* Annotation to find
*
* @return Class<?>[]
*/
public final static Class<?>[] getInterfaces(final Class<?> type,
final Class<? extends Annotation> annClass) {
List<Class<?>> annTypes = new ArrayList<Class<?>>();
Class<?>[] iTypes = type.getInterfaces();
for (Class<?> iType : iTypes) {
if (iType.isAnnotationPresent(annClass)) {
annTypes.add(iType);
}
}
return annTypes.toArray(new Class<?>[annTypes.size()]);
}
}
2008/9/15 Anthony MULLER <[EMAIL PROTECTED]>
> Yep, TotoServiceImpl implements ITotoService and Toto implements IToto.
>
> I will send you a minimal test case tomorrow...
>
> Cheers,
> Anthony
>
> 2008/9/15 [EMAIL PROTECTED] <[EMAIL PROTECTED]>
>
>
>> On Sep 15, 6:19 am, "Anthony MULLER" <[EMAIL PROTECTED]> wrote:
>> > interface ITotoService {
>> > IToto doSomething();
>> > }
>> >
>> > class TotoServiceImpl {
>> > Toto doSomething() {
>> > return ..... ;
>> > }
>> > }
>> Did you intend
>> class TotoServiceImpl implements ITotoService?
>>
>> > Observe the return type of both methods.
>> Is it safe to assume that Toto implements IToto?
>>
>> > I think Guice should not ask for invocation when a method is "synthetic"
>> > ("volatile", "bridge", ... any methods generated by compiler)...
>>
>> I don't think there's any synthetic methods here. Could you
>> send an executable test case? I think I know what you're
>> getting at here but it's a lot easier to fix bugs with a test case.
>>
>> Cheers,
>> Jesse
>>
>> >>
>>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---