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) throws JavaModelException {
37 boolean useIs= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_IS_FOR_GETTERS);
38 return getGetterName(field, excludedNames, useIs);
41 private static String getGetterName(IField field, String[] excludedNames, boolean useIsForBoolGetters) throws JavaModelException {
42 if (excludedNames == null) {
45 return getGetterName(field.getJavaProject(), field.getElementName(), field.getFlags(), useIsForBoolGetters && JavaModelUtil.isBoolean(field), excludedNames);
48 public static String getGetterName(IJavaProject project, String fieldName, int flags, boolean isBoolean, String[] excludedNames){
49 return NamingConventions.suggestGetterName(project, fieldName, flags, isBoolean, excludedNames);
52 public static String getSetterName(IJavaProject project, String fieldName, int flags, boolean isBoolean, String[] excludedNames){
53 return NamingConventions.suggestSetterName(project, fieldName, flags, isBoolean, excludedNames);
56 public static String getSetterName(IField field, String[] excludedNames) throws JavaModelException {
57 if (excludedNames == null) {
60 return NamingConventions.suggestSetterName(field.getJavaProject(), field.getElementName(), field.getFlags(), JavaModelUtil.isBoolean(field), excludedNames);
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());
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());
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
87 public static String getSetterStub(IField field, String setterName, boolean addComments, int flags) throws CoreException {
89 String fieldName= field.getElementName();
90 IType parentType= field.getDeclaringType();
92 String returnSig= field.getTypeSignature();
93 String typeName= Signature.toString(returnSig);
95 IJavaProject project= field.getJavaProject();
97 String accessorName = NamingConventions.removePrefixAndSuffixForFieldName(project, fieldName, field.getFlags());
98 String argname= StubUtility.suggestArgumentName(project, accessorName, EMPTY);
100 boolean isStatic= Flags.isStatic(flags);
101 // boolean isSync= Flags.isSynchronized(flags);
102 boolean isFinal= Flags.isFinal(flags);
104 // create the setter stub
105 StringBuffer buf= new StringBuffer();
107 String comment= CodeGeneration.getSetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, field.getElementName(), typeName, argname, accessorName, String.valueOf('\n'));
108 if (comment != null) {
113 buf.append(JdtFlags.getVisibilityString(flags));
116 buf.append("static "); //$NON-NLS-1$
118 // buf.append("synchronized "); //$NON-NLS-1$
120 buf.append("final "); //$NON-NLS-1$
122 buf.append("void "); //$NON-NLS-1$
123 buf.append(setterName);
125 buf.append(typeName);
128 buf.append(") {\n"); //$NON-NLS-1$
130 boolean useThis= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_KEYWORD_THIS);
131 if (argname.equals(fieldName) || (useThis && !isStatic)) {
133 fieldName= parentType.getElementName() + '.' + fieldName;
135 fieldName= "this." + fieldName; //$NON-NLS-1$
137 String body= CodeGeneration.getSetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, fieldName, argname, String.valueOf('\n'));
141 buf.append("}\n"); //$NON-NLS-1$
142 return buf.toString();
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
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();
159 boolean isStatic= Flags.isStatic(flags);
160 // boolean isSync= Flags.isSynchronized(flags);
161 boolean isFinal= Flags.isFinal(flags);
163 String typeName= Signature.toString(field.getTypeSignature());
164 String accessorName = NamingConventions.removePrefixAndSuffixForFieldName(field.getJavaProject(), fieldName, field.getFlags());
166 // create the getter stub
167 StringBuffer buf= new StringBuffer();
169 String comment= CodeGeneration.getGetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, field.getElementName(), typeName, accessorName, String.valueOf('\n'));
170 if (comment != null) {
176 buf.append(JdtFlags.getVisibilityString(flags));
179 buf.append("static "); //$NON-NLS-1$
181 // buf.append("synchronized "); //$NON-NLS-1$
183 buf.append("final "); //$NON-NLS-1$
185 buf.append(typeName);
187 buf.append(getterName);
188 buf.append("() {\n"); //$NON-NLS-1$
190 boolean useThis= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEGEN_KEYWORD_THIS);
191 if (useThis && !isStatic) {
192 fieldName= "this." + fieldName; //$NON-NLS-1$
195 String body= CodeGeneration.getGetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, fieldName, String.valueOf('\n'));
199 buf.append("}\n"); //$NON-NLS-1$
200 return buf.toString();