1 /* VMClass.java -- VM Specific Class methods 2 Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 package java.lang; 39 40 import java.lang.annotation.Annotation; 41 import java.lang.reflect.Array; 42 import java.lang.reflect.Constructor; 43 import java.lang.reflect.Field; 44 import java.lang.reflect.Method; 45 import java.lang.reflect.Modifier; 46 47 /* 48 * This class is a reference version, mainly for compiling a class library 49 * jar. It is likely that VM implementers replace this with their own 50 * version that can communicate effectively with the VM. 51 */ 52 53 /** 54 * 55 * @author Etienne Gagnon (etienne.gagnon@uqam.ca) 56 * @author Archie Cobbs (archie@dellroad.org) 57 * @author C. Brian Jones (cbj@gnu.org) 58 * @author Tom Tromey (tromey@cygnus.com) 59 * @author Andrew John Hughes (gnu_andrew@member.fsf.org) 60 */ 61 final class VMClass 62 { 63 64 // Only static methods. Cannot be instantiated. VMClass()65 private VMClass() 66 { 67 } 68 69 /** 70 * Discover whether an Object is an instance of this Class. Think of it 71 * as almost like <code>o instanceof (this class)</code>. 72 * 73 * @param klass the Class object that's calling us 74 * @param o the Object to check 75 * @return whether o is an instance of this class 76 * @since 1.1 77 */ isInstance(Class klass, Object o)78 static native boolean isInstance(Class klass, Object o); 79 80 /** 81 * Discover whether an instance of the Class parameter would be an 82 * instance of this Class as well. Think of doing 83 * <code>isInstance(c.newInstance())</code> or even 84 * <code>c.newInstance() instanceof (this class)</code>. While this 85 * checks widening conversions for objects, it must be exact for primitive 86 * types. 87 * 88 * @param klass the Class object that's calling us 89 * @param c the class to check 90 * @return whether an instance of c would be an instance of this class 91 * as well 92 * @throws NullPointerException if c is null 93 * @since 1.1 94 */ isAssignableFrom(Class klass, Class c)95 static native boolean isAssignableFrom(Class klass, Class c); 96 97 /** 98 * Check whether this class is an interface or not. Array types are not 99 * interfaces. 100 * 101 * @param klass the Class object that's calling us 102 * @return whether this class is an interface or not 103 */ isInterface(Class klass)104 static native boolean isInterface(Class klass); 105 106 /** 107 * Return whether this class is a primitive type. A primitive type class 108 * is a class representing a kind of "placeholder" for the various 109 * primitive types, or void. You can access the various primitive type 110 * classes through java.lang.Boolean.TYPE, java.lang.Integer.TYPE, etc., 111 * or through boolean.class, int.class, etc. 112 * 113 * @param klass the Class object that's calling us 114 * @return whether this class is a primitive type 115 * @see Boolean#TYPE 116 * @see Byte#TYPE 117 * @see Character#TYPE 118 * @see Short#TYPE 119 * @see Integer#TYPE 120 * @see Long#TYPE 121 * @see Float#TYPE 122 * @see Double#TYPE 123 * @see Void#TYPE 124 * @since 1.1 125 */ isPrimitive(Class klass)126 static native boolean isPrimitive(Class klass); 127 128 /** 129 * Get the name of this class, separated by dots for package separators. 130 * Primitive types and arrays are encoded as: 131 * <pre> 132 * boolean Z 133 * byte B 134 * char C 135 * short S 136 * int I 137 * long J 138 * float F 139 * double D 140 * void V 141 * array type [<em>element type</em> 142 * class or interface, alone: <dotted name> 143 * class or interface, as element type: L<dotted name>; 144 * 145 * @param klass the Class object that's calling us 146 * @return the name of this class 147 */ getName(Class klass)148 static native String getName(Class klass); 149 150 /** 151 * Get the direct superclass of this class. If this is an interface, 152 * Object, a primitive type, or void, it will return null. If this is an 153 * array type, it will return Object. 154 * 155 * @param klass the Class object that's calling us 156 * @return the direct superclass of this class 157 */ getSuperclass(Class klass)158 static native Class getSuperclass(Class klass); 159 160 /** 161 * Get the interfaces this class <EM>directly</EM> implements, in the 162 * order that they were declared. This returns an empty array, not null, 163 * for Object, primitives, void, and classes or interfaces with no direct 164 * superinterface. Array types return Cloneable and Serializable. 165 * 166 * @param klass the Class object that's calling us 167 * @return the interfaces this class directly implements 168 */ getInterfaces(Class klass)169 static native Class[] getInterfaces(Class klass); 170 171 /** 172 * If this is an array, get the Class representing the type of array. 173 * Examples: "[[Ljava.lang.String;" would return "[Ljava.lang.String;", and 174 * calling getComponentType on that would give "java.lang.String". If 175 * this is not an array, returns null. 176 * 177 * @param klass the Class object that's calling us 178 * @return the array type of this class, or null 179 * @see Array 180 * @since 1.1 181 */ getComponentType(Class klass)182 static native Class getComponentType(Class klass); 183 184 /** 185 * Get the modifiers of this class. These can be decoded using Modifier, 186 * and is limited to one of public, protected, or private, and any of 187 * final, static, abstract, or interface. An array class has the same 188 * public, protected, or private modifier as its component type, and is 189 * marked final but not an interface. Primitive types and void are marked 190 * public and final, but not an interface. 191 * 192 * @param klass the Class object that's calling us 193 * @param ignoreInnerClassesAttrib if set, return the real modifiers, not 194 * the ones specified in the InnerClasses attribute. 195 * @return the modifiers of this class 196 * @see Modifier 197 * @since 1.1 198 */ getModifiers(Class klass, boolean ignoreInnerClassesAttrib)199 static native int getModifiers(Class klass, boolean ignoreInnerClassesAttrib); 200 201 /** 202 * If this is a nested or inner class, return the class that declared it. 203 * If not, return null. 204 * 205 * @param klass the Class object that's calling us 206 * @return the declaring class of this class 207 * @since 1.1 208 */ getDeclaringClass(Class klass)209 static native Class getDeclaringClass(Class klass); 210 211 /** 212 * Like <code>getDeclaredClasses()</code> but without the security checks. 213 * 214 * @param klass the Class object that's calling us 215 * @param publicOnly Only public classes should be returned 216 */ getDeclaredClasses(Class klass, boolean publicOnly)217 static native Class[] getDeclaredClasses(Class klass, boolean publicOnly); 218 219 /** 220 * Like <code>getDeclaredFields()</code> but without the security checks. 221 * 222 * @param klass the Class object that's calling us 223 * @param publicOnly Only public fields should be returned 224 */ getDeclaredFields(Class klass, boolean publicOnly)225 static native Field[] getDeclaredFields(Class klass, boolean publicOnly); 226 227 /** 228 * Like <code>getDeclaredMethods()</code> but without the security checks. 229 * 230 * @param klass the Class object that's calling us 231 * @param publicOnly Only public methods should be returned 232 */ getDeclaredMethods(Class klass, boolean publicOnly)233 static native Method[] getDeclaredMethods(Class klass, boolean publicOnly); 234 235 /** 236 * Like <code>getDeclaredConstructors()</code> but without 237 * the security checks. 238 * 239 * @param klass the Class object that's calling us 240 * @param publicOnly Only public constructors should be returned 241 */ getDeclaredConstructors(Class klass, boolean publicOnly)242 static native Constructor[] getDeclaredConstructors(Class klass, boolean publicOnly); 243 244 /** 245 * Return the class loader of this class. 246 * 247 * @param klass the Class object that's calling us 248 * @return the class loader 249 */ getClassLoader(Class klass)250 static native ClassLoader getClassLoader(Class klass); 251 252 /** 253 * Load the requested class and record the specified loader as the 254 * initiating class loader. 255 * 256 * @param name the name of the class to find 257 * @param initialize should the class initializer be run? 258 * @param loader the class loader to use (or null for the bootstrap loader) 259 * @return the Class object representing the class or null for noop 260 * @throws ClassNotFoundException if the class was not found by the 261 * class loader 262 * @throws LinkageError if linking the class fails 263 * @throws ExceptionInInitializerError if the class loads, but an exception 264 * occurs during initialization 265 */ forName(String name, boolean initialize, ClassLoader loader)266 static native Class forName(String name, boolean initialize, 267 ClassLoader loader) 268 throws ClassNotFoundException; 269 270 /** 271 * Return whether this class is an array type. 272 * 273 * @param klass the Class object that's calling us 274 * @return true if this class is an array type 275 * operation 276 */ isArray(Class klass)277 static native boolean isArray(Class klass); 278 279 /** 280 * Throw a checked exception without declaring it. 281 */ throwException(Throwable t)282 static native void throwException(Throwable t); 283 284 /** 285 * Returns the simple name for the specified class, as used in the source 286 * code. For normal classes, this is the content returned by 287 * <code>getName()</code> which follows the last ".". Anonymous 288 * classes have no name, and so the result of calling this method is 289 * "". The simple name of an array consists of the simple name of 290 * its component type, followed by "[]". Thus, an array with the 291 * component type of an anonymous class has a simple name of simply 292 * "[]". 293 * 294 * @param klass the class whose simple name should be returned. 295 * @return the simple name for this class. 296 */ getSimpleName(Class klass)297 static String getSimpleName(Class klass) 298 { 299 if (isAnonymousClass(klass)) 300 return ""; 301 if (isArray(klass)) 302 { 303 return getComponentType(klass).getSimpleName() + "[]"; 304 } 305 String fullName = getName(klass); 306 Class enclosingClass = getEnclosingClass(klass); 307 if (enclosingClass == null) 308 // It's a top level class. 309 return fullName.substring(fullName.lastIndexOf(".") + 1); 310 311 fullName = fullName.substring(enclosingClass.getName().length()); 312 313 // We've carved off the enclosing class name; now we must have '$' 314 // followed optionally by digits, followed by the class name. 315 int pos = 1; 316 while (Character.isDigit(fullName.charAt(pos))) 317 ++pos; 318 fullName = fullName.substring(pos); 319 320 return fullName; 321 } 322 323 /** 324 * Returns all annotations directly defined by the specified class. If 325 * there are no annotations associated with this class, then a zero-length 326 * array will be returned. The returned array may be modified by the client 327 * code, but this will have no effect on the annotation content of this 328 * class, and hence no effect on the return value of this method for 329 * future callers. 330 * 331 * @param klass the class whose annotations should be returned. 332 * @return the annotations directly defined by the specified class. 333 * @since 1.5 334 */ getDeclaredAnnotations(Class klass)335 static native Annotation[] getDeclaredAnnotations(Class klass); 336 337 /** 338 * <p> 339 * Returns the canonical name of the specified class, as defined by section 340 * 6.7 of the Java language specification. Each package, top-level class, 341 * top-level interface and primitive type has a canonical name. A member 342 * class has a canonical name, if its parent class has one. Likewise, 343 * an array type has a canonical name, if its component type does. 344 * Local or anonymous classes do not have canonical names. 345 * </p> 346 * <p> 347 * The canonical name for top-level classes, top-level interfaces and 348 * primitive types is always the same as the fully-qualified name. 349 * For array types, the canonical name is the canonical name of its 350 * component type with `[]' appended. 351 * </p> 352 * <p> 353 * The canonical name of a member class always refers to the place where 354 * the class was defined, and is composed of the canonical name of the 355 * defining class and the simple name of the member class, joined by `.'. 356 * For example, if a <code>Person</code> class has an inner class, 357 * <code>M</code>, then both its fully-qualified name and canonical name 358 * is <code>Person.M</code>. A subclass, <code>Staff</code>, of 359 * <code>Person</code> refers to the same inner class by the fully-qualified 360 * name of <code>Staff.M</code>, but its canonical name is still 361 * <code>Person.M</code>. 362 * </p> 363 * <p> 364 * Where no canonical name is present, <code>null</code> is returned. 365 * </p> 366 * 367 * @param klass the class whose canonical name should be retrieved. 368 * @return the canonical name of the class, or <code>null</code> if the 369 * class doesn't have a canonical name. 370 * @since 1.5 371 */ getCanonicalName(Class klass)372 static String getCanonicalName(Class klass) 373 { 374 if (isLocalClass(klass) || isAnonymousClass(klass)) 375 return null; 376 if (isArray(klass)) 377 { 378 String componentName = getComponentType(klass).getCanonicalName(); 379 if (componentName != null) 380 return componentName + "[]"; 381 } 382 if (isMemberClass(klass)) 383 { 384 String memberName = getDeclaringClass(klass).getCanonicalName(); 385 if (memberName != null) 386 return memberName + "." + getSimpleName(klass); 387 else 388 return memberName; 389 } 390 return getName(klass); 391 } 392 393 /** 394 * Returns the class which immediately encloses the specified class. If 395 * the class is a top-level class, this method returns <code>null</code>. 396 * 397 * @param klass the class whose enclosing class should be returned. 398 * @return the immediate enclosing class, or <code>null</code> if this is 399 * a top-level class. 400 * @since 1.5 401 */ getEnclosingClass(Class klass)402 static native Class getEnclosingClass(Class klass); 403 404 /** 405 * Returns the constructor which immediately encloses the specified class. 406 * If the class is a top-level class, or a local or anonymous class 407 * immediately enclosed by a type definition, instance initializer 408 * or static initializer, then <code>null</code> is returned. 409 * 410 * @param klass the class whose enclosing constructor should be returned. 411 * @return the immediate enclosing constructor if the specified class is 412 * declared within a constructor. Otherwise, <code>null</code> 413 * is returned. 414 * @since 1.5 415 */ getEnclosingConstructor(Class klass)416 static native Constructor getEnclosingConstructor(Class klass); 417 418 /** 419 * Returns the method which immediately encloses the specified class. If 420 * the class is a top-level class, or a local or anonymous class 421 * immediately enclosed by a type definition, instance initializer 422 * or static initializer, then <code>null</code> is returned. 423 * 424 * @param klass the class whose enclosing method should be returned. 425 * @return the immediate enclosing method if the specified class is 426 * declared within a method. Otherwise, <code>null</code> 427 * is returned. 428 * @since 1.5 429 */ getEnclosingMethod(Class klass)430 static native Method getEnclosingMethod(Class klass); 431 432 /** 433 * Returns the class signature as specified in Class File Format 434 * chapter in the VM specification, or null if the class is not 435 * generic. 436 * 437 * @param klass the klass to test. 438 * @return a ClassSignature string. 439 * @since 1.5 440 */ getClassSignature(Class klass)441 static native String getClassSignature(Class klass); 442 443 /** 444 * Returns true if the specified class represents an anonymous class. 445 * 446 * @param klass the klass to test. 447 * @return true if the specified class represents an anonymous class. 448 * @since 1.5 449 */ isAnonymousClass(Class klass)450 static native boolean isAnonymousClass(Class klass); 451 452 /** 453 * Returns true if the specified class represents an local class. 454 * 455 * @param klass the klass to test. 456 * @return true if the specified class represents an local class. 457 * @since 1.5 458 */ isLocalClass(Class klass)459 static native boolean isLocalClass(Class klass); 460 461 /** 462 * Returns true if the specified class represents an member class. 463 * 464 * @param klass the klass to test. 465 * @return true if the specified class represents an member class. 466 * @since 1.5 467 */ isMemberClass(Class klass)468 static native boolean isMemberClass(Class klass); 469 470 } // class VMClass 471