KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > reflect > factory > FieldFactory


1 package spoon.reflect.factory;
2
3 import java.lang.reflect.Field JavaDoc;
4 import java.util.Set JavaDoc;
5
6 import spoon.reflect.Factory;
7 import spoon.reflect.code.CtExpression;
8 import spoon.reflect.declaration.CtField;
9 import spoon.reflect.declaration.CtSimpleType;
10 import spoon.reflect.declaration.CtType;
11 import spoon.reflect.declaration.ModifierKind;
12 import spoon.reflect.reference.CtFieldReference;
13 import spoon.reflect.reference.CtTypeReference;
14
15 /**
16  * The {@link CtField} sub-factory.
17  */

18 public class FieldFactory extends SubFactory {
19
20     private static final long serialVersionUID = 1L;
21
22     /**
23      * Creates a new field sub-factory.
24      *
25      * @param factory
26      * the parent factory
27      */

28     public FieldFactory(Factory factory) {
29         super(factory);
30     }
31
32     /**
33      * Creates a field.
34      *
35      * @param target
36      * the target type to which the field is added
37      * @param modifiers
38      * the modifiers
39      * @param type
40      * the field's type
41      * @param name
42      * the field's name
43      */

44     public <T> CtField<T> create(CtSimpleType<?> target,
45             Set JavaDoc<ModifierKind> modifiers, CtTypeReference<T> type, String JavaDoc name) {
46         CtField<T> field = factory.Core().createField();
47         field.setModifiers(modifiers);
48         field.setType(type);
49         field.setSimpleName(name);
50         field.setParent(target);
51         if (target != null)
52             target.getFields().add(field);
53         return field;
54     }
55
56     /**
57      * Creates a field.
58      *
59      * @param target
60      * the target type to which the field is added
61      * @param modifiers
62      * the modifiers
63      * @param type
64      * the field's type
65      * @param name
66      * the field's name
67      * @param defaultExpression
68      * the initializing expression
69      */

70     public <T> CtField<T> create(CtSimpleType<?> target,
71             Set JavaDoc<ModifierKind> modifiers, CtTypeReference<T> type, String JavaDoc name,
72             CtExpression<T> defaultExpression) {
73         CtField<T> field = create(target, modifiers, type, name);
74         field.setDefaultExpression(defaultExpression);
75         return field;
76     }
77
78     /**
79      * Creates a field by copying an existing field.
80      *
81      * @param <T>
82      * the type of the field
83      * @param target
84      * the target type where the new field has to be inserted to
85      * @param source
86      * the source field to be copied
87      * @return the newly created field
88      */

89     public <T> CtField<T> create(CtType<?> target, CtField<T> source) {
90         CtField<T> newField = factory.Core().clone(source);
91         if (target != null)
92             target.getFields().add(newField);
93         newField.setParent(target);
94         return newField;
95     }
96
97     /**
98      * Creates a field reference from an existing field.
99      */

100     public <T> CtFieldReference<T> createReference(CtField<T> field) {
101         return createReference(factory.Type().createReference(
102                 field.getDeclaringType()), field.getType(), field
103                 .getSimpleName());
104     }
105
106     /**
107      * Creates a field reference.
108      */

109     public <T> CtFieldReference<T> createReference(
110             CtTypeReference<?> declaringType, CtTypeReference<T> type,
111             String JavaDoc fieldName) {
112         CtFieldReference<T> fieldRef = factory.Core().createFieldReference();
113         fieldRef.setSimpleName(fieldName);
114         fieldRef.setDeclaringType(declaringType);
115         fieldRef.setType(type);
116         return fieldRef;
117     }
118
119     /**
120      * Creates a field reference from a <code>java.lang.reflect</code> field.
121      */

122     @SuppressWarnings JavaDoc("unchecked")
123     public <T> CtFieldReference<T> createReference(Field JavaDoc field) {
124         CtFieldReference fieldRef = factory.Core().createFieldReference();
125         fieldRef.setSimpleName(field.getName());
126         fieldRef.setDeclaringType(factory.Type().createReference(
127                 field.getDeclaringClass()));
128         fieldRef.setType(factory.Type().createReference(field.getType()));
129         return fieldRef;
130     }
131
132     /**
133      * Creates a field reference from its signature, as defined by the field
134      * reference's toString.
135      */

136     public <T> CtFieldReference<T> createReference(String JavaDoc signature) {
137         CtFieldReference<T> fieldRef = factory.Core().createFieldReference();
138         String JavaDoc type = signature.substring(0, signature.indexOf(" "));
139         String JavaDoc declaringType = signature.substring(signature.indexOf(" ") + 1,
140                 signature.indexOf(CtField.FIELD_SEPARATOR));
141         String JavaDoc fieldName = signature.substring(signature
142                 .indexOf(CtField.FIELD_SEPARATOR) + 1);
143         fieldRef.setSimpleName(fieldName);
144         fieldRef
145                 .setDeclaringType(factory.Type().createReference(declaringType));
146         CtTypeReference<T> typeRef = factory.Type().createReference(type);
147         fieldRef.setType(typeRef);
148         return fieldRef;
149     }
150
151 }
152
Popular Tags