View Javadoc
1   package org.exolab.castor.builder.factory;
2   
3   import org.castor.xml.JavaNaming;
4   import org.exolab.castor.builder.SGTypes;
5   import org.exolab.castor.builder.info.FieldInfo;
6   import org.exolab.castor.builder.info.nature.XMLInfoNature;
7   import org.exolab.javasource.JMethod;
8   import org.exolab.javasource.JParameter;
9   import org.exolab.javasource.JSourceCode;
10  import org.exolab.javasource.JType;
11  
12  /**
13   * This class translates a fieldInfo describing an identity into the suitable getter<7setter
14   * methods.
15   */
16  public class IdentityMemberAndAccessorFactory extends FieldMemberAndAccessorFactory {
17  
18    /**
19     * Creates the IdentityMemberAndAccessorFactory.
20     * 
21     * @param naming the javaNaming to use
22     */
23    public IdentityMemberAndAccessorFactory(final JavaNaming naming) {
24      super(naming);
25    }
26  
27    /**
28     * Creats the getter/setter and getReferenceId methods.
29     * 
30     * @param fieldInfo the fieldInfo to translate
31     * @return the created methods
32     */
33    public JMethod[] createAccessMethods(final FieldInfo fieldInfo) {
34      String mname = fieldInfo.getMethodSuffix();
35      JType jType = new XMLInfoNature(fieldInfo).getSchemaType().getJType();
36  
37      JMethod[] methods = new JMethod[3];
38      methods[0] = makeGetMethod(fieldInfo, mname, jType); // -- create get method
39      methods[1] = makeSetMethod(fieldInfo, mname, jType); // -- create set method
40      methods[2] = makeGetReferenceIdMethod(fieldInfo); // -- create getReferenceId
41      // (from Referable Interface)
42  
43      return methods;
44    } // -- createAccessMethods
45  
46    /**
47     * Creates the getter method.
48     * 
49     * @param fieldInfo the fieldInfo to translate
50     * @param mname the name of this field
51     * @param jType the type of this field
52     * @return the getter method for this identity
53     */
54    private JMethod makeGetMethod(final FieldInfo fieldInfo, final String mname, final JType jType) {
55      JMethod method = new JMethod("get" + mname, jType, "the value of field '" + mname + "'.");
56      JSourceCode jsc = method.getSourceCode();
57      jsc.add("return this.");
58      jsc.append(fieldInfo.getName());
59      jsc.append(";");
60      return method;
61    }
62  
63    /**
64     * Creates the setter method.
65     * 
66     * @param fieldInfo the fieldInfo to translate
67     * @param mname the name of this field
68     * @param jType the type of this field
69     * @return the setter method for this identity
70     */
71    private JMethod makeSetMethod(final FieldInfo fieldInfo, final String mname, final JType jType) {
72      JMethod method = new JMethod("set" + mname);
73      method.addParameter(new JParameter(jType, fieldInfo.getName()));
74      JSourceCode jsc = method.getSourceCode();
75      jsc.add("this.");
76      jsc.append(fieldInfo.getName());
77      jsc.append(" = ");
78      jsc.append(fieldInfo.getName());
79      jsc.append(";");
80  
81      // -- add resolver registration
82      // jsc.add("if (idResolver != null) ");
83      // jsc.indent();
84      // jsc.add("idResolver.addResolvable(");
85      // jsc.append(fieldInfo.getName());
86      // jsc.append(", this);");
87      // jsc.unindent();
88  
89      return method;
90    }
91  
92    /**
93     * Creates the getReferenceId method.
94     * 
95     * @param fieldInfo the fieldInfo to translate
96     * @return the getReferenceId method.
97     */
98    private JMethod makeGetReferenceIdMethod(final FieldInfo fieldInfo) {
99      JMethod method = new JMethod("getReferenceId", SGTypes.STRING, "the reference ID");
100     JSourceCode jsc = method.getSourceCode();
101     jsc.add("return this.");
102     jsc.append(fieldInfo.getName());
103     jsc.append(";");
104     return method;
105   }
106 
107 
108 }