001: /*
002: * Copyright 2006 Werner Guttmann
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.exolab.javasource;
017:
018: import java.util.HashMap;
019:
020: /**
021: * Makes an effort to identify where Override annotations belong. We are limited
022: * in what we look at and we do not use reflection to identify if a class truly
023: * extends the appropriate class. We only check the class of the arguments. This
024: * class may not be generically useful, but is useful with the code generated by
025: * Castor.
026: * <br/>
027: * Our one public method, {@link #addOverrideAnnotations(JMethodSignature)},
028: * should only be called when you already know that you are generating Java-5
029: * style code.
030: *
031: * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttman</a>
032: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
033: * @since 1.0.4
034: */
035: public final class Java5HacksHelper {
036: //--------------------------------------------------------------------------
037:
038: /**
039: * As a static utility class, we want a private constructor.
040: */
041: private Java5HacksHelper() {
042: }
043:
044: //--------------------------------------------------------------------------
045:
046: private static class MethodSpec {
047: private String _methodName;
048: private int _paramCount;
049: private String _param1ClassName;
050: private String _param2ClassName;
051:
052: public MethodSpec(final String methodName,
053: final int paramCount, final String param1ClassName,
054: final String param2ClassName) {
055: _methodName = methodName;
056: _paramCount = paramCount;
057: _param1ClassName = param1ClassName;
058: _param2ClassName = param2ClassName;
059: }
060:
061: public String getMethodName() {
062: return _methodName;
063: }
064:
065: public int getParamCount() {
066: return _paramCount;
067: }
068:
069: public String getParam1ClassName() {
070: return _param1ClassName;
071: }
072:
073: public String getParam2ClassName() {
074: return _param2ClassName;
075: }
076: }
077:
078: private static final HashMap DEFINED_SPECS = new HashMap();
079:
080: private static void createMethodSpec(final String methodName,
081: final int paramCount, final String param1ClassName,
082: final String param2ClassName) {
083: MethodSpec temp = new MethodSpec(methodName, paramCount,
084: param1ClassName, param2ClassName);
085:
086: DEFINED_SPECS.put(methodName, temp);
087: }
088:
089: static {
090: createMethodSpec("getAccessMode", 0, "", "");
091: createMethodSpec("getExtends", 0, "", "");
092: createMethodSpec("getIdentity", 0, "", "");
093: createMethodSpec("getJavaClass", 0, "", "");
094: createMethodSpec("getNameSpacePrefix", 0, "", "");
095: createMethodSpec("getNameSpaceURI", 0, "", "");
096: createMethodSpec("getValidator", 0, "", "");
097: createMethodSpec("getXMLName", 0, "", "");
098: createMethodSpec("getXTransients", 0, "", "");
099: createMethodSpec("newInstance", 1, "java.lang.Object", "");
100: createMethodSpec("setValue", 1, "java.lang.Object", "");
101: createMethodSpec("equals", 1, "java.lang.Object", "");
102: createMethodSpec("getValue", 1, "java.lang.Object", "");
103: createMethodSpec("marshal", 1, "java.io.Writer", "");
104: createMethodSpec("newInstance", 1, "java.lang.Object", "");
105: createMethodSpec("setValue", 2, "java.lang.Object",
106: "java.lang.Object");
107: createMethodSpec("setXTransients", 1,
108: "org.openmrm.core.data.castor.XTransients", "");
109: };
110:
111: /** An override annotation we use to see if we can get others of its type. */
112: private static final JAnnotationType OVERRIDE_ANNOTATION = new JAnnotationType(
113: "Override");
114:
115: /**
116: * Given the method signature, add the Override annotation if this class is
117: * one that we know requires this annotation.
118: *
119: * @param jms The method signature to inspect.
120: */
121: public static void addOverrideAnnotations(final JMethodSignature jms) {
122: String name = jms.getName();
123: boolean addOverrideAnnotation = false;
124:
125: // It the method already has an override annotation, then jump out
126: JAnnotation override = jms.getAnnotation(OVERRIDE_ANNOTATION);
127: if (override != null) {
128: return;
129: }
130:
131: // If the method name doesn't exist in our list, then jump out
132: MethodSpec methodSpec = (MethodSpec) DEFINED_SPECS.get(name);
133: if (methodSpec == null) {
134: return;
135: }
136:
137: // If the number of parameters isn't what we're prepared for, then jump out
138: int paramCount = jms.getParameters().length;
139: if (paramCount != methodSpec.getParamCount()) {
140: return;
141: }
142:
143: // Do we add the Override annotation? Check vs number of arguments.
144: switch (paramCount) {
145: case 0:
146: addOverrideAnnotation = true;
147: break;
148: case 1:
149: String incomingClassName1 = jms.getParameter(0).getType()
150: .getName();
151: if (incomingClassName1.equalsIgnoreCase(methodSpec
152: .getParam1ClassName())) {
153: addOverrideAnnotation = true;
154: }
155: break;
156: case 2:
157: String className1 = jms.getParameter(0).getType().getName();
158: String className2 = jms.getParameter(1).getType().getName();
159: if (className1.equalsIgnoreCase(methodSpec
160: .getParam1ClassName())
161: && className2.equalsIgnoreCase(methodSpec
162: .getParam2ClassName())) {
163: addOverrideAnnotation = true;
164: }
165: break;
166: default:
167: // We aren't prepared for > 2 parameters, so we don't add an Override annotation
168: break;
169: }
170:
171: // Do the work if we need to
172: if (addOverrideAnnotation) {
173: jms.addAnnotation(new JAnnotation(new JAnnotationType(
174: "Override")));
175: }
176: }
177:
178: //--------------------------------------------------------------------------
179: }
|