001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.reflect.factory;
019:
020: import java.lang.reflect.Field;
021: import java.util.Set;
022:
023: import spoon.reflect.Factory;
024: import spoon.reflect.code.CtExpression;
025: import spoon.reflect.declaration.CtField;
026: import spoon.reflect.declaration.CtSimpleType;
027: import spoon.reflect.declaration.CtType;
028: import spoon.reflect.declaration.ModifierKind;
029: import spoon.reflect.reference.CtFieldReference;
030: import spoon.reflect.reference.CtTypeReference;
031:
032: /**
033: * The {@link CtField} sub-factory.
034: */
035: public class FieldFactory extends SubFactory {
036:
037: private static final long serialVersionUID = 1L;
038:
039: /**
040: * Creates a new field sub-factory.
041: *
042: * @param factory
043: * the parent factory
044: */
045: public FieldFactory(Factory factory) {
046: super (factory);
047: }
048:
049: /**
050: * Creates a field.
051: *
052: * @param target
053: * the target type to which the field is added
054: * @param modifiers
055: * the modifiers
056: * @param type
057: * the field's type
058: * @param name
059: * the field's name
060: */
061: public <T> CtField<T> create(CtSimpleType<?> target,
062: Set<ModifierKind> modifiers, CtTypeReference<T> type,
063: String name) {
064: CtField<T> field = factory.Core().createField();
065: field.setModifiers(modifiers);
066: field.setType(type);
067: field.setSimpleName(name);
068: field.setParent(target);
069: if (target != null)
070: target.getFields().add(field);
071: return field;
072: }
073:
074: /**
075: * Creates a field.
076: *
077: * @param target
078: * the target type to which the field is added
079: * @param modifiers
080: * the modifiers
081: * @param type
082: * the field's type
083: * @param name
084: * the field's name
085: * @param defaultExpression
086: * the initializing expression
087: */
088: public <T> CtField<T> create(CtSimpleType<?> target,
089: Set<ModifierKind> modifiers, CtTypeReference<T> type,
090: String name, CtExpression<T> defaultExpression) {
091: CtField<T> field = create(target, modifiers, type, name);
092: field.setDefaultExpression(defaultExpression);
093: return field;
094: }
095:
096: /**
097: * Creates a field by copying an existing field.
098: *
099: * @param <T>
100: * the type of the field
101: * @param target
102: * the target type where the new field has to be inserted to
103: * @param source
104: * the source field to be copied
105: * @return the newly created field
106: */
107: public <T> CtField<T> create(CtType<?> target, CtField<T> source) {
108: CtField<T> newField = factory.Core().clone(source);
109: if (target != null)
110: target.getFields().add(newField);
111: newField.setParent(target);
112: return newField;
113: }
114:
115: /**
116: * Creates a field reference from an existing field.
117: */
118: public <T> CtFieldReference<T> createReference(CtField<T> field) {
119: return createReference(factory.Type().createReference(
120: field.getDeclaringType()), field.getType(), field
121: .getSimpleName());
122: }
123:
124: /**
125: * Creates a field reference.
126: */
127: public <T> CtFieldReference<T> createReference(
128: CtTypeReference<?> declaringType, CtTypeReference<T> type,
129: String fieldName) {
130: CtFieldReference<T> fieldRef = factory.Core()
131: .createFieldReference();
132: fieldRef.setSimpleName(fieldName);
133: fieldRef.setDeclaringType(declaringType);
134: fieldRef.setType(type);
135: return fieldRef;
136: }
137:
138: /**
139: * Creates a field reference from a <code>java.lang.reflect</code> field.
140: */
141: @SuppressWarnings("unchecked")
142: public <T> CtFieldReference<T> createReference(Field field) {
143: CtFieldReference fieldRef = factory.Core()
144: .createFieldReference();
145: fieldRef.setSimpleName(field.getName());
146: fieldRef.setDeclaringType(factory.Type().createReference(
147: field.getDeclaringClass()));
148: fieldRef.setType(factory.Type()
149: .createReference(field.getType()));
150: return fieldRef;
151: }
152:
153: /**
154: * Creates a field reference from its signature, as defined by the field
155: * reference's toString.
156: */
157: public <T> CtFieldReference<T> createReference(String signature) {
158: CtFieldReference<T> fieldRef = factory.Core()
159: .createFieldReference();
160: String type = signature.substring(0, signature.indexOf(" "));
161: String declaringType = signature.substring(signature
162: .indexOf(" ") + 1, signature
163: .indexOf(CtField.FIELD_SEPARATOR));
164: String fieldName = signature.substring(signature
165: .indexOf(CtField.FIELD_SEPARATOR) + 1);
166: fieldRef.setSimpleName(fieldName);
167: fieldRef.setDeclaringType(factory.Type().createReference(
168: declaringType));
169: CtTypeReference<T> typeRef = factory.Type().createReference(
170: type);
171: fieldRef.setType(typeRef);
172: return fieldRef;
173: }
174:
175: }
|