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 org.eclipse.core.runtime.CoreException;
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;
24 import net.sourceforge.phpdt.ui.CodeGeneration;
25 import net.sourceforge.phpdt.ui.PreferenceConstants;
27 import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
28 import net.sourceforge.phpdt.internal.corext.util.JdtFlags;
30 public class GetterSetterUtil {
32 private static final String[] EMPTY= new String[0];
35 private GetterSetterUtil(){
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);
43 private static String getGetterName(IField field, String[] excludedNames, boolean useIsForBoolGetters) throws JavaModelException {
44 if (excludedNames == null) {
47 return getGetterName(field.getJavaProject(), field.getElementName(), field.getFlags(), useIsForBoolGetters && JavaModelUtil.isBoolean(field), excludedNames);
50 public static String getGetterName(IJavaProject project, String fieldName, int flags, boolean isBoolean, String[] excludedNames){
51 return NamingConventions.suggestGetterName(project, fieldName, flags, isBoolean, excludedNames);
54 public static String getSetterName(IJavaProject project, String fieldName, int flags, boolean isBoolean, String[] excludedNames){
55 return NamingConventions.suggestSetterName(project, fieldName, flags, isBoolean, excludedNames);
58 public static String getSetterName(IField field, String[] excludedNames) throws JavaModelException {
59 if (excludedNames == null) {
62 return NamingConventions.suggestSetterName(field.getJavaProject(), field.getElementName(), field.getFlags(), JavaModelUtil.isBoolean(field), excludedNames);
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());
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());
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
89 public static String getSetterStub(IField field, String setterName, boolean addComments, int flags) throws CoreException {
91 String fieldName= field.getElementName();
92 IType parentType= field.getDeclaringType();
94 String returnSig= field.getTypeSignature();
95 String typeName= Signature.toString(returnSig);
97 IJavaProject project= field.getJavaProject();
99 String accessorName = NamingConventions.removePrefixAndSuffixForFieldName(project, fieldName, field.getFlags());
100 String argname= StubUtility.suggestArgumentName(project, accessorName, EMPTY);
102 boolean isStatic= Flags.isStatic(flags);
103 // boolean isSync= Flags.isSynchronized(flags);
104 boolean isFinal= Flags.isFinal(flags);
106 // create the setter stub
107 StringBuffer buf= new StringBuffer();
109 String comment= CodeGeneration.getSetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, field.getElementName(), typeName, argname, accessorName, String.valueOf('\n'));
110 if (comment != null) {
115 buf.append(JdtFlags.getVisibilityString(flags));
118 buf.append("static "); //$NON-NLS-1$
120 // buf.append("synchronized "); //$NON-NLS-1$
122 buf.append("final "); //$NON-NLS-1$
124 buf.append("void "); //$NON-NLS-1$
125 buf.append(setterName);
127 buf.append(typeName);
130 buf.append(") {\n"); //$NON-NLS-1$
132 boolean useThis= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_KEYWORD_THIS);
133 if (argname.equals(fieldName) || (useThis && !isStatic)) {
135 fieldName= parentType.getElementName() + '.' + fieldName;
137 fieldName= "this." + fieldName; //$NON-NLS-1$
139 String body= CodeGeneration.getSetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, fieldName, argname, String.valueOf('\n'));
143 buf.append("}\n"); //$NON-NLS-1$
144 return buf.toString();
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
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();
161 boolean isStatic= Flags.isStatic(flags);
162 // boolean isSync= Flags.isSynchronized(flags);
163 boolean isFinal= Flags.isFinal(flags);
165 String typeName= Signature.toString(field.getTypeSignature());
166 String accessorName = NamingConventions.removePrefixAndSuffixForFieldName(field.getJavaProject(), fieldName, field.getFlags());
168 // create the getter stub
169 StringBuffer buf= new StringBuffer();
171 String comment= CodeGeneration.getGetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, field.getElementName(), typeName, accessorName, String.valueOf('\n'));
172 if (comment != null) {
178 buf.append(JdtFlags.getVisibilityString(flags));
181 buf.append("static "); //$NON-NLS-1$
183 // buf.append("synchronized "); //$NON-NLS-1$
185 buf.append("final "); //$NON-NLS-1$
187 buf.append(typeName);
189 buf.append(getterName);
190 buf.append("() {\n"); //$NON-NLS-1$
192 boolean useThis= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_KEYWORD_THIS);
193 if (useThis && !isStatic) {
194 fieldName= "this." + fieldName; //$NON-NLS-1$
197 String body= CodeGeneration.getGetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, fieldName, String.valueOf('\n'));
201 buf.append("}\n"); //$NON-NLS-1$
202 return buf.toString();