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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.codemanipulation;
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;
26 import org.eclipse.core.runtime.CoreException;
28 public class GetterSetterUtil {
30 private static final String[] EMPTY = new String[0];
33 private GetterSetterUtil() {
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);
43 private static String getGetterName(IField field, String[] excludedNames,
44 boolean useIsForBoolGetters) throws JavaModelException {
45 if (excludedNames == null) {
46 excludedNames = EMPTY;
48 return getGetterName(field.getJavaProject(), field.getElementName(),
49 field.getFlags(), useIsForBoolGetters
50 && JavaModelUtil.isBoolean(field), excludedNames);
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);
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);
65 public static String getSetterName(IField field, String[] excludedNames)
66 throws JavaModelException {
67 if (excludedNames == null) {
68 excludedNames = EMPTY;
70 return NamingConventions.suggestSetterName(field.getJavaProject(),
71 field.getElementName(), field.getFlags(), JavaModelUtil
72 .isBoolean(field), excludedNames);
75 public static IMethod getGetter(IField field) throws JavaModelException {
76 IMethod primaryCandidate = JavaModelUtil.findMethod(getGetterName(
77 field, EMPTY, true), new String[0], false, field
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());
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());
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.
99 * The field to create a getter for
101 * The chosen name for the setter
103 * If <code>true</code>, comments will be added.
105 * The flags signaling visibility, if static, synchronized or
107 * @return Returns the generated stub.
108 * @throws CoreException
110 public static String getSetterStub(IField field, String setterName,
111 boolean addComments, int flags) throws CoreException {
113 String fieldName = field.getElementName();
114 IType parentType = field.getDeclaringType();
116 String returnSig = field.getTypeSignature();
117 String typeName = Signature.toString(returnSig);
119 IJavaProject project = field.getJavaProject();
121 String accessorName = NamingConventions
122 .removePrefixAndSuffixForFieldName(project, fieldName, field
124 String argname = StubUtility.suggestArgumentName(project, accessorName,
127 boolean isStatic = Flags.isStatic(flags);
128 // boolean isSync= Flags.isSynchronized(flags);
129 boolean isFinal = Flags.isFinal(flags);
131 // create the setter stub
132 StringBuffer buf = new StringBuffer();
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) {
144 buf.append(JdtFlags.getVisibilityString(flags));
147 buf.append("static "); //$NON-NLS-1$
149 // buf.append("synchronized "); //$NON-NLS-1$
151 buf.append("final "); //$NON-NLS-1$
153 buf.append("void "); //$NON-NLS-1$
154 buf.append(setterName);
156 buf.append(typeName);
159 buf.append(") {\n"); //$NON-NLS-1$
161 boolean useThis = PreferenceConstants.getPreferenceStore().getBoolean(
162 PreferenceConstants.CODEGEN_KEYWORD_THIS);
163 if (argname.equals(fieldName) || (useThis && !isStatic)) {
165 fieldName = parentType.getElementName() + '.' + fieldName;
167 fieldName = "this." + fieldName; //$NON-NLS-1$
169 String body = CodeGeneration.getSetterMethodBodyContent(field
170 .getCompilationUnit(), parentType.getTypeQualifiedName('.'),
171 setterName, fieldName, argname, String.valueOf('\n'));
175 buf.append("}\n"); //$NON-NLS-1$
176 return buf.toString();
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.
184 * The field to create a getter for
186 * The chosen name for the getter
188 * If <code>true</code>, comments will be added.
190 * The flags signaling visibility, if static, synchronized or
192 * @return Returns the generated stub.
193 * @throws CoreException
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();
200 boolean isStatic = Flags.isStatic(flags);
201 // boolean isSync= Flags.isSynchronized(flags);
202 boolean isFinal = Flags.isFinal(flags);
204 String typeName = Signature.toString(field.getTypeSignature());
205 String accessorName = NamingConventions
206 .removePrefixAndSuffixForFieldName(field.getJavaProject(),
207 fieldName, field.getFlags());
209 // create the getter stub
210 StringBuffer buf = new StringBuffer();
212 String comment = CodeGeneration.getGetterComment(field
213 .getCompilationUnit(),
214 parentType.getTypeQualifiedName('.'), getterName, field
215 .getElementName(), typeName, accessorName, String
217 if (comment != null) {
223 buf.append(JdtFlags.getVisibilityString(flags));
226 buf.append("static "); //$NON-NLS-1$
228 // buf.append("synchronized "); //$NON-NLS-1$
230 buf.append("final "); //$NON-NLS-1$
232 buf.append(typeName);
234 buf.append(getterName);
235 buf.append("() {\n"); //$NON-NLS-1$
237 boolean useThis = PreferenceConstants.getPreferenceStore().getBoolean(
238 PreferenceConstants.CODEGEN_KEYWORD_THIS);
239 if (useThis && !isStatic) {
240 fieldName = "this." + fieldName; //$NON-NLS-1$
243 String body = CodeGeneration.getGetterMethodBodyContent(field
244 .getCompilationUnit(), parentType.getTypeQualifiedName('.'),
245 getterName, fieldName, String.valueOf('\n'));
249 buf.append("}\n"); //$NON-NLS-1$
250 return buf.toString();