1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.config.annotation;
20
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.lang.reflect.Modifier;
24
25 import javax.annotation.PostConstruct;
26 import javax.annotation.PreDestroy;
27 import javax.naming.NamingException;
28
29 import org.apache.myfaces.shared_impl.util.ClassUtils;
30
31
32
33
34
35
36
37 public class NoInjectionAnnotationLifecycleProvider implements LifecycleProvider2
38 {
39
40
41 public Object newInstance(String className)
42 throws InstantiationException, IllegalAccessException, NamingException, InvocationTargetException, ClassNotFoundException
43 {
44 Class clazz = ClassUtils.classForName(className);
45 Object object = clazz.newInstance();
46 processAnnotations(object);
47
48 return object;
49 }
50
51
52
53
54 public void postConstruct(Object instance)
55 throws IllegalAccessException, InvocationTargetException
56 {
57
58
59
60 Method[] methods = instance.getClass().getDeclaredMethods();
61 Method postConstruct = null;
62 for (Method method : methods)
63 {
64 if (method.isAnnotationPresent(PostConstruct.class))
65 {
66
67
68
69
70
71
72 if ((postConstruct != null)
73 || (method.getParameterTypes().length != 0)
74 || (Modifier.isStatic(method.getModifiers()))
75 || (method.getExceptionTypes().length > 0)
76 || (!method.getReturnType().getName().equals("void")))
77 {
78 throw new IllegalArgumentException("Invalid PostConstruct annotation");
79 }
80 postConstruct = method;
81 }
82 }
83
84 invokeAnnotatedMethod(postConstruct, instance);
85
86 }
87
88 public void destroyInstance(Object instance)
89 throws IllegalAccessException, InvocationTargetException
90 {
91
92
93
94 Method[] methods = instance.getClass().getDeclaredMethods();
95 Method preDestroy = null;
96 for (Method method : methods)
97 {
98 if (method.isAnnotationPresent(PreDestroy.class))
99 {
100
101
102
103
104
105
106 if ((preDestroy != null)
107 || (method.getParameterTypes().length != 0)
108 || (Modifier.isStatic(method.getModifiers()))
109 || (method.getExceptionTypes().length > 0)
110 || (!method.getReturnType().getName().equals("void")))
111 {
112 throw new IllegalArgumentException("Invalid PreDestroy annotation");
113 }
114 preDestroy = method;
115 }
116 }
117
118 invokeAnnotatedMethod(preDestroy, instance);
119
120 }
121
122 private void invokeAnnotatedMethod(Method method, Object instance)
123 throws IllegalAccessException, InvocationTargetException
124 {
125
126
127 if (method != null)
128 {
129 boolean accessibility = method.isAccessible();
130 method.setAccessible(true);
131 method.invoke(instance);
132 method.setAccessible(accessibility);
133 }
134 }
135
136
137
138
139 protected void processAnnotations(Object instance)
140 throws IllegalAccessException, InvocationTargetException, NamingException
141 {
142
143 }
144
145 }