KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > lang > reflect > _Method


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.java.lang.reflect;
33
34 import java.lang.reflect.*;
35 import net.sf.retrotranslator.runtime.asm.Opcodes;
36 import net.sf.retrotranslator.runtime.impl.*;
37 import net.sf.retrotranslator.runtime.java.lang.annotation.Annotation_;
38
39 /**
40  * @author Taras Puchko
41  */

42 public class _Method {
43
44     public static Annotation_ getAnnotation(Method method, Class JavaDoc annotationType) {
45         return MethodDescriptor.getInstance(method).getAnnotation(annotationType);
46     }
47
48     public static Annotation_[] getAnnotations(Method method) {
49         return MethodDescriptor.getInstance(method).getAnnotations();
50     }
51
52     public static Annotation_[] getDeclaredAnnotations(Method method) {
53         return MethodDescriptor.getInstance(method).getDeclaredAnnotations();
54     }
55
56     public static Object JavaDoc getDefaultValue(Method method) {
57         return MethodDescriptor.getInstance(method).getDefaultValue();
58     }
59
60     public static Type[] getGenericExceptionTypes(Method method) {
61         Type[] types = MethodDescriptor.getInstance(method).getGenericExceptionTypes();
62         return types != null ? types : method.getExceptionTypes();
63     }
64
65     public static Type[] getGenericParameterTypes(Method method) {
66         Type[] types = MethodDescriptor.getInstance(method).getGenericParameterTypes();
67         return types != null ? types : method.getParameterTypes();
68     }
69
70     public static Type getGenericReturnType(Method method) {
71         Type type = MethodDescriptor.getInstance(method).getGenericReturnType();
72         return type != null ? type : method.getReturnType();
73     }
74
75     public static Annotation_[][] getParameterAnnotations(Method method) {
76         return MethodDescriptor.getInstance(method).getParameterAnnotations();
77     }
78
79     public static TypeVariable[] getTypeParameters(Method method) {
80         return MethodDescriptor.getInstance(method).getTypeParameters();
81     }
82
83     public static boolean isAnnotationPresent(Method method, Class JavaDoc annotationType) {
84         return MethodDescriptor.getInstance(method).isAnnotationPresent(annotationType);
85     }
86
87     public static boolean isBridge(Method method) {
88         return MethodDescriptor.getInstance(method).isAccess(Opcodes.ACC_BRIDGE);
89     }
90
91     public static boolean isSynthetic(Method method) {
92         return MethodDescriptor.getInstance(method).isAccess(Opcodes.ACC_SYNTHETIC);
93     }
94
95     public static boolean isVarArgs(Method method) {
96         return MethodDescriptor.getInstance(method).isAccess(Opcodes.ACC_VARARGS);
97     }
98
99     public static String JavaDoc toGenericString(Method method) {
100         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
101         if (method.getModifiers() != 0) {
102             builder.append(Modifier.toString(method.getModifiers())).append(' ');
103         }
104         TypeVariable[] typeParameters = getTypeParameters(method);
105         if (typeParameters.length > 0) {
106             RuntimeTools.append(builder.append('<'), typeParameters).append("> ");
107         }
108         builder.append(RuntimeTools.getString(getGenericReturnType(method))).append(' ');
109         builder.append(RuntimeTools.getString(method.getDeclaringClass())).append('.').append(method.getName());
110         RuntimeTools.append(builder.append('('), getGenericParameterTypes(method)).append(')');
111         Type[] exceptionTypes = getGenericExceptionTypes(method);
112         if (exceptionTypes.length > 0) {
113             RuntimeTools.append(builder.append(" throws "), exceptionTypes);
114         }
115         return builder.toString();
116     }
117
118 }
119
Popular Tags