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.tools;
20 import antlr.RecognitionException;
21 import com.octagroup.foregej.java.lang.ast.AST_IDENT;
22 import com.octagroup.foregej.java.lang.ast.AST_METHOD_DEF;
23 import com.octagroup.foregej.java.lang.ast.AST_PARAMETERS;
24 import com.octagroup.foregej.java.lang.ast.AST_PARAMETER_DEF;
25 import com.octagroup.foregej.java.lang.ast.AST_TYPE;
26 /***
27 * Class that contains functionality associated with the beans specification.
28 */
29 public class BeansUtil
30 {
31 /***
32 * Performs a deep copy of the getter and converts it to a setter.
33 * <p>
34 * Since we perform a deep copy the body of the getter will also be copied.
35 * </p>
36 *
37 *
38 * @param getter
39 * @return
40 */
41 public static AST_METHOD_DEF getter2Setter(AST_METHOD_DEF getter)
42 {
43 try{
44 AST_METHOD_DEF setter=(AST_METHOD_DEF)getter.deepCopy();
45 String name=getter.getAstName().getIdentifier();
46 if(name.startsWith("get")) {
47 name=name.substring(3, name.length());
48 } else if(name.startsWith("is")) {
49 name=name.substring(2, name.length());
50 } else {
51 throw new IllegalArgumentException("Getter method is not a getter:"+name);
52 }
53 setter.setAstName(new AST_IDENT("set"+name));
54 AST_TYPE type=setter.getAstType();
55 setter.setAstType(new AST_TYPE("void"));
56 AST_PARAMETERS params=setter.getAstParameters();
57 params.addAstParameter(new AST_PARAMETER_DEF(type, new AST_IDENT("arg")));
58 //System.out.println(setter);
59 return setter;
60 }catch (RecognitionException e) {
61 throw new RuntimeException("BeansUtil.getter2Setter:RecognitionException:"+e.getMessage());
62 }
63 }
64 /***
65 * Converts a property name to a method name.
66 * <p>
67 * Usually this involves converting the first character to upper case.
68 * </p>
69 *
70 */
71 public static String property2Method(String property)
72 {
73 if(property.length()==0) {
74 return property;
75 }
76 if(property.length()==1) {
77 return property.toUpperCase();
78 }
79 if(Character.isUpperCase(property.charAt(0))) {
80 return property;
81 }
82 return Character.toUpperCase(property.charAt(0))+property.substring(1, property.length());
83 }
84 }
This page was automatically generated by Maven