refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / corext / codemanipulation / GetterSetterUtil.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.internal.corext.codemanipulation;
12
13 import net.sourceforge.phpdt.core.Flags;
14 import net.sourceforge.phpdt.core.IField;
15 import net.sourceforge.phpdt.core.IJavaProject;
16 import net.sourceforge.phpdt.core.IMethod;
17 import net.sourceforge.phpdt.core.IType;
18 import net.sourceforge.phpdt.core.JavaModelException;
19 import net.sourceforge.phpdt.core.NamingConventions;
20 import net.sourceforge.phpdt.core.Signature;
21 import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
22 import net.sourceforge.phpdt.internal.corext.util.JdtFlags;
23 import net.sourceforge.phpdt.ui.CodeGeneration;
24 import net.sourceforge.phpdt.ui.PreferenceConstants;
25
26 import org.eclipse.core.runtime.CoreException;
27
28 public class GetterSetterUtil {
29
30         private static final String[] EMPTY = new String[0];
31
32         // no instances
33         private GetterSetterUtil() {
34         }
35
36         public static String getGetterName(IField field, String[] excludedNames)
37                         throws JavaModelException {
38                 boolean useIs = PreferenceConstants.getPreferenceStore().getBoolean(
39                                 PreferenceConstants.CODEGEN_IS_FOR_GETTERS);
40                 return getGetterName(field, excludedNames, useIs);
41         }
42
43         private static String getGetterName(IField field, String[] excludedNames,
44                         boolean useIsForBoolGetters) throws JavaModelException {
45                 if (excludedNames == null) {
46                         excludedNames = EMPTY;
47                 }
48                 return getGetterName(field.getJavaProject(), field.getElementName(),
49                                 field.getFlags(), useIsForBoolGetters
50                                                 && JavaModelUtil.isBoolean(field), excludedNames);
51         }
52
53         public static String getGetterName(IJavaProject project, String fieldName,
54                         int flags, boolean isBoolean, String[] excludedNames) {
55                 return NamingConventions.suggestGetterName(project, fieldName, flags,
56                                 isBoolean, excludedNames);
57         }
58
59         public static String getSetterName(IJavaProject project, String fieldName,
60                         int flags, boolean isBoolean, String[] excludedNames) {
61                 return NamingConventions.suggestSetterName(project, fieldName, flags,
62                                 isBoolean, excludedNames);
63         }
64
65         public static String getSetterName(IField field, String[] excludedNames)
66                         throws JavaModelException {
67                 if (excludedNames == null) {
68                         excludedNames = EMPTY;
69                 }
70                 return NamingConventions.suggestSetterName(field.getJavaProject(),
71                                 field.getElementName(), field.getFlags(), JavaModelUtil
72                                                 .isBoolean(field), excludedNames);
73         }
74
75         public static IMethod getGetter(IField field) throws JavaModelException {
76                 IMethod primaryCandidate = JavaModelUtil.findMethod(getGetterName(
77                                 field, EMPTY, true), new String[0], false, field
78                                 .getDeclaringType());
79                 if (!JavaModelUtil.isBoolean(field)
80                                 || (primaryCandidate != null && primaryCandidate.exists()))
81                         return primaryCandidate;
82                 // bug 30906 describes why we need to look for other alternatives here
83                 String secondCandidateName = getGetterName(field, EMPTY, false);
84                 return JavaModelUtil.findMethod(secondCandidateName, new String[0],
85                                 false, field.getDeclaringType());
86         }
87
88         public static IMethod getSetter(IField field) throws JavaModelException {
89                 String[] args = new String[] { field.getTypeSignature() };
90                 return JavaModelUtil.findMethod(getSetterName(field, EMPTY), args,
91                                 false, field.getDeclaringType());
92         }
93
94         /**
95          * Create a stub for a getter of the given field using getter/setter
96          * templates. The resulting code has to be formatted and indented.
97          * 
98          * @param field
99          *            The field to create a getter for
100          * @param setterName
101          *            The chosen name for the setter
102          * @param addComments
103          *            If <code>true</code>, comments will be added.
104          * @param flags
105          *            The flags signaling visibility, if static, synchronized or
106          *            final
107          * @return Returns the generated stub.
108          * @throws CoreException
109          */
110         public static String getSetterStub(IField field, String setterName,
111                         boolean addComments, int flags) throws CoreException {
112
113                 String fieldName = field.getElementName();
114                 IType parentType = field.getDeclaringType();
115
116                 String returnSig = field.getTypeSignature();
117                 String typeName = Signature.toString(returnSig);
118
119                 IJavaProject project = field.getJavaProject();
120
121                 String accessorName = NamingConventions
122                                 .removePrefixAndSuffixForFieldName(project, fieldName, field
123                                                 .getFlags());
124                 String argname = StubUtility.suggestArgumentName(project, accessorName,
125                                 EMPTY);
126
127                 boolean isStatic = Flags.isStatic(flags);
128                 // boolean isSync= Flags.isSynchronized(flags);
129                 boolean isFinal = Flags.isFinal(flags);
130
131                 // create the setter stub
132                 StringBuffer buf = new StringBuffer();
133                 if (addComments) {
134                         String comment = CodeGeneration.getSetterComment(field
135                                         .getCompilationUnit(),
136                                         parentType.getTypeQualifiedName('.'), setterName, field
137                                                         .getElementName(), typeName, argname, accessorName,
138                                         String.valueOf('\n'));
139                         if (comment != null) {
140                                 buf.append(comment);
141                                 buf.append('\n');
142                         }
143                 }
144                 buf.append(JdtFlags.getVisibilityString(flags));
145                 buf.append(' ');
146                 if (isStatic)
147                         buf.append("static "); //$NON-NLS-1$
148                         // if (isSync)
149                         // buf.append("synchronized "); //$NON-NLS-1$
150                 if (isFinal)
151                         buf.append("final "); //$NON-NLS-1$                             
152
153                 buf.append("void "); //$NON-NLS-1$
154                 buf.append(setterName);
155                 buf.append('(');
156                 buf.append(typeName);
157                 buf.append(' ');
158                 buf.append(argname);
159                 buf.append(") {\n"); //$NON-NLS-1$
160
161                 boolean useThis = PreferenceConstants.getPreferenceStore().getBoolean(
162                                 PreferenceConstants.CODEGEN_KEYWORD_THIS);
163                 if (argname.equals(fieldName) || (useThis && !isStatic)) {
164                         if (isStatic)
165                                 fieldName = parentType.getElementName() + '.' + fieldName;
166                         else
167                                 fieldName = "this." + fieldName; //$NON-NLS-1$
168                 }
169                 String body = CodeGeneration.getSetterMethodBodyContent(field
170                                 .getCompilationUnit(), parentType.getTypeQualifiedName('.'),
171                                 setterName, fieldName, argname, String.valueOf('\n'));
172                 if (body != null) {
173                         buf.append(body);
174                 }
175                 buf.append("}\n"); //$NON-NLS-1$                
176                 return buf.toString();
177         }
178
179         /**
180          * Create a stub for a getter of the given field using getter/setter
181          * templates. The resulting code has to be formatted and indented.
182          * 
183          * @param field
184          *            The field to create a getter for
185          * @param getterName
186          *            The chosen name for the getter
187          * @param addComments
188          *            If <code>true</code>, comments will be added.
189          * @param flags
190          *            The flags signaling visibility, if static, synchronized or
191          *            final
192          * @return Returns the generated stub.
193          * @throws CoreException
194          */
195         public static String getGetterStub(IField field, String getterName,
196                         boolean addComments, int flags) throws CoreException {
197                 String fieldName = field.getElementName();
198                 IType parentType = field.getDeclaringType();
199
200                 boolean isStatic = Flags.isStatic(flags);
201                 // boolean isSync= Flags.isSynchronized(flags);
202                 boolean isFinal = Flags.isFinal(flags);
203
204                 String typeName = Signature.toString(field.getTypeSignature());
205                 String accessorName = NamingConventions
206                                 .removePrefixAndSuffixForFieldName(field.getJavaProject(),
207                                                 fieldName, field.getFlags());
208
209                 // create the getter stub
210                 StringBuffer buf = new StringBuffer();
211                 if (addComments) {
212                         String comment = CodeGeneration.getGetterComment(field
213                                         .getCompilationUnit(),
214                                         parentType.getTypeQualifiedName('.'), getterName, field
215                                                         .getElementName(), typeName, accessorName, String
216                                                         .valueOf('\n'));
217                         if (comment != null) {
218                                 buf.append(comment);
219                                 buf.append('\n');
220                         }
221                 }
222
223                 buf.append(JdtFlags.getVisibilityString(flags));
224                 buf.append(' ');
225                 if (isStatic)
226                         buf.append("static "); //$NON-NLS-1$
227                         // if (isSync)
228                         // buf.append("synchronized "); //$NON-NLS-1$
229                 if (isFinal)
230                         buf.append("final "); //$NON-NLS-1$
231
232                 buf.append(typeName);
233                 buf.append(' ');
234                 buf.append(getterName);
235                 buf.append("() {\n"); //$NON-NLS-1$
236
237                 boolean useThis = PreferenceConstants.getPreferenceStore().getBoolean(
238                                 PreferenceConstants.CODEGEN_KEYWORD_THIS);
239                 if (useThis && !isStatic) {
240                         fieldName = "this." + fieldName; //$NON-NLS-1$
241                 }
242
243                 String body = CodeGeneration.getGetterMethodBodyContent(field
244                                 .getCompilationUnit(), parentType.getTypeQualifiedName('.'),
245                                 getterName, fieldName, String.valueOf('\n'));
246                 if (body != null) {
247                         buf.append(body);
248                 }
249                 buf.append("}\n"); //$NON-NLS-1$
250                 return buf.toString();
251         }
252
253 }