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.injection.annotation;
16:
17: import java.lang.reflect.Method;
18:
19: import org.apache.commons.beanutils.ConvertUtilsBean;
20: import org.strecks.exceptions.ApplicationRuntimeException;
21: import org.testng.annotations.Test;
22:
23: /**
24: * @author Phil Zoio
25: */
26: public class TestAnnotationChecker {
27:
28: private Integer value;
29:
30: public void check(String className, String someValue)
31: throws Exception {
32:
33: Class<?> forName = Class.forName(className);
34: for (Method m : forName.getMethods()) {
35:
36: if (m.isAnnotationPresent(InjectRequestParameter.class)) {
37:
38: Class<?>[] parameterTypes = m.getParameterTypes();
39: if (parameterTypes.length != 1) {
40: throw new ApplicationRuntimeException(
41: "Method must have one parameter");
42: }
43:
44: Class type = parameterTypes[0];
45:
46: String methodName = m.getName();
47: if (!methodName.startsWith("set")) {
48: throw new ApplicationRuntimeException(
49: "Method begin with set");
50: }
51:
52: Character firstChar = methodName.charAt(3);
53: String propertyName = methodName.substring(4);
54: propertyName = Character.toLowerCase(firstChar)
55: + propertyName;
56:
57: System.out.println("Type: " + type + ", property "
58: + propertyName);
59:
60: Object convert = new ConvertUtilsBean().convert(
61: someValue, type);
62:
63: Method method = this .getClass().getMethod(methodName,
64: new Class[] { type });
65: method.invoke(this , new Object[] { convert });
66:
67: }
68: }
69: }
70:
71: @InjectRequestParameter()
72: public void setIntegerValue(Integer value) {
73: this .value = value;
74: }
75:
76: @Test
77: public void testAnnotationChecker() throws Exception {
78: check(this .getClass().getName(), "1");
79: assert value == 1;
80: }
81: }
|