Class.java revision 92d7211db6d0d1afd981f8e4a51857c88cee0e03
151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 22c87ad3a45cecf9e344487cad1abfdebe79f2c7cNarayan Kamath * Copyright (C) 2014 The Android Open Source Project 351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. 451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it 751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as 851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation. Oracle designates this 951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided 1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code. 1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT 1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that 1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code). 1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version 1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation, 2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any 2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions. 2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.lang; 2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29be1608980825868eb1c70fab6eb86fe06bd304ceYi Kongimport java.lang.reflect.AnnotatedElement; 3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Array; 3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.GenericArrayType; 3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Member; 3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Field; 3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Method; 3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Constructor; 3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.GenericDeclaration; 3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Modifier; 3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Type; 3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.TypeVariable; 4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.InvocationTargetException; 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.ref.SoftReference; 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.InputStream; 4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.ObjectStreamField; 4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.security.AccessController; 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.security.PrivilegedAction; 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.ArrayList; 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Arrays; 4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Collection; 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.HashSet; 5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Iterator; 5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.List; 5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.LinkedList; 5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.LinkedHashSet; 5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Set; 5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Map; 5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.HashMap; 5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.misc.Unsafe; 5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.reflect.CallerSensitive; 5970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniakimport java.lang.annotation.Inherited; 6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.annotation.Annotation; 6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Proxy; 6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.reflect.annotation.*; 6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.reflect.misc.ReflectUtil; 6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 65a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport java.io.Serializable; 66a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport java.lang.reflect.AccessibleObject; 67a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport com.android.dex.Dex; 68a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport dalvik.system.VMStack; 69a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.InternalNames; 70a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.GenericSignatureParser; 71a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.Types; 72a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.BasicLruCache; 73a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.CollectionUtils; 74a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.EmptyArray; 75a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.SneakyThrow; 761ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamathimport java.util.Collections; 77a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Instances of the class {@code Class} represent classes and 8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces in a running Java application. An enum is a kind of 8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class and an annotation is a kind of interface. Every array also 8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * belongs to a class that is reflected as a {@code Class} object 8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that is shared by all arrays with the same element type and number 8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of dimensions. The primitive Java types ({@code boolean}, 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code byte}, {@code char}, {@code short}, 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int}, {@code long}, {@code float}, and 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code double}), and the keyword {@code void} are also 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented as {@code Class} objects. 8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> {@code Class} has no public constructor. Instead {@code Class} 9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects are constructed automatically by the Java Virtual Machine as classes 9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are loaded and by calls to the {@code defineClass} method in the class 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader. 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The following example uses a {@code Class} object to print the 9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class name of an object: 9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> <blockquote><pre> 9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * void printClassName(Object obj) { 10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * System.out.println("The class of " + obj + 10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * " is " + obj.getClass().getName()); 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * } 10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> It is also possible to get the {@code Class} object for a named 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type (or for void) using a class literal. See Section 15.8.2 of 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>. 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For example: 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> <blockquote> 11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());} 11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param <T> the type of the class modeled by this {@code Class} 11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. For example, the type of {@code String.class} is {@code 11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class<String>}. Use {@code Class<?>} if the class being modeled is 11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * unknown. 11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author unascribed 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#defineClass(byte[], int, int) 12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0 12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic final 12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski class Class<T> implements java.io.Serializable, 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski java.lang.reflect.GenericDeclaration, 12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski java.lang.reflect.Type, 12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski java.lang.reflect.AnnotatedElement { 1281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** use serialVersionUID from JDK 1.1 for interoperability */ 1291ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private static final long serialVersionUID = 3206093459760846163L; 1301ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1311ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** defining class loader, or null for the "bootstrap" system loader. */ 1321ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient ClassLoader classLoader; 1331ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1341ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * For array classes, the component class object for instanceof/checkcast (for String[][][], 1361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * this will be String[][]). null for non-array classes. 1371ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Class<?> componentType; 1391ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * DexCache of resolved constant pool entries. Will be null for certain runtime-generated classes 1411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * e.g. arrays and primitive classes. 1421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient DexCache dexCache; 1441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1461ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * The interface table (iftable_) contains pairs of a interface class and an array of the 1471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * interface methods. There is one pair per interface supported by this class. That 1481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * means one pair for each interface we support directly, indirectly via superclass, or 1491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * indirectly via a superinterface. This will be null if neither we nor our superclass 1501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * implement any interfaces. 1511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 1521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()". 1531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Invoke faceObj.blah(), where "blah" is part of the Face interface. We can't easily use a 1541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * single vtable. 1551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 1561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * For every interface a concrete class implements, we create an array of the concrete vtable_ 1571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * methods for the methods in the interface. 1581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1591ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Object[] ifTable; 1601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Lazily computed name of this class; always prefer calling getName(). */ 1621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient String name; 1631ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1641ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** The superclass, or null if this is java.lang.Object, an interface or primitive type. */ 1651ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Class<? super T> superClass; 1661ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 16770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** 16870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * If class verify fails, we must return same error on subsequent tries. We may store either 16970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * the class of the error, or an actual instance of Throwable here. 17070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 17170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private transient Object verifyError; 1721ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1731ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1741ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass 1751ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * is copied in, and virtual methods from our class either replace those from the super or are 1761ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * appended. For abstract classes, methods may be created in the vtable that aren't in 1771ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * virtual_ methods_ for miranda methods. 1781ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1791ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Object vtable; 1801ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 18170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** Short-cut to dexCache.strings */ 18270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private transient long dexCacheStrings; 18370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 1841ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** access flags; low 16 bits are defined by VM spec */ 1851ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int accessFlags; 1861ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1871ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1881ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Instance fields. These describe the layout of the contents of an Object. Note that only the 1891ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * fields directly declared by this class are listed in iFields; fields declared by a 1901ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * superclass are listed in the superclass's Class.iFields. 1911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 1921ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * All instance fields that refer to objects are guaranteed to be at the beginning of the field 1931ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * list. {@link Class#numReferenceInstanceFields} specifies the number of reference fields. 1941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1951ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient long iFields; 1961ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 197551112f15a648b739f842b2466b5fdae3e80d6deAlex Light /** All methods with this class as the base for virtual dispatch. */ 198551112f15a648b739f842b2466b5fdae3e80d6deAlex Light private transient long methods; 199551112f15a648b739f842b2466b5fdae3e80d6deAlex Light 2001ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Static fields */ 2011ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient long sFields; 2021ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 20370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** Class flags to help the GC with object scanning. */ 20470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private transient int classFlags; 20570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 2061ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2071ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Total size of the Class instance; used when allocating storage on GC heap. 2081ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * See also {@link Class#objectSize}. 2091ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2101ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int classSize; 2111ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * tid used to check for recursive static initializer invocation. 2141ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2151ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int clinitThreadId; 2161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Class def index from dex file. An index of 65535 indicates that there is no class definition, 2191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * for example for an array type. 2201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * TODO: really 16bits as type indices are 16bit. 2211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int dexClassDefIndex; 2231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2241ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2251ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Class type index from dex file, lazily computed. An index of 65535 indicates that the type 2261ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * index isn't known. Volatile to avoid double-checked locking bugs. 2271ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * TODO: really 16bits as type indices are 16bit. 2281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2291ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient volatile int dexTypeIndex; 2301ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2311ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Number of instance fields that are object references. */ 2321ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int numReferenceInstanceFields; 2331ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2341ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Number of static fields that are object references. */ 2351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int numReferenceStaticFields; 2361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2371ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Total object size; used when allocating storage on GC heap. For interfaces and abstract 2391ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * classes this will be zero. See also {@link Class#classSize}. 2401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int objectSize; 2421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * The lower 16 bits is the primitive type value, or 0 if not a primitive type; set for 2451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * generated primitive classes. 2461ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int primitiveType; 2481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Bitmap of offsets of iFields. */ 2501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int referenceInstanceOffsets; 2511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** State of class initialization */ 2531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int status; 2541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 255551112f15a648b739f842b2466b5fdae3e80d6deAlex Light /** Offset of the first virtual method copied from an interface in the methods array. */ 256551112f15a648b739f842b2466b5fdae3e80d6deAlex Light private transient short copiedMethodsOffset; 257551112f15a648b739f842b2466b5fdae3e80d6deAlex Light 258551112f15a648b739f842b2466b5fdae3e80d6deAlex Light /** Offset of the first virtual method defined in this class in the methods array. */ 259551112f15a648b739f842b2466b5fdae3e80d6deAlex Light private transient short virtualMethodsOffset; 260551112f15a648b739f842b2466b5fdae3e80d6deAlex Light 2611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private AnnotationType annotationType; 2621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2632ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong private static final int ANNOTATION = 0x00002000; 2642ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong private static final int ENUM = 0x00004000; 2652ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong private static final int SYNTHETIC = 0x00001000; 2662ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong private static final int FINALIZABLE = 0x80000000; 26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor. Only the Java Virtual Machine creates Class 27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects. 27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private Class() {} 27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Converts the object to a string. The string representation is the 27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * string "class" or "interface", followed by a space, and then by the 27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * fully qualified name of the class in the format returned by 27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code getName}. If this {@code Class} object represents a 28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, this method returns the name of the primitive type. If 28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents void this method returns 28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "void". 28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of this class object. 28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String toString() { 28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) 28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski + getName(); 28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} object associated with the class or 29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface with the given string name. Invoking this method is 29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent to: 29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName(className, true, currentLoader)} 29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * where {@code currentLoader} denotes the defining class loader of 30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the current class. 30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> For example, the following code fragment returns the 30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * runtime {@code Class} descriptor for the class named 30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code java.lang.Thread}: 30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class t = Class.forName("java.lang.Thread")} 31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A call to {@code forName("X")} causes the class named 31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code X} to be initialized. 31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param className the fully qualified name of the desired class. 31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Class} object for the class with the 31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified name. 31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception LinkageError if the linkage fails 31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization provoked 32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this method fails 32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ClassNotFoundException if the class cannot be located 32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Class<?> forName(String className) 32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws ClassNotFoundException { 326a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return forName(className, true, VMStack.getCallingClassLoader()); 32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} object associated with the class or 33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface with the given string name, using the given class loader. 33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Given the fully qualified name for a class or interface (in the same 33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * format returned by {@code getName}) this method attempts to 33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * locate, load, and link the class or interface. The specified class 33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader is used to load the class or interface. If the parameter 33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code loader} is null, the class is loaded through the bootstrap 33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader. The class is initialized only if the 33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code initialize} parameter is {@code true} and if it has 34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not been initialized earlier. 34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If {@code name} denotes a primitive type or void, an attempt 34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will be made to locate a user-defined class in the unnamed package whose 34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name is {@code name}. Therefore, this method cannot be used to 34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtain any of the {@code Class} objects representing primitive 34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types or void. 34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If {@code name} denotes an array class, the component type of 34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array class is loaded but not initialized. 35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> For example, in an instance method the expression: 35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName("Foo")} 35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is equivalent to: 35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName("Foo", true, this.getClass().getClassLoader())} 36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method throws errors related to loading, linking or 36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The 36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Java Language Specification</em>. 36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method does not check whether the requested class 36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is accessible to its caller. 36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the {@code loader} is {@code null}, and a security 37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager is present, and the caller's class loader is not null, then this 37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method calls the security manager's {@code checkPermission} method 37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with a {@code RuntimePermission("getClassLoader")} permission to 37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ensure it's ok to access the bootstrap class loader. 37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name fully qualified name of the desired class 37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param initialize whether the class must be initialized 37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param loader class loader from which the class must be loaded 37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return class object representing the desired class 37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception LinkageError if the linkage fails 38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization provoked 38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this method fails 38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ClassNotFoundException if the class cannot be located by 38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified class loader 38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Class#forName(String) 38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader 38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Class<?> forName(String name, boolean initialize, 39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ClassLoader loader) 39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws ClassNotFoundException 39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (loader == null) { 396bac8ef10e8a32316e4eedc6163dd3776cf5e5d7eNarayan Kamath loader = BootClassLoader.getInstance(); 397a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 398a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> result; 399a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski try { 400a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = classForName(name, initialize, loader); 401a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } catch (ClassNotFoundException e) { 402a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Throwable cause = e.getCause(); 403a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (cause instanceof LinkageError) { 404a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw (LinkageError) cause; 40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 406a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw e; 40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 408a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** Called after security checks have been made. */ 412a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski static native Class<?> classForName(String className, boolean shouldInitialize, 413a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader classLoader) throws ClassNotFoundException; 41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Creates a new instance of the class represented by this {@code Class} 41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The class is instantiated as if by a {@code new} 41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * expression with an empty argument list. The class is initialized if it 41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has not already been initialized. 42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that this method propagates any exception thrown by the 42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * nullary constructor, including a checked exception. Use of 42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method effectively bypasses the compile-time exception 42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * checking that would otherwise be performed by the compiler. 42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The {@link 42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Constructor#newInstance(java.lang.Object...) 42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor.newInstance} method avoids this problem by wrapping 42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * any exception thrown by the constructor in a (checked) {@link 42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.InvocationTargetException}. 43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a newly allocated instance of the class represented by this 43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. 43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalAccessException if the class or its nullary 43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructor is not accessible. 43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception InstantiationException 43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if this {@code Class} represents an abstract class, 43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an interface, an array class, a primitive type, or void; 43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or if the class has no nullary constructor; 43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or if the instantiation fails for some other reason. 44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization 44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * provoked by this method fails. 44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 44551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 44651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 44751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 44951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 45151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * creation of new instances of this class 45251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 45651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 45751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 45851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 45951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 46151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 4621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native T newInstance() throws InstantiationException, IllegalAccessException; 46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 46551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Object} is assignment-compatible 46651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with the object represented by this {@code Class}. This method is 46751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the dynamic equivalent of the Java language {@code instanceof} 46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * operator. The method returns {@code true} if the specified 46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} argument is non-null and can be cast to the 47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * reference type represented by this {@code Class} object without 47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * raising a {@code ClassCastException.} It returns {@code false} 47251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise. 47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, if this {@code Class} object represents a 47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared class, this method returns {@code true} if the specified 47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} argument is an instance of the represented class (or 47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of any of its subclasses); it returns {@code false} otherwise. If 47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents an array class, this method 47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code true} if the specified {@code Object} argument 48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * can be converted to an object of the array class by an identity 48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * conversion or by a widening reference conversion; it returns 48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. If this {@code Class} object 48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents an interface, this method returns {@code true} if the 48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class or any superclass of the specified {@code Object} argument 48551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implements this interface; it returns {@code false} otherwise. If 48651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type, this method 48751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code false}. 48851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 4899e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param object the object to check 49051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if {@code obj} is an instance of this class 49151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 49251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 49351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 494a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isInstance(Object object) { 495a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (object == null) { 496a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 497a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 498a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return isAssignableFrom(object.getClass()); 499a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 50051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 50151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 50251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the class or interface represented by this 50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object is either the same as, or is a superclass or 50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterface of, the class or interface represented by the specified 50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} parameter. It returns {@code true} if so; 50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise it returns {@code false}. If this {@code Class} 50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object represents a primitive type, this method returns 50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code true} if the specified {@code Class} parameter is 51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exactly this {@code Class} object; otherwise it returns 51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. 51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, this method tests whether the type represented by the 51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified {@code Class} parameter can be converted to the type 51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object via an identity conversion 51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or via a widening reference conversion. See <em>The Java Language 51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Specification</em>, sections 5.1.1 and 5.1.4 , for details. 51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 5199e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param c the {@code Class} object to be checked 52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code boolean} value indicating whether objects of the 52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type {@code cls} can be assigned to objects of this class 52251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if the specified Class parameter is 52351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null. 52451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 52551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 526a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isAssignableFrom(Class<?> c) { 527a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (this == c) { 528a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; // Can always assign to things of the same type. 529a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (this == Object.class) { 530a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return !c.isPrimitive(); // Can assign any reference to java.lang.Object. 531a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isArray()) { 532a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return c.isArray() && componentType.isAssignableFrom(c.componentType); 533a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isInterface()) { 534a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search iftable which has a flattened and uniqued list of interfaces. 535a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = c.ifTable; 536a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 537a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 5381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath if (iftable[i] == this) { 539a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 540a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 541a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 542a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 543a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 544a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 545a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!c.isInterface()) { 546a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (c = c.superClass; c != null; c = c.superClass) { 547a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == this) { 548a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 549a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 550a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 551a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 552a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 553a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 554a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Class} object represents an 55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface type. 55951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 56051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this object represents an interface; 56151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 56251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 563a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isInterface() { 564a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (accessFlags & Modifier.INTERFACE) != 0; 565a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 56651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 56751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 56851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if this {@code Class} object represents an array class. 56951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 57051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this object represents an array class; 57151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 57251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 57351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 574a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isArray() { 575a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getComponentType() != null; 576a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 57951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Class} object represents a 58051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type. 58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 58251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> There are nine predefined {@code Class} objects to represent 58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the eight primitive types and void. These are created by the Java 58451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Virtual Machine, and have the same names as the primitive types that 58551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * they represent, namely {@code boolean}, {@code byte}, 58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code char}, {@code short}, {@code int}, 58751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code long}, {@code float}, and {@code double}. 58851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 58951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> These objects may only be accessed via the following public static 59051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * final variables, and are the only {@code Class} objects for which 59151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method returns {@code true}. 59251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 59351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if and only if this class represents a primitive type 59451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 59551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Boolean#TYPE 59651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Character#TYPE 59751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Byte#TYPE 59851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Short#TYPE 59951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Integer#TYPE 60051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Long#TYPE 60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Float#TYPE 60251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Double#TYPE 60351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Void#TYPE 60451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 606a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isPrimitive() { 6071ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return (primitiveType & 0xFFFF) != 0; 608a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 60951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 61051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 6112ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * Indicates whether this {@code Class} or its parents override finalize. 6122ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * 6132ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * @return {@code true} if and if this class or its parents override 6142ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * finalize; 6152ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * 6162ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * @hide 6172ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong */ 6182ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong public boolean isFinalizable() { 6192ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong return (getModifiers() & FINALIZABLE) != 0; 6202ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong } 6212ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong 6222ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong /** 62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns true if this {@code Class} object represents an annotation 62451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type. Note that if this method returns true, {@link #isInterface()} 62551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * would also return true, as all annotation types are also interfaces. 62651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 62751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this class object represents an annotation 62851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type; {@code false} otherwise 62951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 63051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 63151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isAnnotation() { 63251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (getModifiers() & ANNOTATION) != 0; 63351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 63451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 63551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if this class is a synthetic class; 63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code false} otherwise. 63851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a synthetic class as 63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * defined by the Java Language Specification. 64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isSynthetic() { 64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (getModifiers() & SYNTHETIC) != 0; 64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 64651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 64751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the name of the entity (class, interface, array class, 64851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, or void) represented by this {@code Class} object, 64951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as a {@code String}. 65051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 65151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a reference type that is not an 65251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array type then the binary name of the class is returned, as specified 65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by 65451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>. 65551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 65651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a primitive type or void, then the 65751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name returned is a {@code String} equal to the Java language 65851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * keyword corresponding to the primitive type or void. 65951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 66051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a class of arrays, then the internal 66151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * form of the name consists of the name of the element type preceded by 66251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one or more '{@code [}' characters representing the depth of the array 66351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * nesting. The encoding of element type names is as follows: 66451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 66551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><table summary="Element types and encodings"> 66651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><th> Element Type <th> <th> Encoding 66751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> boolean <td> <td align=center> Z 66851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> byte <td> <td align=center> B 66951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> char <td> <td align=center> C 67051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> class or interface 67151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td> <td align=center> L<i>classname</i>; 67251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> double <td> <td align=center> D 67351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> float <td> <td align=center> F 67451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> int <td> <td align=center> I 67551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> long <td> <td align=center> J 67651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> short <td> <td align=center> S 67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </table></blockquote> 67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The class or interface name <i>classname</i> is the binary name of 68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class specified above. 68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Examples: 68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 68451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * String.class.getName() 68551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "java.lang.String" 68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * byte.class.getName() 68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "byte" 68851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (new Object[3]).getClass().getName() 68951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "[Ljava.lang.Object;" 69051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (new int[3][4][5][6][7][8][9]).getClass().getName() 69151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "[[[[[[[I" 69251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 69351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 69451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the name of the class or interface 69551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 69651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 69751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getName() { 69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String name = this.name; 69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (name == null) 700a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski this.name = name = getNameNative(); 70151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 70251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 70351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 704a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native String getNameNative(); 70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 70651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 70751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the class loader for the class. Some implementations may use 70851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null to represent the bootstrap class loader. This method will return 70951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null in such implementations if this class was loaded by the bootstrap 71051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader. 71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If a security manager is present, and the caller's class loader is 71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not null and the caller's class loader is not the same as or an ancestor of 71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class loader for the class whose class loader is requested, then 71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method calls the security manager's {@code checkPermission} 71651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method with a {@code RuntimePermission("getClassLoader")} 71751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * permission to ensure it's ok to access the class loader for the class. 71851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 71951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object 72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type or void, null is returned. 72151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 72251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the class loader that loaded the class or interface 72351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if a security manager exists and its 72651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code checkPermission} method denies 72751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the class loader for the class. 72851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader 72951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 73051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.RuntimePermission 73151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 73251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public ClassLoader getClassLoader() { 733a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isPrimitive()) { 73451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 73551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 736a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (classLoader == null) ? BootClassLoader.getInstance() : classLoader; 73751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 73851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 74051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code TypeVariable} objects that represent the 74151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type variables declared by the generic declaration represented by this 74251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code GenericDeclaration} object, in declaration order. Returns an 74351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if the underlying generic declaration declares no type 74451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * variables. 74551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 74651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of {@code TypeVariable} objects that represent 74751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the type variables declared by this generic declaration 74851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError if the generic 74951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * signature of this generic declaration does not conform to 75051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the format specified in 75151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 75251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 75351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 754a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski @Override public synchronized TypeVariable<Class<T>>[] getTypeParameters() { 755d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao String annotationSignature = getSignatureAttribute(); 756a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature == null) { 757a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return EmptyArray.TYPE_VARIABLE; 758a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 759a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 760a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 761a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return parser.formalTypeParameters; 76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} representing the superclass of the entity 76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (class, interface, primitive type or void) represented by this 76751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class}. If this {@code Class} represents either the 76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} class, an interface, a primitive type, or void, then 76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null is returned. If this object represents an array class then the 77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object representing the {@code Object} class is 77151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. 77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the superclass of the class represented by this object. 77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 775a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<? super T> getSuperclass() { 776a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // For interfaces superClass is Object (which agrees with the JNI spec) 777a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // but not with the expected behavior here. 778a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isInterface()) { 779a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 780a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 781a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return superClass; 782a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 783a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Type} representing the direct superclass of 78751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the entity (class, interface, primitive type or void) represented by 78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class}. 78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the superclass is a parameterized type, the {@code Type} 79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object returned must accurately reflect the actual type 79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameters used in the source code. The parameterized type 79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing the superclass is created if it had not been 79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * created before. See the declaration of {@link 79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.ParameterizedType ParameterizedType} for the 79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * semantics of the creation process for parameterized types. If 79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} represents either the {@code Object} 79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, an interface, a primitive type, or void, then null is 79951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. If this object represents an array class then the 80051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object representing the {@code Object} class is 80151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. 80251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 80351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError if the generic 80451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class signature does not conform to the format specified in 80551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 80651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws TypeNotPresentException if the generic superclass 80751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * refers to a non-existent type declaration 80851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.MalformedParameterizedTypeException if the 80951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * generic superclass refers to a parameterized type that cannot be 81051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instantiated for any reason 81151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the superclass of the class represented by this object 81251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 81351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 81451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Type getGenericSuperclass() { 815a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Type genericSuperclass = getSuperclass(); 816a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // This method is specified to return null for all cases where getSuperclass 817a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // returns null, i.e, for primitives, interfaces, void and java.lang.Object. 818a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (genericSuperclass == null) { 819a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 820a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 821a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 822d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao String annotationSignature = getSignatureAttribute(); 823a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature != null) { 824a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 825a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 826a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski genericSuperclass = parser.superclassType; 827a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 828a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return Types.getType(genericSuperclass); 82951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 83051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 83151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 83251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Gets the package for this class. The class loader of this class is used 83351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to find the package. If the class was loaded by the bootstrap class 83451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader the set of packages loaded from CLASSPATH is searched to find the 83551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * package of the class. Null is returned if no package object was created 83651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the class loader of this class. 83751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 83851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Packages have attributes for versions and specifications only if the 83951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * information was defined in the manifests that accompany the classes, and 84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the class loader created the package instance with the attributes 84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * from the manifest. 84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 84351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the package of the class, or null if no package 84451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * information is available from the archive or codebase. 84551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 84651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Package getPackage() { 847a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader loader = getClassLoader(); 848a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (loader != null) { 849a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName = getPackageName$(); 850a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName != null ? loader.getPackage(packageName) : null; 851a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 852a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 853a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 854a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 855a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 856a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns the package name of this class. This returns null for classes in 857a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * the default package. 858a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 859a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 860a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 861a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public String getPackageName$() { 862a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String name = getName(); 863a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int last = name.lastIndexOf('.'); 864a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return last == -1 ? null : name.substring(0, last); 86551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 86651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 86751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 86851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 86951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines the interfaces implemented by the class or interface 87051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 87151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 87251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class, the return value is an array 87351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * containing objects representing all interfaces implemented by the 87451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. The order of the interface objects in the array corresponds to 87551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the order of the interface names in the {@code implements} clause 87651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the declaration of the class represented by this object. For 87751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, given the declaration: 87851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 87951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code class Shimmer implements FloorWax, DessertTopping { ... }} 88051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 88151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * suppose the value of {@code s} is an instance of 88251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Shimmer}; the value of the expression: 88351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 88451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code s.getClass().getInterfaces()[0]} 88551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 88651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is the {@code Class} object that represents interface 88751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code FloorWax}; and the value of: 88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code s.getClass().getInterfaces()[1]} 89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is the {@code Class} object that represents interface 89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code DessertTopping}. 89351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 89451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents an interface, the array contains objects 89551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing all interfaces extended by the interface. The order of the 89651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface objects in the array corresponds to the order of the interface 89751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * names in the {@code extends} clause of the declaration of the 89851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface represented by this object. 89951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 90051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class or interface that implements no 90151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces, the method returns an array of length 0. 90251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 90351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a primitive type or void, the method 90451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns an array of length 0. 90551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 90651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of interfaces implemented by this class. 90751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 908a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?>[] getInterfaces() { 909a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isArray()) { 910a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return new Class<?>[] { Cloneable.class, Serializable.class }; 911a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isProxy()) { 912a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getProxyInterfaces(); 913a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 914a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Dex dex = getDex(); 915a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dex == null) { 916a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return EmptyArray.CLASS; 917a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 918a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski short[] interfaces = dex.interfaceTypeIndicesFromClassDefIndex(dexClassDefIndex); 919a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?>[] result = new Class<?>[interfaces.length]; 920a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < interfaces.length; i++) { 921a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result[i] = getDexCacheType(dex, interfaces[i]); 922a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 923a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 924a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 925a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 926a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Returns the interfaces that this proxy class directly implements. 927a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native Class<?>[] getProxyInterfaces(); 92851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 92951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 93051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Type}s representing the interfaces 93151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * directly implemented by the class or interface represented by 93251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this object. 93351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 93451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If a superinterface is a parameterized type, the 93551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Type} object returned for it must accurately reflect 93651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the actual type parameters used in the source code. The 93751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameterized type representing each superinterface is created 93851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if it had not been created before. See the declaration of 93951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link java.lang.reflect.ParameterizedType ParameterizedType} 94051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for the semantics of the creation process for parameterized 94151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types. 94251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 94351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class, the return value is an 94451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array containing objects representing all interfaces 94551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implemented by the class. The order of the interface objects in 94651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array corresponds to the order of the interface names in 94751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the {@code implements} clause of the declaration of the class 94851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. In the case of an array class, the 94951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces {@code Cloneable} and {@code Serializable} are 95051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned in that order. 95151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 95251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents an interface, the array contains 95351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects representing all interfaces directly extended by the 95451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface. The order of the interface objects in the array 95551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * corresponds to the order of the interface names in the 95651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code extends} clause of the declaration of the interface 95751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 95851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 95951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents a class or interface that 96051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implements no interfaces, the method returns an array of length 96151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 0. 96251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 96351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents a primitive type or void, the 96451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns an array of length 0. 96551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 96651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError 96751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the generic class signature does not conform to the format 96851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified in 96951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 97051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws TypeNotPresentException if any of the generic 97151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces refers to a non-existent type declaration 97251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.MalformedParameterizedTypeException 97351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if any of the generic superinterfaces refer to a parameterized 97451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type that cannot be instantiated for any reason 97551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of interfaces implemented by this class 97651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 97751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 97851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Type[] getGenericInterfaces() { 979a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Type[] result; 980a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized (Caches.genericInterfaces) { 981a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = Caches.genericInterfaces.get(this); 982a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null) { 983d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao String annotationSignature = getSignatureAttribute(); 984a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature == null) { 985a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = getInterfaces(); 986a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 987a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 988a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 989a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = Types.getTypeArray(parser.interfaceTypes, false); 990a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 991a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Caches.genericInterfaces.put(this, result); 992a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 993a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 994a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (result.length == 0) ? result : result.clone(); 99551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 99651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 99751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 99851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 99951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} representing the component type of an 100051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. If this class does not represent an array class this method 100151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns null. 100251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 100351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Class} representing the component type of this 100451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class if this class is an array 100551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.reflect.Array 100651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 100751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1008a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?> getComponentType() { 1009a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return componentType; 1010a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 101151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 101251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 101351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the Java language modifiers for this class or interface, encoded 101451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in an integer. The modifiers consist of the Java Virtual Machine's 101551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constants for {@code public}, {@code protected}, 101651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code private}, {@code final}, {@code static}, 101751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code abstract} and {@code interface}; they should be decoded 101851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * using the methods of class {@code Modifier}. 101951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 102051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the underlying class is an array class, then its 102151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code public}, {@code private} and {@code protected} 102251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * modifiers are the same as those of its component type. If this 102351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} represents a primitive type or void, its 102451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code public} modifier is always {@code true}, and its 102551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code protected} and {@code private} modifiers are always 102651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. If this object represents an array class, a 102751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type or void, then its {@code final} modifier is always 102851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code true} and its interface modifier is always 102951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. The values of its other modifiers are not determined 103051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this specification. 103151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 103251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The modifier encodings are defined in <em>The Java Virtual Machine 103351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Specification</em>, table 4.1. 103451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 103551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code int} representing the modifiers for this class 103651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.reflect.Modifier 103751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 103851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1039a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getModifiers() { 1040a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Array classes inherit modifiers from their component types, but in the case of arrays 1041a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // of an inner class, the class file may contain "fake" access flags because it's not valid 1042a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // for a top-level class to private, say. The real access flags are stored in the InnerClass 1043a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // attribute, so we need to make sure we drill down to the inner class: the accessFlags 1044a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // field is not the value we want to return, and the synthesized array class does not itself 1045a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // have an InnerClass attribute. https://code.google.com/p/android/issues/detail?id=56267 1046a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isArray()) { 1047a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int componentModifiers = getComponentType().getModifiers(); 1048a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if ((componentModifiers & Modifier.INTERFACE) != 0) { 1049a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski componentModifiers &= ~(Modifier.INTERFACE | Modifier.STATIC); 1050a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1051a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return Modifier.ABSTRACT | Modifier.FINAL | componentModifiers; 1052a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1053a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int JAVA_FLAGS_MASK = 0xffff; 1054d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao int modifiers = this.getInnerClassFlags(accessFlags & JAVA_FLAGS_MASK); 1055a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return modifiers & JAVA_FLAGS_MASK; 1056a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 105751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 105851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 105951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Gets the signers of this class. 106051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 106151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the signers of this class, or null if there are no signers. In 106251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular, this method returns null if this object represents 106351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a primitive type or void. 106451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 106551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1066a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Object[] getSigners() { 1067a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 1068a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 106951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 107070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native Method getEnclosingMethodNative(); 107151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 107251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 107351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents a local or anonymous 107451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class within a method, returns a {@link 107551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Method Method} object representing the 107651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * immediately enclosing method of the underlying class. Returns 107751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} otherwise. 107851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 107951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * In particular, this method returns {@code null} if the underlying 108051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class is a local or anonymous class immediately enclosed by a type 108151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declaration, instance initializer or static initializer. 108251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 108351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing method of the underlying class, if 108451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that class is a local or anonymous class; otherwise {@code null}. 108551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 108651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 108751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getEnclosingMethod() { 1088a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classNameImpliesTopLevel()) { 108951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 109051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 109170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return getEnclosingMethodNative(); 109251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 109351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 109451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 109551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents a local or anonymous 109651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class within a constructor, returns a {@link 109751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Constructor Constructor} object representing 109851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the immediately enclosing constructor of the underlying 109951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. Returns {@code null} otherwise. In particular, this 110051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns {@code null} if the underlying class is a local 110151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or anonymous class immediately enclosed by a type declaration, 110251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instance initializer or static initializer. 110351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 110451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing constructor of the underlying class, if 110551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that class is a local or anonymous class; otherwise {@code null}. 110651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 110751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 110851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?> getEnclosingConstructor() { 1109a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classNameImpliesTopLevel()) { 111051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 111151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 111270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return getEnclosingConstructorNative(); 1113a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1114a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 111570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native Constructor<?> getEnclosingConstructorNative(); 111670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 1117a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean classNameImpliesTopLevel() { 1118a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return !getName().contains("$"); 111951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 112051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 112151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 112251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the class or interface represented by this {@code Class} object 112351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a member of another class, returns the {@code Class} object 112451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing the class in which it was declared. This method returns 112551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null if this class or interface is not a member of any other class. If 112651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents an array class, a primitive 112751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type, or void,then this method returns null. 112851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 112951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the declaring class for this class 113051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 113151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 113270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public native Class<?> getDeclaringClass(); 113351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 113451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 113551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the immediately enclosing class of the underlying 113651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. If the underlying class is a top level class this 113751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns {@code null}. 113851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing class of the underlying class 113951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 114051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 114170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public native Class<?> getEnclosingClass(); 114251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 114351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 114451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the simple name of the underlying class as given in the 114551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code. Returns an empty string if the underlying class is 114651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * anonymous. 114751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 114851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The simple name of an array is the simple name of the 114951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * component type with "[]" appended. In particular the simple 115051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name of an array whose component type is anonymous is "[]". 115151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 115251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the simple name of the underlying class 115351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 115451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 115551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getSimpleName() { 115651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isArray()) 115751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getComponentType().getSimpleName()+"[]"; 115851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 115992d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath if (isAnonymousClass()) { 116092d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath return ""; 116192d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath } 116292d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath 116392d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath if (isMemberClass() || isLocalClass()) { 116492d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath // Note that we obtain this information from getInnerClassName(), which uses 116592d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath // dex system annotations to obtain the name. It is possible for this information 116692d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath // to disagree with the actual enclosing class name. For example, if dex 116792d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath // manipulation tools have renamed the enclosing class without adjusting 116892d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath // the system annotation to match. See http://b/28800927. 116992d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath return getInnerClassName(); 117092d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath } 117192d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath 117292d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath String simpleName = getName(); 117392d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath final int dot = simpleName.lastIndexOf("."); 117492d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath if (dot > 0) { 117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name 117651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 117751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 117892d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath return simpleName; 117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 118151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the canonical name of the underlying class as 118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * defined by the Java Language Specification. Returns null if 118451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the underlying class does not have a canonical name (i.e., if 118551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is a local or anonymous class or an array whose component 118651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type does not have a canonical name). 118751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the canonical name of the underlying class if it exists, and 118851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} otherwise. 118951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 119051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 119151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getCanonicalName() { 119251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isArray()) { 119351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String canonicalName = getComponentType().getCanonicalName(); 119451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (canonicalName != null) 119551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return canonicalName + "[]"; 119651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 119751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isLocalOrAnonymousClass()) 120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> enclosingClass = getEnclosingClass(); 120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingClass == null) { // top level class 120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getName(); 120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String enclosingName = enclosingClass.getCanonicalName(); 120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingName == null) 120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return enclosingName + "." + getSimpleName(); 120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is an anonymous class. 121551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 121651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is an anonymous class. 121751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 121851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 121970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public native boolean isAnonymousClass(); 122051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 122151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 122251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 122351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a local class. 122451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 122551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a local class. 122651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 122751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 122851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isLocalClass() { 1229d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao return (getEnclosingMethod() != null || getEnclosingConstructor() != null) 1230a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski && !isAnonymousClass(); 123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 123351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 123451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a member class. 123651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 123751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a member class. 123851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 123951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 124051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isMemberClass() { 124192d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath return getDeclaringClass() != null; 124251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 124351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 124451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 124551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if this is a local class or an anonymous 124651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. Returns {@code false} otherwise. 124751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 124851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private boolean isLocalOrAnonymousClass() { 124951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // JVM Spec 4.8.6: A class must have an EnclosingMethod 125051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // attribute if and only if it is a local class or an 125151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // anonymous class. 1252a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return isLocalClass() || isAnonymousClass(); 125351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 125451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 125551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 125651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Class} objects representing all 125751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the public classes and interfaces that are members of the class 125851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object. This includes public 125951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class and interface members inherited from superclasses and public class 126051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and interface members declared by the class. This method returns an 126151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if this {@code Class} object has no public member 126251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * classes or interfaces. This method also returns an array of length 0 if 126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type, an array 126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, or void. 126551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 126651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Class} objects representing the public 126751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * members of this class 126851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 126951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 127051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 127151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 127251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 127351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 127451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 127551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 127651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} method 127751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * denies access to the classes within this class 127851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 127951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 128051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 128151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 128251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 128351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 128451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 128551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 128651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 128751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 128851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 128951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 129051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Class<?>[] getClasses() { 12911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath List<Class<?>> result = new ArrayList<Class<?>>(); 12921ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath for (Class<?> c = this; c != null; c = c.superClass) { 12931ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath for (Class<?> member : c.getDeclaredClasses()) { 12941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath if (Modifier.isPublic(member.getModifiers())) { 12951ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath result.add(member); 12961ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 12971ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 12981ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 12991ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return result.toArray(new Class[result.size()]); 130051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 130151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 130251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 130351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 130451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Field} objects reflecting all 130551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the accessible public fields of the class or interface represented by 130651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object. The elements in the array returned are 130751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not sorted and are not in any particular order. This method returns an 130851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if the class or interface has no accessible public 130951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * fields, or if it represents an array class, a primitive type, or void. 131051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 131151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, if this {@code Class} object represents a class, 131251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method returns the public fields of this class and of all its 131351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superclasses. If this {@code Class} object represents an 131451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface, this method returns the fields of this interface and of all 131551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * its superinterfaces. 131651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 131751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The implicit length field for array class is not reflected by this 131851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method. User code should use the methods of class {@code Array} to 131951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manipulate arrays. 132051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 132151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 132251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 132351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Field} objects representing the 132451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public fields 132551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 132651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 132751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 132851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 132951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 133051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 133151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 133251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 133351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 133451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the fields within this class 133551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 133651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 133751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 133851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 133951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 134051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 134151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Field[] getFields() throws SecurityException { 1348a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski List<Field> fields = new ArrayList<Field>(); 13491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath getPublicFieldsRecursive(fields); 13501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return fields.toArray(new Field[fields.size()]); 13511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 13521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 13531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 13541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates {@code result} with public fields defined by this class, its 13551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * superclasses, and all implemented interfaces. 13561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 13571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private void getPublicFieldsRecursive(List<Field> result) { 1358a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search superclasses 1359a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = this; c != null; c = c.superClass) { 13601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, c.getPublicDeclaredFields()); 1361a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1362a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1363a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search iftable which has a flattened and uniqued list of interfaces 1364a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 1365a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 1366a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 13671ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, ((Class<?>) iftable[i]).getPublicDeclaredFields()); 1368a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1369a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 137051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 137151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 137251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 137351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Method} objects reflecting all 137451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the public <em>member</em> methods of the class or interface represented 137551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this {@code Class} object, including those declared by the class 137651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or interface and those inherited from superclasses and 137751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces. Array classes return all the (public) member methods 137851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * inherited from the {@code Object} class. The elements in the array 137951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned are not sorted and are not in any particular order. This 138051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns an array of length 0 if this {@code Class} object 138151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a class or interface that has no public member methods, or if 138251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type or void. 138351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 138451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The class initialization method {@code <clinit>} is not 138551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * included in the returned array. If the class declares multiple public 138651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member methods with the same parameter types, they are all included in 138751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the returned array. 138851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 138951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 139051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 139151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Method} objects representing the 139251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public methods of this class 139351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 139451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 139551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 139651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 139751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 139851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 139951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 140051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 140151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 140251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the methods within this class 140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 140451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 140551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 140651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 140751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 140851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 140951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 141051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 141151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 141251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 141351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 141451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 141551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method[] getMethods() throws SecurityException { 1416a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski List<Method> methods = new ArrayList<Method>(); 14171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath getPublicMethodsInternal(methods); 14181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /* 14191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Remove duplicate methods defined by superclasses and 14201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * interfaces, preferring to keep methods declared by derived 14211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * types. 14221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 14231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath CollectionUtils.removeDuplicates(methods, Method.ORDER_BY_SIGNATURE); 14241ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return methods.toArray(new Method[methods.size()]); 14251ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 14261ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 14271ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 14281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates {@code result} with public methods defined by this class, its 14291ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * superclasses, and all implemented interfaces, including overridden methods. 14301ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 14311ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private void getPublicMethodsInternal(List<Method> result) { 14321ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, getDeclaredMethodsUnchecked(true)); 1433a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!isInterface()) { 1434a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search superclasses, for interfaces don't search java.lang.Object. 1435a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = superClass; c != null; c = c.superClass) { 14361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, c.getDeclaredMethodsUnchecked(true)); 1437a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1438a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1439a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search iftable which has a flattened and uniqued list of interfaces. 1440a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 1441a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 1442a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 1443a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) iftable[i]; 14441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, ifc.getDeclaredMethodsUnchecked(true)); 1445a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1446a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 144751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 144851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 144951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 145051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Constructor} objects reflecting 145151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * all the public constructors of the class represented by this 145251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. An array of length 0 is returned if the 145351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class has no public constructors, or if the class is an array class, or 145451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the class reflects a primitive type or void. 145551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 145651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that while this method returns an array of {@code 145751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor<T>} objects (that is an array of constructors from 145851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this class), the return type of this method is {@code 145951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as 146051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * might be expected. This less informative return type is 146151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * necessary since after being returned from this method, the 146251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array could be modified to hold {@code Constructor} objects for 146351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * different classes, which would violate the type guarantees of 146451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Constructor<T>[]}. 146551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 146651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Constructor} objects representing the 146751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public constructors of this class 146851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 146951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 147051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 147151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 147251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 147351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 147451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 147551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 147651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 147751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the constructors within this class 147851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 148151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 148251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 148351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 148451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 148551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 148651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 148751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 148851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 148951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 149051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?>[] getConstructors() throws SecurityException { 14911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return getDeclaredConstructorsInternal(true); 149251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 149351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 149451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 149551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Field} object that reflects the specified public 149651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member field of the class or interface represented by this 149751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 149851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} specifying the simple name of the desired field. 149951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 150051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The field to be reflected is determined by the algorithm that 150151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * follows. Let C be the class represented by this object: 150251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <OL> 150351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If C declares a public field with the name specified, that is the 150451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * field to be reflected.</LI> 150551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no field was found in step 1 above, this algorithm is applied 150651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * recursively to each direct superinterface of C. The direct 150751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces are searched in the order they were declared.</LI> 150851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no field was found in steps 1 and 2 above, and C has a 150951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superclass S, then this algorithm is invoked recursively upon S. 151051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If C has no superclass, then a {@code NoSuchFieldException} 151151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is thrown.</LI> 151251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </OL> 151351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 151451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 151551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 151651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the field name 151751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Field} object of this class specified by 151851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code name} 151951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchFieldException if a field with the specified name is 152051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not found. 152151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 152251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 152351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 152451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 152551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 152651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 152751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 152851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 152951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 153051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 153151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the field 153251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 153351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 153451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 153551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 153651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 153751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 153851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 153951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 154051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 154151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 154251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 154370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public Field getField(String name) throws NoSuchFieldException { 1544fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski if (name == null) { 1545fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski throw new NullPointerException("name == null"); 1546fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski } 154770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak Field result = getPublicFieldRecursive(name); 154870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (result == null) { 154951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NoSuchFieldException(name); 155051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 155170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return result; 1552a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1553a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 155470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** 155570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * The native implementation of the {@code getField} method. 155670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 155770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * @throws NullPointerException 155870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * if name is null. 155970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * @see #getField(String) 156070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 156170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native Field getPublicFieldRecursive(String name); 156251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 156351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 156451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Method} object that reflects the specified public 156551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member method of the class or interface represented by this 156651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 156751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} specifying the simple name of the desired method. The 156851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code parameterTypes} parameter is an array of {@code Class} 156951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects that identify the method's formal parameter types, in declared 157051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * order. If {@code parameterTypes} is {@code null}, it is 157151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * treated as if it were an empty array. 157251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 157351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a 157451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code NoSuchMethodException} is raised. Otherwise, the method to 157551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be reflected is determined by the algorithm that follows. Let C be the 157651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class represented by this object: 157751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <OL> 157851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> C is searched for any <I>matching methods</I>. If no matching 157951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method is found, the algorithm of step 1 is invoked recursively on 158051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the superclass of C.</LI> 158151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no method was found in step 1 above, the superinterfaces of C 158251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are searched for a matching method. If any such method is found, it 158351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is reflected.</LI> 158451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </OL> 158551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 158651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * To find a matching method in a class C: If C declares exactly one 158751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public method with the specified name and exactly the same formal 158851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types, that is the method reflected. If more than one such 158951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method is found in C, and one of these methods has a return type that is 159051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * more specific than any of the others, that method is reflected; 159151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise one of the methods is chosen arbitrarily. 159251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 159351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that there may be more than one matching method in a 159451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class because while the Java language forbids a class to 159551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declare multiple methods with the same signature but different 159651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return types, the Java virtual machine does not. This 159751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * increased flexibility in the virtual machine can be used to 159851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implement various language features. For example, covariant 159951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns can be implemented with {@linkplain 160051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Method#isBridge bridge methods}; the bridge 160151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method and the method being overridden would have the same 160251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * signature but different return types. 160351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 160451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 160551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 160651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the method 160751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the list of parameters 160851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Method} object that matches the specified 160951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code name} and {@code parameterTypes} 161051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found 161151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or if the name is "<init>"or "<clinit>". 161251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 161351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 161451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 161551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 161651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 161751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 161851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 161951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 162051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 162151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 162251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the method 162351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 162451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 162551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 162651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 162751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 162851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 162951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 163051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 163151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 163251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 163351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 163451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 163551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getMethod(String name, Class<?>... parameterTypes) 163651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 1637a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getMethod(name, parameterTypes, true); 163851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 163951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 164051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 164151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 164251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Constructor} object that reflects the specified 164351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public constructor of the class represented by this {@code Class} 164451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The {@code parameterTypes} parameter is an array of 164551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that identify the constructor's formal 164651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types, in declared order. 164751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 164851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents an inner class 164951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared in a non-static context, the formal parameter types 165051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * include the explicit enclosing instance as the first parameter. 165151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 165251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The constructor to reflect is the public constructor of the class 165351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object whose formal parameter 165451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types match those specified by {@code parameterTypes}. 165551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 165651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 165751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Constructor} object of the public constructor that 165851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * matches the specified {@code parameterTypes} 165951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found. 166051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 166151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 166251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 166351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 166451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 166551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 166651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 166751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 166851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 166951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the constructor 167051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 167151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 167251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 167351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 167451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 167551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 167651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 167751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 167851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 167951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 168051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 168151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<T> getConstructor(Class<?>... parameterTypes) 168251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 168351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getConstructor0(parameterTypes, Member.PUBLIC); 168451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 168551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 168651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 168751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Class} objects reflecting all the 168851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * classes and interfaces declared as members of the class represented by 168951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object. This includes public, protected, default 169051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private classes and interfaces declared by the 169151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, but excludes inherited classes and interfaces. This method 169251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns an array of length 0 if the class declares no classes or 169351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces as members, or if this {@code Class} object represents a 169451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, an array class, or void. 169551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 169651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Class} objects representing all the 169751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared members of this class 169851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 169951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 170051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 170151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 170351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 170551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 170651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 170751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared classes within this class 170851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 171051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 171151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 171251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 171351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 171451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 171551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 171651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 171751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 171851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 171970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public native Class<?>[] getDeclaredClasses(); 172051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 172151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 172251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Field} objects reflecting all the fields 172351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared by the class or interface represented by this 172451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. This includes public, protected, default 172551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private fields, but excludes inherited fields. 172651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The elements in the array returned are not sorted and are not in any 172751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular order. This method returns an array of length 0 if the class 172851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or interface declares no fields, or if this {@code Class} object 172951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type, an array class, or void. 173051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 173151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 173251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 173351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Field} objects representing all the 173451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared fields of this class 173551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 173651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 173751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 173851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 173951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 174051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 174151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 174251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 174351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 174451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared fields within this class 174551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 174651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 174751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 174851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 174951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 175051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 175151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 175251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 175351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 175451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 175551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 17561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Field[] getDeclaredFields(); 175751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 17581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 17591ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates a list of fields without performing any security or type 17601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * resolution checks first. If no fields exist, the list is not modified. 17611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 17621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param publicOnly Whether to return only public fields. 17631ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param fields A list to populate with declared fields. 17641ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @hide 17651ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 17661ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Field[] getDeclaredFieldsUnchecked(boolean publicOnly); 176751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 176851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 176951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Method} objects reflecting all the 177051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * methods declared by the class or interface represented by this 177151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. This includes public, protected, default 177251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private methods, but excludes inherited methods. 177351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The elements in the array returned are not sorted and are not in any 177451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular order. This method returns an array of length 0 if the class 177551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or interface declares no methods, or if this {@code Class} object 177651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type, an array class, or void. The class 177751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * initialization method {@code <clinit>} is not included in the 177851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned array. If the class declares multiple public member methods 177951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with the same parameter types, they are all included in the returned 178051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. 178151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 178251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, section 8.2. 178351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 178451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Method} objects representing all the 178551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared methods of this class 178651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 178751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 178851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 178951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 179051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 179151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 179251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 179351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 179451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 179551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared methods within this class 179651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 179751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 179851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 179951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 180051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 180151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 180251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 180351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 180451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 180551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 180651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 180751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method[] getDeclaredMethods() throws SecurityException { 18081ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Method[] result = getDeclaredMethodsUnchecked(false); 1809a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Method m : result) { 1810a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Throw NoClassDefFoundError if types cannot be resolved. 1811a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski m.getReturnType(); 1812a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski m.getParameterTypes(); 1813a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1814a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 1815a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1816a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 18171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 18181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates a list of methods without performing any security or type 18191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * resolution checks first. If no methods exist, the list is not modified. 18201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 18211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param publicOnly Whether to return only public methods. 18221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param methods A list to populate with declared methods. 18231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @hide 18241ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 18251ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Method[] getDeclaredMethodsUnchecked(boolean publicOnly); 182651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 182751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 182851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Constructor} objects reflecting all the 182951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructors declared by the class represented by this 183051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. These are public, protected, default 183151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private constructors. The elements in the array 183251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned are not sorted and are not in any particular order. If the 183351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class has a default constructor, it is included in the returned array. 183451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method returns an array of length 0 if this {@code Class} 183551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object represents an interface, a primitive type, an array class, or 183651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * void. 183751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 183851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, section 8.2. 183951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 184051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Constructor} objects representing all the 184151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared constructors of this class 184251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 184351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 184451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 184551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 184651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 184751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 184851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 184951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 185051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 185151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared constructors within this class 185251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 185351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 185451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 185551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 185651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 185751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 185851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 185951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 186051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 186151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 186251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 186351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?>[] getDeclaredConstructors() throws SecurityException { 18641ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return getDeclaredConstructorsInternal(false); 186551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 186651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 186751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 18681ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the constructor with the given parameters if it is defined by this class; 18691ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * {@code null} otherwise. This may return a non-public member. 18701ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 18711ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param args the types of the parameters to the constructor. 18721ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 18731ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Constructor<?>[] getDeclaredConstructorsInternal(boolean publicOnly); 18741ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 18751ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 187651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Field} object that reflects the specified declared 187751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * field of the class or interface represented by this {@code Class} 187851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The {@code name} parameter is a {@code String} that 187951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specifies the simple name of the desired field. Note that this method 188051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will not reflect the {@code length} field of an array class. 188151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 188251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the field 188351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Field} object for the specified field in this 188451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class 188551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchFieldException if a field with the specified name is 188651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not found. 188751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 188851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 188951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 189051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 189151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 189251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 189351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 189451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 189551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 189651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 189751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared field 189851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 189951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 190051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 190151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 190251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 190351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 190451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 190551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 190651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 190751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 190851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 19091ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Field getDeclaredField(String name) throws NoSuchFieldException; 191051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 19111ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 19121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the subset of getDeclaredFields which are public. 19131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 19141ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Field[] getPublicDeclaredFields(); 1915a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 191651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 191751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Method} object that reflects the specified 191851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared method of the class or interface represented by this 191951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 192051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} that specifies the simple name of the desired 192151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method, and the {@code parameterTypes} parameter is an array of 192251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that identify the method's formal parameter 192351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types, in declared order. If more than one method with the same 192451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types is declared in a class, and one of these methods has a 192551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type that is more specific than any of the others, that method is 192651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned; otherwise one of the methods is chosen arbitrarily. If the 192751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name is "<init>"or "<clinit>" a {@code NoSuchMethodException} 192851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is raised. 192951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 193051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the method 193151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 193251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Method} object for the method of this class 193351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * matching the specified name and parameters 193451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found. 193551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 193651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 193751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 193851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 193951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 194051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 194151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 194251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 194351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 194451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 194551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared method 194651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 194751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 194851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 194951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 195051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 195151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 195251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 195351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 195451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 195551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 195651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 195751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 195851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getDeclaredMethod(String name, Class<?>... parameterTypes) 195951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 1960a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getMethod(name, parameterTypes, false); 1961a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1962a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1963a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Method getMethod(String name, Class<?>[] parameterTypes, boolean recursivePublicMethods) 1964a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throws NoSuchMethodException { 1965a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (name == null) { 1966a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NullPointerException("name == null"); 1967a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1968a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parameterTypes == null) { 1969a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parameterTypes = EmptyArray.CLASS; 1970a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1971a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c : parameterTypes) { 1972a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == null) { 1973a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("parameter type is null"); 1974a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1975a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1976a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = recursivePublicMethods ? getPublicMethodRecursive(name, parameterTypes) 1977a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski : getDeclaredMethodInternal(name, parameterTypes); 1978a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Fail if we didn't find the method or it was expected to be public. 1979a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null || 1980a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski (recursivePublicMethods && !Modifier.isPublic(result.getAccessFlags()))) { 1981a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException(name + " " + Arrays.toString(parameterTypes)); 1982a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1983a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 1984a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1985a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Method getPublicMethodRecursive(String name, Class<?>[] parameterTypes) { 1986a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search superclasses 1987a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = this; c != null; c = c.getSuperclass()) { 1988a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = c.getDeclaredMethodInternal(name, parameterTypes); 1989a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && Modifier.isPublic(result.getAccessFlags())) { 1990a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 1991a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1992a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1993a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search iftable which has a flattened and uniqued list of interfaces 1994a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 1995a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 1996d559f4f94cb0b9b4c80a3cc2040c0bd32276c225Alex Light // Search backwards so more specific interfaces are searched first. This ensures that 1997d559f4f94cb0b9b4c80a3cc2040c0bd32276c225Alex Light // the method we return is not overridden by one of it's subtypes that this class also 1998d559f4f94cb0b9b4c80a3cc2040c0bd32276c225Alex Light // implements. 1999d559f4f94cb0b9b4c80a3cc2040c0bd32276c225Alex Light for (int i = iftable.length - 2; i >= 0; i -= 2) { 2000a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) iftable[i]; 2001a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = ifc.getPublicMethodRecursive(name, parameterTypes); 2002a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && Modifier.isPublic(result.getAccessFlags())) { 2003a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2004a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2005a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 200651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2007a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 200851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 200951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 201051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 201151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 201251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Constructor} object that reflects the specified 201351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructor of the class or interface represented by this 201451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code parameterTypes} parameter is 201551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array of {@code Class} objects that identify the constructor's 201651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * formal parameter types, in declared order. 201751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 201851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents an inner class 201951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared in a non-static context, the formal parameter types 202051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * include the explicit enclosing instance as the first parameter. 202151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 202251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 202351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return The {@code Constructor} object for the constructor with the 202451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified parameter list 202551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found. 202651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 202751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 202851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 202951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 203051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 203151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 203251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 203351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 203451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 203551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared constructor 203651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 203751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 203851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 203951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 204051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 204151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 204251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 204351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 204451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 204551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 204651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 204751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 204851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 204951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 205051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getConstructor0(parameterTypes, Member.DECLARED); 205151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 205251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 205351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 205451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Finds a resource with a given name. The rules for searching resources 205551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * associated with a given class are implemented by the defining 205651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain ClassLoader class loader} of the class. This method 205751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * delegates to this object's class loader. If this object was loaded by 205851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the bootstrap class loader, the method delegates to {@link 205951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader#getSystemResourceAsStream}. 206051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 206151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Before delegation, an absolute resource name is constructed from the 206251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * given resource name using this algorithm: 206351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 206451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 206551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 206651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> If the {@code name} begins with a {@code '/'} 206751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 206851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * portion of the {@code name} following the {@code '/'}. 206951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 207051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> Otherwise, the absolute name is of the following form: 207151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 207251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 207351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code modified_package_name/name} 207451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 207551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 207651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Where the {@code modified_package_name} is the package name of this 207751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object with {@code '/'} substituted for {@code '.'} 207851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002e'</tt>). 207951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 208051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 208151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 208251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name name of the desired resource 208351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return A {@link java.io.InputStream} object or {@code null} if 208451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * no resource with this name is found 208551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException If {@code name} is {@code null} 208651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 208751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 208851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public InputStream getResourceAsStream(String name) { 208951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = resolveName(name); 2090a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader cl = getClassLoader(); 209151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cl==null) { 209251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // A system class. 209351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ClassLoader.getSystemResourceAsStream(name); 209451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 209551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return cl.getResourceAsStream(name); 209651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 209751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 209851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 209951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Finds a resource with a given name. The rules for searching resources 210051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * associated with a given class are implemented by the defining 210151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain ClassLoader class loader} of the class. This method 210251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * delegates to this object's class loader. If this object was loaded by 210351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the bootstrap class loader, the method delegates to {@link 210451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader#getSystemResource}. 210551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 210651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Before delegation, an absolute resource name is constructed from the 210751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * given resource name using this algorithm: 210851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 210951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 211051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 211151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> If the {@code name} begins with a {@code '/'} 211251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 211351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * portion of the {@code name} following the {@code '/'}. 211451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 211551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> Otherwise, the absolute name is of the following form: 211651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 211751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 211851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code modified_package_name/name} 211951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 212051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 212151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Where the {@code modified_package_name} is the package name of this 212251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object with {@code '/'} substituted for {@code '.'} 212351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002e'</tt>). 212451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 212551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 212651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 212751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name name of the desired resource 212851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return A {@link java.net.URL} object or {@code null} if no 212951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * resource with this name is found 213051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 213151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 213251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public java.net.URL getResource(String name) { 213351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = resolveName(name); 2134a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader cl = getClassLoader(); 213551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cl==null) { 213651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // A system class. 213751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ClassLoader.getSystemResource(name); 213851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 213951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return cl.getResource(name); 214051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 214151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 214251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 214351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code ProtectionDomain} of this class. If there is a 214451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * security manager installed, this method first calls the security 214551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager's {@code checkPermission} method with a 214651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code RuntimePermission("getProtectionDomain")} permission to 214751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ensure it's ok to get the 214851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code ProtectionDomain}. 214951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 215051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the ProtectionDomain of this class 215151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 215251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 215351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if a security manager exists and its 215451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code checkPermission} method doesn't allow 215551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * getting the ProtectionDomain. 215651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 215751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.security.ProtectionDomain 215851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 215951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.RuntimePermission 216051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 216151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 216251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public java.security.ProtectionDomain getProtectionDomain() { 2163a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 216451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 216551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 216651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 216751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Add a package name prefix if the name is not absolute Remove leading "/" 216851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if name is absolute 216951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 217051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String resolveName(String name) { 217151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (name == null) { 217251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 217351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 217451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (!name.startsWith("/")) { 217551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> c = this; 217651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (c.isArray()) { 217751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski c = c.getComponentType(); 217851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 217951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String baseName = c.getName(); 218051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int index = baseName.lastIndexOf('.'); 218151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (index != -1) { 218251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = baseName.substring(0, index).replace('.', '/') 218351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski +"/"+name; 218451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 218551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 218651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = name.substring(1); 218751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 218851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 218951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 219051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2191a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Constructor<T> getConstructor0(Class<?>[] parameterTypes, 2192a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int which) throws NoSuchMethodException 2193a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski { 2194a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parameterTypes == null) { 2195a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parameterTypes = EmptyArray.CLASS; 219651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2197a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c : parameterTypes) { 2198a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == null) { 2199a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("parameter type is null"); 2200a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 220151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2202a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Constructor<T> result = getDeclaredConstructorInternal(parameterTypes); 2203a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null || which == Member.PUBLIC && !Modifier.isPublic(result.getAccessFlags())) { 2204a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("<init> " + Arrays.toString(parameterTypes)); 2205a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2206a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 220751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 220851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 22091ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 22101ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the constructor with the given parameters if it is defined by this class; 22111ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * {@code null} otherwise. This may return a non-public member. 22121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 22131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param args the types of the parameters to the constructor. 22141ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 22151ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Constructor<T> getDeclaredConstructorInternal(Class<?>[] args); 221651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 221751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 221851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the assertion status that would be assigned to this 221951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class if it were to be initialized at the time this method is invoked. 222051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this class has had its assertion status set, the most recent 222151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * setting will be returned; otherwise, if any package default assertion 222251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * status pertains to this class, the most recent setting for the most 222351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specific pertinent package default assertion status is returned; 222451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, if this class is not a system class (i.e., it has a 222551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader) its class loader's default assertion status is returned; 222651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, the system class default assertion status is returned. 222751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 222851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Few programmers will have any need for this method; it is provided 222951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for the benefit of the JRE itself. (It allows a class to determine at 223051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the time that it is initialized whether assertions should be enabled.) 223151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method is not guaranteed to return the actual 223251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * assertion status that was (or will be) associated with the specified 223351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class when it was (or will be) initialized. 223451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 223551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the desired assertion status of the specified class. 223651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setClassAssertionStatus 223751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setPackageAssertionStatus 223851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setDefaultAssertionStatus 223951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 224051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 224151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean desiredAssertionStatus() { 2242a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 224351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 224451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 224551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 224670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * Returns the simple name of a member or local class, or {@code null} otherwise. 224770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 224870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native String getInnerClassName(); 224970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 225070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native int getInnerClassFlags(int defaultValue); 225170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 225270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** 225351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns true if and only if this class was declared as an enum in the 225451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code. 225551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 225651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if and only if this class was declared as an enum in the 225751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code 225851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 225951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 226051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isEnum() { 226151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // An enum must both directly extend java.lang.Enum and have 226251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // the ENUM bit set; classes for specialized enum constants 226351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // don't do the former. 226451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (this.getModifiers() & ENUM) != 0 && 226551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.getSuperclass() == java.lang.Enum.class; 226651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 226751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 226851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 226951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the elements of this enum class or null if this 227051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class object does not represent an enum type. 227151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 227251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array containing the values comprising the enum class 227351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this Class object in the order they're 227451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared, or null if this Class object does not 227551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represent an enum type 227651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 227751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 227851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public T[] getEnumConstants() { 227951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] values = getEnumConstantsShared(); 228051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (values != null) ? values.clone() : null; 228151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 228251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 228351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 228451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the elements of this enum class or null if this 228551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class object does not represent an enum type; 228651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * identical to getEnumConstants except that the result is 228751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * uncloned, cached, and shared by all callers. 228851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 228951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] getEnumConstantsShared() { 2290a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!isEnum()) return null; 2291a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (T[]) Enum.getSharedConstants((Class) this); 229251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 229351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 229451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 229551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Casts an object to the class or interface represented 229651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this {@code Class} object. 229751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 229851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param obj the object to be cast 229951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the object after casting, or null if obj is null 230051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 230151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the object is not 230251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null and is not assignable to the type T. 230351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 230451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 230551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 230651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public T cast(Object obj) { 230751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (obj != null && !isInstance(obj)) 230851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new ClassCastException(cannotCastMsg(obj)); 230951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (T) obj; 231051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 231151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 231251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String cannotCastMsg(Object obj) { 231351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "Cannot cast " + obj.getClass().getName() + " to " + getName(); 231451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 231551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 231651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 231751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Casts this {@code Class} object to represent a subclass of the class 231851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the specified class object. Checks that that the cast 231951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is valid, and throws a {@code ClassCastException} if it is not. If 232051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method succeeds, it always returns a reference to this class object. 232151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 232251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This method is useful when a client needs to "narrow" the type of 232351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a {@code Class} object to pass it to an API that restricts the 232451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that it is willing to accept. A cast would 232551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * generate a compile-time warning, as the correctness of the cast 232651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * could not be checked at runtime (because generic types are implemented 232751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by erasure). 232851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 232951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return this {@code Class} object, cast to represent a subclass of 233051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified class object. 233151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if this {@code Class} object does not 233251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represent a subclass of the specified class (here "subclass" includes 233351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class itself). 233451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 233551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 233651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public <U> Class<? extends U> asSubclass(Class<U> clazz) { 233751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (clazz.isAssignableFrom(this)) 233851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (Class<? extends U>) this; 233951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 234087778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak throw new ClassCastException(this.toString() + 234187778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak " cannot be cast to " + clazz.getName()); 234251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 234351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 234451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 234551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException {@inheritDoc} 234651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 234751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 234851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public <A extends Annotation> A getAnnotation(Class<A> annotationClass) { 234951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (annotationClass == null) 235051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NullPointerException(); 235151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 235270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak A annotation = getDeclaredAnnotation(annotationClass); 235370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotation != null) { 235470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return annotation; 235570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 235670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 235770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) { 235870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) { 235970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak annotation = sup.getDeclaredAnnotation(annotationClass); 236070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotation != null) { 236170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return annotation; 236270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 236370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 236470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 236570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 236670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return null; 236770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 236870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 236970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak @Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { 237070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotationType == null) { 237170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak throw new NullPointerException("annotationType == null"); 237270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 237370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 237470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (isDeclaredAnnotationPresent(annotationType)) { 237570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return true; 237670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 237770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 237870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotationType.isDeclaredAnnotationPresent(Inherited.class)) { 237970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) { 238070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (sup.isDeclaredAnnotationPresent(annotationType)) { 238170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return true; 238270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 238370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 238470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 238570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 238670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return false; 238751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 238851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 238951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 239070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * Returns an array containing all the annotations of this class. If there are no annotations 239170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * then an empty array is returned. 239270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 239370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * @see #getDeclaredAnnotations() 239451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 239570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak @Override public Annotation[] getAnnotations() { 239670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /* 239770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * We need to get the annotations declared on this class, plus the 239870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * annotations from superclasses that have the "@Inherited" annotation 239970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * set. We create a temporary map to use while we accumulate the 240070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * annotations and convert it to an array at the end. 240170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 240270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * It's possible to have duplicates when annotations are inherited. 240370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * We use a Map to filter those out. 240470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 240570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * HashMap might be overkill here. 240670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 240770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak HashMap<Class<?>, Annotation> map = new HashMap<Class<?>, Annotation>(); 240870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Annotation declaredAnnotation : getDeclaredAnnotations()) { 240970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak map.put(declaredAnnotation.annotationType(), declaredAnnotation); 241070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 241170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) { 241270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Annotation declaredAnnotation : sup.getDeclaredAnnotations()) { 241370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak Class<? extends Annotation> clazz = declaredAnnotation.annotationType(); 241470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (!map.containsKey(clazz) && clazz.isDeclaredAnnotationPresent(Inherited.class)) { 241570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak map.put(clazz, declaredAnnotation); 241670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 241770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 241870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 241951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 242070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /* Convert annotation values from HashMap to array. */ 242170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak Collection<Annotation> coll = map.values(); 242270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return coll.toArray(new Annotation[coll.size()]); 242351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 242451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 242570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** 242670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * Returns the annotations that are directly defined on the class 242770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * represented by this {@code Class}. Annotations that are inherited are not 242870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * included in the result. If there are no annotations at all, an empty 242970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * array is returned. 243070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 243170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * @see #getAnnotations() 243270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 243370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak @Override public native Annotation[] getDeclaredAnnotations(); 243451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 243551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 24369599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin * {@inheritDoc} 24379599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin * @since 1.8 243851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 24399599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin @Override 24409599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) { 24419599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin // Find any associated annotations [directly or repeatably (indirectly) present on this]. 2442be1608980825868eb1c70fab6eb86fe06bd304ceYi Kong T[] annotations = AnnotatedElement.super.getAnnotationsByType(annotationClass); 24439599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin 24449599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin if (annotations.length != 0) { 24459599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin return annotations; 24469599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin } 24479599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin 24489599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin // Nothing was found, attempt looking for associated annotations recursively up to the root 24499599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin // class if and only if: 24509599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin // * The annotation class was marked with @Inherited. 24519599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin // 24529599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin // Inherited annotations are not coalesced into a single set: the first declaration found is 24539599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin // returned. 24549599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin 24559599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) { 24569599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin Class<?> superClass = getSuperclass(); // Returns null if klass's base is Object. 24579599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin 24589599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin if (superClass != null) { 24599599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin return superClass.getAnnotationsByType(annotationClass); 24609599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin } 24619599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin } 24629599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin 24639599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin // Annotated was not marked with @Inherited, or no superclass. 24649599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin return (T[]) Array.newInstance(annotationClass, 0); // Safe by construction. 24659599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin } 24669599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin 24679599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin /** 24689599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin * {@inheritDoc} 24699599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin * 24709599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin * @since 1.8 24719599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin */ 24729599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin @Override 24739599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin public native <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass); 247451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 247551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 247670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * Returns true if the annotation exists. 247751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 247870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native boolean isDeclaredAnnotationPresent(Class<? extends Annotation> annotationClass); 247951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 248051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Annotation types cache their internal (AnnotationType) form 248151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2482a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** @hide */ 2483a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public void setAnnotationType(AnnotationType type) { 248451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski annotationType = type; 248551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 248651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2487a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** @hide */ 2488a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public AnnotationType getAnnotationType() { 248951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return annotationType; 249051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 249151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2492d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao private String getSignatureAttribute() { 2493d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao String[] annotation = getSignatureAnnotation(); 2494d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao if (annotation == null) { 2495d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao return null; 2496d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao } 2497d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao StringBuilder result = new StringBuilder(); 2498d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao for (String s : annotation) { 2499d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao result.append(s); 2500d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao } 2501d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao return result.toString(); 2502d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao } 2503d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao 2504d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao private native String[] getSignatureAnnotation(); 2505d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao 2506a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2507a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Is this a runtime created proxy class? 2508a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2509a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2510a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2511a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isProxy() { 2512a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (accessFlags & 0x00040000) != 0; 2513a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2514a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2515a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2516a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns the dex file from which this class was loaded. 2517a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2518a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2519a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2520a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Dex getDex() { 2521a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dexCache == null) { 2522a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 2523a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2524a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return dexCache.getDex(); 2525a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2526a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2527a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns a string from the dex cache, computing the string from the dex file if necessary. 2528a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2529a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2530a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2531a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public String getDexCacheString(Dex dex, int dexStringIndex) { 253270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak String s = dexCache.getResolvedString(dexStringIndex); 2533a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (s == null) { 2534a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski s = dex.strings().get(dexStringIndex).intern(); 253570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak dexCache.setResolvedString(dexStringIndex, s); 2536a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2537a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return s; 2538a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2539a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2540a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2541a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns a resolved type from the dex cache, computing the type from the dex file if 2542a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * necessary. 2543a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2544a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2545a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2546a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?> getDexCacheType(Dex dex, int dexTypeIndex) { 254770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak Class<?> resolvedType = dexCache.getResolvedType(dexTypeIndex); 2548a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (resolvedType == null) { 2549a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int descriptorIndex = dex.typeIds().get(dexTypeIndex); 2550a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String descriptor = getDexCacheString(dex, descriptorIndex); 2551a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski resolvedType = InternalNames.getClass(getClassLoader(), descriptor); 255270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak dexCache.setResolvedType(dexTypeIndex, resolvedType); 2553a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2554a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return resolvedType; 2555a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2556a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2557a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The annotation directory offset of this class in its own Dex, or 0 if it 2558a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * is unknown. 2559a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2560a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * TODO: 0 is a sentinel that means 'no annotations directory'; this should be -1 if unknown 2561a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2562a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2563a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2564a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getDexAnnotationDirectoryOffset() { 2565a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Dex dex = getDex(); 2566a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dex == null) { 2567a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return 0; 2568a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2569a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int classDefIndex = getDexClassDefIndex(); 2570a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classDefIndex < 0) { 2571a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return 0; 2572a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2573a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return dex.annotationDirectoryOffsetFromClassDefIndex(classDefIndex); 2574a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2575a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2576a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The type index of this class in its own Dex, or -1 if it is unknown. If a class is referenced 2577a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * by multiple Dex files, it will have a different type index in each. Dex files support 65534 2578a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * type indices, with 65535 representing no index. 2579a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2580a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2581a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2582a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getDexTypeIndex() { 2583a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int typeIndex = dexTypeIndex; 2584a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (typeIndex != 65535) { 2585a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return typeIndex; 2586a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2587a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized (this) { 2588a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = dexTypeIndex; 2589a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (typeIndex == 65535) { 2590a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dexClassDefIndex >= 0) { 2591a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = getDex().typeIndexFromClassDefIndex(dexClassDefIndex); 2592a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 2593a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = getDex().findTypeIndex(InternalNames.getInternalName(this)); 2594a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (typeIndex < 0) { 2595a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = -1; 2596a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2597a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2598a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski dexTypeIndex = typeIndex; 2599a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2600a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2601a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return typeIndex; 2602a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2603a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean canAccess(Class<?> c) { 2604a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if(Modifier.isPublic(c.accessFlags)) { 2605a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 2606a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2607a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return inSamePackage(c); 2608a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2609a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2610a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean canAccessMember(Class<?> memberClass, int memberModifiers) { 2611a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (memberClass == this || Modifier.isPublic(memberModifiers)) { 2612a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 2613a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2614a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Modifier.isPrivate(memberModifiers)) { 2615a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 2616a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2617a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Modifier.isProtected(memberModifiers)) { 2618a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> parent = this.superClass; parent != null; parent = parent.superClass) { 2619a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parent == memberClass) { 2620a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 2621a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2622a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2623a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2624a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return inSamePackage(memberClass); 2625a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2626a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2627a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean inSamePackage(Class<?> c) { 2628a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classLoader != c.classLoader) { 2629a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 2630a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2631a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName1 = getPackageName$(); 2632a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName2 = c.getPackageName$(); 2633a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (packageName1 == null) { 2634a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName2 == null; 2635a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (packageName2 == null) { 2636a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 2637a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 2638a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName1.equals(packageName2); 2639a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2640a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2641a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2642a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2643a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2644a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getAccessFlags() { 2645a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return accessFlags; 2646a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2647a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 26481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 26491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 26501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the method if it is defined by this class; {@code null} otherwise. This may return a 26511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * non-public member. 26521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 26531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param name the method name 26541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param args the method's parameter types 26551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 26561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Method getDeclaredMethodInternal(String name, Class<?>[] args); 2657a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2658a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2659a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The class def of this class in its own Dex, or -1 if there is no class def. 2660a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2661a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2662a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2663a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getDexClassDefIndex() { 2664a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (dexClassDefIndex == 65535) ? -1 : dexClassDefIndex; 2665a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2666a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static class Caches { 2667a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2668a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Cache to avoid frequent recalculation of generic interfaces, which is generally uncommon. 2669a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Sized sufficient to allow ConcurrentHashMapTest to run without recalculating its generic 2670a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * interfaces (required to avoid time outs). Validated by running reflection heavy code 2671a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * such as applications using Guice-like frameworks. 2672a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2673a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static final BasicLruCache<Class, Type[]> genericInterfaces 2674a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski = new BasicLruCache<Class, Type[]>(8); 2675a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 267651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 2677