1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.exolab.javasource;
15
16 import java.util.HashMap;
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public final class Java5HacksHelper {
31
32
33
34
35 private Java5HacksHelper() {}
36
37 private static class MethodSpec {
38 private String _methodName;
39 private int _paramCount;
40 private String _param1ClassName;
41 private String _param2ClassName;
42
43 public MethodSpec(final String methodName, final int paramCount, final String param1ClassName,
44 final String param2ClassName) {
45 _methodName = methodName;
46 _paramCount = paramCount;
47 _param1ClassName = param1ClassName;
48 _param2ClassName = param2ClassName;
49 }
50
51 public String getMethodName() {
52 return _methodName;
53 }
54
55 public int getParamCount() {
56 return _paramCount;
57 }
58
59 public String getParam1ClassName() {
60 return _param1ClassName;
61 }
62
63 public String getParam2ClassName() {
64 return _param2ClassName;
65 }
66 }
67
68 private static final HashMap<String, MethodSpec> DEFINED_SPECS =
69 new HashMap<String, MethodSpec>();
70
71 private static void createMethodSpec(final String methodName, final int paramCount,
72 final String param1ClassName, final String param2ClassName) {
73 MethodSpec temp = new MethodSpec(methodName, paramCount, param1ClassName, param2ClassName);
74
75 DEFINED_SPECS.put(methodName, temp);
76 }
77
78 static {
79 createMethodSpec("getAccessMode", 0, "", "");
80
81
82 createMethodSpec("getNameSpacePrefix", 0, "", "");
83 createMethodSpec("getNameSpaceURI", 0, "", "");
84 createMethodSpec("getValidator", 0, "", "");
85 createMethodSpec("getXMLName", 0, "", "");
86 createMethodSpec("getXTransients", 0, "", "");
87 createMethodSpec("newInstance", 1, "java.lang.Object", "");
88 createMethodSpec("setValue", 1, "java.lang.Object", "");
89 createMethodSpec("equals", 1, "java.lang.Object", "");
90 createMethodSpec("getValue", 1, "java.lang.Object", "");
91 createMethodSpec("marshal", 1, "java.io.Writer", "");
92 createMethodSpec("newInstance", 1, "java.lang.Object", "");
93 createMethodSpec("setValue", 2, "java.lang.Object", "java.lang.Object");
94 createMethodSpec("setXTransients", 1, "org.openmrm.core.data.castor.XTransients", "");
95 };
96
97
98 private static final JAnnotationType OVERRIDE_ANNOTATION = new JAnnotationType("Override");
99
100
101
102
103
104
105
106 public static void addOverrideAnnotations(final JMethodSignature jms) {
107 String name = jms.getName();
108 boolean addOverrideAnnotation = false;
109
110
111 JAnnotation override = jms.getAnnotation(OVERRIDE_ANNOTATION);
112 if (override != null) {
113 return;
114 }
115
116
117 MethodSpec methodSpec = DEFINED_SPECS.get(name);
118 if (methodSpec == null) {
119 return;
120 }
121
122
123 int paramCount = jms.getParameters().length;
124 if (paramCount != methodSpec.getParamCount()) {
125 return;
126 }
127
128
129 switch (paramCount) {
130 case 0:
131 addOverrideAnnotation = true;
132 break;
133 case 1:
134 String incomingClassName1 = jms.getParameter(0).getType().getName();
135 if (incomingClassName1.equalsIgnoreCase(methodSpec.getParam1ClassName())) {
136 addOverrideAnnotation = true;
137 }
138 break;
139 case 2:
140 String className1 = jms.getParameter(0).getType().getName();
141 String className2 = jms.getParameter(1).getType().getName();
142 if (className1.equalsIgnoreCase(methodSpec.getParam1ClassName())
143 && className2.equalsIgnoreCase(methodSpec.getParam2ClassName())) {
144 addOverrideAnnotation = true;
145 }
146 break;
147 default:
148
149 break;
150 }
151
152
153 if (addOverrideAnnotation) {
154 jms.addAnnotation(new JAnnotation(new JAnnotationType("Override")));
155 }
156 }
157
158 }