1 package org.apache.onami.lifecycle.core;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.google.inject.TypeLiteral;
23 import com.google.inject.spi.TypeEncounter;
24 import com.google.inject.spi.TypeListener;
25
26 import java.lang.annotation.Annotation;
27 import java.lang.reflect.Method;
28 import java.util.List;
29
30
31
32
33 abstract class AbstractMethodTypeListener
34 implements TypeListener
35 {
36
37
38
39
40 private static final String JAVA_PACKAGE = "java";
41
42
43
44
45 protected final List<? extends Class<? extends Annotation>> annotationTypes;
46
47
48
49
50
51
52 public AbstractMethodTypeListener( List<? extends Class<? extends Annotation>> annotationTypes )
53 {
54 this.annotationTypes = annotationTypes;
55 }
56
57
58
59
60 @Override
61 public final <I> void hear( TypeLiteral<I> type, TypeEncounter<I> encounter )
62 {
63 hear( type, type.getRawType(), encounter );
64 }
65
66
67
68
69
70
71
72
73 private <I> void hear( final TypeLiteral<I> parentType, Class<? super I> klass, TypeEncounter<I> encounter )
74 {
75 Package pkg;
76 if ( klass == null || ( ( pkg = klass.getPackage() ) != null && pkg.getName().startsWith( JAVA_PACKAGE ) ) )
77 {
78 return;
79 }
80
81 for ( Class<? extends Annotation> annotationType : annotationTypes )
82 {
83 for ( Method method : klass.getDeclaredMethods() )
84 {
85 if ( method.isAnnotationPresent( annotationType ) )
86 {
87 if ( method.getParameterTypes().length != 0 )
88 {
89 encounter.addError( "Annotated methods with @%s must not accept any argument, found %s",
90 annotationType.getName(), method );
91 }
92
93 hear( method, parentType, encounter, annotationType );
94 }
95 }
96 }
97
98 hear( parentType, klass.getSuperclass(), encounter );
99 }
100
101
102
103
104
105
106
107
108
109 protected abstract <I> void hear( Method method, TypeLiteral<I> parentType, TypeEncounter<I> encounter,
110 Class<? extends Annotation> annotationType );
111
112 }