KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > codemanipulation > GetterSetterUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.codemanipulation;
12
13 import org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.jdt.core.Flags;
16 import org.eclipse.jdt.core.IField;
17 import org.eclipse.jdt.core.IJavaProject;
18 import org.eclipse.jdt.core.IMethod;
19 import org.eclipse.jdt.core.IType;
20 import org.eclipse.jdt.core.JavaModelException;
21 import org.eclipse.jdt.core.NamingConventions;
22 import org.eclipse.jdt.core.Signature;
23 import org.eclipse.jdt.core.dom.IVariableBinding;
24
25 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
26 import org.eclipse.jdt.internal.corext.util.JdtFlags;
27
28 import org.eclipse.jdt.ui.CodeGeneration;
29
30 public class GetterSetterUtil {
31     
32     private static final String JavaDoc[] EMPTY= new String JavaDoc[0];
33     
34     //no instances
35
private GetterSetterUtil(){
36     }
37     
38     public static String JavaDoc getGetterName(IField field, String JavaDoc[] excludedNames) throws JavaModelException {
39         boolean useIs= StubUtility.useIsForBooleanGetters(field.getJavaProject());
40         return getGetterName(field, excludedNames, useIs);
41     }
42     
43     private static String JavaDoc getGetterName(IField field, String JavaDoc[] 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 JavaDoc getGetterName(IVariableBinding variableType, IJavaProject project, String JavaDoc[] excludedNames, boolean isBoolean) {
51         boolean useIs= StubUtility.useIsForBooleanGetters(project) && isBoolean;
52         return getGetterName(project, variableType.getName(), variableType.getModifiers(), useIs, excludedNames);
53     }
54     
55     public static String JavaDoc getGetterName(IJavaProject project, String JavaDoc fieldName, int flags, boolean isBoolean, String JavaDoc[] excludedNames){
56         return NamingConventions.suggestGetterName(project, fieldName, flags, isBoolean, excludedNames);
57     }
58
59     public static String JavaDoc getSetterName(IVariableBinding variableType, IJavaProject project, String JavaDoc[] excludedNames, boolean isBoolean) {
60         return getSetterName(project, variableType.getName(), variableType.getModifiers(), isBoolean, excludedNames);
61     }
62     
63     public static String JavaDoc getSetterName(IJavaProject project, String JavaDoc fieldName, int flags, boolean isBoolean, String JavaDoc[] excludedNames){
64         return NamingConventions.suggestSetterName(project, fieldName, flags, isBoolean, excludedNames);
65     }
66
67     public static String JavaDoc getSetterName(IField field, String JavaDoc[] excludedNames) throws JavaModelException {
68         if (excludedNames == null) {
69             excludedNames= EMPTY;
70         }
71         return NamingConventions.suggestSetterName(field.getJavaProject(), field.getElementName(), field.getFlags(), JavaModelUtil.isBoolean(field), excludedNames);
72     }
73
74     public static IMethod getGetter(IField field) throws JavaModelException{
75         String JavaDoc getterName= getGetterName(field, EMPTY, true);
76         IMethod primaryCandidate= JavaModelUtil.findMethod(getterName, new String JavaDoc[0], false, field.getDeclaringType());
77         if (! JavaModelUtil.isBoolean(field) || (primaryCandidate != null && primaryCandidate.exists()))
78             return primaryCandidate;
79         //bug 30906 describes why we need to look for other alternatives here (try with get... for booleans)
80
String JavaDoc secondCandidateName= getGetterName(field, EMPTY, false);
81         return JavaModelUtil.findMethod(secondCandidateName, new String JavaDoc[0], false, field.getDeclaringType());
82     }
83     
84     public static IMethod getSetter(IField field) throws JavaModelException{
85         String JavaDoc[] args= new String JavaDoc[] { field.getTypeSignature() };
86         return JavaModelUtil.findMethod(getSetterName(field, EMPTY), args, false, field.getDeclaringType());
87     }
88     
89     /**
90      * Create a stub for a getter of the given field using getter/setter templates. The resulting code
91      * has to be formatted and indented.
92      * @param field The field to create a getter for
93      * @param setterName The chosen name for the setter
94      * @param addComments If <code>true</code>, comments will be added.
95      * @param flags The flags signaling visibility, if static, synchronized or final
96      * @return Returns the generated stub.
97      * @throws CoreException
98      */

99     public static String JavaDoc getSetterStub(IField field, String JavaDoc setterName, boolean addComments, int flags) throws CoreException {
100         
101         String JavaDoc fieldName= field.getElementName();
102         IType parentType= field.getDeclaringType();
103         
104         String JavaDoc returnSig= field.getTypeSignature();
105         String JavaDoc typeName= Signature.toString(returnSig);
106         
107         IJavaProject project= field.getJavaProject();
108
109         String JavaDoc accessorName = NamingConventions.removePrefixAndSuffixForFieldName(project, fieldName, field.getFlags());
110         String JavaDoc argname= StubUtility.suggestArgumentName(project, accessorName, EMPTY);
111
112         boolean isStatic= Flags.isStatic(flags);
113         boolean isSync= Flags.isSynchronized(flags);
114         boolean isFinal= Flags.isFinal(flags);
115
116         String JavaDoc lineDelim= "\n"; // Use default line delimiter, as generated stub has to be formatted anyway //$NON-NLS-1$
117
StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
118         if (addComments) {
119             String JavaDoc comment= CodeGeneration.getSetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, field.getElementName(), typeName, argname, accessorName, lineDelim);
120             if (comment != null) {
121                 buf.append(comment);
122                 buf.append(lineDelim);
123             }
124         }
125         buf.append(JdtFlags.getVisibilityString(flags));
126         buf.append(' ');
127         if (isStatic)
128             buf.append("static "); //$NON-NLS-1$
129
if (isSync)
130             buf.append("synchronized "); //$NON-NLS-1$
131
if (isFinal)
132             buf.append("final "); //$NON-NLS-1$
133

134         buf.append("void "); //$NON-NLS-1$
135
buf.append(setterName);
136         buf.append('(');
137         buf.append(typeName);
138         buf.append(' ');
139         buf.append(argname);
140         buf.append(") {"); //$NON-NLS-1$
141
buf.append(lineDelim);
142         
143         boolean useThis= StubUtility.useThisForFieldAccess(project);
144         if (argname.equals(fieldName) || (useThis && !isStatic)) {
145             if (isStatic)
146                 fieldName= parentType.getElementName() + '.' + fieldName;
147             else
148                 fieldName= "this." + fieldName; //$NON-NLS-1$
149
}
150         String JavaDoc body= CodeGeneration.getSetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, fieldName, argname, lineDelim);
151         if (body != null) {
152             buf.append(body);
153         }
154         buf.append("}"); //$NON-NLS-1$
155
buf.append(lineDelim);
156         return buf.toString();
157     }
158     
159     /**
160      * Create a stub for a getter of the given field using getter/setter templates. The resulting code
161      * has to be formatted and indented.
162      * @param field The field to create a getter for
163      * @param getterName The chosen name for the getter
164      * @param addComments If <code>true</code>, comments will be added.
165      * @param flags The flags signaling visibility, if static, synchronized or final
166      * @return Returns the generated stub.
167      * @throws CoreException
168      */

