Class.java revision 70b617fe562806bc3e15452a2792fec4355bd54f
151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. 351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it 651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as 751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation. Oracle designates this 851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided 951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code. 1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT 1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that 1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code). 1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version 1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation, 1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any 2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions. 2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.lang; 2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Array; 2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.GenericArrayType; 3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Member; 3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Field; 3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Method; 3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Constructor; 3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.GenericDeclaration; 3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Modifier; 3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Type; 3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.TypeVariable; 3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.InvocationTargetException; 3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.ref.SoftReference; 4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.InputStream; 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.ObjectStreamField; 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.security.AccessController; 4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.security.PrivilegedAction; 4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.ArrayList; 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Arrays; 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Collection; 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.HashSet; 4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Iterator; 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.List; 5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.LinkedList; 5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.LinkedHashSet; 5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Set; 5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Map; 5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.HashMap; 5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.misc.Unsafe; 5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.reflect.CallerSensitive; 5770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniakimport java.lang.annotation.Inherited; 5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.annotation.Annotation; 5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Proxy; 6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.reflect.annotation.*; 6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.reflect.misc.ReflectUtil; 6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 63a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport java.io.Serializable; 64a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport java.lang.reflect.AccessibleObject; 65a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport com.android.dex.Dex; 66a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport dalvik.system.VMStack; 67a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.AnnotationAccess; 68a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.InternalNames; 69a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.GenericSignatureParser; 70a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.Types; 71a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.BasicLruCache; 72a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.CollectionUtils; 73a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.EmptyArray; 74a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.SneakyThrow; 751ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamathimport java.util.Collections; 76a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Instances of the class {@code Class} represent classes and 7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces in a running Java application. An enum is a kind of 8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class and an annotation is a kind of interface. Every array also 8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * belongs to a class that is reflected as a {@code Class} object 8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that is shared by all arrays with the same element type and number 8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of dimensions. The primitive Java types ({@code boolean}, 8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code byte}, {@code char}, {@code short}, 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int}, {@code long}, {@code float}, and 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code double}), and the keyword {@code void} are also 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented as {@code Class} objects. 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> {@code Class} has no public constructor. Instead {@code Class} 9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects are constructed automatically by the Java Virtual Machine as classes 9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are loaded and by calls to the {@code defineClass} method in the class 9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader. 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The following example uses a {@code Class} object to print the 9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class name of an object: 9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> <blockquote><pre> 9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * void printClassName(Object obj) { 9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * System.out.println("The class of " + obj + 10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * " is " + obj.getClass().getName()); 10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * } 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> It is also possible to get the {@code Class} object for a named 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type (or for void) using a class literal. See Section 15.8.2 of 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>. 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For example: 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> <blockquote> 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());} 11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param <T> the type of the class modeled by this {@code Class} 11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. For example, the type of {@code String.class} is {@code 11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class<String>}. Use {@code Class<?>} if the class being modeled is 11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * unknown. 11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author unascribed 11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#defineClass(byte[], int, int) 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0 12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic final 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski class Class<T> implements java.io.Serializable, 12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski java.lang.reflect.GenericDeclaration, 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski java.lang.reflect.Type, 12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski java.lang.reflect.AnnotatedElement { 1271ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** use serialVersionUID from JDK 1.1 for interoperability */ 1281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private static final long serialVersionUID = 3206093459760846163L; 1291ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1301ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** defining class loader, or null for the "bootstrap" system loader. */ 1311ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient ClassLoader classLoader; 1321ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1331ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1341ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * For array classes, the component class object for instanceof/checkcast (for String[][][], 1351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * this will be String[][]). null for non-array classes. 1361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1371ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Class<?> componentType; 1381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1391ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * DexCache of resolved constant pool entries. Will be null for certain runtime-generated classes 1401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * e.g. arrays and primitive classes. 1411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient DexCache dexCache; 1431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * The interface table (iftable_) contains pairs of a interface class and an array of the 1461ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * interface methods. There is one pair per interface supported by this class. That 1471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * means one pair for each interface we support directly, indirectly via superclass, or 1481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * indirectly via a superinterface. This will be null if neither we nor our superclass 1491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * implement any interfaces. 1501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 1511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()". 1521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Invoke faceObj.blah(), where "blah" is part of the Face interface. We can't easily use a 1531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * single vtable. 1541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 1551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * For every interface a concrete class implements, we create an array of the concrete vtable_ 1561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * methods for the methods in the interface. 1571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Object[] ifTable; 1591ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Lazily computed name of this class; always prefer calling getName(). */ 1611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient String name; 1621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1631ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** The superclass, or null if this is java.lang.Object, an interface or primitive type. */ 1641ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Class<? super T> superClass; 1651ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 16670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** 16770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * If class verify fails, we must return same error on subsequent tries. We may store either 16870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * the class of the error, or an actual instance of Throwable here. 16970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 17070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private transient Object verifyError; 1711ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1721ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1731ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass 1741ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * is copied in, and virtual methods from our class either replace those from the super or are 1751ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * appended. For abstract classes, methods may be created in the vtable that aren't in 1761ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * virtual_ methods_ for miranda methods. 1771ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1781ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Object vtable; 1791ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 18070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** Short-cut to dexCache.strings */ 18170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private transient long dexCacheStrings; 18270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 1831ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** access flags; low 16 bits are defined by VM spec */ 1841ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int accessFlags; 1851ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1861ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** static, private, and <init> methods. */ 1871ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient long directMethods; 1881ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1891ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1901ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Instance fields. These describe the layout of the contents of an Object. Note that only the 1911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * fields directly declared by this class are listed in iFields; fields declared by a 1921ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * superclass are listed in the superclass's Class.iFields. 1931ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 1941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * All instance fields that refer to objects are guaranteed to be at the beginning of the field 1951ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * list. {@link Class#numReferenceInstanceFields} specifies the number of reference fields. 1961ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1971ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient long iFields; 1981ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1991ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Static fields */ 2001ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient long sFields; 2011ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2021ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Virtual methods defined in this class; invoked through vtable. */ 2031ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient long virtualMethods; 2041ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 20570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** Class flags to help the GC with object scanning. */ 20670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private transient int classFlags; 20770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 2081ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2091ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Total size of the Class instance; used when allocating storage on GC heap. 2101ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * See also {@link Class#objectSize}. 2111ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int classSize; 2131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2141ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2151ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * tid used to check for recursive static initializer invocation. 2161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int clinitThreadId; 2181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Class def index from dex file. An index of 65535 indicates that there is no class definition, 2211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * for example for an array type. 2221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * TODO: really 16bits as type indices are 16bit. 2231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2241ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int dexClassDefIndex; 2251ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2261ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2271ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Class type index from dex file, lazily computed. An index of 65535 indicates that the type 2281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * index isn't known. Volatile to avoid double-checked locking bugs. 2291ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * TODO: really 16bits as type indices are 16bit. 2301ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2311ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient volatile int dexTypeIndex; 2321ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2331ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Number of instance fields that are object references. */ 2341ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int numReferenceInstanceFields; 2351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Number of static fields that are object references. */ 2371ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int numReferenceStaticFields; 2381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2391ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Total object size; used when allocating storage on GC heap. For interfaces and abstract 2411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * classes this will be zero. See also {@link Class#classSize}. 2421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int objectSize; 2441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2461ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * The lower 16 bits is the primitive type value, or 0 if not a primitive type; set for 2471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * generated primitive classes. 2481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int primitiveType; 2501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Bitmap of offsets of iFields. */ 2521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int referenceInstanceOffsets; 2531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** State of class initialization */ 2551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int status; 2561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private AnnotationType annotationType; 2581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2592ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong private static final int ANNOTATION = 0x00002000; 2602ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong private static final int ENUM = 0x00004000; 2612ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong private static final int SYNTHETIC = 0x00001000; 2622ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong private static final int FINALIZABLE = 0x80000000; 26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor. Only the Java Virtual Machine creates Class 26651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects. 26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private Class() {} 26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Converts the object to a string. The string representation is the 27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * string "class" or "interface", followed by a space, and then by the 27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * fully qualified name of the class in the format returned by 27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code getName}. If this {@code Class} object represents a 27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, this method returns the name of the primitive type. If 27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents void this method returns 27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "void". 27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of this class object. 28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String toString() { 28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) 28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski + getName(); 28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} object associated with the class or 29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface with the given string name. Invoking this method is 29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent to: 29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName(className, true, currentLoader)} 29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * where {@code currentLoader} denotes the defining class loader of 29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the current class. 29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> For example, the following code fragment returns the 30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * runtime {@code Class} descriptor for the class named 30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code java.lang.Thread}: 30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class t = Class.forName("java.lang.Thread")} 30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A call to {@code forName("X")} causes the class named 30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code X} to be initialized. 31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param className the fully qualified name of the desired class. 31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Class} object for the class with the 31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified name. 31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception LinkageError if the linkage fails 31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization provoked 31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this method fails 31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ClassNotFoundException if the class cannot be located 31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Class<?> forName(String className) 32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws ClassNotFoundException { 322a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return forName(className, true, VMStack.getCallingClassLoader()); 32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} object associated with the class or 32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface with the given string name, using the given class loader. 32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Given the fully qualified name for a class or interface (in the same 33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * format returned by {@code getName}) this method attempts to 33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * locate, load, and link the class or interface. The specified class 33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader is used to load the class or interface. If the parameter 33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code loader} is null, the class is loaded through the bootstrap 33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader. The class is initialized only if the 33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code initialize} parameter is {@code true} and if it has 33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not been initialized earlier. 33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If {@code name} denotes a primitive type or void, an attempt 33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will be made to locate a user-defined class in the unnamed package whose 34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name is {@code name}. Therefore, this method cannot be used to 34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtain any of the {@code Class} objects representing primitive 34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types or void. 34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If {@code name} denotes an array class, the component type of 34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array class is loaded but not initialized. 34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> For example, in an instance method the expression: 34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName("Foo")} 35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is equivalent to: 35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName("Foo", true, this.getClass().getClassLoader())} 35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method throws errors related to loading, linking or 36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The 36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Java Language Specification</em>. 36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method does not check whether the requested class 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is accessible to its caller. 36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the {@code loader} is {@code null}, and a security 36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager is present, and the caller's class loader is not null, then this 36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method calls the security manager's {@code checkPermission} method 36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with a {@code RuntimePermission("getClassLoader")} permission to 36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ensure it's ok to access the bootstrap class loader. 37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name fully qualified name of the desired class 37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param initialize whether the class must be initialized 37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param loader class loader from which the class must be loaded 37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return class object representing the desired class 37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception LinkageError if the linkage fails 37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization provoked 37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this method fails 37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ClassNotFoundException if the class cannot be located by 38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified class loader 38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Class#forName(String) 38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader 38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Class<?> forName(String name, boolean initialize, 38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ClassLoader loader) 38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws ClassNotFoundException 39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (loader == null) { 392bac8ef10e8a32316e4eedc6163dd3776cf5e5d7eNarayan Kamath loader = BootClassLoader.getInstance(); 393a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 394a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> result; 395a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski try { 396a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = classForName(name, initialize, loader); 397a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } catch (ClassNotFoundException e) { 398a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Throwable cause = e.getCause(); 399a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (cause instanceof LinkageError) { 400a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw (LinkageError) cause; 40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 402a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw e; 40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 404a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** Called after security checks have been made. */ 408a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski static native Class<?> classForName(String className, boolean shouldInitialize, 409a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader classLoader) throws ClassNotFoundException; 41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Creates a new instance of the class represented by this {@code Class} 41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The class is instantiated as if by a {@code new} 41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * expression with an empty argument list. The class is initialized if it 41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has not already been initialized. 41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that this method propagates any exception thrown by the 41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * nullary constructor, including a checked exception. Use of 41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method effectively bypasses the compile-time exception 42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * checking that would otherwise be performed by the compiler. 42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The {@link 42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Constructor#newInstance(java.lang.Object...) 42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor.newInstance} method avoids this problem by wrapping 42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * any exception thrown by the constructor in a (checked) {@link 42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.InvocationTargetException}. 42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a newly allocated instance of the class represented by this 42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. 42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalAccessException if the class or its nullary 43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructor is not accessible. 43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception InstantiationException 43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if this {@code Class} represents an abstract class, 43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an interface, an array class, a primitive type, or void; 43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or if the class has no nullary constructor; 43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or if the instantiation fails for some other reason. 43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization 43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * provoked by this method fails. 43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 44551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 44651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 44751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * creation of new instances of this class 44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 44951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 45151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 45251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 45651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 45751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 4581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native T newInstance() throws InstantiationException, IllegalAccessException; 45951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 46151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Object} is assignment-compatible 46251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with the object represented by this {@code Class}. This method is 46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the dynamic equivalent of the Java language {@code instanceof} 46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * operator. The method returns {@code true} if the specified 46551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} argument is non-null and can be cast to the 46651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * reference type represented by this {@code Class} object without 46751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * raising a {@code ClassCastException.} It returns {@code false} 46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise. 46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, if this {@code Class} object represents a 47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared class, this method returns {@code true} if the specified 47251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} argument is an instance of the represented class (or 47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of any of its subclasses); it returns {@code false} otherwise. If 47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents an array class, this method 47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code true} if the specified {@code Object} argument 47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * can be converted to an object of the array class by an identity 47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * conversion or by a widening reference conversion; it returns 47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. If this {@code Class} object 47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents an interface, this method returns {@code true} if the 48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class or any superclass of the specified {@code Object} argument 48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implements this interface; it returns {@code false} otherwise. If 48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type, this method 48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code false}. 48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 4859e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param object the object to check 48651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if {@code obj} is an instance of this class 48751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 48851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 48951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 490a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isInstance(Object object) { 491a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (object == null) { 492a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 493a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 494a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return isAssignableFrom(object.getClass()); 495a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 49651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 49751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 49851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 49951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the class or interface represented by this 50051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object is either the same as, or is a superclass or 50151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterface of, the class or interface represented by the specified 50251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} parameter. It returns {@code true} if so; 50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise it returns {@code false}. If this {@code Class} 50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object represents a primitive type, this method returns 50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code true} if the specified {@code Class} parameter is 50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exactly this {@code Class} object; otherwise it returns 50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. 50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, this method tests whether the type represented by the 51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified {@code Class} parameter can be converted to the type 51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object via an identity conversion 51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or via a widening reference conversion. See <em>The Java Language 51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Specification</em>, sections 5.1.1 and 5.1.4 , for details. 51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 5159e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param c the {@code Class} object to be checked 51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code boolean} value indicating whether objects of the 51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type {@code cls} can be assigned to objects of this class 51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if the specified Class parameter is 51951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null. 52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 522a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isAssignableFrom(Class<?> c) { 523a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (this == c) { 524a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; // Can always assign to things of the same type. 525a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (this == Object.class) { 526a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return !c.isPrimitive(); // Can assign any reference to java.lang.Object. 527a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isArray()) { 528a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return c.isArray() && componentType.isAssignableFrom(c.componentType); 529a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isInterface()) { 530a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search iftable which has a flattened and uniqued list of interfaces. 531a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = c.ifTable; 532a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 533a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 5341ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath if (iftable[i] == this) { 535a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 536a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 537a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 538a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 539a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 540a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 541a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!c.isInterface()) { 542a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (c = c.superClass; c != null; c = c.superClass) { 543a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == this) { 544a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 545a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 546a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 547a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 548a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 549a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 550a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 55251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 55351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Class} object represents an 55451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface type. 55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this object represents an interface; 55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 559a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isInterface() { 560a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (accessFlags & Modifier.INTERFACE) != 0; 561a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 56251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 56351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 56451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if this {@code Class} object represents an array class. 56551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 56651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this object represents an array class; 56751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 56851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 56951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 570a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isArray() { 571a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getComponentType() != null; 572a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 57351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 57551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Class} object represents a 57651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type. 57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> There are nine predefined {@code Class} objects to represent 57951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the eight primitive types and void. These are created by the Java 58051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Virtual Machine, and have the same names as the primitive types that 58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * they represent, namely {@code boolean}, {@code byte}, 58251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code char}, {@code short}, {@code int}, 58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code long}, {@code float}, and {@code double}. 58451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 58551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> These objects may only be accessed via the following public static 58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * final variables, and are the only {@code Class} objects for which 58751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method returns {@code true}. 58851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 58951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if and only if this class represents a primitive type 59051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 59151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Boolean#TYPE 59251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Character#TYPE 59351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Byte#TYPE 59451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Short#TYPE 59551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Integer#TYPE 59651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Long#TYPE 59751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Float#TYPE 59851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Double#TYPE 59951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Void#TYPE 60051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 602a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isPrimitive() { 6031ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return (primitiveType & 0xFFFF) != 0; 604a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 60651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 6072ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * Indicates whether this {@code Class} or its parents override finalize. 6082ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * 6092ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * @return {@code true} if and if this class or its parents override 6102ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * finalize; 6112ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * 6122ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * @hide 6132ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong */ 6142ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong public boolean isFinalizable() { 6152ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong return (getModifiers() & FINALIZABLE) != 0; 6162ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong } 6172ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong 6182ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong /** 61951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns true if this {@code Class} object represents an annotation 62051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type. Note that if this method returns true, {@link #isInterface()} 62151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * would also return true, as all annotation types are also interfaces. 62251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this class object represents an annotation 62451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type; {@code false} otherwise 62551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 62651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 62751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isAnnotation() { 62851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (getModifiers() & ANNOTATION) != 0; 62951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 63051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 63151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 63251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if this class is a synthetic class; 63351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code false} otherwise. 63451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a synthetic class as 63551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * defined by the Java Language Specification. 63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 63851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isSynthetic() { 63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (getModifiers() & SYNTHETIC) != 0; 64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the name of the entity (class, interface, array class, 64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, or void) represented by this {@code Class} object, 64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as a {@code String}. 64651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 64751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a reference type that is not an 64851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array type then the binary name of the class is returned, as specified 64951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by 65051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>. 65151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 65251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a primitive type or void, then the 65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name returned is a {@code String} equal to the Java language 65451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * keyword corresponding to the primitive type or void. 65551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 65651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a class of arrays, then the internal 65751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * form of the name consists of the name of the element type preceded by 65851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one or more '{@code [}' characters representing the depth of the array 65951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * nesting. The encoding of element type names is as follows: 66051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 66151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><table summary="Element types and encodings"> 66251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><th> Element Type <th> <th> Encoding 66351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> boolean <td> <td align=center> Z 66451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> byte <td> <td align=center> B 66551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> char <td> <td align=center> C 66651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> class or interface 66751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td> <td align=center> L<i>classname</i>; 66851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> double <td> <td align=center> D 66951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> float <td> <td align=center> F 67051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> int <td> <td align=center> I 67151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> long <td> <td align=center> J 67251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> short <td> <td align=center> S 67351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </table></blockquote> 67451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 67551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The class or interface name <i>classname</i> is the binary name of 67651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class specified above. 67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Examples: 67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * String.class.getName() 68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "java.lang.String" 68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * byte.class.getName() 68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "byte" 68451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (new Object[3]).getClass().getName() 68551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "[Ljava.lang.Object;" 68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (new int[3][4][5][6][7][8][9]).getClass().getName() 68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "[[[[[[[I" 68851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 68951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 69051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the name of the class or interface 69151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 69251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 69351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getName() { 69451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String name = this.name; 69551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (name == null) 696a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski this.name = name = getNameNative(); 69751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 700a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native String getNameNative(); 70151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 70251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 70351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the class loader for the class. Some implementations may use 70451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null to represent the bootstrap class loader. This method will return 70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null in such implementations if this class was loaded by the bootstrap 70651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader. 70751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 70851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If a security manager is present, and the caller's class loader is 70951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not null and the caller's class loader is not the same as or an ancestor of 71051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class loader for the class whose class loader is requested, then 71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method calls the security manager's {@code checkPermission} 71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method with a {@code RuntimePermission("getClassLoader")} 71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * permission to ensure it's ok to access the class loader for the class. 71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object 71651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type or void, null is returned. 71751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 71851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the class loader that loaded the class or interface 71951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 72151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if a security manager exists and its 72251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code checkPermission} method denies 72351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the class loader for the class. 72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader 72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 72651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.RuntimePermission 72751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 72851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public ClassLoader getClassLoader() { 729a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isPrimitive()) { 73051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 73151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 732a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (classLoader == null) ? BootClassLoader.getInstance() : classLoader; 73351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 73451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 73551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 73651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code TypeVariable} objects that represent the 73751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type variables declared by the generic declaration represented by this 73851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code GenericDeclaration} object, in declaration order. Returns an 73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if the underlying generic declaration declares no type 74051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * variables. 74151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 74251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of {@code TypeVariable} objects that represent 74351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the type variables declared by this generic declaration 74451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError if the generic 74551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * signature of this generic declaration does not conform to 74651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the format specified in 74751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 74851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 74951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 750a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski @Override public synchronized TypeVariable<Class<T>>[] getTypeParameters() { 751a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String annotationSignature = AnnotationAccess.getSignature(this); 752a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature == null) { 753a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return EmptyArray.TYPE_VARIABLE; 754a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 755a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 756a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 757a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return parser.formalTypeParameters; 75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 76151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} representing the superclass of the entity 76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (class, interface, primitive type or void) represented by this 76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class}. If this {@code Class} represents either the 76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} class, an interface, a primitive type, or void, then 76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null is returned. If this object represents an array class then the 76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object representing the {@code Object} class is 76751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. 76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the superclass of the class represented by this object. 77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 771a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<? super T> getSuperclass() { 772a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // For interfaces superClass is Object (which agrees with the JNI spec) 773a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // but not with the expected behavior here. 774a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isInterface()) { 775a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 776a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 777a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return superClass; 778a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 779a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 78051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 78151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Type} representing the direct superclass of 78351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the entity (class, interface, primitive type or void) represented by 78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class}. 78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the superclass is a parameterized type, the {@code Type} 78751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object returned must accurately reflect the actual type 78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameters used in the source code. The parameterized type 78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing the superclass is created if it had not been 79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * created before. See the declaration of {@link 79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.ParameterizedType ParameterizedType} for the 79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * semantics of the creation process for parameterized types. If 79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} represents either the {@code Object} 79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, an interface, a primitive type, or void, then null is 79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. If this object represents an array class then the 79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object representing the {@code Object} class is 79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. 79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 79951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError if the generic 80051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class signature does not conform to the format specified in 80151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 80251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws TypeNotPresentException if the generic superclass 80351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * refers to a non-existent type declaration 80451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.MalformedParameterizedTypeException if the 80551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * generic superclass refers to a parameterized type that cannot be 80651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instantiated for any reason 80751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the superclass of the class represented by this object 80851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 80951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 81051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Type getGenericSuperclass() { 811a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Type genericSuperclass = getSuperclass(); 812a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // This method is specified to return null for all cases where getSuperclass 813a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // returns null, i.e, for primitives, interfaces, void and java.lang.Object. 814a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (genericSuperclass == null) { 815a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 816a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 817a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 818a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String annotationSignature = AnnotationAccess.getSignature(this); 819a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature != null) { 820a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 821a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 822a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski genericSuperclass = parser.superclassType; 823a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 824a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return Types.getType(genericSuperclass); 82551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 82651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 82751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 82851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Gets the package for this class. The class loader of this class is used 82951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to find the package. If the class was loaded by the bootstrap class 83051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader the set of packages loaded from CLASSPATH is searched to find the 83151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * package of the class. Null is returned if no package object was created 83251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the class loader of this class. 83351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 83451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Packages have attributes for versions and specifications only if the 83551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * information was defined in the manifests that accompany the classes, and 83651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the class loader created the package instance with the attributes 83751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * from the manifest. 83851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 83951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the package of the class, or null if no package 84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * information is available from the archive or codebase. 84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Package getPackage() { 843a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader loader = getClassLoader(); 844a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (loader != null) { 845a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName = getPackageName$(); 846a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName != null ? loader.getPackage(packageName) : null; 847a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 848a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 849a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 850a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 851a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 852a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns the package name of this class. This returns null for classes in 853a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * the default package. 854a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 855a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 856a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 857a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public String getPackageName$() { 858a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String name = getName(); 859a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int last = name.lastIndexOf('.'); 860a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return last == -1 ? null : name.substring(0, last); 86151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 86251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 86351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 86451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 86551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines the interfaces implemented by the class or interface 86651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 86751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 86851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class, the return value is an array 86951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * containing objects representing all interfaces implemented by the 87051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. The order of the interface objects in the array corresponds to 87151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the order of the interface names in the {@code implements} clause 87251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the declaration of the class represented by this object. For 87351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, given the declaration: 87451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 87551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code class Shimmer implements FloorWax, DessertTopping { ... }} 87651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 87751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * suppose the value of {@code s} is an instance of 87851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Shimmer}; the value of the expression: 87951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 88051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code s.getClass().getInterfaces()[0]} 88151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 88251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is the {@code Class} object that represents interface 88351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code FloorWax}; and the value of: 88451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 88551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code s.getClass().getInterfaces()[1]} 88651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 88751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is the {@code Class} object that represents interface 88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code DessertTopping}. 88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents an interface, the array contains objects 89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing all interfaces extended by the interface. The order of the 89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface objects in the array corresponds to the order of the interface 89351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * names in the {@code extends} clause of the declaration of the 89451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface represented by this object. 89551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 89651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class or interface that implements no 89751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces, the method returns an array of length 0. 89851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 89951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a primitive type or void, the method 90051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns an array of length 0. 90151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 90251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of interfaces implemented by this class. 90351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 904a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?>[] getInterfaces() { 905a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isArray()) { 906a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return new Class<?>[] { Cloneable.class, Serializable.class }; 907a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isProxy()) { 908a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getProxyInterfaces(); 909a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 910a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Dex dex = getDex(); 911a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dex == null) { 912a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return EmptyArray.CLASS; 913a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 914a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski short[] interfaces = dex.interfaceTypeIndicesFromClassDefIndex(dexClassDefIndex); 915a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?>[] result = new Class<?>[interfaces.length]; 916a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < interfaces.length; i++) { 917a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result[i] = getDexCacheType(dex, interfaces[i]); 918a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 919a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 920a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 921a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 922a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Returns the interfaces that this proxy class directly implements. 923a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native Class<?>[] getProxyInterfaces(); 92451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 92551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 92651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Type}s representing the interfaces 92751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * directly implemented by the class or interface represented by 92851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this object. 92951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 93051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If a superinterface is a parameterized type, the 93151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Type} object returned for it must accurately reflect 93251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the actual type parameters used in the source code. The 93351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameterized type representing each superinterface is created 93451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if it had not been created before. See the declaration of 93551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link java.lang.reflect.ParameterizedType ParameterizedType} 93651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for the semantics of the creation process for parameterized 93751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types. 93851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 93951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class, the return value is an 94051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array containing objects representing all interfaces 94151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implemented by the class. The order of the interface objects in 94251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array corresponds to the order of the interface names in 94351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the {@code implements} clause of the declaration of the class 94451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. In the case of an array class, the 94551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces {@code Cloneable} and {@code Serializable} are 94651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned in that order. 94751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 94851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents an interface, the array contains 94951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects representing all interfaces directly extended by the 95051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface. The order of the interface objects in the array 95151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * corresponds to the order of the interface names in the 95251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code extends} clause of the declaration of the interface 95351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 95451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 95551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents a class or interface that 95651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implements no interfaces, the method returns an array of length 95751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 0. 95851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 95951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents a primitive type or void, the 96051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns an array of length 0. 96151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 96251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError 96351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the generic class signature does not conform to the format 96451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified in 96551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 96651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws TypeNotPresentException if any of the generic 96751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces refers to a non-existent type declaration 96851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.MalformedParameterizedTypeException 96951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if any of the generic superinterfaces refer to a parameterized 97051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type that cannot be instantiated for any reason 97151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of interfaces implemented by this class 97251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 97351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 97451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Type[] getGenericInterfaces() { 975a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Type[] result; 976a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized (Caches.genericInterfaces) { 977a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = Caches.genericInterfaces.get(this); 978a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null) { 979a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String annotationSignature = AnnotationAccess.getSignature(this); 980a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature == null) { 981a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = getInterfaces(); 982a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 983a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 984a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 985a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = Types.getTypeArray(parser.interfaceTypes, false); 986a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 987a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Caches.genericInterfaces.put(this, result); 988a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 989a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 990a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (result.length == 0) ? result : result.clone(); 99151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 99251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 99351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 99451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 99551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} representing the component type of an 99651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. If this class does not represent an array class this method 99751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns null. 99851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 99951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Class} representing the component type of this 100051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class if this class is an array 100151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.reflect.Array 100251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 100351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1004a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?> getComponentType() { 1005a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return componentType; 1006a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 100751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 100851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 100951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the Java language modifiers for this class or interface, encoded 101051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in an integer. The modifiers consist of the Java Virtual Machine's 101151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constants for {@code public}, {@code protected}, 101251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code private}, {@code final}, {@code static}, 101351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code abstract} and {@code interface}; they should be decoded 101451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * using the methods of class {@code Modifier}. 101551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 101651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the underlying class is an array class, then its 101751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code public}, {@code private} and {@code protected} 101851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * modifiers are the same as those of its component type. If this 101951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} represents a primitive type or void, its 102051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code public} modifier is always {@code true}, and its 102151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code protected} and {@code private} modifiers are always 102251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. If this object represents an array class, a 102351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type or void, then its {@code final} modifier is always 102451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code true} and its interface modifier is always 102551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. The values of its other modifiers are not determined 102651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this specification. 102751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 102851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The modifier encodings are defined in <em>The Java Virtual Machine 102951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Specification</em>, table 4.1. 103051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 103151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code int} representing the modifiers for this class 103251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.reflect.Modifier 103351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 103451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1035a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getModifiers() { 1036a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Array classes inherit modifiers from their component types, but in the case of arrays 1037a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // of an inner class, the class file may contain "fake" access flags because it's not valid 1038a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // for a top-level class to private, say. The real access flags are stored in the InnerClass 1039a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // attribute, so we need to make sure we drill down to the inner class: the accessFlags 1040a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // field is not the value we want to return, and the synthesized array class does not itself 1041a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // have an InnerClass attribute. https://code.google.com/p/android/issues/detail?id=56267 1042a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isArray()) { 1043a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int componentModifiers = getComponentType().getModifiers(); 1044a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if ((componentModifiers & Modifier.INTERFACE) != 0) { 1045a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski componentModifiers &= ~(Modifier.INTERFACE | Modifier.STATIC); 1046a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1047a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return Modifier.ABSTRACT | Modifier.FINAL | componentModifiers; 1048a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1049a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int JAVA_FLAGS_MASK = 0xffff; 1050a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int modifiers = AnnotationAccess.getInnerClassFlags(this, accessFlags & JAVA_FLAGS_MASK); 1051a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return modifiers & JAVA_FLAGS_MASK; 1052a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 105351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 105451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 105551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Gets the signers of this class. 105651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 105751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the signers of this class, or null if there are no signers. In 105851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular, this method returns null if this object represents 105951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a primitive type or void. 106051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 106151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1062a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Object[] getSigners() { 1063a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 1064a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 106551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 106670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native Method getEnclosingMethodNative(); 106751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 106851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 106951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents a local or anonymous 107051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class within a method, returns a {@link 107151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Method Method} object representing the 107251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * immediately enclosing method of the underlying class. Returns 107351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} otherwise. 107451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 107551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * In particular, this method returns {@code null} if the underlying 107651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class is a local or anonymous class immediately enclosed by a type 107751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declaration, instance initializer or static initializer. 107851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 107951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing method of the underlying class, if 108051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that class is a local or anonymous class; otherwise {@code null}. 108151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 108251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 108351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getEnclosingMethod() { 1084a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classNameImpliesTopLevel()) { 108551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 108651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 108770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return getEnclosingMethodNative(); 108851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 108951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 109051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 109151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents a local or anonymous 109251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class within a constructor, returns a {@link 109351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Constructor Constructor} object representing 109451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the immediately enclosing constructor of the underlying 109551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. Returns {@code null} otherwise. In particular, this 109651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns {@code null} if the underlying class is a local 109751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or anonymous class immediately enclosed by a type declaration, 109851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instance initializer or static initializer. 109951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 110051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing constructor of the underlying class, if 110151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that class is a local or anonymous class; otherwise {@code null}. 110251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 110351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 110451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?> getEnclosingConstructor() { 1105a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classNameImpliesTopLevel()) { 110651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 110751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 110870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return getEnclosingConstructorNative(); 1109a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1110a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 111170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native Constructor<?> getEnclosingConstructorNative(); 111270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 1113a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean classNameImpliesTopLevel() { 1114a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return !getName().contains("$"); 111551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 111651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 111751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 111851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the class or interface represented by this {@code Class} object 111951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a member of another class, returns the {@code Class} object 112051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing the class in which it was declared. This method returns 112151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null if this class or interface is not a member of any other class. If 112251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents an array class, a primitive 112351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type, or void,then this method returns null. 112451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 112551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the declaring class for this class 112651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 112751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 112870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public native Class<?> getDeclaringClass(); 112951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 113051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 113151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the immediately enclosing class of the underlying 113251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. If the underlying class is a top level class this 113351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns {@code null}. 113451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing class of the underlying class 113551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 113651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 113770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public native Class<?> getEnclosingClass(); 113851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 113951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 114051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the simple name of the underlying class as given in the 114151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code. Returns an empty string if the underlying class is 114251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * anonymous. 114351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 114451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The simple name of an array is the simple name of the 114551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * component type with "[]" appended. In particular the simple 114651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name of an array whose component type is anonymous is "[]". 114751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 114851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the simple name of the underlying class 114951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 115051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 115151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getSimpleName() { 115251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isArray()) 115351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getComponentType().getSimpleName()+"[]"; 115451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 115551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String simpleName = getSimpleBinaryName(); 115651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (simpleName == null) { // top level class 115751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski simpleName = getName(); 115851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name 115951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 116051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // According to JLS3 "Binary Compatibility" (13.1) the binary 116151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // name of non-package classes (not top level) is the binary 116251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // name of the immediately enclosing class followed by a '$' followed by: 116351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // (for nested and inner classes): the simple name. 116451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // (for local classes): 1 or more digits followed by the simple name. 116551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // (for anonymous classes): 1 or more digits. 116651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 116751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Since getSimpleBinaryName() will strip the binary name of 116851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // the immediatly enclosing class, we are now looking at a 116951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // string that matches the regular expression "\$[0-9]*" 117051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // followed by a simple name (considering the simple of an 117151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // anonymous class to be the empty string). 117251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 117351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Remove leading "\$[0-9]*" from the name 117451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = simpleName.length(); 117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (length < 1 || simpleName.charAt(0) != '$') 117651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new InternalError("Malformed class name"); 117751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int index = 1; 117851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (index < length && isAsciiDigit(simpleName.charAt(index))) 117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski index++; 118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Eventually, this is the empty string iff this is an anonymous class 118151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return simpleName.substring(index); 118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 118451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 118551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Character.isDigit answers {@code true} to some non-ascii 118651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * digits. This one does not. 118751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 118851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static boolean isAsciiDigit(char c) { 118951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return '0' <= c && c <= '9'; 119051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 119151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 119251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 119351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the canonical name of the underlying class as 119451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * defined by the Java Language Specification. Returns null if 119551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the underlying class does not have a canonical name (i.e., if 119651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is a local or anonymous class or an array whose component 119751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type does not have a canonical name). 119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the canonical name of the underlying class if it exists, and 119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} otherwise. 120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getCanonicalName() { 120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isArray()) { 120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String canonicalName = getComponentType().getCanonicalName(); 120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (canonicalName != null) 120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return canonicalName + "[]"; 120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isLocalOrAnonymousClass()) 121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> enclosingClass = getEnclosingClass(); 121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingClass == null) { // top level class 121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getName(); 121551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 121651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String enclosingName = enclosingClass.getCanonicalName(); 121751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingName == null) 121851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 121951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return enclosingName + "." + getSimpleName(); 122051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 122151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 122251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 122351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 122451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 122551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is an anonymous class. 122651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 122751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is an anonymous class. 122851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 122951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 123070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public native boolean isAnonymousClass(); 123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 123351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 123451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a local class. 123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 123651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a local class. 123751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 123851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 123951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isLocalClass() { 1240a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return !classNameImpliesTopLevel() 1241a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski && AnnotationAccess.getEnclosingMethodOrConstructor(this) != null 1242a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski && !isAnonymousClass(); 124351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 124451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 124551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 124651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 124751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a member class. 124851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 124951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a member class. 125051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 125151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 125251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isMemberClass() { 125351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getSimpleBinaryName() != null && !isLocalOrAnonymousClass(); 125451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 125551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 125651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 125751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the "simple binary name" of the underlying class, i.e., 125851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary name without the leading enclosing class name. 125951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code null} if the underlying class is a top level 126051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. 126151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 126251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String getSimpleBinaryName() { 126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> enclosingClass = getEnclosingClass(); 126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingClass == null) // top level class 126551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 126651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Otherwise, strip the enclosing class' name 126751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski try { 126851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getName().substring(enclosingClass.getName().length()); 126951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } catch (IndexOutOfBoundsException ex) { 127051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new InternalError("Malformed class name"); 127151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 127251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 127351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 127451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 127551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if this is a local class or an anonymous 127651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. Returns {@code false} otherwise. 127751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 127851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private boolean isLocalOrAnonymousClass() { 127951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // JVM Spec 4.8.6: A class must have an EnclosingMethod 128051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // attribute if and only if it is a local class or an 128151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // anonymous class. 1282a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return isLocalClass() || isAnonymousClass(); 128351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 128451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 128551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 128651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Class} objects representing all 128751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the public classes and interfaces that are members of the class 128851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object. This includes public 128951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class and interface members inherited from superclasses and public class 129051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and interface members declared by the class. This method returns an 129151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if this {@code Class} object has no public member 129251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * classes or interfaces. This method also returns an array of length 0 if 129351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type, an array 129451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, or void. 129551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 129651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Class} objects representing the public 129751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * members of this class 129851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 129951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 130051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 130151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 130251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 130351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 130451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 130551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 130651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} method 130751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * denies access to the classes within this class 130851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 130951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 131051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 131151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 131251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 131351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 131451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 131551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 131651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 131751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 131851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 131951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 132051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Class<?>[] getClasses() { 13211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath List<Class<?>> result = new ArrayList<Class<?>>(); 13221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath for (Class<?> c = this; c != null; c = c.superClass) { 13231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath for (Class<?> member : c.getDeclaredClasses()) { 13241ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath if (Modifier.isPublic(member.getModifiers())) { 13251ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath result.add(member); 13261ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 13271ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 13281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 13291ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return result.toArray(new Class[result.size()]); 133051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 133151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 133251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 133351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 133451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Field} objects reflecting all 133551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the accessible public fields of the class or interface represented by 133651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object. The elements in the array returned are 133751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not sorted and are not in any particular order. This method returns an 133851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if the class or interface has no accessible public 133951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * fields, or if it represents an array class, a primitive type, or void. 134051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, if this {@code Class} object represents a class, 134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method returns the public fields of this class and of all its 134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superclasses. If this {@code Class} object represents an 134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface, this method returns the fields of this interface and of all 134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * its superinterfaces. 134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The implicit length field for array class is not reflected by this 134851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method. User code should use the methods of class {@code Array} to 134951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manipulate arrays. 135051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 135151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 135251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 135351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Field} objects representing the 135451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public fields 135551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 135651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 135751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 135851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 135951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 136051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 136151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 136251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 136351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 136451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the fields within this class 136551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 136651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 136751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 136851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 136951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 137051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 137151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 137251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 137351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 137451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 137551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 137651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 137751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Field[] getFields() throws SecurityException { 1378a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski List<Field> fields = new ArrayList<Field>(); 13791ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath getPublicFieldsRecursive(fields); 13801ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return fields.toArray(new Field[fields.size()]); 13811ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 13821ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 13831ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 13841ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates {@code result} with public fields defined by this class, its 13851ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * superclasses, and all implemented interfaces. 13861ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 13871ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private void getPublicFieldsRecursive(List<Field> result) { 1388a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search superclasses 1389a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = this; c != null; c = c.superClass) { 13901ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, c.getPublicDeclaredFields()); 1391a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1392a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1393a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search iftable which has a flattened and uniqued list of interfaces 1394a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 1395a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 1396a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 13971ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, ((Class<?>) iftable[i]).getPublicDeclaredFields()); 1398a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1399a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 140051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 140151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 140251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Method} objects reflecting all 140451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the public <em>member</em> methods of the class or interface represented 140551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this {@code Class} object, including those declared by the class 140651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or interface and those inherited from superclasses and 140751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces. Array classes return all the (public) member methods 140851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * inherited from the {@code Object} class. The elements in the array 140951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned are not sorted and are not in any particular order. This 141051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns an array of length 0 if this {@code Class} object 141151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a class or interface that has no public member methods, or if 141251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type or void. 141351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 141451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The class initialization method {@code <clinit>} is not 141551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * included in the returned array. If the class declares multiple public 141651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member methods with the same parameter types, they are all included in 141751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the returned array. 141851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 141951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 142051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 142151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Method} objects representing the 142251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public methods of this class 142351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 142451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 142551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 142651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 142751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 142851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 142951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 143051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 143151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 143251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the methods within this class 143351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 143451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 143551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 143651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 143751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 143851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 143951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 144051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 144151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 144251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 144351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 144451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 144551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method[] getMethods() throws SecurityException { 1446a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski List<Method> methods = new ArrayList<Method>(); 14471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath getPublicMethodsInternal(methods); 14481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /* 14491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Remove duplicate methods defined by superclasses and 14501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * interfaces, preferring to keep methods declared by derived 14511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * types. 14521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 14531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath CollectionUtils.removeDuplicates(methods, Method.ORDER_BY_SIGNATURE); 14541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return methods.toArray(new Method[methods.size()]); 14551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 14561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 14571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 14581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates {@code result} with public methods defined by this class, its 14591ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * superclasses, and all implemented interfaces, including overridden methods. 14601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 14611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private void getPublicMethodsInternal(List<Method> result) { 14621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, getDeclaredMethodsUnchecked(true)); 1463a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!isInterface()) { 1464a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search superclasses, for interfaces don't search java.lang.Object. 1465a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = superClass; c != null; c = c.superClass) { 14661ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, c.getDeclaredMethodsUnchecked(true)); 1467a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1468a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1469a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search iftable which has a flattened and uniqued list of interfaces. 1470a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 1471a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 1472a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 1473a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) iftable[i]; 14741ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, ifc.getDeclaredMethodsUnchecked(true)); 1475a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1476a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 147751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 147851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Constructor} objects reflecting 148151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * all the public constructors of the class represented by this 148251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. An array of length 0 is returned if the 148351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class has no public constructors, or if the class is an array class, or 148451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the class reflects a primitive type or void. 148551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 148651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that while this method returns an array of {@code 148751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor<T>} objects (that is an array of constructors from 148851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this class), the return type of this method is {@code 148951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as 149051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * might be expected. This less informative return type is 149151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * necessary since after being returned from this method, the 149251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array could be modified to hold {@code Constructor} objects for 149351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * different classes, which would violate the type guarantees of 149451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Constructor<T>[]}. 149551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 149651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Constructor} objects representing the 149751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public constructors of this class 149851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 149951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 150051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 150151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 150251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 150351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 150451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 150551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 150651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 150751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the constructors within this class 150851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 150951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 151051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 151151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 151251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 151351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 151451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 151551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 151651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 151751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 151851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 151951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 152051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?>[] getConstructors() throws SecurityException { 15211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return getDeclaredConstructorsInternal(true); 152251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 152351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 152451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 152551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Field} object that reflects the specified public 152651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member field of the class or interface represented by this 152751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 152851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} specifying the simple name of the desired field. 152951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 153051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The field to be reflected is determined by the algorithm that 153151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * follows. Let C be the class represented by this object: 153251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <OL> 153351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If C declares a public field with the name specified, that is the 153451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * field to be reflected.</LI> 153551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no field was found in step 1 above, this algorithm is applied 153651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * recursively to each direct superinterface of C. The direct 153751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces are searched in the order they were declared.</LI> 153851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no field was found in steps 1 and 2 above, and C has a 153951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superclass S, then this algorithm is invoked recursively upon S. 154051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If C has no superclass, then a {@code NoSuchFieldException} 154151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is thrown.</LI> 154251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </OL> 154351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 154451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 154551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 154651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the field name 154751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Field} object of this class specified by 154851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code name} 154951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchFieldException if a field with the specified name is 155051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not found. 155151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 155251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 155351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 155451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 155551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 155651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 155751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 155851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 155951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 156051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 156151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the field 156251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 156351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 156451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 156551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 156651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 156751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 156851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 156951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 157051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 157151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 157251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 157370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public Field getField(String name) throws NoSuchFieldException { 1574fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski if (name == null) { 1575fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski throw new NullPointerException("name == null"); 1576fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski } 157770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak Field result = getPublicFieldRecursive(name); 157870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (result == null) { 157951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NoSuchFieldException(name); 158051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 158170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return result; 1582a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1583a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 158470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** 158570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * The native implementation of the {@code getField} method. 158670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 158770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * @throws NullPointerException 158870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * if name is null. 158970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * @see #getField(String) 159070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 159170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native Field getPublicFieldRecursive(String name); 159251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 159351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 159451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Method} object that reflects the specified public 159551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member method of the class or interface represented by this 159651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 159751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} specifying the simple name of the desired method. The 159851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code parameterTypes} parameter is an array of {@code Class} 159951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects that identify the method's formal parameter types, in declared 160051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * order. If {@code parameterTypes} is {@code null}, it is 160151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * treated as if it were an empty array. 160251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 160351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a 160451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code NoSuchMethodException} is raised. Otherwise, the method to 160551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be reflected is determined by the algorithm that follows. Let C be the 160651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class represented by this object: 160751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <OL> 160851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> C is searched for any <I>matching methods</I>. If no matching 160951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method is found, the algorithm of step 1 is invoked recursively on 161051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the superclass of C.</LI> 161151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no method was found in step 1 above, the superinterfaces of C 161251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are searched for a matching method. If any such method is found, it 161351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is reflected.</LI> 161451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </OL> 161551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 161651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * To find a matching method in a class C: If C declares exactly one 161751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public method with the specified name and exactly the same formal 161851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types, that is the method reflected. If more than one such 161951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method is found in C, and one of these methods has a return type that is 162051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * more specific than any of the others, that method is reflected; 162151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise one of the methods is chosen arbitrarily. 162251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 162351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that there may be more than one matching method in a 162451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class because while the Java language forbids a class to 162551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declare multiple methods with the same signature but different 162651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return types, the Java virtual machine does not. This 162751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * increased flexibility in the virtual machine can be used to 162851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implement various language features. For example, covariant 162951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns can be implemented with {@linkplain 163051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Method#isBridge bridge methods}; the bridge 163151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method and the method being overridden would have the same 163251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * signature but different return types. 163351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 163451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 163551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 163651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the method 163751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the list of parameters 163851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Method} object that matches the specified 163951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code name} and {@code parameterTypes} 164051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found 164151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or if the name is "<init>"or "<clinit>". 164251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 164351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 164451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 164551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 164651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 164751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 164851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 164951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 165051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 165151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 165251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the method 165351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 165451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 165551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 165651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 165751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 165851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 165951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 166051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 166151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 166251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 166351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 166451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 166551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getMethod(String name, Class<?>... parameterTypes) 166651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 1667a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getMethod(name, parameterTypes, true); 166851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 166951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 167051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 167151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 167251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Constructor} object that reflects the specified 167351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public constructor of the class represented by this {@code Class} 167451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The {@code parameterTypes} parameter is an array of 167551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that identify the constructor's formal 167651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types, in declared order. 167751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 167851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents an inner class 167951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared in a non-static context, the formal parameter types 168051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * include the explicit enclosing instance as the first parameter. 168151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 168251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The constructor to reflect is the public constructor of the class 168351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object whose formal parameter 168451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types match those specified by {@code parameterTypes}. 168551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 168651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 168751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Constructor} object of the public constructor that 168851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * matches the specified {@code parameterTypes} 168951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found. 169051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 169151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 169251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 169351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 169451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 169551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 169651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 169751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 169851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 169951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the constructor 170051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 170251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 170351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 170451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 170551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 170651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 170851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 171051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 171151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<T> getConstructor(Class<?>... parameterTypes) 171251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 171351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getConstructor0(parameterTypes, Member.PUBLIC); 171451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 171551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 171651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 171751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Class} objects reflecting all the 171851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * classes and interfaces declared as members of the class represented by 171951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object. This includes public, protected, default 172051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private classes and interfaces declared by the 172151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, but excludes inherited classes and interfaces. This method 172251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns an array of length 0 if the class declares no classes or 172351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces as members, or if this {@code Class} object represents a 172451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, an array class, or void. 172551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 172651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Class} objects representing all the 172751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared members of this class 172851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 172951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 173051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 173151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 173251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 173351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 173451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 173551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 173651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 173751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared classes within this class 173851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 173951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 174051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 174151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 174251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 174351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 174451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 174551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 174651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 174751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 174851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 174970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public native Class<?>[] getDeclaredClasses(); 175051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 175151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 175251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Field} objects reflecting all the fields 175351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared by the class or interface represented by this 175451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. This includes public, protected, default 175551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private fields, but excludes inherited fields. 175651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The elements in the array returned are not sorted and are not in any 175751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular order. This method returns an array of length 0 if the class 175851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or interface declares no fields, or if this {@code Class} object 175951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type, an array class, or void. 176051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 176151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 176251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 176351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Field} objects representing all the 176451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared fields of this class 176551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 176651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 176751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 176851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 176951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 177051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 177151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 177251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 177351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 177451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared fields within this class 177551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 177651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 177751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 177851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 177951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 178051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 178151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 178251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 178351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 178451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 178551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 17861ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Field[] getDeclaredFields(); 178751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 17881ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 17891ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates a list of fields without performing any security or type 17901ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * resolution checks first. If no fields exist, the list is not modified. 17911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 17921ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param publicOnly Whether to return only public fields. 17931ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param fields A list to populate with declared fields. 17941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @hide 17951ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 17961ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Field[] getDeclaredFieldsUnchecked(boolean publicOnly); 179751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 179851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 179951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Method} objects reflecting all the 180051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * methods declared by the class or interface represented by this 180151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. This includes public, protected, default 180251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private methods, but excludes inherited methods. 180351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The elements in the array returned are not sorted and are not in any 180451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular order. This method returns an array of length 0 if the class 180551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or interface declares no methods, or if this {@code Class} object 180651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type, an array class, or void. The class 180751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * initialization method {@code <clinit>} is not included in the 180851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned array. If the class declares multiple public member methods 180951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with the same parameter types, they are all included in the returned 181051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. 181151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 181251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, section 8.2. 181351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 181451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Method} objects representing all the 181551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared methods of this class 181651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 181751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 181851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 181951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 182051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 182151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 182251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 182351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 182451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 182551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared methods within this class 182651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 182751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 182851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 182951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 183051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 183151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 183251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 183351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 183451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 183551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 183651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 183751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method[] getDeclaredMethods() throws SecurityException { 18381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Method[] result = getDeclaredMethodsUnchecked(false); 1839a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Method m : result) { 1840a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Throw NoClassDefFoundError if types cannot be resolved. 1841a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski m.getReturnType(); 1842a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski m.getParameterTypes(); 1843a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1844a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 1845a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1846a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 18471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 18481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates a list of methods without performing any security or type 18491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * resolution checks first. If no methods exist, the list is not modified. 18501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 18511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param publicOnly Whether to return only public methods. 18521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param methods A list to populate with declared methods. 18531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @hide 18541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 18551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Method[] getDeclaredMethodsUnchecked(boolean publicOnly); 185651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 185751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 185851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Constructor} objects reflecting all the 185951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructors declared by the class represented by this 186051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. These are public, protected, default 186151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private constructors. The elements in the array 186251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned are not sorted and are not in any particular order. If the 186351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class has a default constructor, it is included in the returned array. 186451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method returns an array of length 0 if this {@code Class} 186551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object represents an interface, a primitive type, an array class, or 186651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * void. 186751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 186851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, section 8.2. 186951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 187051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Constructor} objects representing all the 187151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared constructors of this class 187251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 187351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 187451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 187551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 187651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 187751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 187851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 187951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 188051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 188151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared constructors within this class 188251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 188351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 188451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 188551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 188651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 188751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 188851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 188951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 189051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 189151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 189251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 189351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?>[] getDeclaredConstructors() throws SecurityException { 18941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return getDeclaredConstructorsInternal(false); 189551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 189651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 189751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 18981ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the constructor with the given parameters if it is defined by this class; 18991ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * {@code null} otherwise. This may return a non-public member. 19001ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 19011ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param args the types of the parameters to the constructor. 19021ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 19031ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Constructor<?>[] getDeclaredConstructorsInternal(boolean publicOnly); 19041ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 19051ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 190651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Field} object that reflects the specified declared 190751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * field of the class or interface represented by this {@code Class} 190851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The {@code name} parameter is a {@code String} that 190951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specifies the simple name of the desired field. Note that this method 191051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will not reflect the {@code length} field of an array class. 191151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 191251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the field 191351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Field} object for the specified field in this 191451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class 191551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchFieldException if a field with the specified name is 191651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not found. 191751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 191851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 191951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 192051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 192151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 192251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 192351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 192451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 192551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 192651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 192751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared field 192851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 192951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 193051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 193151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 193251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 193351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 193451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 193551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 193651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 193751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 193851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 19391ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Field getDeclaredField(String name) throws NoSuchFieldException; 194051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 19411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 19421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the field if it is defined by this class; {@code null} otherwise. This 19431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * may return a non-public member. 19441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 19451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Field getDeclaredFieldInternal(String name); 19461ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 19471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 19481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the subset of getDeclaredFields which are public. 19491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 19501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Field[] getPublicDeclaredFields(); 1951a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 195251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 195351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Method} object that reflects the specified 195451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared method of the class or interface represented by this 195551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 195651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} that specifies the simple name of the desired 195751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method, and the {@code parameterTypes} parameter is an array of 195851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that identify the method's formal parameter 195951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types, in declared order. If more than one method with the same 196051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types is declared in a class, and one of these methods has a 196151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type that is more specific than any of the others, that method is 196251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned; otherwise one of the methods is chosen arbitrarily. If the 196351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name is "<init>"or "<clinit>" a {@code NoSuchMethodException} 196451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is raised. 196551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 196651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the method 196751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 196851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Method} object for the method of this class 196951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * matching the specified name and parameters 197051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found. 197151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 197251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 197351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 197451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 197551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 197651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 197751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 197851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 197951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 198051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 198151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared method 198251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 198351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 198451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 198551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 198651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 198751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 198851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 198951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 199051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 199151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 199251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 199351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 199451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getDeclaredMethod(String name, Class<?>... parameterTypes) 199551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 1996a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getMethod(name, parameterTypes, false); 1997a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1998a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1999a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Method getMethod(String name, Class<?>[] parameterTypes, boolean recursivePublicMethods) 2000a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throws NoSuchMethodException { 2001a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (name == null) { 2002a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NullPointerException("name == null"); 2003a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2004a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parameterTypes == null) { 2005a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parameterTypes = EmptyArray.CLASS; 2006a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2007a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c : parameterTypes) { 2008a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == null) { 2009a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("parameter type is null"); 2010a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2011a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2012a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = recursivePublicMethods ? getPublicMethodRecursive(name, parameterTypes) 2013a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski : getDeclaredMethodInternal(name, parameterTypes); 2014a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Fail if we didn't find the method or it was expected to be public. 2015a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null || 2016a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski (recursivePublicMethods && !Modifier.isPublic(result.getAccessFlags()))) { 2017a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException(name + " " + Arrays.toString(parameterTypes)); 2018a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2019a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2020a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2021a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Method getPublicMethodRecursive(String name, Class<?>[] parameterTypes) { 2022a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search superclasses 2023a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = this; c != null; c = c.getSuperclass()) { 2024a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = c.getDeclaredMethodInternal(name, parameterTypes); 2025a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && Modifier.isPublic(result.getAccessFlags())) { 2026a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2027a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2028a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2029a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search iftable which has a flattened and uniqued list of interfaces 2030a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 2031a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 2032a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 2033a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) iftable[i]; 2034a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = ifc.getPublicMethodRecursive(name, parameterTypes); 2035a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && Modifier.isPublic(result.getAccessFlags())) { 2036a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2037a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2038a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 203951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2040a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 204151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 204251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 204351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 204451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 204551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Constructor} object that reflects the specified 204651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructor of the class or interface represented by this 204751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code parameterTypes} parameter is 204851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array of {@code Class} objects that identify the constructor's 204951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * formal parameter types, in declared order. 205051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 205151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents an inner class 205251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared in a non-static context, the formal parameter types 205351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * include the explicit enclosing instance as the first parameter. 205451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 205551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 205651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return The {@code Constructor} object for the constructor with the 205751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified parameter list 205851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found. 205951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 206051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 206151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 206251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 206351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 206451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 206551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 206651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 206751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 206851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared constructor 206951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 207051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 207151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 207251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 207351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 207451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 207551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 207651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 207751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 207851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 207951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 208051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 208151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 208251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 208351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getConstructor0(parameterTypes, Member.DECLARED); 208451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 208551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 208651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 208751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Finds a resource with a given name. The rules for searching resources 208851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * associated with a given class are implemented by the defining 208951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain ClassLoader class loader} of the class. This method 209051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * delegates to this object's class loader. If this object was loaded by 209151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the bootstrap class loader, the method delegates to {@link 209251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader#getSystemResourceAsStream}. 209351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 209451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Before delegation, an absolute resource name is constructed from the 209551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * given resource name using this algorithm: 209651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 209751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 209851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 209951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> If the {@code name} begins with a {@code '/'} 210051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 210151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * portion of the {@code name} following the {@code '/'}. 210251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 210351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> Otherwise, the absolute name is of the following form: 210451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 210551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 210651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code modified_package_name/name} 210751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 210851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 210951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Where the {@code modified_package_name} is the package name of this 211051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object with {@code '/'} substituted for {@code '.'} 211151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002e'</tt>). 211251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 211351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 211451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 211551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name name of the desired resource 211651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return A {@link java.io.InputStream} object or {@code null} if 211751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * no resource with this name is found 211851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException If {@code name} is {@code null} 211951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 212051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 212151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public InputStream getResourceAsStream(String name) { 212251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = resolveName(name); 2123a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader cl = getClassLoader(); 212451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cl==null) { 212551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // A system class. 212651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ClassLoader.getSystemResourceAsStream(name); 212751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 212851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return cl.getResourceAsStream(name); 212951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 213051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 213151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 213251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Finds a resource with a given name. The rules for searching resources 213351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * associated with a given class are implemented by the defining 213451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain ClassLoader class loader} of the class. This method 213551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * delegates to this object's class loader. If this object was loaded by 213651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the bootstrap class loader, the method delegates to {@link 213751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader#getSystemResource}. 213851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 213951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Before delegation, an absolute resource name is constructed from the 214051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * given resource name using this algorithm: 214151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 214251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 214351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 214451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> If the {@code name} begins with a {@code '/'} 214551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 214651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * portion of the {@code name} following the {@code '/'}. 214751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 214851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> Otherwise, the absolute name is of the following form: 214951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 215051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 215151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code modified_package_name/name} 215251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 215351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 215451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Where the {@code modified_package_name} is the package name of this 215551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object with {@code '/'} substituted for {@code '.'} 215651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002e'</tt>). 215751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 215851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 215951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 216051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name name of the desired resource 216151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return A {@link java.net.URL} object or {@code null} if no 216251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * resource with this name is found 216351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 216451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 216551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public java.net.URL getResource(String name) { 216651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = resolveName(name); 2167a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader cl = getClassLoader(); 216851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cl==null) { 216951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // A system class. 217051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ClassLoader.getSystemResource(name); 217151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 217251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return cl.getResource(name); 217351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 217451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 217551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 217651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code ProtectionDomain} of this class. If there is a 217751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * security manager installed, this method first calls the security 217851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager's {@code checkPermission} method with a 217951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code RuntimePermission("getProtectionDomain")} permission to 218051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ensure it's ok to get the 218151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code ProtectionDomain}. 218251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 218351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the ProtectionDomain of this class 218451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 218551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 218651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if a security manager exists and its 218751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code checkPermission} method doesn't allow 218851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * getting the ProtectionDomain. 218951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 219051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.security.ProtectionDomain 219151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 219251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.RuntimePermission 219351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 219451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 219551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public java.security.ProtectionDomain getProtectionDomain() { 2196a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 219751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 219851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 219951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 220051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Add a package name prefix if the name is not absolute Remove leading "/" 220151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if name is absolute 220251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 220351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String resolveName(String name) { 220451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (name == null) { 220551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 220651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 220751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (!name.startsWith("/")) { 220851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> c = this; 220951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (c.isArray()) { 221051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski c = c.getComponentType(); 221151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 221251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String baseName = c.getName(); 221351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int index = baseName.lastIndexOf('.'); 221451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (index != -1) { 221551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = baseName.substring(0, index).replace('.', '/') 221651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski +"/"+name; 221751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 221851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 221951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = name.substring(1); 222051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 222151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 222251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 222351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2224a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Constructor<T> getConstructor0(Class<?>[] parameterTypes, 2225a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int which) throws NoSuchMethodException 2226a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski { 2227a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parameterTypes == null) { 2228a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parameterTypes = EmptyArray.CLASS; 222951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2230a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c : parameterTypes) { 2231a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == null) { 2232a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("parameter type is null"); 2233a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 223451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2235a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Constructor<T> result = getDeclaredConstructorInternal(parameterTypes); 2236a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null || which == Member.PUBLIC && !Modifier.isPublic(result.getAccessFlags())) { 2237a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("<init> " + Arrays.toString(parameterTypes)); 2238a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2239a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 224051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 224151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 22421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 22431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the constructor with the given parameters if it is defined by this class; 22441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * {@code null} otherwise. This may return a non-public member. 22451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 22461ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param args the types of the parameters to the constructor. 22471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 22481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Constructor<T> getDeclaredConstructorInternal(Class<?>[] args); 224951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 225051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 225151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the assertion status that would be assigned to this 225251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class if it were to be initialized at the time this method is invoked. 225351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this class has had its assertion status set, the most recent 225451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * setting will be returned; otherwise, if any package default assertion 225551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * status pertains to this class, the most recent setting for the most 225651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specific pertinent package default assertion status is returned; 225751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, if this class is not a system class (i.e., it has a 225851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader) its class loader's default assertion status is returned; 225951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, the system class default assertion status is returned. 226051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 226151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Few programmers will have any need for this method; it is provided 226251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for the benefit of the JRE itself. (It allows a class to determine at 226351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the time that it is initialized whether assertions should be enabled.) 226451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method is not guaranteed to return the actual 226551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * assertion status that was (or will be) associated with the specified 226651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class when it was (or will be) initialized. 226751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 226851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the desired assertion status of the specified class. 226951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setClassAssertionStatus 227051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setPackageAssertionStatus 227151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setDefaultAssertionStatus 227251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 227351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 227451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean desiredAssertionStatus() { 2275a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 227651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 227751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 227851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 227970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * Returns the simple name of a member or local class, or {@code null} otherwise. 228070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 228170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native String getInnerClassName(); 228270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 228370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native int getInnerClassFlags(int defaultValue); 228470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 228570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** 228651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns true if and only if this class was declared as an enum in the 228751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code. 228851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 228951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if and only if this class was declared as an enum in the 229051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code 229151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 229251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 229351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isEnum() { 229451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // An enum must both directly extend java.lang.Enum and have 229551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // the ENUM bit set; classes for specialized enum constants 229651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // don't do the former. 229751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (this.getModifiers() & ENUM) != 0 && 229851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.getSuperclass() == java.lang.Enum.class; 229951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 230051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 230151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 230251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the elements of this enum class or null if this 230351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class object does not represent an enum type. 230451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 230551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array containing the values comprising the enum class 230651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this Class object in the order they're 230751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared, or null if this Class object does not 230851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represent an enum type 230951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 231051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 231151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public T[] getEnumConstants() { 231251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] values = getEnumConstantsShared(); 231351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (values != null) ? values.clone() : null; 231451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 231551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 231651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 231751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the elements of this enum class or null if this 231851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class object does not represent an enum type; 231951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * identical to getEnumConstants except that the result is 232051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * uncloned, cached, and shared by all callers. 232151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 232251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] getEnumConstantsShared() { 2323a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!isEnum()) return null; 2324a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (T[]) Enum.getSharedConstants((Class) this); 232551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 232651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 232751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 232851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Casts an object to the class or interface represented 232951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this {@code Class} object. 233051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 233151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param obj the object to be cast 233251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the object after casting, or null if obj is null 233351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 233451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the object is not 233551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null and is not assignable to the type T. 233651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 233751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 233851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 233951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public T cast(Object obj) { 234051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (obj != null && !isInstance(obj)) 234151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new ClassCastException(cannotCastMsg(obj)); 234251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (T) obj; 234351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 234451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 234551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String cannotCastMsg(Object obj) { 234651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "Cannot cast " + obj.getClass().getName() + " to " + getName(); 234751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 234851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 234951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 235051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Casts this {@code Class} object to represent a subclass of the class 235151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the specified class object. Checks that that the cast 235251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is valid, and throws a {@code ClassCastException} if it is not. If 235351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method succeeds, it always returns a reference to this class object. 235451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 235551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This method is useful when a client needs to "narrow" the type of 235651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a {@code Class} object to pass it to an API that restricts the 235751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that it is willing to accept. A cast would 235851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * generate a compile-time warning, as the correctness of the cast 235951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * could not be checked at runtime (because generic types are implemented 236051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by erasure). 236151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 236251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return this {@code Class} object, cast to represent a subclass of 236351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified class object. 236451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if this {@code Class} object does not 236551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represent a subclass of the specified class (here "subclass" includes 236651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class itself). 236751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 236851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 236951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public <U> Class<? extends U> asSubclass(Class<U> clazz) { 237051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (clazz.isAssignableFrom(this)) 237151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (Class<? extends U>) this; 237251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 237387778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak throw new ClassCastException(this.toString() + 237487778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak " cannot be cast to " + clazz.getName()); 237551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 237651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 237751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 237851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException {@inheritDoc} 237951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 238051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 238151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public <A extends Annotation> A getAnnotation(Class<A> annotationClass) { 238251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (annotationClass == null) 238351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NullPointerException(); 238451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 238570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak A annotation = getDeclaredAnnotation(annotationClass); 238670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotation != null) { 238770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return annotation; 238870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 238970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 239070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) { 239170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) { 239270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak annotation = sup.getDeclaredAnnotation(annotationClass); 239370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotation != null) { 239470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return annotation; 239570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 239670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 239770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 239870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 239970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return null; 240070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 240170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 240270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak @Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { 240370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotationType == null) { 240470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak throw new NullPointerException("annotationType == null"); 240570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 240670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 240770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (isDeclaredAnnotationPresent(annotationType)) { 240870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return true; 240970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 241070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 241170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotationType.isDeclaredAnnotationPresent(Inherited.class)) { 241270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) { 241370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (sup.isDeclaredAnnotationPresent(annotationType)) { 241470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return true; 241570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 241670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 241770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 241870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 241970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return false; 242051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 242151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 242270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 242351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 242470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * Returns an array containing all the annotations of this class. If there are no annotations 242570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * then an empty array is returned. 242670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 242770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * @see #getDeclaredAnnotations() 242851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 242970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak @Override public Annotation[] getAnnotations() { 243070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /* 243170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * We need to get the annotations declared on this class, plus the 243270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * annotations from superclasses that have the "@Inherited" annotation 243370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * set. We create a temporary map to use while we accumulate the 243470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * annotations and convert it to an array at the end. 243570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 243670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * It's possible to have duplicates when annotations are inherited. 243770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * We use a Map to filter those out. 243870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 243970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * HashMap might be overkill here. 244070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 244170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak HashMap<Class<?>, Annotation> map = new HashMap<Class<?>, Annotation>(); 244270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Annotation declaredAnnotation : getDeclaredAnnotations()) { 244370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak map.put(declaredAnnotation.annotationType(), declaredAnnotation); 244470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 244570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) { 244670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Annotation declaredAnnotation : sup.getDeclaredAnnotations()) { 244770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak Class<? extends Annotation> clazz = declaredAnnotation.annotationType(); 244870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (!map.containsKey(clazz) && clazz.isDeclaredAnnotationPresent(Inherited.class)) { 244970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak map.put(clazz, declaredAnnotation); 245070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 245170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 245270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 245351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 245470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /* Convert annotation values from HashMap to array. */ 245570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak Collection<Annotation> coll = map.values(); 245670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return coll.toArray(new Annotation[coll.size()]); 245751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 245851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 245970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** 246070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * Returns the annotations that are directly defined on the class 246170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * represented by this {@code Class}. Annotations that are inherited are not 246270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * included in the result. If there are no annotations at all, an empty 246370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * array is returned. 246470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 246570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * @see #getAnnotations() 246670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 246770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak @Override public native Annotation[] getDeclaredAnnotations(); 246851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 246951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 247070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * Returns the annotation if it exists. 247151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 247270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass); 247351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 247451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 247570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * Returns true if the annotation exists. 247651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 247770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native boolean isDeclaredAnnotationPresent(Class<? extends Annotation> annotationClass); 247851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 247951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Annotation types cache their internal (AnnotationType) form 248051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2481a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** @hide */ 2482a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public void setAnnotationType(AnnotationType type) { 248351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski annotationType = type; 248451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 248551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2486a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** @hide */ 2487a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public AnnotationType getAnnotationType() { 248851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return annotationType; 248951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 249051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2491a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2492a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Is this a runtime created proxy class? 2493a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2494a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2495a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2496a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isProxy() { 2497a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (accessFlags & 0x00040000) != 0; 2498a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2499a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2500a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2501a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns the dex file from which this class was loaded. 2502a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2503a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2504a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2505a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Dex getDex() { 2506a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dexCache == null) { 2507a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 2508a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2509a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return dexCache.getDex(); 2510a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2511a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2512a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns a string from the dex cache, computing the string from the dex file if necessary. 2513a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2514a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2515a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2516a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public String getDexCacheString(Dex dex, int dexStringIndex) { 251770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak String s = dexCache.getResolvedString(dexStringIndex); 2518a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (s == null) { 2519a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski s = dex.strings().get(dexStringIndex).intern(); 252070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak dexCache.setResolvedString(dexStringIndex, s); 2521a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2522a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return s; 2523a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2524a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2525a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2526a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns a resolved type from the dex cache, computing the type from the dex file if 2527a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * necessary. 2528a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2529a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2530a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2531a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?> getDexCacheType(Dex dex, int dexTypeIndex) { 253270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak Class<?> resolvedType = dexCache.getResolvedType(dexTypeIndex); 2533a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (resolvedType == null) { 2534a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int descriptorIndex = dex.typeIds().get(dexTypeIndex); 2535a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String descriptor = getDexCacheString(dex, descriptorIndex); 2536a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski resolvedType = InternalNames.getClass(getClassLoader(), descriptor); 253770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak dexCache.setResolvedType(dexTypeIndex, resolvedType); 2538a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2539a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return resolvedType; 2540a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2541a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2542a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The annotation directory offset of this class in its own Dex, or 0 if it 2543a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * is unknown. 2544a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2545a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * TODO: 0 is a sentinel that means 'no annotations directory'; this should be -1 if unknown 2546a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2547a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2548a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2549a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getDexAnnotationDirectoryOffset() { 2550a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Dex dex = getDex(); 2551a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dex == null) { 2552a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return 0; 2553a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2554a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int classDefIndex = getDexClassDefIndex(); 2555a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classDefIndex < 0) { 2556a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return 0; 2557a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2558a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return dex.annotationDirectoryOffsetFromClassDefIndex(classDefIndex); 2559a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2560a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2561a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The type index of this class in its own Dex, or -1 if it is unknown. If a class is referenced 2562a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * by multiple Dex files, it will have a different type index in each. Dex files support 65534 2563a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * type indices, with 65535 representing no index. 2564a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2565a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2566a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2567a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getDexTypeIndex() { 2568a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int typeIndex = dexTypeIndex; 2569a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (typeIndex != 65535) { 2570a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return typeIndex; 2571a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2572a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized (this) { 2573a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = dexTypeIndex; 2574a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (typeIndex == 65535) { 2575a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dexClassDefIndex >= 0) { 2576a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = getDex().typeIndexFromClassDefIndex(dexClassDefIndex); 2577a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 2578a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = getDex().findTypeIndex(InternalNames.getInternalName(this)); 2579a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (typeIndex < 0) { 2580a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = -1; 2581a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2582a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2583a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski dexTypeIndex = typeIndex; 2584a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2585a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2586a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return typeIndex; 2587a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2588a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean canAccess(Class<?> c) { 2589a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if(Modifier.isPublic(c.accessFlags)) { 2590a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 2591a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2592a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return inSamePackage(c); 2593a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2594a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2595a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean canAccessMember(Class<?> memberClass, int memberModifiers) { 2596a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (memberClass == this || Modifier.isPublic(memberModifiers)) { 2597a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 2598a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2599a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Modifier.isPrivate(memberModifiers)) { 2600a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 2601a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2602a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Modifier.isProtected(memberModifiers)) { 2603a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> parent = this.superClass; parent != null; parent = parent.superClass) { 2604a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parent == memberClass) { 2605a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 2606a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2607a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2608a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2609a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return inSamePackage(memberClass); 2610a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2611a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2612a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean inSamePackage(Class<?> c) { 2613a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classLoader != c.classLoader) { 2614a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 2615a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2616a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName1 = getPackageName$(); 2617a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName2 = c.getPackageName$(); 2618a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (packageName1 == null) { 2619a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName2 == null; 2620a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (packageName2 == null) { 2621a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 2622a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 2623a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName1.equals(packageName2); 2624a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2625a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2626a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2627a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2628a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2629a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getAccessFlags() { 2630a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return accessFlags; 2631a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2632a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 26331ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 26341ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 26351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the method if it is defined by this class; {@code null} otherwise. This may return a 26361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * non-public member. 26371ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 26381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param name the method name 26391ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param args the method's parameter types 26401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 26411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Method getDeclaredMethodInternal(String name, Class<?>[] args); 2642a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2643a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2644a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The class def of this class in its own Dex, or -1 if there is no class def. 2645a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2646a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2647a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2648a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getDexClassDefIndex() { 2649a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (dexClassDefIndex == 65535) ? -1 : dexClassDefIndex; 2650a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2651a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static class Caches { 2652a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2653a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Cache to avoid frequent recalculation of generic interfaces, which is generally uncommon. 2654a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Sized sufficient to allow ConcurrentHashMapTest to run without recalculating its generic 2655a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * interfaces (required to avoid time outs). Validated by running reflection heavy code 2656a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * such as applications using Guice-like frameworks. 2657a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2658a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static final BasicLruCache<Class, Type[]> genericInterfaces 2659a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski = new BasicLruCache<Class, Type[]>(8); 2660a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 266151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 2662