1
2
3
4
5
6
7
8
9
10 package org.mule.registry;
11
12 import org.mule.api.registry.InjectProcessor;
13 import org.mule.config.i18n.CoreMessages;
14 import org.mule.util.annotation.AnnotationMetaData;
15 import org.mule.util.annotation.AnnotationUtils;
16
17 import java.lang.reflect.Method;
18 import java.lang.reflect.Modifier;
19 import java.util.List;
20
21 import javax.annotation.PostConstruct;
22 import javax.annotation.PreDestroy;
23
24
25
26
27
28
29
30
31
32
33
34
35
36 public class JSR250ValidatorProcessor implements InjectProcessor
37 {
38 public Object process(Object object)
39 {
40 List<AnnotationMetaData> annos = AnnotationUtils.getMethodAnnotations(object.getClass(), PostConstruct.class);
41 if (annos.size() > 1)
42 {
43 throw new IllegalArgumentException(CoreMessages.objectHasMoreThanOnePostConstructAnnotation(object.getClass()).getMessage());
44 }
45 else if(annos.size()==1)
46 {
47 validateLifecycleMethod((Method)annos.get(0).getMember());
48 }
49
50 annos = AnnotationUtils.getMethodAnnotations(object.getClass(), PreDestroy.class);
51 if (annos.size() > 1)
52 {
53 throw new IllegalArgumentException(CoreMessages.objectHasMoreThanOnePreDestroyAnnotation(object.getClass()).getMessage());
54 }
55 else if(annos.size()==1)
56 {
57 validateLifecycleMethod((Method)annos.get(0).getMember());
58 }
59
60 return object;
61 }
62
63 public final void validateLifecycleMethod(Method method)
64 {
65 if(method.getParameterTypes().length != 0)
66 {
67 throw new IllegalArgumentException(CoreMessages.lifecycleMethodNotVoidOrHasParams(method).getMessage());
68 }
69
70 if(!method.getReturnType().equals(Void.TYPE))
71 {
72 throw new IllegalArgumentException(CoreMessages.lifecycleMethodNotVoidOrHasParams(method).getMessage());
73 }
74
75 if(Modifier.isStatic(method.getModifiers()))
76 {
77 throw new IllegalArgumentException(CoreMessages.lifecycleMethodCannotBeStatic(method).getMessage());
78 }
79
80 for (Class<?> aClass : method.getExceptionTypes())
81 {
82 if(!RuntimeException.class.isAssignableFrom(aClass))
83 {
84 throw new IllegalArgumentException(CoreMessages.lifecycleMethodCannotThrowChecked(method).getMessage());
85 }
86 }
87 }
88 }