| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package org.apache.tapestry5.internal.transform; |
| 16 | |
|
| 17 | |
import org.apache.tapestry5.internal.services.ComponentClassCache; |
| 18 | |
import org.apache.tapestry5.ioc.ObjectLocator; |
| 19 | |
import org.apache.tapestry5.ioc.annotations.InjectService; |
| 20 | |
import org.apache.tapestry5.model.MutableComponentModel; |
| 21 | |
import org.apache.tapestry5.services.ClassTransformation; |
| 22 | |
import org.apache.tapestry5.services.ComponentClassTransformWorker; |
| 23 | |
|
| 24 | |
import java.util.List; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public class InjectServiceWorker implements ComponentClassTransformWorker |
| 32 | |
{ |
| 33 | |
private final ObjectLocator locator; |
| 34 | |
|
| 35 | |
private final ComponentClassCache cache; |
| 36 | |
|
| 37 | |
public InjectServiceWorker(ObjectLocator locator, ComponentClassCache cache) |
| 38 | 58 | { |
| 39 | 58 | this.locator = locator; |
| 40 | 58 | this.cache = cache; |
| 41 | 58 | } |
| 42 | |
|
| 43 | |
public void transform(ClassTransformation transformation, MutableComponentModel model) |
| 44 | |
{ |
| 45 | 814 | List<String> names = transformation.findFieldsWithAnnotation(InjectService.class); |
| 46 | |
|
| 47 | 814 | if (names.isEmpty()) return; |
| 48 | |
|
| 49 | 2 | for (String name : names) |
| 50 | |
{ |
| 51 | 2 | InjectService annotation = transformation.getFieldAnnotation(name, InjectService.class); |
| 52 | |
|
| 53 | 2 | String typeName = transformation.getFieldType(name); |
| 54 | |
|
| 55 | 2 | Class fieldType = cache.forName(typeName); |
| 56 | |
|
| 57 | 2 | Object service = locator.getService(annotation.value(), fieldType); |
| 58 | |
|
| 59 | 2 | transformation.injectField(name, service); |
| 60 | |
|
| 61 | 2 | transformation.claimField(name, annotation); |
| 62 | 2 | } |
| 63 | 2 | } |
| 64 | |
} |