Organized imports
[phpeclipse.git] / net.sourceforge.phpeclipse / 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) throws JavaModelException {
37                 boolean useIs= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_IS_FOR_GETTERS);
38                 return getGetterName(field, excludedNames, useIs);
39         }
40         
41         private static String getGetterName(IField field, String[] excludedNames, boolean useIsForBoolGetters) throws JavaModelException {
42                 if (excludedNames == null) {
43                         excludedNames= EMPTY;
44                 }
45                 return getGetterName(field.getJavaProject(), field.getElementName(), field.getFlags(), useIsForBoolGetters && JavaModelUtil.isBoolean(field), excludedNames);
46         }       
47         
48         public static String getGetterName(IJavaProject project, String fieldName, int flags, boolean isBoolean, String[] excludedNames){
49                 return NamingConventions.suggestGetterName(project, fieldName, flags, isBoolean, excludedNames);        
50         }
51
52         public static String getSetterName(IJavaProject project, String fieldName, int flags, boolean isBoolean, String[] excludedNames){
53                 return NamingConventions.suggestSetterName(project, fieldName, flags, isBoolean, excludedNames);        
54         }
55
56         public static String getSetterName(IField field, String[] excludedNames) throws JavaModelException {
57                 if (excludedNames == null) {
58                         excludedNames= EMPTY;
59                 }               
60                 return NamingConventions.suggestSetterName(field.getJavaProject(), field.getElementName(), field.getFlags(), JavaModelUtil.isBoolean(field), excludedNames);
61         }       
62
63         public static IMethod getGetter(IField field) throws JavaModelException{
64                 IMethod primaryCandidate= JavaModelUtil.findMethod(getGetterName(field, EMPTY, true), new String[0], false, field.getDeclaringType());
65                 if (! JavaModelUtil.isBoolean(field) || (primaryCandidate != null && primaryCandidate.exists()))
66                         return primaryCandidate;
67                 //bug 30906 describes why we need to look for other alternatives here
68                 String secondCandidateName= getGetterName(field, EMPTY, false);
69                 return JavaModelUtil.findMethod(secondCandidateName, new String[0], false, field.getDeclaringType());
70         }
71         
72         public static IMethod getSetter(IField field) throws JavaModelException{
73                 String[] args= new String[] { field.getTypeSignature() };       
74                 return JavaModelUtil.findMethod(getSetterName(field, EMPTY), args, false, field.getDeclaringType());
75         }
76         
77         /**
78          * Create a stub for a getter of the given field using getter/setter templates. The resulting code
79          * has to be formatted and indented.
80          * @param field The field to create a getter for
81          * @param setterName The chosen name for the setter
82          * @param addComments If <code>true</code>, comments will be added.
83          * @param flags The flags signaling visibility, if static, synchronized or final
84          * @return Returns the generated stub.
85          * @throws CoreException
86          */
87         public static String getSetterStub(IField field, String setterName, boolean addComments, int flags) throws CoreException {
88                 
89                 String fieldName= field.getElementName();
90                 IType parentType= field.getDeclaringType();
91                 
92                 String returnSig= field.getTypeSignature();
93                 String typeName= Signature.toString(returnSig);
94                 
95                 IJavaProject project= field.getJavaProject();
96
97                 String accessorName = NamingConventions.removePrefixAndSuffixForFieldName(project, fieldName, field.getFlags());
98                 String argname= StubUtility.suggestArgumentName(project, accessorName, EMPTY);
99
100                 boolean isStatic= Flags.isStatic(flags);
101 //              boolean isSync= Flags.isSynchronized(flags);
102                 boolean isFinal= Flags.isFinal(flags);
103                 
104                 // create the setter stub
105                 StringBuffer buf= new StringBuffer();
106                 if (addComments) {
107                         String comment= CodeGeneration.getSetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, field.getElementName(), typeName, argname, accessorName, String.valueOf('\n'));
108                         if (comment != null) {
109                                 buf.append(comment);
110                                 buf.append('\n');
111                         }
112                 }
113                 buf.append(JdtFlags.getVisibilityString(flags));
114                 buf.append(' ');        
115                 if (isStatic)
116                         buf.append("static "); //$NON-NLS-1$
117 //              if (isSync)
118 //                      buf.append("synchronized "); //$NON-NLS-1$
119                 if (isFinal)
120                         buf.append("final "); //$NON-NLS-1$                             
121                         
122                 buf.append("void "); //$NON-NLS-1$
123                 buf.append(setterName);
124                 buf.append('('); 
125                 buf.append(typeName); 
126                 buf.append(' '); 
127                 buf.append(argname); 
128                 buf.append(") {\n"); //$NON-NLS-1$
129                 
130                 boolean useThis= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_KEYWORD_THIS);
131                 if (argname.equals(fieldName) || (useThis && !isStatic)) {
132                         if (isStatic)
133                                 fieldName= parentType.getElementName() + '.' + fieldName;
134                         else
135                                 fieldName= "this." + fieldName; //$NON-NLS-1$
136                 }
137                 String body= CodeGeneration.getSetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, fieldName, argname, String.valueOf('\n'));
138                 if (body != null) {
139                         buf.append(body);
140                 }
141                 buf.append("}\n"); //$NON-NLS-1$                
142                 return buf.toString();
143         }
144         
145         /**
146          * Create a stub for a getter of the given field using getter/setter templates. The resulting code
147          * has to be formatted and indented.
148          * @param field The field to create a getter for
149          * @param getterName The chosen name for the getter
150          * @param addComments If <code>true</code>, comments will be added.
151          * @param flags The flags signaling visibility, if static, synchronized or final
152          * @return Returns the generated stub.
153          * @throws CoreException
154          */
155         public static String getGetterStub(IField field, String getterName, boolean addComments, int flags) throws CoreException {
156                 String fieldName= field.getElementName();
157                 IType parentType= field.getDeclaringType();
158                 
159                 boolean isStatic= Flags.isStatic(flags);
160 //              boolean isSync= Flags.isSynchronized(flags);
161                 boolean isFinal= Flags.isFinal(flags);
162                 
163                 String typeName= Signature.toString(field.getTypeSignature());
164                 String accessorName = NamingConventions.removePrefixAndSuffixForFieldName(field.getJavaProject(), fieldName, field.getFlags());
165
166                 // create the getter stub
167                 StringBuffer buf= new StringBuffer();
168                 if (addComments) {
169                         String comment= CodeGeneration.getGetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, field.getElementName(), typeName, accessorName, String.valueOf('\n'));
170                         if (comment != null) {
171                                 buf.append(comment);
172                                 buf.append('\n');
173                         }                                       
174                 }
175                 
176                 buf.append(JdtFlags.getVisibilityString(flags));
177                 buf.append(' ');                        
178                 if (isStatic)
179                         buf.append("static "); //$NON-NLS-1$
180 //              if (isSync)
181 //                      buf.append("synchronized "); //$NON-NLS-1$
182                 if (isFinal)
183                         buf.append("final "); //$NON-NLS-1$
184                         
185                 buf.append(typeName);
186                 buf.append(' ');
187                 buf.append(getterName);
188                 buf.append("() {\n"); //$NON-NLS-1$
189                 
190                 boolean useThis= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_KEYWORD_THIS);
191                 if (useThis && !isStatic) {
192                         fieldName= "this." + fieldName; //$NON-NLS-1$
193                 }
194                 
195                 String body= CodeGeneration.getGetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, fieldName, String.valueOf('\n'));
196                 if (body != null) {
197                         buf.append(body);
198                 }
199                 buf.append("}\n"); //$NON-NLS-1$
200                 return buf.toString(); 
201         }
202
203 }