169     public static String JavaDoc getGetterStub(IField field, String JavaDoc getterName, boolean addComments, int flags) throws CoreException {
170         String JavaDoc fieldName= field.getElementName();
171         IType parentType= field.getDeclaringType();
172         
173         boolean isStatic= Flags.isStatic(flags);
174         boolean isSync= Flags.isSynchronized(flags);
175         boolean isFinal= Flags.isFinal(flags);
176         
177         String JavaDoc typeName= Signature.toString(field.getTypeSignature());
178         String JavaDoc accessorName = NamingConventions.removePrefixAndSuffixForFieldName(field.getJavaProject(), fieldName, field.getFlags());
179
180         String JavaDoc lineDelim= "\n"; // Use default line delimiter, as generated stub has to be formatted anyway //$NON-NLS-1$
181
StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
182         if (addComments) {
183             String JavaDoc comment= CodeGeneration.getGetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, field.getElementName(), typeName, accessorName, lineDelim);
184             if (comment != null) {
185                 buf.append(comment);
186                 buf.append(lineDelim);
187             }
188         }
189         
190         buf.append(JdtFlags.getVisibilityString(flags));
191         buf.append(' ');
192         if (isStatic)
193             buf.append("static "); //$NON-NLS-1$
194
if (isSync)
195             buf.append("synchronized "); //$NON-NLS-1$
196
if (isFinal)
197             buf.append("final "); //$NON-NLS-1$
198

199         buf.append(typeName);
200         buf.append(' ');
201         buf.append(getterName);
202         buf.append("() {"); //$NON-NLS-1$
203
buf.append(lineDelim);
204         
205         boolean useThis= StubUtility.useThisForFieldAccess(field.getJavaProject());
206         if (useThis && !isStatic) {
207             fieldName= "this." + fieldName; //$NON-NLS-1$
208
}
209         
210         String JavaDoc body= CodeGeneration.getGetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, fieldName, lineDelim);
211         if (body != null) {
212             buf.append(body);
213         }
214         buf.append("}"); //$NON-NLS-1$
215
buf.append(lineDelim);
216         return buf.toString();
217     }
218
219
220 }
221
Popular Tags