1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.xbean.recipe;
19
20 import java.util.Collections;
21 import java.util.List;
22 import java.lang.reflect.Type;
23
24 import org.apache.xbean.recipe.AbstractRecipe;
25 import org.apache.xbean.recipe.ConstructionException;
26 import org.apache.xbean.recipe.ExecutionContext;
27 import org.apache.xbean.recipe.NoSuchObjectException;
28 import org.apache.xbean.recipe.Recipe;
29 import org.apache.xbean.recipe.RecipeHelper;
30
31
32
33
34
35 public class ReferenceNameRecipe extends AbstractRecipe {
36
37 private String referenceName;
38
39 public ReferenceNameRecipe(String referenceName) {
40 this.referenceName = referenceName;
41 }
42
43 public String getReferenceName() {
44 return referenceName;
45 }
46
47 private Object getReference() {
48 if (referenceName == null) {
49 throw new ConstructionException("Reference name has not been set");
50 }
51 ExecutionContext context = ExecutionContext.getContext();
52 if (!context.containsObject(referenceName)) {
53 throw new NoSuchObjectException(referenceName);
54 }
55 return context.getObject(referenceName);
56 }
57
58 public List<Recipe> getNestedRecipes() {
59 Object object = getReference();
60 if (object instanceof Recipe) {
61 Recipe recipe = (Recipe) object;
62 return Collections.singletonList(recipe);
63 } else {
64 return Collections.emptyList();
65 }
66 }
67
68 public List<Recipe> getConstructorRecipes() {
69 return getNestedRecipes();
70 }
71
72 public boolean canCreate(Type type) {
73 Object object = getReference();
74 return String.class == RecipeHelper.toClass(type);
75 }
76
77 protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException {
78 Object object = getReference();
79 return referenceName;
80 }
81
82 }