1 /*******************************************************************************
2 * Copyright (c) 2001, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Rational Software - initial implementation
10 * Anton Leherbauer (Wind River Systems)
11 * Sergey Prigogin (Google)
12 *******************************************************************************/
13 package org.eclipse.cdt.ui;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.jface.text.templates.Template;
18 import org.eclipse.cdt.core.model.ITranslationUnit;
20 import org.eclipse.cdt.internal.corext.codemanipulation.StubUtility;
23 * Class that offers access to the templates contained in the 'Code Templates' preference page.
25 * @noextend This class is not intended to be subclassed by clients.
26 * @noinstantiate This class is not intended to be instantiated by clients.
29 public class CodeGeneration {
31 private CodeGeneration() {
35 * Returns the content for a new header file using the default 'header file' code template.
36 * @param tu The translation unit to create the source for. The translation unit does not need
38 * @param lineDelimiter The line delimiter to be used.
39 * @return Returns the new content or <code>null</code> if the template is undefined or empty.
40 * @throws CoreException
43 public static String getHeaderFileContent(Template template, ITranslationUnit tu,
44 String lineDelimiter) throws CoreException {
45 return getHeaderFileContent(template, tu, null, null, null, null, null, null, null,
50 * Returns the content for a new header file using the default 'header file' code template.
51 * @param tu The translation unit to create the source for. The translation unit does not need
53 * @param typeComment The comment for the type to created. Used when the code template contains
54 * a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
55 * @param typeContent The code of the type, including type declaration and body.
56 * @param lineDelimiter The line delimiter to be used.
57 * @return Returns the new content or <code>null</code> if the template is undefined or empty.
58 * @throws CoreException
60 public static String getHeaderFileContent(ITranslationUnit tu, String typeComment,
61 String typeContent, String lineDelimiter) throws CoreException {
62 return getHeaderFileContent(tu, null, null, null, null, typeComment, typeContent, null,
67 * Returns the content for a new header file using the default 'header file' code template.
68 * @param template The file template to use or <code>null</code> for the default template
69 * @param tu The translation unit to create the source for. The translation unit does not need
71 * @param typeComment The comment for the type to created. Used when the code template contains
72 * a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
73 * @param typeContent The code of the type, including type declaration and body.
74 * @param lineDelimiter The line delimiter to be used.
75 * @return Returns the new content or <code>null</code> if the template is undefined or empty.
76 * @throws CoreException
78 public static String getHeaderFileContent(Template template, ITranslationUnit tu,
79 String typeComment, String typeContent, String lineDelimiter) throws CoreException {
80 return getHeaderFileContent(template, tu, null, null, null, null, typeComment, typeContent,
85 * Returns the content for a new header file using the default 'header file' code template.
86 * @param tu The translation unit to create the source for. The translation unit does not need
88 * @param includes Include statements, or {@code null} if there are no includes.
89 * @param namespaceBegin Beginning of namespace declarations, or {@code null} if there is no
91 * @param namespaceEnd End of namespace declarations, or {@code null} if there is no
93 * @param namespaceName Fully qualified namespace name, or {@code null} if there is no
95 * @param typeComment The comment for the type to created. Used when the code template contains
96 * a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
97 * @param typeContent The code of the type, including type declaration and body.
98 * @param typeName The name of the type.
99 * @param lineDelimiter The line delimiter to be used.
100 * @return Returns the new content or <code>null</code> if the template is undefined or empty.
101 * @throws CoreException
104 public static String getHeaderFileContent(ITranslationUnit tu, String includes,
105 String namespaceBegin, String namespaceEnd, String namespaceName, String typeComment,
106 String typeContent, String typeName, String lineDelimiter) throws CoreException {
107 return StubUtility.getHeaderFileContent(tu, typeContent,
108 getFileComment(tu, lineDelimiter), includes, namespaceBegin, namespaceEnd,
109 namespaceName, typeComment, typeName, lineDelimiter);
113 * Returns the content for a new header file using the default 'header file' code template.
114 * @param template The file template to use or <code>null</code> for the default template
115 * @param tu The translation unit to create the source for. The translation unit does not need
117 * @param includes Include statements, or {@code null} if there are no includes.
118 * @param namespaceBegin Beginning of namespace declarations, or {@code null} if there is no
120 * @param namespaceEnd End of namespace declarations, or {@code null} if there is no
122 * @param namespaceName Fully qualified namespace name, or {@code null} if there is no
124 * @param typeComment The comment for the type to created. Used when the code template contains
125 * a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
126 * @param typeContent The code of the type, including type declaration and body.
127 * @param typeName The name of the type.
128 * @param lineDelimiter The line delimiter to be used.
129 * @return Returns the new content or <code>null</code> if the template is undefined or empty.
130 * @throws CoreException
133 public static String getHeaderFileContent(Template template, ITranslationUnit tu,
134 String includes, String namespaceBegin, String namespaceEnd, String namespaceName,
135 String typeComment, String typeContent, String typeName, String lineDelimiter)
136 throws CoreException {
137 return StubUtility.getHeaderFileContent(template, tu, typeContent,
138 getFileComment(tu, lineDelimiter), includes, namespaceBegin, namespaceEnd,
139 namespaceName, typeComment, typeName, lineDelimiter);
143 * Returns the content for a new translation unit using the 'source file' code template.
144 * @param tu The translation unit to create the source for. The translation unit does not need
146 * @param typeComment The comment for the type to created. Used when the code template contains
147 * a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
148 * @param typeContent The code of the type, including type declaration and body.
149 * @param lineDelimiter The line delimiter to be used.
150 * @return Returns the new content or <code>null</code> if the template is undefined or empty.
151 * @throws CoreException
153 public static String getBodyFileContent(ITranslationUnit tu, String typeComment,
154 String typeContent, String lineDelimiter) throws CoreException {
155 return getBodyFileContent(tu, typeContent, null, null, null, null, typeComment, null,
160 * Returns the content for a new translation unit using the 'source file' code template.
161 * @param template The file template to use or <code>null</code> for the default template
162 * @param tu The translation unit to create the source for. The translation unit does not need
164 * @param typeComment The comment for the type to created. Used when the code template contains
165 * a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
166 * @param typeContent The code of the type, including type declaration and body.
167 * @param lineDelimiter The line delimiter to be used.
168 * @return Returns the new content or <code>null</code> if the template is undefined or empty.
169 * @throws CoreException
171 public static String getBodyFileContent(Template template, ITranslationUnit tu,
172 String typeComment, String typeContent, String lineDelimiter) throws CoreException {
173 return getBodyFileContent(template, tu, typeContent, null, null, null, null, typeComment,
174 null, lineDelimiter);
178 * Returns the content for a new source file using the default 'source file' code template.
179 * @param tu The translation unit to create the source for. The translation unit does not need
181 * @param includes Include statements, or {@code null} if there are no includes.
182 * @param namespaceBegin Beginning of namespace declarations, or {@code null} if there is no
184 * @param namespaceEnd End of namespace declarations, or {@code null} if there is no
186 * @param namespaceName Fully qualified namespace name, or {@code null} if there is no
188 * @param typeComment The comment for the type to created. Used when the code template contains
189 * a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
190 * @param typeContent The code of the type, including type declaration and body.
191 * @param typeName The name of the type.
192 * @param lineDelimiter The line delimiter to be used.
193 * @return Returns the new content or <code>null</code> if the template is undefined or empty.
194 * @throws CoreException
197 public static String getBodyFileContent(ITranslationUnit tu,
198 String includes, String namespaceBegin, String namespaceEnd, String namespaceName,
199 String typeComment, String typeContent, String typeName, String lineDelimiter)
200 throws CoreException {
201 return StubUtility.getBodyFileContent(tu, typeContent, getFileComment(tu, lineDelimiter),
202 includes, namespaceBegin, namespaceEnd, namespaceName, typeComment, typeName,
207 * Returns the content for a new source file using the default 'source file' code template.
208 * @param template The file template to use or <code>null</code> for the default template
209 * @param tu The translation unit to create the source for. The translation unit does not need
211 * @param includes Include statements, or {@code null} if there are no includes.
212 * @param namespaceBegin Beginning of namespace declarations, or {@code null} if there is no
214 * @param namespaceEnd End of namespace declarations, or {@code null} if there is no
216 * @param namespaceName Fully qualified namespace name, or {@code null} if there is no
218 * @param typeComment The comment for the type to created. Used when the code template contains
219 * a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
220 * @param typeContent The code of the type, including type declaration and body.
221 * @param typeName The name of the type.
222 * @param lineDelimiter The line delimiter to be used.
223 * @return Returns the new content or <code>null</code> if the template is undefined or empty.
224 * @throws CoreException
227 public static String getBodyFileContent(Template template, ITranslationUnit tu,
228 String includes, String namespaceBegin, String namespaceEnd, String namespaceName,
229 String typeComment, String typeContent, String typeName, String lineDelimiter)
230 throws CoreException {
231 return StubUtility.getBodyFileContent(template, tu, typeContent,
232 getFileComment(tu, lineDelimiter), includes, namespaceBegin, namespaceEnd,
233 namespaceName, typeComment, typeName, lineDelimiter);
237 * Returns the content for a new translation unit using the 'test file' code template.
238 * @param tu The translation unit to create the source for. The translation unit does not need
240 * @param includes Include statements, or {@code null} if there are no includes.
241 * @param namespaceBegin Beginning of namespace declarations, or {@code null} if there is no
243 * @param namespaceEnd End of namespace declarations, or {@code null} if there is no
245 * @param namespaceName Fully qualified namespace name, or {@code null} if there is no
247 * @param testCases The test cases code.
248 * @param typeName The name of the type.
249 * @param lineDelimiter The line delimiter to be used.
250 * @return Returns the new content or <code>null</code> if the template is undefined or empty.
251 * @throws CoreException
254 public static String getTestFileContent(ITranslationUnit tu,
255 String includes, String namespaceBegin, String namespaceEnd, String namespaceName,
256 String testCases, String typeName, String lineDelimiter) throws CoreException {
257 return StubUtility.getTestFileContent(tu, testCases, getFileComment(tu, lineDelimiter),
258 includes, namespaceBegin, namespaceEnd, namespaceName, typeName, lineDelimiter);
262 * Returns the content of the body for a method using the method body template.
263 * <code>null</code> is returned if the template is empty.
264 * <p>The returned string is unformatted and not indented.
266 * @param tu The translation unit to which the method belongs. The translation unit does not need to exist.
267 * @param typeName Name of the type to which the method belongs.
268 * @param methodName Name of the method.
269 * @param bodyStatement The code to be entered at the place of the variable ${body_statement}.
270 * @param lineDelimiter The line delimiter to be used.
271 * @return Returns the constructed body content or <code>null</code> if
272 * the comment code template is empty. The returned string is unformatted and and has no indent (formatting required).
273 * @throws CoreException Thrown when the evaluation of the code template fails.
275 public static String getMethodBodyContent(ITranslationUnit tu, String typeName,
276 String methodName, String bodyStatement, String lineDelimiter) throws CoreException {
277 return StubUtility.getMethodBodyContent(tu.getCProject(), typeName, methodName,
278 bodyStatement, lineDelimiter);
282 * Returns the content of the body for a constructor using the constructor body template.
283 * <code>null</code> is returned if the template is empty.
284 * <p>The returned string is unformatted and not indented.
286 * @param tu The translation unit to which the method belongs. The translation unit
287 * does not need to exist.
288 * @param typeName Name of the type to which the constructor belongs.
289 * @param bodyStatement The code to be entered at the place of the variable ${body_statement}.
290 * @param lineDelimiter The line delimiter to be used.
291 * @return Returns the constructed body content or <code>null</code> if the comment code
292 * template is empty. The returned string is unformatted and and has no indent
293 * (formatting required).
294 * @throws CoreException Thrown when the evaluation of the code template fails.
296 public static String getConstructorBodyContent(ITranslationUnit tu, String typeName,
297 String bodyStatement, String lineDelimiter) throws CoreException {
298 return StubUtility.getConstructorBodyContent(tu.getCProject(), typeName, bodyStatement,
303 * Returns the content of the body for a destructor using the destructor body template.
304 * <code>null</code> is returned if the template is empty.
305 * <p>The returned string is unformatted and not indented.
307 * @param tu The translation unit to which the method belongs. The translation unit
308 * does not need to exist.
309 * @param typeName Name of the type to which the constructor belongs.
310 * @param bodyStatement The code to be entered at the place of the variable ${body_statement}.
311 * @param lineDelimiter The line delimiter to be used.
312 * @return Returns the constructed body content or <code>null</code> if the comment code
313 * template is empty. The returned string is unformatted and and has no indent
314 * (formatting required).
315 * @throws CoreException Thrown when the evaluation of the code template fails.
317 public static String getDestructorBodyContent(ITranslationUnit tu, String typeName,
318 String bodyStatement, String lineDelimiter) throws CoreException {
319 return StubUtility.getDestructorBodyContent(tu.getCProject(), typeName, bodyStatement,
324 * Returns the content of the class definition body using the class body template.
325 * <code>null</code> is returned if the template is empty.
326 * <p>The returned string is unformatted and not indented.
328 * @param tu The translation unit to which the method belongs. The translation unit
329 * does not need to exist.
330 * @param className The name of the class.
331 * @param classMemberDeclarations The code to be entered at the place of the variable
333 * @param lineDelimiter The line delimiter to be used.
334 * @return Returns the constructed class body content or <code>null</code> if
335 * the class body code template is empty. The returned string is unformatted and
336 * has no indent (formatting required).
337 * @throws CoreException Thrown when the evaluation of the code template fails.
340 public static String getClassBodyContent(ITranslationUnit tu, String className,
341 String classMemberDeclarations, String lineDelimiter)
342 throws CoreException {
343 return StubUtility.getClassBodyContent(tu.getCProject(), className, classMemberDeclarations,
348 * Returns the content of the beginning of a namespace declaration using the corresponding
349 * template. <code>null</code> is returned if the template is empty.
350 * <p>The returned string is unformatted and not indented.
352 * @param tu The translation unit to which the method belongs. The translation unit
353 * does not need to exist.
354 * @param namespaceName The name of the namespace.
355 * @param lineDelimiter The line delimiter to be used.
356 * @return Returns the constructed beginning of a namespace declaration,or <code>null</code> if
357 * the namespace code template is empty. The returned string is unformatted and has no
358 * indent (formatting required).
359 * @throws CoreException Thrown when the evaluation of the code template fails.
362 public static String getNamespaceBeginContent(ITranslationUnit tu, String namespaceName,
363 String lineDelimiter) throws CoreException {
364 return StubUtility.getNamespaceBeginContent(tu.getCProject(), namespaceName, lineDelimiter);
368 * Returns the content of the end of a namespace declaration using the corresponding
369 * template. <code>null</code> is returned if the template is empty.
370 * <p>The returned string is unformatted and not indented.
372 * @param tu The translation unit to which the method belongs. The translation unit
373 * does not need to exist.
374 * @param namespaceName The name of the namespace.
375 * @param lineDelimiter The line delimiter to be used.
376 * @return Returns the constructed end of a namespace declaration,or <code>null</code> if
377 * the namespace code template is empty. The returned string is unformatted and has no
378 * indent (formatting required).
379 * @throws CoreException Thrown when the evaluation of the code template fails.
382 public static String getNamespaceEndContent(ITranslationUnit tu, String namespaceName,
383 String lineDelimiter) throws CoreException {
384 return StubUtility.getNamespaceEndContent(tu.getCProject(), namespaceName, lineDelimiter);
388 * Returns the content for a new file comment using the 'file comment' code template.
389 * The returned content is unformatted and is not indented.
390 * @param tu The translation unit to add the comment to. The translation unit does not need
392 * @param lineDelimiter The line delimiter to be used.
393 * @return Returns the new content or <code>null</code> if the code template is undefined
394 * or empty. The returned content is unformatted and is not indented.
395 * @throws CoreException Thrown when the evaluation of the code template fails.
398 public static String getFileComment(ITranslationUnit tu, String lineDelimiter)
399 throws CoreException {
400 return StubUtility.getFileComment(tu, lineDelimiter);
404 * Returns the content for a new type comment using the 'typecomment' code template.
405 * The returned content is unformatted and is not indented.
406 * @param tu The translation unit where the type is contained. The translation unit
407 * does not need to exist.
408 * @param typeQualifiedName The name of the type to which the comment is added. For inner types
409 * the name must be qualified and include the outer types names (dot separated).
410 * @param lineDelimiter The line delimiter to be used.
411 * @return Returns the new content or <code>null</code> if the code template is undefined
412 * or empty. The returned content is unformatted and is not indented.
413 * @throws CoreException
415 public static String getClassComment(ITranslationUnit tu, String typeQualifiedName,
416 String lineDelimiter) throws CoreException {
417 return StubUtility.getClassComment(tu, typeQualifiedName, lineDelimiter);
421 * Returns the comment for a method using the method comment code template.
422 * <code>null</code> is returned if the template is empty.
423 * <p>The returned string is unformatted and not indented.
425 * @param tu The translation unit to which the method belongs. The translation unit
426 * does not need to exist.
427 * @param declaringTypeName Name of the type to which the method belongs.
428 * @param methodName Name of the method.
429 * @param paramNames Names of the parameters for the method.
430 * @param excTypeSig Thrown exceptions.
431 * @param retTypeSig Return type.
432 * @param lineDelimiter The line delimiter to be used.
433 * @return Returns the constructed comment or <code>null</code> if the comment code template
434 * is empty. The returned content is unformatted and not indented (formatting required).
435 * @throws CoreException Thrown when the evaluation of the code template fails.
437 public static String getMethodComment(ITranslationUnit tu, String declaringTypeName,
438 String methodName, String[] paramNames, String[] excTypeSig, String retTypeSig,
439 String lineDelimiter) throws CoreException {
440 return StubUtility.getMethodComment(tu, declaringTypeName, methodName, paramNames,
441 excTypeSig, retTypeSig, lineDelimiter);
445 * Returns the comment for a constructor using the constructor comment code template.
446 * <code>null</code> is returned if the template is empty.
447 * <p>The returned string is unformatted and not indented.
449 * @param tu The translation unit to which the method belongs. The translation unit
450 * does not need to exist.
451 * @param declaringTypeName Name of the type to which the method belongs.
452 * @param paramNames Names of the parameters for the method.
453 * @param excTypeSig Thrown exceptions.
454 * @param lineDelimiter The line delimiter to be used.
455 * @return Returns the constructed comment or <code>null</code> if the comment code template
456 * is empty. The returned content is unformatted and not indented (formatting required).
457 * @throws CoreException Thrown when the evaluation of the code template fails.
459 public static String getConstructorComment(ITranslationUnit tu, String declaringTypeName,
460 String[] paramNames, String[] excTypeSig, String lineDelimiter) throws CoreException {
461 return StubUtility.getConstructorComment(tu, declaringTypeName, paramNames, excTypeSig,
466 * Returns the comment for a destructor using the destructor comment code template.
467 * <code>null</code> is returned if the template is empty.
468 * <p>The returned string is unformatted and not indented.
470 * @param tu The translation unit to which the method belongs. The translation unit
471 * does not need to exist.
472 * @param declaringTypeName Name of the type to which the method belongs.
473 * @param excTypeSig Thrown exceptions.
474 * @param lineDelimiter The line delimiter to be used.
475 * @return Returns the constructed comment or <code>null</code> if the comment code template
476 * is empty. The returned content is unformatted and not indented (formatting required).
477 * @throws CoreException Thrown when the evaluation of the code template fails.
479 public static String getDestructorComment(ITranslationUnit tu, String declaringTypeName,
480 String[] excTypeSig, String lineDelimiter) throws CoreException {
481 return StubUtility.getDestructorComment(tu, declaringTypeName, excTypeSig, lineDelimiter);