1 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 ; 29 import java.util.Set ; 30 import java.lang.reflect.Field ; 31 import java.lang.reflect.Modifier ; 32 import java.lang.reflect.Method ; 33 34 41 public abstract class InjectionTargetTest extends VerifierTest implements VerifierCheck { 42 protected abstract List <InjectionCapable> getInjectables(String className); 45 protected abstract String 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 cl = getVerifierContext().getClassLoader(); 55 List <InjectionCapable> injectables = getInjectables(getClassName()); 56 for (InjectionCapable injectionCapable : injectables) { 57 Set <InjectionTarget> iTargets = injectionCapable.getInjectionTargets(); 58 for (InjectionTarget target : iTargets) { 59 try { 60 if(target.isFieldInjectable()) { 61 Class classObj = Class.forName(getClassName(), false, cl); 62 Field field = classObj.getDeclaredField(target.getFieldName()); 63 testMethodModifiers(field.getModifiers(), "field", field); 64 } 65 if(target.isMethodInjectable()) { 66 Class classObj = Class.forName(getClassName(), false, cl); 67 Method method = getInjectedMethod(classObj, target.getMethodName()); 68 if(method == null) continue; 69 testMethodModifiers(method.getModifiers(), "method", method); 70 } 71 } catch (Exception e) {} } 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 targetType, Object 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 [] {targetType, fieldOrMethod})); 96 } 97 } 98 99 private Method getInjectedMethod(Class classObj, String methodName) { 100 for (Method method : classObj.getDeclaredMethods()) { 101 if(method.getName().equals(methodName)) 102 return method; 103 } 104 return null; 105 } 106 } | Popular Tags |