01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.converter.internal;
16:
17: import java.lang.annotation.Annotation;
18: import java.lang.reflect.Method;
19:
20: import org.strecks.converter.Converter;
21: import org.strecks.converter.factory.ConverterFactoryClass;
22: import org.strecks.converter.factory.ConverterFactory;
23: import org.strecks.exceptions.ApplicationConfigurationException;
24: import org.strecks.util.ReflectHelper;
25:
26: /**
27: * Reads any <code>Converter</code> declared using an annotation which itself has a <code>ConverterFactoryClass</code>
28: * annotation
29: */
30: public class ConverterReader {
31:
32: /**
33: * Returns a <code>Converter</code> instance if one of the relevant annotations is used, otherwise returns null
34: */
35: public Converter readConverter(Method method) {
36:
37: Annotation[] annotations = method.getAnnotations();
38: Converter converter = null;
39:
40: boolean found = false;
41:
42: for (Annotation annotation : annotations) {
43:
44: Class<? extends Annotation> annotationType = annotation
45: .annotationType();
46: ConverterFactoryClass factoryClass = annotationType
47: .getAnnotation(ConverterFactoryClass.class);
48:
49: if (factoryClass != null) {
50:
51: if (found) {
52: throw new ApplicationConfigurationException(
53: "Only one converter annotation may be placed in method "
54: + method.getName()
55: + "() in class "
56: + method.getDeclaringClass()
57: .getName());
58: }
59:
60: ConverterFactory factory = ReflectHelper
61: .createInstance(factoryClass.value(),
62: ConverterFactory.class);
63: converter = factory.createConverter(annotation);
64: found = true;
65:
66: }
67: }
68:
69: return converter;
70:
71: }
72:
73: }
|