1 | package net.sourceforge.retroweaver.runtime.java.lang; |
2 | |
3 | import java.lang.reflect.Constructor; |
4 | import java.lang.reflect.Method; |
5 | |
6 | import net.sourceforge.retroweaver.runtime.java.lang.annotation.AIB; |
7 | import net.sourceforge.retroweaver.runtime.java.lang.annotation.Annotation; |
8 | import net.sourceforge.retroweaver.runtime.java.lang.reflect.GenericSignatureFormatError; |
9 | import net.sourceforge.retroweaver.runtime.java.lang.reflect.MalformedParameterizedTypeException; |
10 | import net.sourceforge.retroweaver.runtime.java.lang.reflect.ReflectionDescriptor; |
11 | import net.sourceforge.retroweaver.runtime.java.lang.reflect.Type; |
12 | import net.sourceforge.retroweaver.runtime.java.lang.reflect.TypeVariable; |
13 | |
14 | /** |
15 | * Replacements for methods added to java.lang.Class in Java 1.5. |
16 | */ |
17 | public final class Class_ { |
18 | |
19 | private Class_() { |
20 | // private constructor |
21 | } |
22 | |
23 | public static boolean isAnnotation( Class c ) { |
24 | return Annotation.class.isAssignableFrom(c); |
25 | } |
26 | |
27 | /** |
28 | * Returns this element's annotation for the specified type if such an annotation is present, else null. |
29 | * |
30 | */ |
31 | public static <T extends Annotation> T getAnnotation( Class c, Class<T> annotationType ) { |
32 | if ( annotationType == null ) { |
33 | throw new NullPointerException( "Null annotationType" ); |
34 | } |
35 | |
36 | return AIB.getAib(c).getClassAnnotation(annotationType); |
37 | } |
38 | |
39 | /** |
40 | * Returns all annotations present on this element. |
41 | */ |
42 | public static Annotation[] getAnnotations( Class c ) { |
43 | return AIB.getAib(c).getClassAnnotations(); |
44 | } |
45 | |
46 | /** |
47 | * Returns all annotations that are directly present on this element. |
48 | */ |
49 | public static Annotation[] getDeclaredAnnotations( Class c ) { |
50 | return AIB.getAib(c).getDeclaredClassAnnotations(); |
51 | } |
52 | |
53 | /** |
54 | * Returns true if an annotation for the specified type is present on this element, else false. |
55 | */ |
56 | public static boolean isAnnotationPresent( Class c, Class<? extends Annotation> annotationType ) { |
57 | return getAnnotation( c, annotationType ) != null; |
58 | } |
59 | |
60 | /** |
61 | * Replacement for Class.asSubclass(Class). |
62 | * |
63 | * @param c a Class |
64 | * @param superclass another Class which must be a superclass of <i>c</i> |
65 | * @return <i>c</i> |
66 | * @throws java.lang.ClassCastException if <i>c</i> is |
67 | */ |
68 | public static Class asSubclass(Class<?> c, Class<?> superclass) { |
69 | if (!superclass.isAssignableFrom(c)) { |
70 | throw new ClassCastException(superclass.getName()); |
71 | } |
72 | return c; |
73 | } |
74 | |
75 | /** |
76 | * Replacement for Class.cast(Object). Throws a ClassCastException if <i>obj</i> |
77 | * is not an instance of class <var>c</var>, or a subtype of <var>c</var>. |
78 | * |
79 | * @param c Class we want to cast <var>obj</var> to |
80 | * @param object object we want to cast |
81 | * @return The object, or <code>null</code> if the object is |
82 | * <code>null</code>. |
83 | * @throws java.lang.ClassCastException if <var>obj</var> is not |
84 | * <code>null</code> or an instance of <var>c</var> |
85 | */ |
86 | public static Object cast(Class c, Object object) { |
87 | if (object == null || c.isInstance(object)) { |
88 | return object; |
89 | } else { |
90 | throw new ClassCastException(c.getName()); |
91 | } |
92 | } |
93 | |
94 | /** |
95 | * Replacement for Class.isEnum(). |
96 | * |
97 | * @param class_ class we want to test. |
98 | * @return true if the class was declared as an Enum. |
99 | */ |
100 | public static <T> boolean isEnum(Class<T> class_) { |
101 | Class c = class_.getSuperclass(); |
102 | |
103 | if (c == null) { |
104 | return false; |
105 | } |
106 | |
107 | return Enum.class.isAssignableFrom(c); |
108 | } |
109 | |
110 | /** |
111 | * Replacement for Class.getEnumConstants(). |
112 | * |
113 | * @param class_ class we want to get Enum constants for. |
114 | * @return The elements of this enum class or null if this does not represent an enum type. |
115 | */ |
116 | public static <T> T[] getEnumConstants(Class<T> class_) { |
117 | if (!isEnum(class_)) { |
118 | return null; |
119 | } |
120 | |
121 | return Enum.getEnumValues(class_).clone(); |
122 | } |
123 | |
124 | /** |
125 | * replacement for Class.isAnonymousClass() |
126 | */ |
127 | public static boolean isAnonymousClass(Class class_) { |
128 | return getSimpleName(class_).length() == 0; |
129 | } |
130 | |
131 | /** |
132 | * replacement for Class.getSimpleName() |
133 | */ |
134 | public static String getSimpleName(Class class_) { |
135 | if (class_.isArray()) { |
136 | return getSimpleName(class_.getComponentType()) + "[]"; |
137 | } |
138 | |
139 | String className = class_.getName(); |
140 | |
141 | int i = className.lastIndexOf('$'); |
142 | if (i != -1) { |
143 | do { |
144 | i++; |
145 | } while (i < className.length() && Character.isDigit(className.charAt(i))); |
146 | return className.substring(i); |
147 | } |
148 | |
149 | return className.substring(className.lastIndexOf('.') + 1); |
150 | } |
151 | |
152 | /** |
153 | * replacement for Class.isSynthetic() |
154 | */ |
155 | public static boolean isSynthetic(Class class_) { |
156 | throw new UnsupportedOperationException("NotImplemented"); |
157 | } |
158 | |
159 | public static TypeVariable[] getTypeParameters(Class class_) |
160 | throws GenericSignatureFormatError { |
161 | return ReflectionDescriptor.getReflectionDescriptor(class_).getTypeParameters(); |
162 | } |
163 | |
164 | public static Type getGenericSuperclass(Class class_) |
165 | throws GenericSignatureFormatError, TypeNotPresentException, |
166 | MalformedParameterizedTypeException |
167 | { |
168 | return ReflectionDescriptor.getReflectionDescriptor(class_).getGenericSuperclass(); |
169 | } |
170 | |
171 | public static Type[] getGenericInterfaces(Class class_) throws GenericSignatureFormatError, |
172 | TypeNotPresentException, MalformedParameterizedTypeException |
173 | { |
174 | return ReflectionDescriptor.getReflectionDescriptor(class_).getGenericInterfaces(); |
175 | } |
176 | |
177 | public static Method getEnclosingMethod(Class class_) { |
178 | return ReflectionDescriptor.getReflectionDescriptor(class_).getEnclosingMethod(); |
179 | } |
180 | |
181 | public static Constructor<?> getEnclosingConstructor(Class class_) { |
182 | return ReflectionDescriptor.getReflectionDescriptor(class_).getEnclosingConstructor(); |
183 | } |
184 | |
185 | public static Class<?> getEnclosingClass(Class class_) { |
186 | return ReflectionDescriptor.getReflectionDescriptor(class_).getEnclosingClass(); |
187 | } |
188 | |
189 | public static String getCanonicalName(Class class_) { |
190 | if (class_.isArray()) { |
191 | Class component = class_.getComponentType(); |
192 | String s = getCanonicalName(component); |
193 | return s == null ? null : s + "[]"; |
194 | } |
195 | |
196 | if (isLocalClass(class_) || isAnonymousClass(class_)) { |
197 | return null; |
198 | } |
199 | |
200 | return class_.getName().replace('$', '.'); |
201 | } |
202 | |
203 | public static boolean isLocalClass(Class class_) { |
204 | return getEnclosingMethod(class_) != null && !isAnonymousClass(class_); |
205 | } |
206 | |
207 | public static boolean isMemberClass(Class class_) { |
208 | if (getEnclosingClass(class_) != null) { |
209 | return !(isLocalClass(class_) || isAnonymousClass(class_)); |
210 | } |
211 | return false; |
212 | } |
213 | |
214 | } |