|
24 | 24 | import java.io.IOException; |
25 | 25 | import java.lang.annotation.Annotation; |
26 | 26 | import java.lang.reflect.Method; |
| 27 | +import java.lang.reflect.Modifier; |
| 28 | +import java.lang.reflect.Type; |
27 | 29 |
|
28 | 30 | /** |
29 | 31 | * Provides common methods to retrieve information from JoinPoint and not only. |
@@ -123,4 +125,79 @@ public static <T extends Annotation> Optional<T> getAnnotation(Class<?> type, Cl |
123 | 125 | return Optional.absent(); |
124 | 126 | } |
125 | 127 |
|
| 128 | + public static String getMethodInfo(Method m) { |
| 129 | + StringBuilder info = new StringBuilder(); |
| 130 | + info.append("Method signature:").append("\n"); |
| 131 | + info.append(m.toGenericString()).append("\n"); |
| 132 | + |
| 133 | + info.append("Declaring class:\n"); |
| 134 | + info.append(m.getDeclaringClass().getCanonicalName()).append("\n"); |
| 135 | + |
| 136 | + info.append("\nFlags:").append("\n"); |
| 137 | + info.append("Bridge=").append(m.isBridge()).append("\n"); |
| 138 | + info.append("Synthetic=").append(m.isSynthetic()).append("\n"); |
| 139 | + info.append("Final=").append(Modifier.isFinal(m.getModifiers())).append("\n"); |
| 140 | + info.append("Native=").append(Modifier.isNative(m.getModifiers())).append("\n"); |
| 141 | + info.append("Synchronized=").append(Modifier.isSynchronized(m.getModifiers())).append("\n"); |
| 142 | + info.append("Abstract=").append(Modifier.isAbstract(m.getModifiers())).append("\n"); |
| 143 | + info.append("AccessLevel=").append(getAccessLevel(m.getModifiers())).append("\n"); |
| 144 | + |
| 145 | + info.append("\nReturn Type: \n"); |
| 146 | + info.append("ReturnType=").append(m.getReturnType()).append("\n"); |
| 147 | + info.append("GenericReturnType=").append(m.getGenericReturnType()).append("\n"); |
| 148 | + |
| 149 | + info.append("\nParameters:"); |
| 150 | + Class<?>[] pType = m.getParameterTypes(); |
| 151 | + Type[] gpType = m.getGenericParameterTypes(); |
| 152 | + if (pType.length != 0) { |
| 153 | + info.append("\n"); |
| 154 | + } else { |
| 155 | + info.append("empty\n"); |
| 156 | + } |
| 157 | + for (int i = 0; i < pType.length; i++) { |
| 158 | + info.append("parameter [").append(i).append("]:\n"); |
| 159 | + info.append("ParameterType=").append(pType[i]).append("\n"); |
| 160 | + info.append("GenericParameterType=").append(gpType[i]).append("\n"); |
| 161 | + } |
| 162 | + |
| 163 | + info.append("\nExceptions:"); |
| 164 | + Class<?>[] xType = m.getExceptionTypes(); |
| 165 | + Type[] gxType = m.getGenericExceptionTypes(); |
| 166 | + if (xType.length != 0) { |
| 167 | + info.append("\n"); |
| 168 | + } else { |
| 169 | + info.append("empty\n"); |
| 170 | + } |
| 171 | + for (int i = 0; i < xType.length; i++) { |
| 172 | + info.append("exception [").append(i).append("]:\n"); |
| 173 | + info.append("ExceptionType=").append(xType[i]).append("\n"); |
| 174 | + info.append("GenericExceptionType=").append(gxType[i]).append("\n"); |
| 175 | + } |
| 176 | + |
| 177 | + info.append("\nAnnotations:"); |
| 178 | + if (m.getAnnotations().length != 0) { |
| 179 | + info.append("\n"); |
| 180 | + } else { |
| 181 | + info.append("empty\n"); |
| 182 | + } |
| 183 | + |
| 184 | + for (int i = 0; i < m.getAnnotations().length; i++) { |
| 185 | + info.append("annotation[").append(i).append("]=").append(m.getAnnotations()[i]).append("\n"); |
| 186 | + } |
| 187 | + |
| 188 | + return info.toString(); |
| 189 | + } |
| 190 | + |
| 191 | + private static String getAccessLevel(int modifiers) { |
| 192 | + if (Modifier.isPublic(modifiers)) { |
| 193 | + return "public"; |
| 194 | + } else if (Modifier.isProtected(modifiers)) { |
| 195 | + return "protected"; |
| 196 | + } else if (Modifier.isPrivate(modifiers)) { |
| 197 | + return "private"; |
| 198 | + } else { |
| 199 | + return "default"; |
| 200 | + } |
| 201 | + } |
| 202 | + |
126 | 203 | } |
0 commit comments