KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > InjectionTargetTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 package com.sun.enterprise.tools.verifier.tests;
22
23 import com.sun.enterprise.tools.verifier.Result;
24 import com.sun.enterprise.deployment.Descriptor;
25 import com.sun.enterprise.deployment.InjectionCapable;
26 import com.sun.enterprise.deployment.InjectionTarget;
27
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.lang.reflect.Field JavaDoc;
31 import java.lang.reflect.Modifier JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33
34 /**
35  * The field or method where injection annotation is used may have any access
36  * qualifier (public , private , etc.) but must not be static or final.
37  * This is the base class of all the InjectionAnnotation tests.
38  *
39  * @author Vikas Awasthi
40  */

41 public abstract class InjectionTargetTest extends VerifierTest implements VerifierCheck {
42 // Currently only ejbs are checked for injection annotations. Other modules can
43
// also use this class to test the assertion.
44
protected abstract List JavaDoc<InjectionCapable> getInjectables(String JavaDoc className);
45     protected abstract String JavaDoc getClassName();
46     private Descriptor descriptor;
47     Result result;
48     ComponentNameConstructor compName;
49     
50     public Result check(Descriptor descriptor) {
51         this.descriptor = descriptor;
52         result = getInitializedResult();
53         compName = getVerifierContext().getComponentNameConstructor();
54         ClassLoader JavaDoc cl = getVerifierContext().getClassLoader();
55         List JavaDoc<InjectionCapable> injectables = getInjectables(getClassName());
56         for (InjectionCapable injectionCapable : injectables) {
57             Set JavaDoc<InjectionTarget> iTargets = injectionCapable.getInjectionTargets();
58             for (InjectionTarget target : iTargets) {
59                 try {
60                     if(target.isFieldInjectable()) {
61                         Class JavaDoc classObj = Class.forName(getClassName(), false, cl);
62                         Field JavaDoc field = classObj.getDeclaredField(target.getFieldName());
63                         testMethodModifiers(field.getModifiers(), "field", field);
64                     }
65                     if(target.isMethodInjectable()) {
66                         Class JavaDoc classObj = Class.forName(getClassName(), false, cl);
67                         Method JavaDoc method = getInjectedMethod(classObj, target.getMethodName());
68                         if(method == null) continue;
69                         testMethodModifiers(method.getModifiers(), "method", method);
70                     }
71                 } catch (Exception JavaDoc e) {} //ignore as it will be caught in other tests
72
}
73         }
74         
75         if(result.getStatus() != Result.FAILED) {
76             addGoodDetails(result, compName);
77             result.passed(smh.getLocalString
78                     (getClass().getName()+".passed",
79                     "Valid injection method(s)."));
80         }
81         return result;
82     }
83
84     protected Descriptor getDescriptor() {
85         return descriptor;
86     }
87     
88     private void testMethodModifiers(int modifier, String JavaDoc targetType, Object JavaDoc fieldOrMethod) {
89         if(Modifier.isStatic(modifier) ||
90                 Modifier.isFinal(modifier)) {
91             addErrorDetails(result, compName);
92             result.failed(smh.getLocalString
93                     ("com.sun.enterprise.tools.verifier.tests.InjectionTargetTest.failed",
94                     "Invalid annotation in {0} [ {1} ].",
95                     new Object JavaDoc[] {targetType, fieldOrMethod}));
96         }
97     }
98     
99     private Method JavaDoc getInjectedMethod(Class JavaDoc classObj, String JavaDoc methodName) {
100         for (Method JavaDoc method : classObj.getDeclaredMethods()) {
101             if(method.getName().equals(methodName))
102                 return method;
103         }
104         return null;
105     }
106 }
Popular Tags