1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.onami.factoryannotation; |
20 | |
|
21 | |
import java.lang.annotation.Annotation; |
22 | |
import java.lang.reflect.Field; |
23 | |
|
24 | |
import org.apache.onami.factoryannotation.AccessibleHelper.AccessibleHelperCallback; |
25 | |
|
26 | |
import com.google.inject.MembersInjector; |
27 | |
import com.google.inject.ProvisionException; |
28 | |
|
29 | 812 | class FactoryAnnotationSingleFieldMemberInjector<T, A extends Annotation> |
30 | |
implements MembersInjector<T> |
31 | |
{ |
32 | |
|
33 | |
private final FactoryAnnotationProvider<T, A> factoryAnnotationProvider; |
34 | |
|
35 | |
private final Field field; |
36 | |
|
37 | |
private final A annotation; |
38 | |
|
39 | |
private final FactoryAnnotationProvision<T, A> factoryAnnotationProvision; |
40 | |
|
41 | |
private final FactoryAnnotationProvisionListener<T, A> provisionListener; |
42 | |
|
43 | |
FactoryAnnotationSingleFieldMemberInjector( final FactoryAnnotationProvider<T, A> factoryAnnotationProvider, final Field field, |
44 | |
final FactoryAnnotationProvisionListener<T, A> provisionListener, final A annotation, |
45 | |
final FactoryAnnotationProvision<T, A> factoryAnnotationProvision ) |
46 | 176 | { |
47 | |
|
48 | 176 | this.factoryAnnotationProvider = factoryAnnotationProvider; |
49 | 176 | this.field = field; |
50 | 176 | this.annotation = annotation; |
51 | 176 | this.factoryAnnotationProvision = factoryAnnotationProvision; |
52 | 176 | this.provisionListener = provisionListener; |
53 | 176 | } |
54 | |
|
55 | |
public void injectMembers( final T instance ) |
56 | |
{ |
57 | 176 | AccessibleHelper.executePrivileged( field, new AccessibleHelperCallback<Field>() |
58 | 352 | { |
59 | |
|
60 | |
public void execute( final Field accessibleObject ) |
61 | |
{ |
62 | |
try |
63 | |
{ |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 176 | if ( accessibleObject.get( instance ) == null ) |
69 | |
{ |
70 | 156 | final FactoryAnnotationProvision<T, A> ip = FactoryAnnotationProvisionImpl.modify( factoryAnnotationProvision, instance ); |
71 | |
|
72 | 156 | final T value = factoryAnnotationProvider.buildValue( annotation ); |
73 | |
|
74 | |
|
75 | 148 | if ( provisionListener != null ) |
76 | |
{ |
77 | 12 | provisionListener.beforeInjection( ip, annotation ); |
78 | |
} |
79 | |
|
80 | 148 | accessibleObject.set( instance, value ); |
81 | |
|
82 | |
|
83 | 148 | if ( provisionListener != null ) |
84 | |
{ |
85 | 12 | provisionListener.afterInjection( ip, annotation, value ); |
86 | |
} |
87 | |
} |
88 | |
|
89 | |
} |
90 | 0 | catch ( final IllegalAccessException e ) |
91 | |
{ |
92 | 0 | throw new ProvisionException( "Provision failed", e ); |
93 | 168 | } |
94 | 168 | } |
95 | |
|
96 | |
} ); |
97 | 168 | } |
98 | |
|
99 | |
@SuppressWarnings( "unchecked" ) |
100 | |
static final <A extends Annotation, I, T> MembersInjector<I> buildMemberInjector( final FactoryAnnotationProvider<T, A> factoryAnnotationProvider, |
101 | |
final Field field, |
102 | |
final FactoryAnnotationProvisionListener<T, A> provisionListener, |
103 | |
final A annotation, |
104 | |
final FactoryAnnotationProvision<T, A> factoryAnnotationProvision ) |
105 | |
{ |
106 | |
|
107 | 176 | return (MembersInjector<I>) new FactoryAnnotationSingleFieldMemberInjector<T, A>( factoryAnnotationProvider, field, |
108 | |
provisionListener, annotation, |
109 | |
factoryAnnotationProvision ); |
110 | |
|
111 | |
} |
112 | |
|
113 | |
} |