KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > lang > _Class


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;
33
34 import java.lang.reflect.*;
35 import java.util.Arrays JavaDoc;
36 import net.sf.retrotranslator.runtime.asm.Opcodes;
37 import net.sf.retrotranslator.runtime.impl.*;
38 import net.sf.retrotranslator.runtime.java.lang.annotation.Annotation_;
39
40 /**
41  * @author Taras Puchko
42  */

43 public class _Class {
44
45     public static Class JavaDoc asSubclass(Class JavaDoc aClass, Class JavaDoc superclass) {
46         if (superclass.isAssignableFrom(aClass)) return aClass;
47         throw new ClassCastException JavaDoc(aClass.toString());
48     }
49
50     public static Object JavaDoc cast(Class JavaDoc aClass, Object JavaDoc obj) {
51         if (obj == null || aClass.isInstance(obj)) return obj;
52         throw new ClassCastException JavaDoc(aClass.toString());
53     }
54
55     public static Annotation_ getAnnotation(Class JavaDoc aClass, Class JavaDoc annotationType) {
56         return ClassDescriptor.getInstance(aClass).getAnnotation(annotationType);
57     }
58
59     public static Annotation_[] getAnnotations(final Class JavaDoc aClass) {
60         return ClassDescriptor.getInstance(aClass).getAnnotations();
61     }
62
63     public static String JavaDoc getCanonicalName(Class JavaDoc aClass) {
64         if (aClass.isArray()) {
65             String JavaDoc name = getCanonicalName(aClass.getComponentType());
66             return name == null ? null : name + "[]";
67         }
68         if (ClassDescriptor.getInstance(aClass).isLocalOrAnonymous()) return null;
69         Class JavaDoc declaringClass = aClass.getDeclaringClass();
70         return declaringClass == null ? aClass.getName()
71                 : getCanonicalName(declaringClass) + "." + getSimpleName(aClass);
72     }
73
74     public static Annotation_[] getDeclaredAnnotations(Class JavaDoc aClass) {
75         return ClassDescriptor.getInstance(aClass).getDeclaredAnnotations();
76     }
77
78     @Advanced
79     public static Method getDeclaredMethod(Class JavaDoc aClass, String JavaDoc name, Class JavaDoc... parameterTypes)
80             throws NoSuchMethodException JavaDoc, SecurityException JavaDoc {
81         Method method = findMethod(aClass.getDeclaredMethods(), name, parameterTypes);
82         return method != null ? method : aClass.getDeclaredMethod(name, parameterTypes);
83     }
84
85     public static Class JavaDoc getEnclosingClass(Class JavaDoc aClass) {
86         MethodDescriptor descriptor = ClassDescriptor.getInstance(aClass).getEnclosingMethodDescriptor();
87         return descriptor == null ? aClass.getDeclaringClass() : descriptor.getClassDescriptor().getTarget();
88     }
89
90     public static Constructor getEnclosingConstructor(Class JavaDoc aClass) {
91         MethodDescriptor descriptor = ClassDescriptor.getInstance(aClass).getEnclosingMethodDescriptor();
92         return descriptor == null ? null : descriptor.getConstructor();
93     }
94
95     public static Method getEnclosingMethod(Class JavaDoc aClass) {
96         MethodDescriptor descriptor = ClassDescriptor.getInstance(aClass).getEnclosingMethodDescriptor();
97         return descriptor == null ? null : descriptor.getMethod();
98     }
99
100     public static Object JavaDoc[] getEnumConstants(Class JavaDoc aClass) {
101         Object JavaDoc[] constants = Enum_.getEnumConstants(aClass);
102         return constants == null ? null : constants.clone();
103     }
104
105     public static Type[] getGenericInterfaces(Class JavaDoc aClass) {
106         Type[] interfaces = ClassDescriptor.getInstance(aClass).getGenericInterfaces();
107         return interfaces != null ? interfaces : aClass.getInterfaces();
108     }
109
110     public static Type getGenericSuperclass(Class JavaDoc aClass) {
111         Type genericSuperclass = ClassDescriptor.getInstance(aClass).getGenericSuperclass();
112         return genericSuperclass != null ? genericSuperclass : aClass.getSuperclass();
113     }
114
115     @Advanced
116     public static Method getMethod(Class JavaDoc aClass, String JavaDoc name, Class JavaDoc... parameterTypes)
117             throws NoSuchMethodException JavaDoc, SecurityException JavaDoc {
118         Method method = findMethod(aClass.getMethods(), name, parameterTypes);
119         return method != null ? method : aClass.getMethod(name, parameterTypes);
120     }
121
122     public static String JavaDoc getSimpleName(Class JavaDoc aClass) {
123         if (aClass.isArray()) return getSimpleName(aClass.getComponentType()) + "[]";
124         String JavaDoc thisName = aClass.getName();
125         Class JavaDoc enclosingClass = getEnclosingClass(aClass);
126         if (enclosingClass == null) return thisName.substring(thisName.lastIndexOf('.') + 1);
127         String JavaDoc enclosingName = enclosingClass.getName();
128         if (!thisName.startsWith(enclosingName)) throw new InternalError JavaDoc();
129         String JavaDoc name = thisName.substring(enclosingName.length());
130         if (!name.startsWith("$")) throw new InternalError JavaDoc();
131         for (int i = 1; i < name.length(); i++) {
132             char c = name.charAt(i);
133             if (c < '0' || c > '9') return name.substring(i);
134         }
135         return "";
136     }
137
138     public static TypeVariable[] getTypeParameters(Class JavaDoc aClass) {
139         return ClassDescriptor.getInstance(aClass).getTypeParameters();
140     }
141
142     public static boolean isAnnotation(Class JavaDoc aClass) {
143         return ClassDescriptor.getInstance(aClass).isAccess(Opcodes.ACC_ANNOTATION);
144     }
145
146     public static boolean isAnnotationPresent(Class JavaDoc aClass, Class JavaDoc annotationType) {
147         return ClassDescriptor.getInstance(aClass).isAnnotationPresent(annotationType);
148     }
149
150     public static boolean isAnonymousClass(Class JavaDoc aClass) {
151         return "".equals(getSimpleName(aClass));
152     }
153
154     public static boolean isEnum(Class JavaDoc aClass) {
155         return aClass.getSuperclass() == Enum JavaDoc.class && ClassDescriptor.getInstance(aClass).isAccess(Opcodes.ACC_ENUM);
156     }
157
158     public static boolean isLocalClass(Class JavaDoc aClass) {
159         return ClassDescriptor.getInstance(aClass).isLocalOrAnonymous() && !isAnonymousClass(aClass);
160     }
161
162     public static boolean isMemberClass(Class JavaDoc aClass) {
163         return aClass.getDeclaringClass() != null;
164     }
165
166     public static boolean isSynthetic(Class JavaDoc aClass) {
167         return ClassDescriptor.getInstance(aClass).isAccess(Opcodes.ACC_SYNTHETIC);
168     }
169
170     private static Method findMethod(Method[] methods, String JavaDoc name, Class JavaDoc... parameterTypes) {
171         Method result = null;
172         for (Method method : methods) {
173             if (method.getName().equals(name) && Arrays.equals(method.getParameterTypes(), parameterTypes) &&
174                     (result == null || result.getReturnType().isAssignableFrom(method.getReturnType()))) {
175                 result = method;
176             }
177         }
178         return result;
179     }
180 }
181
Popular Tags