1 package spoon.reflect.factory; 2 3 import java.lang.reflect.Field ; 4 import java.util.Set ; 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 18 public class FieldFactory extends SubFactory { 19 20 private static final long serialVersionUID = 1L; 21 22 28 public FieldFactory(Factory factory) { 29 super(factory); 30 } 31 32 44 public <T> CtField<T> create(CtSimpleType<?> target, 45 Set <ModifierKind> modifiers, CtTypeReference<T> type, String 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 70 public <T> CtField<T> create(CtSimpleType<?> target, 71 Set <ModifierKind> modifiers, CtTypeReference<T> type, String name, 72 CtExpression<T> defaultExpression) { 73 CtField<T> field = create(target, modifiers, type, name); 74 field.setDefaultExpression(defaultExpression); 75 return field; 76 } 77 78 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 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 109 public <T> CtFieldReference<T> createReference( 110 CtTypeReference<?> declaringType, CtTypeReference<T> type, 111 String 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 122 @SuppressWarnings ("unchecked") 123 public <T> CtFieldReference<T> createReference(Field 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 136 public <T> CtFieldReference<T> createReference(String signature) { 137 CtFieldReference<T> fieldRef = factory.Core().createFieldReference(); 138 String type = signature.substring(0, signature.indexOf(" ")); 139 String declaringType = signature.substring(signature.indexOf(" ") + 1, 140 signature.indexOf(CtField.FIELD_SEPARATOR)); 141 String 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 |