1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.ui;
13 import net.sourceforge.phpdt.core.ICompilationUnit;
14 import net.sourceforge.phpdt.core.IMethod;
15 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
17 import org.eclipse.core.runtime.CoreException;
20 * Class that offers access to the templates contained in the 'code templates'
25 public class CodeGeneration {
27 private CodeGeneration() {
31 * Returns the content for a new compilation unit using the 'new Java file'
35 * The compilation to create the source for. The compilation unit
36 * does not need to exist.
38 * The comment for the type to be created. Used when the code
39 * template contains a <i>${typecomment}</i> variable. Can be
40 * <code>null</code> if no comment should be added.
42 * The code of the type, including type declaration and body.
43 * @param lineDelimiter
44 * The line delimiter to be used.
45 * @return Returns the new content or <code>null</code> if the template is
47 * @throws CoreException
48 * Thrown when the evaluation of the code template fails.
50 public static String getCompilationUnitContent(ICompilationUnit cu,
51 String typeComment, String typeContent, String lineDelimiter)
52 throws CoreException {
53 return StubUtility.getCompilationUnitContent(cu, typeComment,
54 typeContent, lineDelimiter);
58 * Returns the content for a new type comment using the 'type comment' code
59 * template. The returned content is unformatted and is not indented.
62 * The compilation where the type is contained. The compilation
63 * unit does not need to exist.
64 * @param typeQualifiedName
65 * The name of the type to which the comment is added. For inner
66 * types the name must be qualified and include the outer types
67 * names (dot separated). See
68 * {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
69 * @param lineDelimiter
70 * The line delimiter to be used.
71 * @return Returns the new content or <code>null</code> if the code
72 * template is undefined or empty. The returned content is
73 * unformatted and is not indented.
74 * @throws CoreException
75 * Thrown when the evaluation of the code template fails.
77 public static String getTypeComment(ICompilationUnit cu,
78 String typeQualifiedName, String lineDelimiter)
79 throws CoreException {
80 return StubUtility.getTypeComment(cu, typeQualifiedName, lineDelimiter);
84 * Returns the content for a new field comment using the 'field comment'
85 * code template. The returned content is unformatted and is not indented.
88 * The compilation where the field is contained. The compilation
89 * unit does not need to exist.
91 * The name of the field declared type.
93 * The name of the field to which the comment is added.
94 * @param lineDelimiter
95 * The line delimiter to be used.
96 * @return Returns the new content or <code>null</code> if the code
97 * template is undefined or empty. The returned content is
98 * unformatted and is not indented.
99 * @throws CoreException
100 * Thrown when the evaluation of the code template fails.
103 public static String getFieldComment(ICompilationUnit cu, String typeName,
104 String fieldName, String lineDelimiter) throws CoreException {
105 return StubUtility.getFieldComment(cu, typeName, fieldName,
110 * Returns the comment for a method or constructor using the comment code
111 * templates (constructor / method / overriding method). <code>null</code>
112 * is returned if the template is empty.
115 * The compilation unit to which the method belongs. The
116 * compilation unit does not need to exist.
117 * @param declaringTypeName
118 * Name of the type to which the method belongs. For inner types
119 * the name must be qualified and include the outer types names
120 * (dot separated). See
121 * {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
123 * The MethodDeclaration AST node that will be added as new
124 * method. The node does not need to exist in an AST (no parent
125 * needed) and does not need to resolve. See
126 * {@link net.sourceforge.phpdt.core.dom.AST#newMethodDeclaration()}
127 * for how to create such a node.
129 * The binding of the method that will be overridden by the
130 * created method or <code>null</code> if no method is
132 * @param lineDelimiter
133 * The line delimiter to be used.
134 * @return Returns the generated method comment or <code>null</code> if
135 * the code template is empty. The returned content is unformatted
136 * and not indented (formatting required).
137 * @throws CoreException
138 * Thrown when the evaluation of the code template fails.
140 // public static String getMethodComment(ICompilationUnit cu, String
141 // declaringTypeName, MethodDeclaration decl, IMethodBinding overridden,
142 // String lineDelimiter) throws CoreException {
143 // return StubUtility.getMethodComment(cu, declaringTypeName, decl,
144 // overridden, lineDelimiter);
147 * Returns the comment for a method or constructor using the comment code
148 * templates (constructor / method / overriding method). <code>null</code>
149 * is returned if the template is empty.
151 * The returned string is unformatted and not indented.
153 * Exception types and return type are in signature notation. e.g. a source
154 * method declared as <code>public void foo(String text, int length)</code>
155 * would return the array <code>{"QString;","I"}</code> as parameter
156 * types. See {@link net.sourceforge.phpdt.core.Signature}.
159 * The compilation unit to which the method belongs. The
160 * compilation unit does not need to exist.
161 * @param declaringTypeName
162 * Name of the type to which the method belongs. For inner types
163 * the name must be qualified and include the outer types names
164 * (dot separated). See
165 * {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
167 * Name of the method.
169 * Names of the parameters for the method.
171 * Thrown exceptions (Signature notation).
173 * Return type (Signature notation) or <code>null</code> for
176 * The method that will be overridden by the created method or
177 * <code>null</code> for non-overriding methods. If not
178 * <code>null</code>, the method must exist.
179 * @param lineDelimiter
180 * The line delimiter to be used.
181 * @return Returns the constructed comment or <code>null</code> if the
182 * comment code template is empty. The returned content is
183 * unformatted and not indented (formatting required).
184 * @throws CoreException
185 * Thrown when the evaluation of the code template fails.
187 public static String getMethodComment(ICompilationUnit cu,
188 String declaringTypeName, String methodName, String[] paramNames,
189 String[] excTypeSig, String retTypeSig, IMethod overridden,
190 String lineDelimiter) throws CoreException {
191 return StubUtility.getMethodComment(cu, declaringTypeName, methodName,
192 paramNames, excTypeSig, retTypeSig, overridden, lineDelimiter);
196 * Returns the comment for a method or constructor using the comment code
197 * templates (constructor / method / overriding method). <code>null</code>
198 * is returned if the template is empty.
200 * The returned string is unformatted and not indented.
203 * The method to be documented. The method must exist.
205 * The method that will be overridden by the created method or
206 * <code>null</code> for non-overriding methods. If not
207 * <code>null</code>, the method must exist.
208 * @param lineDelimiter
209 * The line delimiter to be used.
210 * @return Returns the constructed comment or <code>null</code> if the
211 * comment code template is empty. The returned string is
212 * unformatted and and has no indent (formatting required).
213 * @throws CoreException
214 * Thrown when the evaluation of the code template fails.
216 public static String getMethodComment(IMethod method, IMethod overridden,
217 String lineDelimiter) throws CoreException {
218 return StubUtility.getMethodComment(method, overridden, lineDelimiter);
222 * Returns the content of the body for a method or constructor using the
223 * method body templates. <code>null</code> is returned if the template is
226 * The returned string is unformatted and not indented.
229 * The compilation unit to which the method belongs. The
230 * compilation unit does not need to exist.
231 * @param declaringTypeName
232 * Name of the type to which the method belongs. For inner types
233 * the name must be qualified and include the outer types names
234 * (dot separated). See
235 * {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
237 * Name of the method.
238 * @param isConstructor
239 * Defines if the created body is for a constructor.
240 * @param bodyStatement
241 * The code to be entered at the place of the variable
243 * @param lineDelimiter
244 * The line delimiter to be used.
245 * @return Returns the constructed body content or <code>null</code> if
246 * the comment code template is empty. The returned string is
247 * unformatted and and has no indent (formatting required).
248 * @throws CoreException
249 * Thrown when the evaluation of the code template fails.
251 public static String getMethodBodyContent(ICompilationUnit cu,
252 String declaringTypeName, String methodName, boolean isConstructor,
253 String bodyStatement, String lineDelimiter) throws CoreException {
254 return StubUtility.getMethodBodyContent(isConstructor, cu
255 .getJavaProject(), declaringTypeName, methodName,
256 bodyStatement, lineDelimiter);
260 * Returns the content of body for a getter method using the getter method
261 * body template. <code>null</code> is returned if the template is empty.
263 * The returned string is unformatted and not indented.
266 * The compilation unit to which the method belongs. The
267 * compilation unit does not need to exist.
268 * @param declaringTypeName
269 * Name of the type to which the method belongs. For inner types
270 * the name must be qualified and include the outer types names
271 * (dot separated). See
272 * {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
274 * The name of the getter method.
276 * The name of the field to get in the getter method,
277 * corresponding to the template variable for ${field}.
278 * @param lineDelimiter
279 * The line delimiter to be used.
280 * @return Returns the constructed body content or <code>null</code> if
281 * the comment code template is empty. The returned string is
282 * unformatted and and has no indent (formatting required).
283 * @throws CoreException
284 * Thrown when the evaluation of the code template fails.
287 public static String getGetterMethodBodyContent(ICompilationUnit cu,
288 String declaringTypeName, String methodName, String fieldName,
289 String lineDelimiter) throws CoreException {
290 return StubUtility.getGetterMethodBodyContent(cu.getJavaProject(),
291 declaringTypeName, methodName, fieldName, lineDelimiter);
295 * Returns the content of body for a setter method using the setter method
296 * body template. <code>null</code> is returned if the template is empty.
298 * The returned string is unformatted and not indented.
301 * The compilation unit to which the method belongs. The
302 * compilation unit does not need to exist.
303 * @param declaringTypeName
304 * Name of the type to which the method belongs. For inner types
305 * the name must be qualified and include the outer types names
306 * (dot separated). See
307 * {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
309 * The name of the setter method.
311 * The name of the field to be set in the setter method,
312 * corresponding to the template variable for ${field}.
314 * The name of the parameter passed to the setter method,
315 * corresponding to the template variable for $(param).
316 * @param lineDelimiter
317 * The line delimiter to be used.
318 * @return Returns the constructed body content or <code>null</code> if
319 * the comment code template is empty. The returned string is
320 * unformatted and and has no indent (formatting required).
321 * @throws CoreException
322 * Thrown when the evaluation of the code template fails.
325 public static String getSetterMethodBodyContent(ICompilationUnit cu,
326 String declaringTypeName, String methodName, String fieldName,
327 String paramName, String lineDelimiter) throws CoreException {
328 return StubUtility.getSetterMethodBodyContent(cu.getJavaProject(),
329 declaringTypeName, methodName, fieldName, paramName,
334 * Returns the comment for a getter method using the getter comment
335 * template. <code>null</code> is returned if the template is empty.
337 * The returned string is unformatted and not indented.
340 * The compilation unit to which the method belongs. The
341 * compilation unit does not need to exist.
342 * @param declaringTypeName
343 * Name of the type to which the method belongs. For inner types
344 * the name must be qualified and include the outer types names
345 * (dot separated). See
346 * {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
348 * Name of the method.
350 * Name of the field to get.
352 * The type of the field to get.
353 * @param bareFieldName
354 * The field name without prefix or suffix.
355 * @param lineDelimiter
356 * The line delimiter to be used.
357 * @return Returns the generated getter comment or <code>null</code> if
358 * the code template is empty. The returned content is not indented.
359 * @throws CoreException
360 * Thrown when the evaluation of the code template fails.
363 public static String getGetterComment(ICompilationUnit cu,
364 String declaringTypeName, String methodName, String fieldName,
365 String fieldType, String bareFieldName, String lineDelimiter)
366 throws CoreException {
367 return StubUtility.getGetterComment(cu, declaringTypeName, methodName,
368 fieldName, fieldType, bareFieldName, lineDelimiter);
372 * Returns the comment for a setter method using the setter method body
373 * template. <code>null</code> is returned if the template is empty.
375 * The returned string is unformatted and not indented.
378 * The compilation unit to which the method belongs. The
379 * compilation unit does not need to exist.
380 * @param declaringTypeName
381 * Name of the type to which the method belongs. For inner types
382 * the name must be qualified and include the outer types names
383 * (dot separated). See
384 * {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
386 * Name of the method.
388 * Name of the field that is set.
390 * The type of the field that is to set.
392 * The name of the parameter that used to set.
393 * @param bareFieldName
394 * The field name without prefix or suffix.
395 * @param lineDelimiter
396 * The line delimiter to be used.
397 * @return Returns the generated setter comment or <code>null</code> if
398 * the code template is empty. The returned comment is not indented.
399 * @throws CoreException
400 * Thrown when the evaluation of the code template fails.
403 public static String getSetterComment(ICompilationUnit cu,
404 String declaringTypeName, String methodName, String fieldName,
405 String fieldType, String paramName, String bareFieldName,
406 String lineDelimiter) throws CoreException {
407 return StubUtility.getSetterComment(cu, declaringTypeName, methodName,
408 fieldName, fieldType, paramName, bareFieldName, lineDelimiter);