1 package spoon.reflect.factory; 2 3 import java.lang.reflect.Constructor ; 4 import java.util.Arrays ; 5 import java.util.List ; 6 import java.util.Set ; 7 8 import spoon.reflect.Factory; 9 import spoon.reflect.code.CtBlock; 10 import spoon.reflect.declaration.CtClass; 11 import spoon.reflect.declaration.CtConstructor; 12 import spoon.reflect.declaration.CtMethod; 13 import spoon.reflect.declaration.CtParameter; 14 import spoon.reflect.declaration.ModifierKind; 15 import spoon.reflect.reference.CtExecutableReference; 16 import spoon.reflect.reference.CtTypeReference; 17 18 21 public class ConstructorFactory extends ExecutableFactory { 22 23 private static final long serialVersionUID = 1L; 24 25 29 public ConstructorFactory(Factory factory) { 30 super(factory); 31 } 32 33 42 public CtConstructor create(CtClass<?> target, CtConstructor source) { 43 CtConstructor newConstructor = factory.Core().clone(source); 44 target.getConstructors().add(newConstructor); 45 newConstructor.setParent(target); 46 return newConstructor; 47 } 48 49 59 public <T> CtConstructor<T> create(CtClass<?> target, CtMethod<T> source) { 60 CtMethod<T> method = factory.Core().clone(source); 61 CtConstructor<T> newConstructor = factory.Core().createConstructor(); 62 newConstructor.setAnnotations(method.getAnnotations()); 63 newConstructor.setBody(method.getBody()); 64 newConstructor.setDocComment(method.getDocComment()); 65 newConstructor 66 .setFormalTypeParameters(method.getFormalTypeParameters()); 67 newConstructor.setModifiers(method.getModifiers()); 68 newConstructor.setParameters(method.getParameters()); 69 setParent(newConstructor, method.getAnnotations(), method 70 .getBody(), method.getParameters(), method 71 .getFormalTypeParameters()); 72 target.getConstructors().add(newConstructor); 73 newConstructor.setParent(target); 74 return newConstructor; 75 } 76 77 87 public <T> CtConstructor<T> create(CtClass<?> target, Set <ModifierKind> modifiers, 88 List <CtParameter<?>> parameters, 89 Set <CtTypeReference<? extends Throwable >> thrownTypes) { 90 CtConstructor<T> constructor = factory.Core().createConstructor(); 91 constructor.setModifiers(modifiers); 92 constructor.setParameters(parameters); 93 constructor.setThrownTypes(thrownTypes); 94 setParent(constructor, parameters); 95 target.getConstructors().add(constructor); 96 return constructor; 97 } 98 99 111 public <T> CtConstructor<T> create(CtClass<T> target, Set <ModifierKind> modifiers, 112 List <CtParameter<?>> parameters, 113 Set <CtTypeReference<? extends Throwable >> thrownTypes, 114 CtBlock<T> body) { 115 CtConstructor<T> constructor = create(target, modifiers, parameters, 116 thrownTypes); 117 constructor.setBody(body); 118 body.setParent(constructor); 119 return constructor; 120 } 121 122 125 public <T> CtExecutableReference<T> createReference(CtConstructor<T> c) { 126 return factory.Executable().createReference(c); 127 } 128 129 132 @SuppressWarnings ("unchecked") 133 public <T> CtExecutableReference<T> createReference(Constructor constructor) { 134 return createReference(factory.Type().createReference( 135 constructor.getDeclaringClass()), null, constructor.getName(), factory.Type() 136 .createReferences((List <Class <?>>)(List )Arrays.asList(constructor.getParameterTypes()))); 137 } 138 139 } 140 | Popular Tags |