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.Constructor;
021: import java.util.Arrays;
022: import java.util.List;
023: import java.util.Set;
024:
025: import spoon.reflect.Factory;
026: import spoon.reflect.code.CtBlock;
027: import spoon.reflect.declaration.CtClass;
028: import spoon.reflect.declaration.CtConstructor;
029: import spoon.reflect.declaration.CtMethod;
030: import spoon.reflect.declaration.CtParameter;
031: import spoon.reflect.declaration.ModifierKind;
032: import spoon.reflect.reference.CtExecutableReference;
033: import spoon.reflect.reference.CtTypeReference;
034:
035: /**
036: * The {@link CtConstructor} sub-factory.
037: */
038: public class ConstructorFactory extends ExecutableFactory {
039:
040: private static final long serialVersionUID = 1L;
041:
042: /**
043: * Creates a new constructor sub-factory.
044: *
045: * @param factory
046: * the parent factory
047: */
048: public ConstructorFactory(Factory factory) {
049: super (factory);
050: }
051:
052: /**
053: * Copies a constructor into a target class.
054: *
055: * @param target
056: * the target class
057: * @param source
058: * the constructor to be copied
059: * @return the new constructor
060: */
061: @SuppressWarnings("unchecked")
062: public <T> CtConstructor<T> create(CtClass<T> target,
063: CtConstructor<?> source) {
064: CtConstructor<T> newConstructor = factory.Core().clone(
065: (CtConstructor<T>) source);
066: target.getConstructors().add(newConstructor);
067: newConstructor.setParent(target);
068: return newConstructor;
069: }
070:
071: /**
072: * Creates a constructor into a target class by copying it from a source
073: * method.
074: *
075: * @param target
076: * the target class
077: * @param source
078: * the method to be copied
079: * @return the new constructor
080: */
081: @SuppressWarnings("unchecked")
082: public <T> CtConstructor<T> create(CtClass<T> target,
083: CtMethod<?> source) {
084: CtMethod<T> method = factory.Core().clone((CtMethod<T>) source);
085: CtConstructor<T> newConstructor = factory.Core()
086: .createConstructor();
087: newConstructor.setAnnotations(method.getAnnotations());
088: newConstructor.setBody(method.getBody());
089: newConstructor.setDocComment(method.getDocComment());
090: newConstructor.setFormalTypeParameters(method
091: .getFormalTypeParameters());
092: newConstructor.setModifiers(method.getModifiers());
093: newConstructor.setParameters(method.getParameters());
094: setParent(newConstructor, method.getAnnotations(), method
095: .getBody(), method.getParameters(), method
096: .getFormalTypeParameters());
097: target.getConstructors().add(newConstructor);
098: newConstructor.setParent(target);
099: return newConstructor;
100: }
101:
102: /**
103: * Creates an empty constructor.
104: *
105: * @param modifiers
106: * the modifiers
107: * @param parameters
108: * the parameters
109: * @param thrownTypes
110: * the thrown types
111: */
112: public <T> CtConstructor<T> create(CtClass<T> target,
113: Set<ModifierKind> modifiers,
114: List<CtParameter<?>> parameters,
115: Set<CtTypeReference<? extends Throwable>> thrownTypes) {
116: CtConstructor<T> constructor = factory.Core()
117: .createConstructor();
118: constructor.setModifiers(modifiers);
119: constructor.setParent(target);
120: constructor.setParameters(parameters);
121: constructor.setThrownTypes(thrownTypes);
122: setParent(constructor, parameters);
123: target.getConstructors().add(constructor);
124: return constructor;
125: }
126:
127: /**
128: * Creates a constructor.
129: *
130: * @param modifiers
131: * the modifiers
132: * @param parameters
133: * the parameters
134: * @param thrownTypes
135: * the thrown types
136: * @param body
137: * the body
138: */
139: public <T> CtConstructor<T> create(CtClass<T> target,
140: Set<ModifierKind> modifiers,
141: List<CtParameter<?>> parameters,
142: Set<CtTypeReference<? extends Throwable>> thrownTypes,
143: CtBlock<T> body) {
144: CtConstructor<T> constructor = create(target, modifiers,
145: parameters, thrownTypes);
146: constructor.setBody(body);
147: body.setParent(constructor);
148: return constructor;
149: }
150:
151: /**
152: * Creates a constructor reference from an existing constructor.
153: */
154: public <T> CtExecutableReference<T> createReference(
155: CtConstructor<T> c) {
156: return factory.Executable().createReference(c);
157: }
158:
159: /**
160: * Creates a constructor reference from an actual constructor.
161: */
162: @SuppressWarnings("unchecked")
163: public <T> CtExecutableReference<T> createReference(
164: Constructor constructor) {
165: return createReference(factory.Type().createReference(
166: constructor.getDeclaringClass()), null, constructor
167: .getName(), factory.Type().createReferences(
168: (List) Arrays.asList(constructor.getParameterTypes())));
169: }
170:
171: }
|