View Javadoc
1 /* FOREGEJ - FOrmatting REfactoring GEnerating Java 2 * 3 * Copyright (C) 2003 Andreas Arrgard 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2.1 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 package com.octagroup.foregej.java.util; 20 import java.util.List; 21 import antlr.RecognitionException; 22 import com.octagroup.foregej.java.lang.ast.AST_IDENT; 23 import com.octagroup.foregej.java.lang.ast.AST_METHOD_DEF; 24 import com.octagroup.foregej.java.lang.ast.AST_PARAMETERS; 25 import com.octagroup.foregej.java.lang.ast.AST_PARAMETER_DEF; 26 import com.octagroup.foregej.java.lang.ast.AST_TYPE; 27 import com.octagroup.foregej.java.lang.ast.lit.AST_LITERAL_int; 28 import com.octagroup.foregej.java.lang.ast.lit.AST_LITERAL_void; 29 /*** 30 * Class that contains functionality associated with the beans 31 * specification. 32 */ 33 public class BeansUtil 34 { 35 /*** 36 * Returns the property name of the supplied method. 37 * <p> 38 * This method extracts the name of the method and returns the 39 * property name for that. First it checks if it is a getter / setter. 40 * </p> 41 * 42 * @param method the method name to determine the property name from. 43 * @return the property name of the supplied method name. 44 */ 45 public static String getPropertyName(AST_METHOD_DEF method) 46 { 47 if(false==isGetterOrSetter(method)) { 48 throw new IllegalArgumentException("Method not a getter or setter"); 49 } 50 return getPropertyName(method.getName()); 51 } 52 /*** 53 * Returns the property name of the supplied method name. 54 * <p> 55 * This method 56 * </p> 57 * 58 * @param methodName the method name to determine the property name 59 * from. 60 * @return the property name of the supplied method name. 61 */ 62 public static String getPropertyName(String methodName) 63 { 64 if(methodName.startsWith("is")) { 65 methodName=methodName.substring(2); 66 } else if(methodName.startsWith("get")) { 67 methodName=methodName.substring(3); 68 } else if(methodName.startsWith("set")) { 69 methodName=methodName.substring(3); 70 } else { 71 throw new IllegalArgumentException("Supplied name is not a getter or setter"); 72 } 73 if(methodName.length()<=1) { 74 return methodName; 75 } 76 if(Character.isLowerCase(methodName.charAt(1))) { 77 StringBuffer sb=new StringBuffer(methodName); 78 sb.setCharAt(0, Character.toLowerCase(sb.charAt(0))); 79 return sb.toString(); 80 } 81 return methodName; 82 } 83 /*** 84 * Performs a deep copy of the getter and converts it to a setter. 85 * <p> 86 * Since we perform a deep copy the body of the getter will also be 87 * copied. 88 * </p> 89 * 90 * @param getter 91 * @return 92 */ 93 public static AST_METHOD_DEF getter2Setter(AST_METHOD_DEF getter) 94 { 95 try{ 96 AST_METHOD_DEF setter=(AST_METHOD_DEF)getter.deepCopy(); 97 String name=getter.getAstName().getIdentifier(); 98 if(name.startsWith("get")) { 99 name=name.substring(3, name.length()); 100 } else if(name.startsWith("is")) { 101 name=name.substring(2, name.length()); 102 } else { 103 throw new IllegalArgumentException("Getter method is not a getter:"+name); 104 } 105 setter.setAstName(new AST_IDENT("set"+name)); 106 AST_TYPE type=setter.getAstType(); 107 setter.setAstType(new AST_TYPE("void")); 108 AST_PARAMETERS params=setter.getAstParameters(); 109 params.addAstParameter(new AST_PARAMETER_DEF(type, 110 new AST_IDENT("arg"))); 111 //System.out.println(setter); 112 return setter; 113 }catch (RecognitionException e) { 114 throw new RuntimeException("BeansUtil.getter2Setter:RecognitionException:"+e.getMessage()); 115 } 116 } 117 /*** 118 * Returns true if the method is a getter or setter. 119 * 120 * @param method the method to switch on. 121 * @return true if the method is a getter or setter. 122 */ 123 public static boolean isGetterOrSetter(AST_METHOD_DEF method) 124 { 125 return isGetter(method)||isSetter(method); 126 } 127 /*** 128 * Returns true if the method name is a getter or setter. 129 * 130 * @param methodName the method name to switch on. 131 * @return true if the method name is a getter or setter. 132 */ 133 public static boolean isGetterOrSetter(String methodName) 134 { 135 return isGetter(methodName)||isSetter(methodName); 136 } 137 /*** 138 * Returns true if the suplied method is a getter method. 139 * 140 * @param method the method 141 * @return true if the suplied method is a getter method. 142 */ 143 public static boolean isGetter(AST_METHOD_DEF method) 144 { 145 // 146 // the name must start with a "get" 147 // 148 if(false==isGetter(method.getName())) { 149 return false; 150 } 151 // 152 // and the method cannot return a void. 153 // 154 return false==method.getAstType().getAstType() instanceof AST_LITERAL_void; 155 } 156 /*** 157 * Returns true if the suplied name is a getter method. 158 * 159 * @param methodName the name of the method 160 * @return true if the suplied name is a getter method. 161 */ 162 public static boolean isGetter(String methodName) 163 { 164 return methodName.startsWith("is")||methodName.startsWith("get"); 165 } 166 /*** 167 * Returns true if the suplied method is a setter method. 168 * 169 * @param method the method 170 * @return true if the suplied method is a setter method. 171 */ 172 public static boolean isSetter(AST_METHOD_DEF method) 173 { 174 if(false==isSetter(method.getName())) { 175 return false; 176 } 177 // make sure that we accept ONE argument or two with 178 // the first being an integer(array setter) 179 AST_PARAMETERS astParams=method.getAstParameters(); 180 if(astParams==null) { 181 return false; 182 } 183 List params=astParams.getAstParameters(); 184 if(params.size()==1) { 185 // normal setter 186 return true; 187 } else if(params.size()==2) { 188 // array setter 189 return ((AST_PARAMETER_DEF)params.get(1)).getAstType().getAstType() instanceof AST_LITERAL_int; 190 } 191 return false; 192 } 193 /*** 194 * Returns true if the suplied name is a setter method. 195 * 196 * @param methodName the name of the method 197 * @return true if the suplied name is a setter method. 198 */ 199 public static boolean isSetter(String methodName) 200 { 201 return methodName.startsWith("set"); 202 } 203 /*** 204 * Converts a property name to a method name. 205 * <p> 206 * Usually this involves converting the first character to upper case. 207 * </p> 208 * 209 * @param property 210 * @return 211 */ 212 public static String property2Method(String property) 213 { 214 if(property.length()==0) { 215 return property; 216 } 217 if(property.length()==1) { 218 return property.toUpperCase(); 219 } 220 if(Character.isUpperCase(property.charAt(0))) { 221 return property; 222 } 223 return Character.toUpperCase(property.charAt(0))+property.substring(1, 224 property.length()); 225 } 226 /*** 227 * Returns the type of property that this method accepts or returns. 228 * <p> 229 * The type is determined from the return value if this is a getter 230 * and from the argument if this is a setter. 231 * </p> 232 * 233 * @param method 234 * @return 235 */ 236 public static AST_TYPE getPropertyType(AST_METHOD_DEF method) 237 { 238 if(isSetter(method)) { 239 List params=method.getAstParameters().getAstParameters(); 240 if(params.size()==1) { 241 // ordinary setter 242 return ((AST_PARAMETER_DEF)params.get(0)).getAstType(); 243 } else if(params.size()==2) { 244 // array setter 245 throw new UnsupportedOperationException("Implement..."); 246 // Todo: must convert to an array... 247 //return ((AST_PARAMETER_DEF) params.get(1)).getAstType(); 248 } else { 249 throw new IllegalStateException("This should not happen since it should be a setter."); 250 } 251 } else if(isGetter(method)) { 252 return method.getAstType(); 253 } else { 254 throw new IllegalArgumentException("Method not a getter nor setter."); 255 } 256 } 257 /*** 258 * Returns the name of the argument for the setter method. 259 * 260 * @param setter the method to get the setter argument from. 261 * @return the name of the argument for the setter method. 262 */ 263 public static String getSetterArgumentName(AST_METHOD_DEF setter) 264 { 265 if(false==isSetter(setter)) { 266 throw new IllegalArgumentException("Not a setter"); 267 } 268 List params=setter.getAstParameters().getAstParameters(); 269 if(params.size()==1) { 270 // normal setter 271 return ((AST_PARAMETER_DEF)params.get(0)).getAstName().getIdentifier(); 272 } else if(params.size()==2) { 273 // normal setter 274 return ((AST_PARAMETER_DEF)params.get(1)).getAstName().getIdentifier(); 275 } else { 276 throw new IllegalStateException("This should not happen"); 277 } 278 } 279 }

This page was automatically generated by Maven