1 package pl.aislib.tools.mapping.structure;
2
3 import pl.aislib.tools.mapping.generators.Utils;
4
5 public class CallParam {
6
7 private String accessType;
8 private String fieldRef;
9 private String methodRef;
10 private String type;
11
12 private static final String ACCESS_TYPE_IN = "in";
13 private static final String ACCESS_TYPE_OUT = "out";
14 private static final String ACCESS_TYPE_INOUT = "inout";
15
16 public CallParam(String accessType, String fieldRef, String methodRef, String type) {
17 this.accessType = accessType;
18 this.fieldRef = fieldRef;
19 this.methodRef = methodRef;
20 this.type = type;
21 }
22
23 public String getAccessType() {
24 return accessType;
25 }
26 public String getFieldRef() {
27 return fieldRef;
28 }
29 public String getMethodRef() {
30 return methodRef;
31 }
32 public String getType() {
33 return type;
34 }
35 public boolean hasFieldRef() {
36 return (fieldRef != null);
37 }
38 public boolean hasMethodRef() {
39 return (methodRef != null);
40 }
41 public boolean hasType() {
42 return (type != null);
43 }
44 public boolean isInParam() {
45 return (accessType.equals(ACCESS_TYPE_IN));
46 }
47 public boolean isOutParam() {
48 return (accessType.equals(ACCESS_TYPE_OUT));
49 }
50 public boolean isInOrInoutParam() {
51 return (accessType.equals(ACCESS_TYPE_IN) || accessType.equals(ACCESS_TYPE_INOUT));
52 }
53 public boolean isOutOrInoutParam() {
54 return (accessType.equals(ACCESS_TYPE_OUT) || accessType.equals(ACCESS_TYPE_INOUT));
55 }
56
57
58
59 /***
60 * Hides methodRef/fieldRef branch.
61 * The returned JavaParam may be used to determine type of this CallParam.
62 * @return a copy from JavaMethod (methodRef) or a newly created from a JavaField (fieldRef)
63 */
64 public JavaParam createJavaParam(Fields fields, JavaMethod javaMethod) {
65 if (hasFieldRef()) {
66 JavaField javaField = fields.findFieldByName(getFieldRef()).getJavaField();
67 String name = "object." + Utils.getter(javaField.getName());
68 String type = javaField.getType();
69 return new JavaParam(name, type);
70 }
71 if (hasMethodRef()) {
72 JavaParam javaParam = javaMethod.findJavaParamByName(getMethodRef());
73 return javaParam;
74 }
75 return null;
76 }
77
78 public String determineType(Fields fields, JavaMethod javaMethod) {
79 if (hasType()) {
80 return type;
81 }
82 return createJavaParam(fields, javaMethod).getType();
83 }
84
85 public void checkAttributes(Fields fields, JavaMethod javaMethod) {
86 boolean valid;
87
88 if (isOutParam()) {
89 valid = (!hasFieldRef() && !hasMethodRef() && hasType());
90 if (!valid) {
91 throw new IllegalArgumentException("<call-param>: when access-type=\"out\", only 'type' is allowed and required.");
92 }
93 }
94
95 if (isInOrInoutParam()) {
96 valid = ((hasFieldRef() ^ hasMethodRef()) && !hasType());
97 if (!valid) {
98 throw new IllegalArgumentException("<call-param>: when access-type!=\"out\", either 'field-ref' or 'method-ref' " +
99 "is required and 'type' is forbidden.");
100 }
101 }
102
103 if (isInOrInoutParam() && hasFieldRef()) {
104 if (fields.findFieldByName(fieldRef) == null) {
105 throw new IllegalArgumentException("<call-param>: no <field> exists with name \"" + fieldRef + "\"");
106 }
107 }
108
109 if (isInOrInoutParam() && hasMethodRef()) {
110 if (javaMethod.findJavaParamByName(methodRef) == null) {
111 throw new IllegalArgumentException("<call-param>: no <java-param> exists with name \"" + methodRef + "\"");
112 }
113 }
114
115 }
116 }
117
118 /***
119 * $Log: CallParam.java,v $
120 * Revision 1.2 2003/09/10 07:33:25 milosz
121 * createJavaParam method added.
122 *
123 * Revision 1.1 2003/09/09 13:13:45 milosz
124 * Initial commit
125 *
126 */
127