1 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 [] EMPTY= new String [0]; 33 34 private GetterSetterUtil(){ 36 } 37 38 public static String getGetterName(IField field, String [] excludedNames) throws JavaModelException { 39 boolean useIs= StubUtility.useIsForBooleanGetters(field.getJavaProject()); 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(IVariableBinding variableType, IJavaProject project, String [] 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 getGetterName(IJavaProject project, String fieldName, int flags, boolean isBoolean, String [] excludedNames){ 56 return NamingConventions.suggestGetterName(project, fieldName, flags, isBoolean, excludedNames); 57 } 58 59 public static String getSetterName(IVariableBinding variableType, IJavaProject project, String [] excludedNames, boolean isBoolean) { 60 return getSetterName(project, variableType.getName(), variableType.getModifiers(), isBoolean, excludedNames); 61 } 62 63 public static String getSetterName(IJavaProject project, String fieldName, int flags, boolean isBoolean, String [] excludedNames){ 64 return NamingConventions.suggestSetterName(project, fieldName, flags, isBoolean, excludedNames); 65 } 66 67 public static String getSetterName(IField field, String [] 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 getterName= getGetterName(field, EMPTY, true); 76 IMethod primaryCandidate= JavaModelUtil.findMethod(getterName, new String [0], false, field.getDeclaringType()); 77 if (! JavaModelUtil.isBoolean(field) || (primaryCandidate != null && primaryCandidate.exists())) 78 return primaryCandidate; 79 String secondCandidateName= getGetterName(field, EMPTY, false); 81 return JavaModelUtil.findMethod(secondCandidateName, new String [0], false, field.getDeclaringType()); 82 } 83 84 public static IMethod getSetter(IField field) throws JavaModelException{ 85 String [] args= new String [] { field.getTypeSignature() }; 86 return JavaModelUtil.findMethod(getSetterName(field, EMPTY), args, false, field.getDeclaringType()); 87 } 88 89 99 public static String getSetterStub(IField field, String setterName, boolean addComments, int flags) throws CoreException { 100 101 String fieldName= field.getElementName(); 102 IType parentType= field.getDeclaringType(); 103 104 String returnSig= field.getTypeSignature(); 105 String typeName= Signature.toString(returnSig); 106 107 IJavaProject project= field.getJavaProject(); 108 109 String accessorName = NamingConventions.removePrefixAndSuffixForFieldName(project, fieldName, field.getFlags()); 110 String 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 lineDelim= "\n"; StringBuffer buf= new StringBuffer (); 118 if (addComments) { 119 String 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 "); if (isSync) 130 buf.append("synchronized "); if (isFinal) 132 buf.append("final "); 134 buf.append("void "); buf.append(setterName); 136 buf.append('('); 137 buf.append(typeName); 138 buf.append(' '); 139 buf.append(argname); 140 buf.append(") {"); 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; } 150 String body= CodeGeneration.getSetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, fieldName, argname, lineDelim); 151 if (body != null) { 152 buf.append(body); 153 } 154 buf.append("}"); buf.append(lineDelim); 156 return buf.toString(); 157 } 158 159 169 public static String getGetterStub(IField field, String getterName, boolean addComments, int flags) throws CoreException { 170 String 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 typeName= Signature.toString(field.getTypeSignature()); 178 String accessorName = NamingConventions.removePrefixAndSuffixForFieldName(field.getJavaProject(), fieldName, field.getFlags()); 179 180 String lineDelim= "\n"; StringBuffer buf= new StringBuffer (); 182 if (addComments) { 183 String 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 "); if (isSync) 195 buf.append("synchronized "); if (isFinal) 197 buf.append("final "); 199 buf.append(typeName); 200 buf.append(' '); 201 buf.append(getterName); 202 buf.append("() {"); buf.append(lineDelim); 204 205 boolean useThis= StubUtility.useThisForFieldAccess(field.getJavaProject()); 206 if (useThis && !isStatic) { 207 fieldName= "this." + fieldName; } 209 210 String body= CodeGeneration.getGetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, fieldName, lineDelim); 211 if (body != null) { 212 buf.append(body); 213 } 214 buf.append("}"); buf.append(lineDelim); 216 return buf.toString(); 217 } 218 219 220 } 221 | Popular Tags |