added first version of codetemplates (needs to fix some bugs)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / CodeGeneration.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.ui;
12
13 import org.eclipse.core.runtime.CoreException;
14
15 import net.sourceforge.phpdt.core.ICompilationUnit;
16 import net.sourceforge.phpdt.core.IMethod;
17 //import net.sourceforge.phpdt.core.dom.IMethodBinding;
18 //import net.sourceforge.phpdt.core.dom.MethodDeclaration;
19
20 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
21
22 /**
23  * Class that offers access to the templates contained in the 'code templates' preference page.
24  * 
25  * @since 2.1
26  */
27 public class CodeGeneration {
28         
29         private CodeGeneration() {
30         }
31         
32         /**
33          * Returns the content for a new compilation unit using the 'new Java file' code template.
34          * @param cu The compilation to create the source for. The compilation unit does not need to exist.
35          * @param typeComment The comment for the type to be created. Used when the code template contains a <i>${typecomment}</i> variable. Can be <code>null</code> if
36          * no comment should be added.
37          * @param typeContent The code of the type, including type declaration and body.
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 Thrown when the evaluation of the code template fails.
41          */
42         public static String getCompilationUnitContent(ICompilationUnit cu, String typeComment, String typeContent, String lineDelimiter) throws CoreException {        
43                 return StubUtility.getCompilationUnitContent(cu, typeComment, typeContent, lineDelimiter);
44         }
45         
46         /**
47          * Returns the content for a new type comment using the 'type comment' code template. The returned content is unformatted and is not indented.
48          * @param cu The compilation where the type is contained. The compilation unit does not need to exist.
49          * @param typeQualifiedName The name of the type to which the comment is added. For inner types the name must be qualified and include the outer
50          * types names (dot separated). See {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
51          * @param lineDelimiter The line delimiter to be used.
52          * @return Returns the new content or <code>null</code> if the code template is undefined or empty. The returned content is unformatted and is not indented.
53          * @throws CoreException Thrown when the evaluation of the code template fails.
54          */     
55         public static String getTypeComment(ICompilationUnit cu, String typeQualifiedName, String lineDelimiter) throws CoreException {
56                 return StubUtility.getTypeComment(cu, typeQualifiedName, lineDelimiter);
57         }
58
59         /**
60          * Returns the content for a new field comment using the 'field comment' code template. The returned content is unformatted and is not indented.
61          * @param cu The compilation where the field is contained. The compilation unit does not need to exist.
62          * @param typeName The name of the field declared type.
63          * @param fieldName The name of the field to which the comment is added.
64          * @param lineDelimiter The line delimiter to be used.
65          * @return Returns the new content or <code>null</code> if the code template is undefined or empty. The returned content is unformatted and is not indented.
66          * @throws CoreException Thrown when the evaluation of the code template fails.
67          * @since 3.0
68          */     
69         public static String getFieldComment(ICompilationUnit cu, String typeName, String fieldName, String lineDelimiter) throws CoreException {
70                 return StubUtility.getFieldComment(cu, typeName, fieldName, lineDelimiter);
71         }       
72         
73         /**
74          * Returns the comment for a method or constructor using the comment code templates (constructor / method / overriding method).
75          * <code>null</code> is returned if the template is empty.
76          * @param cu The compilation unit to which the method belongs. The compilation unit does not need to exist.
77          * @param declaringTypeName Name of the type to which the method belongs. For inner types the name must be qualified and include the outer
78          * types names (dot separated). See {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
79          * @param decl The MethodDeclaration AST node that will be added as new
80          * method. The node does not need to exist in an AST (no parent needed) and does not need to resolve.
81          * See {@link net.sourceforge.phpdt.core.dom.AST#newMethodDeclaration()} for how to create such a node.
82          * @param overridden The binding of the method that will be overridden by the created
83          * method or <code>null</code> if no method is overridden.
84          * @param lineDelimiter The line delimiter to be used.
85          * @return Returns the generated method comment or <code>null</code> if the
86          * code template is empty. The returned content is unformatted and not indented (formatting required).
87          * @throws CoreException Thrown when the evaluation of the code template fails.
88          */
89 //      public static String getMethodComment(ICompilationUnit cu, String declaringTypeName, MethodDeclaration decl, IMethodBinding overridden, String lineDelimiter) throws CoreException {
90 //              return StubUtility.getMethodComment(cu, declaringTypeName, decl, overridden, lineDelimiter);
91 //      }
92
93         /**
94          * Returns the comment for a method or constructor using the comment code templates (constructor / method / overriding method).
95          * <code>null</code> is returned if the template is empty.
96          * <p>The returned string is unformatted and not indented.
97          * <p>Exception types and return type are in signature notation. e.g. a source method declared as <code>public void foo(String text, int length)</code>
98          * would return the array <code>{"QString;","I"}</code> as parameter types. See {@link net.sourceforge.phpdt.core.Signature}.
99          * 
100          * @param cu The compilation unit to which the method belongs. The compilation unit does not need to exist.
101          * @param declaringTypeName Name of the type to which the method belongs. For inner types the name must be qualified and include the outer
102          * types names (dot separated). See {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
103          * @param methodName Name of the method.
104          * @param paramNames Names of the parameters for the method.
105          * @param excTypeSig Thrown exceptions (Signature notation).
106          * @param retTypeSig Return type (Signature notation) or <code>null</code>
107          * for constructors.
108          * @param overridden The method that will be overridden by the created method or
109          * <code>null</code> for non-overriding methods. If not <code>null</code>, the method must exist.
110          * @param lineDelimiter The line delimiter to be used.
111          * @return Returns the constructed comment or <code>null</code> if
112          * the comment code template is empty. The returned content is unformatted and not indented (formatting required).
113          * @throws CoreException Thrown when the evaluation of the code template fails.
114          */
115         public static String getMethodComment(ICompilationUnit cu, String declaringTypeName, String methodName, String[] paramNames, String[] excTypeSig, String retTypeSig, IMethod overridden, String lineDelimiter) throws CoreException {
116                 return StubUtility.getMethodComment(cu, declaringTypeName, methodName, paramNames, excTypeSig, retTypeSig, overridden, lineDelimiter);
117         }
118                 
119         /**
120          * Returns the comment for a method or constructor using the comment code templates (constructor / method / overriding method).
121          * <code>null</code> is returned if the template is empty.
122          * <p>The returned string is unformatted and not indented.
123          * 
124          * @param method The method to be documented. The method must exist.
125          * @param overridden The method that will be overridden by the created method or
126          * <code>null</code> for non-overriding methods. If not <code>null</code>, the method must exist.
127          * @param lineDelimiter The line delimiter to be used.
128          * @return Returns the constructed comment or <code>null</code> if
129          * the comment code template is empty. The returned string is unformatted and and has no indent (formatting required).
130          * @throws CoreException Thrown when the evaluation of the code template fails.
131          */
132         public static String getMethodComment(IMethod method, IMethod overridden, String lineDelimiter) throws CoreException {
133                 return StubUtility.getMethodComment(method, overridden, lineDelimiter);
134         }       
135
136         /**
137          * Returns the content of the body for a method or constructor using the method body templates.
138          * <code>null</code> is returned if the template is empty.
139          * <p>The returned string is unformatted and not indented.
140          * 
141          * @param cu The compilation unit to which the method belongs. The compilation unit does not need to exist.
142          * @param declaringTypeName Name of the type to which the method belongs. For inner types the name must be qualified and include the outer
143          * types names (dot separated). See {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
144          * @param methodName Name of the method.
145          * @param isConstructor Defines if the created body is for a constructor.
146          * @param bodyStatement The code to be entered at the place of the variable ${body_statement}. 
147          * @param lineDelimiter The line delimiter to be used.
148          * @return Returns the constructed body content or <code>null</code> if
149          * the comment code template is empty. The returned string is unformatted and and has no indent (formatting required).
150          * @throws CoreException Thrown when the evaluation of the code template fails.
151          */     
152         public static String getMethodBodyContent(ICompilationUnit cu, String declaringTypeName, String methodName, boolean isConstructor, String bodyStatement, String lineDelimiter) throws CoreException {
153                 return StubUtility.getMethodBodyContent(isConstructor, cu.getJavaProject(), declaringTypeName, methodName, bodyStatement, lineDelimiter);
154         }
155         
156         /**
157          * Returns the content of body for a getter method using the getter method body template.
158          * <code>null</code> is returned if the template is empty.
159          * <p>The returned string is unformatted and not indented.
160          * 
161          * @param cu The compilation unit to which the method belongs. The compilation unit does not need to exist.
162          * @param declaringTypeName Name of the type to which the method belongs. For inner types the name must be qualified and include the outer
163          * types names (dot separated). See {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
164          * @param methodName The name of the getter method.
165          * @param fieldName The name of the field to get in the getter method, corresponding to the template variable for ${field}. 
166          * @param lineDelimiter The line delimiter to be used.
167          * @return Returns the constructed body content or <code>null</code> if
168          * the comment code template is empty. The returned string is unformatted and and has no indent (formatting required).
169          * @throws CoreException Thrown when the evaluation of the code template fails.
170          * @since 3.0
171          */     
172         public static String getGetterMethodBodyContent(ICompilationUnit cu, String declaringTypeName, String methodName, String fieldName, String lineDelimiter) throws CoreException {
173                 return StubUtility.getGetterMethodBodyContent(cu.getJavaProject(), declaringTypeName, methodName, fieldName, lineDelimiter);
174         }
175         
176         /**
177          * Returns the content of body for a setter method using the setter method body template.
178          * <code>null</code> is returned if the template is empty.
179          * <p>The returned string is unformatted and not indented.
180          * 
181          * @param cu The compilation unit to which the method belongs. The compilation unit does not need to exist.
182          * @param declaringTypeName Name of the type to which the method belongs. For inner types the name must be qualified and include the outer
183          * types names (dot separated). See {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
184          * @param methodName The name of the setter method.
185          * @param fieldName The name of the field to be set in the setter method, corresponding to the template variable for ${field}.
186          * @param paramName The name of the parameter passed to the setter method, corresponding to the template variable for $(param).
187          * @param lineDelimiter The line delimiter to be used.
188          * @return Returns the constructed body content or <code>null</code> if
189          * the comment code template is empty. The returned string is unformatted and and has no indent (formatting required).
190          * @throws CoreException Thrown when the evaluation of the code template fails.
191          * @since 3.0
192          */     
193         public static String getSetterMethodBodyContent(ICompilationUnit cu, String declaringTypeName, String methodName, String fieldName, String paramName, String lineDelimiter) throws CoreException {
194                 return StubUtility.getSetterMethodBodyContent(cu.getJavaProject(), declaringTypeName, methodName, fieldName, paramName, lineDelimiter);
195         }
196         
197         /**
198          * Returns the comment for a getter method using the getter comment template.
199          * <code>null</code> is returned if the template is empty.
200          * <p>The returned string is unformatted and not indented.
201          * 
202          * @param cu The compilation unit to which the method belongs. The compilation unit does not need to exist.
203          * @param declaringTypeName Name of the type to which the method belongs. For inner types the name must be qualified and include the outer
204          * types names (dot separated). See {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
205          * @param methodName Name of the method.
206          * @param fieldName Name of the field to get.
207          * @param fieldType The type of the field to get.
208          * @param bareFieldName The field name without prefix or suffix.
209          * @param lineDelimiter The line delimiter to be used.
210          * @return Returns the generated getter comment or <code>null</code> if the
211          * code template is empty. The returned content is not indented.
212          * @throws CoreException Thrown when the evaluation of the code template fails.
213          * @since 3.0
214          */
215         public static String getGetterComment(ICompilationUnit cu, String declaringTypeName, String methodName, String fieldName, String fieldType, String bareFieldName, String lineDelimiter) throws CoreException {
216                 return StubUtility.getGetterComment(cu, declaringTypeName, methodName, fieldName, fieldType, bareFieldName, lineDelimiter);
217         }
218         
219         /**
220          * Returns the comment for a setter method using the setter method body template.
221          * <code>null</code> is returned if the template is empty.
222          * <p>The returned string is unformatted and not indented.
223          * 
224          * @param cu The compilation unit to which the method belongs. The compilation unit does not need to exist.
225          * @param declaringTypeName Name of the type to which the method belongs. For inner types the name must be qualified and include the outer
226          * types names (dot separated). See {@link net.sourceforge.phpdt.core.IType#getTypeQualifiedName(char)}.
227          * @param methodName Name of the method.
228          * @param fieldName Name of the field that is set.
229          * @param fieldType The type of the field that is to set.
230          * @param paramName The name of the parameter that used to set.
231          * @param bareFieldName The field name without prefix or suffix.
232          * @param lineDelimiter The line delimiter to be used.
233          * @return Returns the generated setter comment or <code>null</code> if the
234          * code template is empty. The returned comment is not indented.
235          * @throws CoreException Thrown when the evaluation of the code template fails.
236          * @since 3.0
237          */
238         public static String getSetterComment(ICompilationUnit cu, String declaringTypeName, String methodName, String fieldName, String fieldType, String paramName, String bareFieldName, String lineDelimiter) throws CoreException {
239                 return StubUtility.getSetterComment(cu, declaringTypeName, methodName, fieldName, fieldType, paramName, bareFieldName, lineDelimiter);
240         }               
241 }