MethodUtils.java
01 /*
02  * Copyright 2008-2015 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.codehaus.griffon.runtime.injection;
17 
18 import griffon.exceptions.InstanceMethodInvocationException;
19 
20 import javax.annotation.Nonnull;
21 import java.lang.annotation.Annotation;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.security.AccessController;
25 import java.security.PrivilegedAction;
26 import java.util.ArrayList;
27 import java.util.List;
28 
29 public final class MethodUtils {
30     public static void invokeAnnotatedMethod(@Nonnull final Object instance, @Nonnull final Class<? extends Annotation> annotation) {
31         List<Method> methods = new ArrayList<>();
32         Class<?> klass = instance.getClass();
33         while (klass != null) {
34             boolean found = false;
35             for (Method method : klass.getDeclaredMethods()) {
36                 if (method.isAnnotationPresent(annotation&&
37                     method.getParameterTypes().length == 0) {
38                     if (found) {
39                         throw new InstanceMethodInvocationException(instance, method, buildCause(instance.getClass(), method, methods));
40                     }
41                     methods.add(method);
42                     found = true;
43                 }
44             }
45 
46             klass = klass.getSuperclass();
47         }
48 
49         for (final Method method : methods) {
50             AccessController.doPrivileged(new PrivilegedAction<Object>() {
51                 @Override
52                 public Object run() {
53                     boolean wasAccessible = method.isAccessible();
54                     try {
55                         method.setAccessible(true);
56                         return method.invoke(instance);
57                     catch (IllegalAccessException | IllegalArgumentException e) {
58                         throw new InstanceMethodInvocationException(instance, method.getName(), null, e);
59                     catch (InvocationTargetException e) {
60                         throw new InstanceMethodInvocationException(instance, method.getName(), null, e.getTargetException());
61                     finally {
62                         method.setAccessible(wasAccessible);
63                     }
64                 }
65             });
66         }
67     }
68 
69     @Nonnull
70     private static Throwable buildCause(@Nonnull Class<?> clazz, @Nonnull Method method, @Nonnull List<Method> methods) {
71         StringBuilder b = new StringBuilder("The following methods were found annotated with @PostConstruct on ")
72             .append(clazz);
73         for (Method m : methods) {
74             b.append("\n  ").append(m.toGenericString());
75         }
76         b.append("\n  ").append(method.toGenericString());
77         return new IllegalStateException(b.toString());
78     }
79 }