001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.lang.reflect;
032:
033: import java.lang.annotation.Annotation;
034: import java.lang.reflect.*;
035: import net.sf.retrotranslator.runtime.asm.Opcodes;
036: import net.sf.retrotranslator.runtime.impl.*;
037:
038: /**
039: * @author Taras Puchko
040: */
041: public class _Method {
042:
043: public static Annotation getAnnotation(Method method,
044: Class annotationType) {
045: return MethodDescriptor.getInstance(method).getAnnotation(
046: annotationType);
047: }
048:
049: public static Annotation[] getAnnotations(Method method) {
050: return MethodDescriptor.getInstance(method).getAnnotations();
051: }
052:
053: public static Annotation[] getDeclaredAnnotations(Method method) {
054: return MethodDescriptor.getInstance(method)
055: .getDeclaredAnnotations();
056: }
057:
058: public static Object getDefaultValue(Method method) {
059: return MethodDescriptor.getInstance(method).getDefaultValue();
060: }
061:
062: public static Type[] getGenericExceptionTypes(Method method) {
063: return RuntimeTools.getTypes(method.getExceptionTypes(),
064: MethodDescriptor.getInstance(method)
065: .getGenericExceptionTypes());
066: }
067:
068: public static Type[] getGenericParameterTypes(Method method) {
069: return RuntimeTools.getTypes(method.getParameterTypes(),
070: MethodDescriptor.getInstance(method)
071: .getGenericParameterTypes());
072: }
073:
074: public static Type getGenericReturnType(Method method) {
075: return RuntimeTools.getType(method.getReturnType(),
076: MethodDescriptor.getInstance(method)
077: .getGenericReturnType());
078: }
079:
080: public static Annotation[][] getParameterAnnotations(Method method) {
081: return MethodDescriptor.getInstance(method)
082: .getParameterAnnotations();
083: }
084:
085: public static TypeVariable[] getTypeParameters(Method method) {
086: return MethodDescriptor.getInstance(method).getTypeParameters();
087: }
088:
089: public static boolean isAnnotationPresent(Method method,
090: Class annotationType) {
091: return MethodDescriptor.getInstance(method)
092: .isAnnotationPresent(annotationType);
093: }
094:
095: public static boolean isBridge(Method method) {
096: return MethodDescriptor.getInstance(method).isAccess(
097: Opcodes.ACC_BRIDGE);
098: }
099:
100: public static boolean isSynthetic(Method method) {
101: return MethodDescriptor.getInstance(method).isAccess(
102: Opcodes.ACC_SYNTHETIC);
103: }
104:
105: public static boolean isVarArgs(Method method) {
106: return MethodDescriptor.getInstance(method).isAccess(
107: Opcodes.ACC_VARARGS);
108: }
109:
110: public static String toGenericString(Method method) {
111: StringBuilder builder = new StringBuilder();
112: if (method.getModifiers() != 0) {
113: builder.append(Modifier.toString(method.getModifiers()))
114: .append(' ');
115: }
116: TypeVariable[] typeParameters = getTypeParameters(method);
117: if (typeParameters.length > 0) {
118: RuntimeTools.append(builder.append('<'), typeParameters)
119: .append("> ");
120: }
121: builder.append(
122: RuntimeTools.getString(getGenericReturnType(method)))
123: .append(' ');
124: builder.append(
125: RuntimeTools.getString(method.getDeclaringClass()))
126: .append('.').append(method.getName());
127: RuntimeTools.append(builder.append('('),
128: getGenericParameterTypes(method)).append(')');
129: Type[] exceptionTypes = getGenericExceptionTypes(method);
130: if (exceptionTypes.length > 0) {
131: RuntimeTools.append(builder.append(" throws "),
132: exceptionTypes);
133: }
134: return builder.toString();
135: }
136:
137: }
|