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