151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 22c87ad3a45cecf9e344487cad1abfdebe79f2c7cNarayan Kamath * Copyright (C) 2014 The Android Open Source Project 37accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * Copyright (c) 1994, 2014, 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 29fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkinimport dalvik.annotation.optimization.FastNative; 3020938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller 3120938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport java.io.InputStream; 3220938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport java.io.Serializable; 3320938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport java.lang.annotation.Annotation; 3420938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport java.lang.annotation.Inherited; 35be1608980825868eb1c70fab6eb86fe06bd304ceYi Kongimport java.lang.reflect.AnnotatedElement; 3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Array; 3720938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport java.lang.reflect.Constructor; 3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Field; 397accc5fb09911a1dcc027f48089845f617f2912eNeil Fullerimport java.lang.reflect.GenericDeclaration; 4020938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport java.lang.reflect.Member; 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Method; 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Modifier; 4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Type; 4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.TypeVariable; 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.ArrayList; 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Arrays; 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Collection; 4820938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport java.util.Collections; 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.HashMap; 5020938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport java.util.List; 517accc5fb09911a1dcc027f48089845f617f2912eNeil Fullerimport java.util.Objects; 52a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.GenericSignatureParser; 5320938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport libcore.reflect.InternalNames; 54a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.Types; 55a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.BasicLruCache; 56a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.CollectionUtils; 57a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.EmptyArray; 5820938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller 5902e8f0793940b5fd42b038dd912006c6d98a843cAlex Lightimport dalvik.system.ClassExt; 6020938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport dalvik.system.VMStack; 6120938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport sun.reflect.CallerSensitive; 62a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Instances of the class {@code Class} represent classes and 6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces in a running Java application. An enum is a kind of 6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class and an annotation is a kind of interface. Every array also 6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * belongs to a class that is reflected as a {@code Class} object 6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that is shared by all arrays with the same element type and number 6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of dimensions. The primitive Java types ({@code boolean}, 7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code byte}, {@code char}, {@code short}, 7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int}, {@code long}, {@code float}, and 7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code double}), and the keyword {@code void} are also 7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented as {@code Class} objects. 7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> {@code Class} has no public constructor. Instead {@code Class} 7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects are constructed automatically by the Java Virtual Machine as classes 7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are loaded and by calls to the {@code defineClass} method in the class 7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader. 7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The following example uses a {@code Class} object to print the 8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class name of an object: 8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <blockquote><pre> 8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * void printClassName(Object obj) { 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * System.out.println("The class of " + obj + 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * " is " + obj.getClass().getName()); 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * } 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> It is also possible to get the {@code Class} object for a named 9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type (or for void) using a class literal. See Section 15.8.2 of 9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>. 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For example: 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 957accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <blockquote> 9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());} 9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param <T> the type of the class modeled by this {@code Class} 10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. For example, the type of {@code String.class} is {@code 10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class<String>}. Use {@code Class<?>} if the class being modeled is 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * unknown. 10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author unascribed 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#defineClass(byte[], int, int) 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1087accc5fb09911a1dcc027f48089845f617f2912eNeil Fullerpublic final class Class<T> implements java.io.Serializable, 1097accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller GenericDeclaration, 1107accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller Type, 1117accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller AnnotatedElement { 1127accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller private static final int ANNOTATION= 0x00002000; 1137accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller private static final int ENUM = 0x00004000; 1147accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller private static final int SYNTHETIC = 0x00001000; 1157accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller private static final int FINALIZABLE = 0x80000000; 1161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** defining class loader, or null for the "bootstrap" system loader. */ 1181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient ClassLoader classLoader; 1191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * For array classes, the component class object for instanceof/checkcast (for String[][][], 1221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * this will be String[][]). null for non-array classes. 1231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1241ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Class<?> componentType; 12502e8f0793940b5fd42b038dd912006c6d98a843cAlex Light 1261ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1271ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * DexCache of resolved constant pool entries. Will be null for certain runtime-generated classes 1281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * e.g. arrays and primitive classes. 1291ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 130e77a467884a8b323a1ac6a229f06f9a032b141b5Narayan Kamath private transient Object dexCache; 1311ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1321ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 13302e8f0793940b5fd42b038dd912006c6d98a843cAlex Light * Extra data that only some classes possess. This is allocated lazily as needed. 13402e8f0793940b5fd42b038dd912006c6d98a843cAlex Light */ 13502e8f0793940b5fd42b038dd912006c6d98a843cAlex Light private transient ClassExt extData; 13602e8f0793940b5fd42b038dd912006c6d98a843cAlex Light 13702e8f0793940b5fd42b038dd912006c6d98a843cAlex Light /** 1381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * The interface table (iftable_) contains pairs of a interface class and an array of the 1391ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * interface methods. There is one pair per interface supported by this class. That 1401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * means one pair for each interface we support directly, indirectly via superclass, or 1411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * indirectly via a superinterface. This will be null if neither we nor our superclass 1421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * implement any interfaces. 1431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 1441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()". 1451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Invoke faceObj.blah(), where "blah" is part of the Face interface. We can't easily use a 1461ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * single vtable. 1471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 1481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * For every interface a concrete class implements, we create an array of the concrete vtable_ 1491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * methods for the methods in the interface. 1501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Object[] ifTable; 1521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Lazily computed name of this class; always prefer calling getName(). */ 1541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient String name; 1551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** The superclass, or null if this is java.lang.Object, an interface or primitive type. */ 1571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Class<? super T> superClass; 1581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 15970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** 1601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass 1611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * is copied in, and virtual methods from our class either replace those from the super or are 1621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * appended. For abstract classes, methods may be created in the vtable that aren't in 1631ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * virtual_ methods_ for miranda methods. 1641ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1651ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Object vtable; 1661ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1671ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1681ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Instance fields. These describe the layout of the contents of an Object. Note that only the 1691ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * fields directly declared by this class are listed in iFields; fields declared by a 1701ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * superclass are listed in the superclass's Class.iFields. 1711ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 1721ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * All instance fields that refer to objects are guaranteed to be at the beginning of the field 1731ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * list. {@link Class#numReferenceInstanceFields} specifies the number of reference fields. 1741ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1751ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient long iFields; 1761ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 177551112f15a648b739f842b2466b5fdae3e80d6deAlex Light /** All methods with this class as the base for virtual dispatch. */ 178551112f15a648b739f842b2466b5fdae3e80d6deAlex Light private transient long methods; 179551112f15a648b739f842b2466b5fdae3e80d6deAlex Light 1801ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Static fields */ 1811ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient long sFields; 1821ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 18302e8f0793940b5fd42b038dd912006c6d98a843cAlex Light /** access flags; low 16 bits are defined by VM spec */ 18402e8f0793940b5fd42b038dd912006c6d98a843cAlex Light private transient int accessFlags; 18502e8f0793940b5fd42b038dd912006c6d98a843cAlex Light 18670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** Class flags to help the GC with object scanning. */ 18770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private transient int classFlags; 18870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 1891ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1901ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Total size of the Class instance; used when allocating storage on GC heap. 1911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * See also {@link Class#objectSize}. 1921ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1931ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int classSize; 1941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1951ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1961ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * tid used to check for recursive static initializer invocation. 1971ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1981ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int clinitThreadId; 1991ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2001ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2011ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Class def index from dex file. An index of 65535 indicates that there is no class definition, 2021ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * for example for an array type. 2031ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * TODO: really 16bits as type indices are 16bit. 2041ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2051ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int dexClassDefIndex; 2061ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2071ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2081ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Class type index from dex file, lazily computed. An index of 65535 indicates that the type 2091ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * index isn't known. Volatile to avoid double-checked locking bugs. 2101ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * TODO: really 16bits as type indices are 16bit. 2111ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient volatile int dexTypeIndex; 2131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2141ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Number of instance fields that are object references. */ 2151ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int numReferenceInstanceFields; 2161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Number of static fields that are object references. */ 2181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int numReferenceStaticFields; 2191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Total object size; used when allocating storage on GC heap. For interfaces and abstract 2221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * classes this will be zero. See also {@link Class#classSize}. 2231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2241ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int objectSize; 2251ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2261ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 227265a876206ebe30470777d444d7fc5087087913aMathieu Chartier * Aligned object size for allocation fast path. The value is max int if the object is 228265a876206ebe30470777d444d7fc5087087913aMathieu Chartier * uninitialized or finalizable, otherwise the aligned object size. 229265a876206ebe30470777d444d7fc5087087913aMathieu Chartier */ 230265a876206ebe30470777d444d7fc5087087913aMathieu Chartier private transient int objectSizeAllocFastPath; 231265a876206ebe30470777d444d7fc5087087913aMathieu Chartier 232265a876206ebe30470777d444d7fc5087087913aMathieu Chartier /** 2331ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * The lower 16 bits is the primitive type value, or 0 if not a primitive type; set for 2341ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * generated primitive classes. 2351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int primitiveType; 2371ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Bitmap of offsets of iFields. */ 2391ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int referenceInstanceOffsets; 2401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** State of class initialization */ 2421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int status; 2431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 244551112f15a648b739f842b2466b5fdae3e80d6deAlex Light /** Offset of the first virtual method copied from an interface in the methods array. */ 245551112f15a648b739f842b2466b5fdae3e80d6deAlex Light private transient short copiedMethodsOffset; 246551112f15a648b739f842b2466b5fdae3e80d6deAlex Light 247551112f15a648b739f842b2466b5fdae3e80d6deAlex Light /** Offset of the first virtual method defined in this class in the methods array. */ 248551112f15a648b739f842b2466b5fdae3e80d6deAlex Light private transient short virtualMethodsOffset; 249551112f15a648b739f842b2466b5fdae3e80d6deAlex Light 25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 2517accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * Private constructor. Only the Java Virtual Machine creates Class objects. 2527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * This constructor is not used and prevents the default constructor being 2537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * generated. 25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private Class() {} 25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Converts the object to a string. The string representation is the 26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * string "class" or "interface", followed by a space, and then by the 26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * fully qualified name of the class in the format returned by 26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code getName}. If this {@code Class} object represents a 26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, this method returns the name of the primitive type. If 26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents void this method returns 26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "void". 26651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of this class object. 26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String toString() { 27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) 27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski + getName(); 27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2747accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller /** 2757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * Returns a string describing this {@code Class}, including 2767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * information about modifiers and type parameters. 2777accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 2787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * The string is formatted as a list of type modifiers, if any, 2797accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * followed by the kind of type (empty string for primitive types 2807accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * and {@code class}, {@code enum}, {@code interface}, or 2817accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <code>@</code>{@code interface}, as appropriate), followed 2827accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * by the type's name, followed by an angle-bracketed 2837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * comma-separated list of the type's type parameters, if any. 2847accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 2857accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * A space is used to separate modifiers from one another and to 2867accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * separate any modifiers from the kind of type. The modifiers 2877accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * occur in canonical order. If there are no type parameters, the 2887accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * type parameter list is elided. 2897accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 2907accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p>Note that since information about the runtime representation 2917accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of a type is being generated, modifiers not present on the 2927accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * originating source code or illegal on the originating source 2937accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * code may be present. 2947accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 2957accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @return a string describing this {@code Class}, including 2967accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * information about modifiers and type parameters 2977accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 2987accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @since 1.8 2997accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller */ 3007accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller public String toGenericString() { 3017accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (isPrimitive()) { 3027accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller return toString(); 3037accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } else { 3047accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller StringBuilder sb = new StringBuilder(); 3057accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 3067accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller // Class modifiers are a superset of interface modifiers 3077accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller int modifiers = getModifiers() & Modifier.classModifiers(); 3087accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (modifiers != 0) { 3097accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller sb.append(Modifier.toString(modifiers)); 3107accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller sb.append(' '); 3117accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } 3127accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 3137accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (isAnnotation()) { 3147accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller sb.append('@'); 3157accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } 3167accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (isInterface()) { // Note: all annotation types are interfaces 3177accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller sb.append("interface"); 3187accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } else { 3197accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (isEnum()) 3207accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller sb.append("enum"); 3217accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller else 3227accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller sb.append("class"); 3237accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } 3247accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller sb.append(' '); 3257accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller sb.append(getName()); 3267accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 3277accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller TypeVariable<?>[] typeparms = getTypeParameters(); 3287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (typeparms.length > 0) { 3297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller boolean first = true; 3307accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller sb.append('<'); 3317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller for(TypeVariable<?> typeparm: typeparms) { 3327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (!first) 3337accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller sb.append(','); 3347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller sb.append(typeparm.getTypeName()); 3357accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller first = false; 3367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } 3377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller sb.append('>'); 3387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } 3397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 3407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller return sb.toString(); 3417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } 3427accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } 34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} object associated with the class or 34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface with the given string name. Invoking this method is 34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent to: 34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName(className, true, currentLoader)} 35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * where {@code currentLoader} denotes the defining class loader of 35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the current class. 35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> For example, the following code fragment returns the 35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * runtime {@code Class} descriptor for the class named 35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code java.lang.Thread}: 35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class t = Class.forName("java.lang.Thread")} 36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A call to {@code forName("X")} causes the class named 36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code X} to be initialized. 36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param className the fully qualified name of the desired class. 36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Class} object for the class with the 36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified name. 37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception LinkageError if the linkage fails 37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization provoked 37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this method fails 37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ClassNotFoundException if the class cannot be located 37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Class<?> forName(String className) 37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws ClassNotFoundException { 378a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return forName(className, true, VMStack.getCallingClassLoader()); 37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} object associated with the class or 38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface with the given string name, using the given class loader. 38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Given the fully qualified name for a class or interface (in the same 38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * format returned by {@code getName}) this method attempts to 38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * locate, load, and link the class or interface. The specified class 38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader is used to load the class or interface. If the parameter 38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code loader} is null, the class is loaded through the bootstrap 39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader. The class is initialized only if the 39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code initialize} parameter is {@code true} and if it has 39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not been initialized earlier. 39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If {@code name} denotes a primitive type or void, an attempt 39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will be made to locate a user-defined class in the unnamed package whose 39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name is {@code name}. Therefore, this method cannot be used to 39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtain any of the {@code Class} objects representing primitive 39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types or void. 39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If {@code name} denotes an array class, the component type of 40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array class is loaded but not initialized. 40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> For example, in an instance method the expression: 40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName("Foo")} 40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is equivalent to: 41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName("Foo", true, this.getClass().getClassLoader())} 41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method throws errors related to loading, linking or 41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The 41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Java Language Specification</em>. 41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method does not check whether the requested class 41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is accessible to its caller. 42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the {@code loader} is {@code null}, and a security 42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager is present, and the caller's class loader is not null, then this 42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method calls the security manager's {@code checkPermission} method 42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with a {@code RuntimePermission("getClassLoader")} permission to 42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ensure it's ok to access the bootstrap class loader. 42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name fully qualified name of the desired class 4287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @param initialize if {@code true} the class will be initialized. 4297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * See Section 12.4 of <em>The Java Language Specification</em>. 43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param loader class loader from which the class must be loaded 43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return class object representing the desired class 43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception LinkageError if the linkage fails 43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization provoked 43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this method fails 43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ClassNotFoundException if the class cannot be located by 43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified class loader 43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Class#forName(String) 44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader 44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Class<?> forName(String name, boolean initialize, 44551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ClassLoader loader) 44651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws ClassNotFoundException 44751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (loader == null) { 449bac8ef10e8a32316e4eedc6163dd3776cf5e5d7eNarayan Kamath loader = BootClassLoader.getInstance(); 450a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 451a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> result; 452a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski try { 453a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = classForName(name, initialize, loader); 454a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } catch (ClassNotFoundException e) { 455a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Throwable cause = e.getCause(); 456a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (cause instanceof LinkageError) { 457a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw (LinkageError) cause; 45851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 459a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw e; 46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 461a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 46251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** Called after security checks have been made. */ 465fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 466a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski static native Class<?> classForName(String className, boolean shouldInitialize, 467a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader classLoader) throws ClassNotFoundException; 46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Creates a new instance of the class represented by this {@code Class} 47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The class is instantiated as if by a {@code new} 47251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * expression with an empty argument list. The class is initialized if it 47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has not already been initialized. 47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that this method propagates any exception thrown by the 47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * nullary constructor, including a checked exception. Use of 47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method effectively bypasses the compile-time exception 47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * checking that would otherwise be performed by the compiler. 47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The {@link 48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Constructor#newInstance(java.lang.Object...) 48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor.newInstance} method avoids this problem by wrapping 48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * any exception thrown by the constructor in a (checked) {@link 48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.InvocationTargetException}. 48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 4857accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @return a newly allocated instance of the class represented by this 4867accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * object. 4877accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws IllegalAccessException if the class or its nullary 4887accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * constructor is not accessible. 4897accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws InstantiationException 4907accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * if this {@code Class} represents an abstract class, 4917accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * an interface, an array class, a primitive type, or void; 4927accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * or if the class has no nullary constructor; 4937accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * or if the instantiation fails for some other reason. 4947accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws ExceptionInInitializerError if the initialization 4957accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * provoked by this method fails. 4967accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 4977accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and 4987accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * the caller's class loader is not the same as or an 4997accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 5007accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 5017accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 5027accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class. 50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 504fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 5051ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native T newInstance() throws InstantiationException, IllegalAccessException; 50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Object} is assignment-compatible 50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with the object represented by this {@code Class}. This method is 51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the dynamic equivalent of the Java language {@code instanceof} 51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * operator. The method returns {@code true} if the specified 51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} argument is non-null and can be cast to the 51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * reference type represented by this {@code Class} object without 51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * raising a {@code ClassCastException.} It returns {@code false} 51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise. 51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, if this {@code Class} object represents a 51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared class, this method returns {@code true} if the specified 51951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} argument is an instance of the represented class (or 52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of any of its subclasses); it returns {@code false} otherwise. If 52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents an array class, this method 52251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code true} if the specified {@code Object} argument 52351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * can be converted to an object of the array class by an identity 52451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * conversion or by a widening reference conversion; it returns 52551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. If this {@code Class} object 52651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents an interface, this method returns {@code true} if the 52751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class or any superclass of the specified {@code Object} argument 52851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implements this interface; it returns {@code false} otherwise. If 52951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type, this method 53051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code false}. 53151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 5327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @param obj the object to check 53351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if {@code obj} is an instance of this class 53451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 53551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 53651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 5377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller public boolean isInstance(Object obj) { 5387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (obj == null) { 539a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 540a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 5417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller return isAssignableFrom(obj.getClass()); 542a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 54351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 54451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 54551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 54651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the class or interface represented by this 54751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object is either the same as, or is a superclass or 54851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterface of, the class or interface represented by the specified 54951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} parameter. It returns {@code true} if so; 55051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise it returns {@code false}. If this {@code Class} 55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object represents a primitive type, this method returns 55251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code true} if the specified {@code Class} parameter is 55351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exactly this {@code Class} object; otherwise it returns 55451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. 55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, this method tests whether the type represented by the 55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified {@code Class} parameter can be converted to the type 55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object via an identity conversion 55951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or via a widening reference conversion. See <em>The Java Language 56051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Specification</em>, sections 5.1.1 and 5.1.4 , for details. 56151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 5627accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @param cls the {@code Class} object to be checked 56351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code boolean} value indicating whether objects of the 56451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type {@code cls} can be assigned to objects of this class 56551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if the specified Class parameter is 56651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null. 56751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 56851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 5697accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller public boolean isAssignableFrom(Class<?> cls) { 5707accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (this == cls) { 571a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; // Can always assign to things of the same type. 572a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (this == Object.class) { 5737accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller return !cls.isPrimitive(); // Can assign any reference to java.lang.Object. 574a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isArray()) { 5757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller return cls.isArray() && componentType.isAssignableFrom(cls.componentType); 576a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isInterface()) { 577a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search iftable which has a flattened and uniqued list of interfaces. 5787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller Object[] iftable = cls.ifTable; 579a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 580a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 5811ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath if (iftable[i] == this) { 582a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 583a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 584a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 585a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 586a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 587a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 5887accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (!cls.isInterface()) { 5897accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller for (cls = cls.superClass; cls != null; cls = cls.superClass) { 5907accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (cls == this) { 591a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 592a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 593a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 594a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 595a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 596a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 597a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 59851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 59951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 60051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Class} object represents an 60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface type. 60251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 60351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this object represents an interface; 60451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 606a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isInterface() { 607a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (accessFlags & Modifier.INTERFACE) != 0; 608a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 60951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 61051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 61151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if this {@code Class} object represents an array class. 61251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 61351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this object represents an array class; 61451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 61551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 61651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 617a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isArray() { 618a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getComponentType() != null; 619a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 62051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 62151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 62251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Class} object represents a 62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type. 62451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 62551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> There are nine predefined {@code Class} objects to represent 62651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the eight primitive types and void. These are created by the Java 62751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Virtual Machine, and have the same names as the primitive types that 62851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * they represent, namely {@code boolean}, {@code byte}, 62951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code char}, {@code short}, {@code int}, 63051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code long}, {@code float}, and {@code double}. 63151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 63251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> These objects may only be accessed via the following public static 63351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * final variables, and are the only {@code Class} objects for which 63451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method returns {@code true}. 63551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if and only if this class represents a primitive type 63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 63851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Boolean#TYPE 63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Character#TYPE 64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Byte#TYPE 64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Short#TYPE 64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Integer#TYPE 64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Long#TYPE 64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Float#TYPE 64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Double#TYPE 64651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Void#TYPE 64751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 64851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 649a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isPrimitive() { 6501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return (primitiveType & 0xFFFF) != 0; 651a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 65251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 6542ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * Indicates whether this {@code Class} or its parents override finalize. 6552ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * 6562ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * @return {@code true} if and if this class or its parents override 6572ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * finalize; 6582ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * 6592ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * @hide 6602ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong */ 6612ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong public boolean isFinalizable() { 6622ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong return (getModifiers() & FINALIZABLE) != 0; 6632ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong } 6642ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong 6652ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong /** 66651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns true if this {@code Class} object represents an annotation 66751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type. Note that if this method returns true, {@link #isInterface()} 66851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * would also return true, as all annotation types are also interfaces. 66951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 67051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this class object represents an annotation 67151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type; {@code false} otherwise 67251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 67351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 67451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isAnnotation() { 67551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (getModifiers() & ANNOTATION) != 0; 67651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if this class is a synthetic class; 68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code false} otherwise. 68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a synthetic class as 68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * defined by the Java Language Specification. 6837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 13.1 The Form of a Binary 68451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 68551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isSynthetic() { 68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (getModifiers() & SYNTHETIC) != 0; 68851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 68951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 69051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 69151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the name of the entity (class, interface, array class, 69251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, or void) represented by this {@code Class} object, 69351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as a {@code String}. 69451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 69551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a reference type that is not an 69651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array type then the binary name of the class is returned, as specified 69751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by 69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>. 69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 70051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a primitive type or void, then the 70151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name returned is a {@code String} equal to the Java language 70251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * keyword corresponding to the primitive type or void. 70351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 70451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a class of arrays, then the internal 70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * form of the name consists of the name of the element type preceded by 70651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one or more '{@code [}' characters representing the depth of the array 70751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * nesting. The encoding of element type names is as follows: 70851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 70951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><table summary="Element types and encodings"> 71051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><th> Element Type <th> <th> Encoding 71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> boolean <td> <td align=center> Z 71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> byte <td> <td align=center> B 71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> char <td> <td align=center> C 71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> class or interface 71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td> <td align=center> L<i>classname</i>; 71651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> double <td> <td align=center> D 71751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> float <td> <td align=center> F 71851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> int <td> <td align=center> I 71951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> long <td> <td align=center> J 72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> short <td> <td align=center> S 72151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </table></blockquote> 72251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 72351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The class or interface name <i>classname</i> is the binary name of 72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class specified above. 72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 72651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Examples: 72751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 72851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * String.class.getName() 72951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "java.lang.String" 73051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * byte.class.getName() 73151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "byte" 73251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (new Object[3]).getClass().getName() 73351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "[Ljava.lang.Object;" 73451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (new int[3][4][5][6][7][8][9]).getClass().getName() 73551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "[[[[[[[I" 73651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 73751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 73851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the name of the class or interface 73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 74051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 74151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getName() { 74251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String name = this.name; 74351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (name == null) 744a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski this.name = name = getNameNative(); 74551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 74651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 74751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 748fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 749a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native String getNameNative(); 75051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 75151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 75251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the class loader for the class. Some implementations may use 75351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null to represent the bootstrap class loader. This method will return 75451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null in such implementations if this class was loaded by the bootstrap 75551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader. 75651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 75751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If a security manager is present, and the caller's class loader is 75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not null and the caller's class loader is not the same as or an ancestor of 75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class loader for the class whose class loader is requested, then 76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method calls the security manager's {@code checkPermission} 76151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method with a {@code RuntimePermission("getClassLoader")} 76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * permission to ensure it's ok to access the class loader for the class. 76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object 76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type or void, null is returned. 76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 76751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the class loader that loaded the class or interface 76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if a security manager exists and its 77151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code checkPermission} method denies 77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the class loader for the class. 77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader 77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 77551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.RuntimePermission 77651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 77751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public ClassLoader getClassLoader() { 778a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isPrimitive()) { 77951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 78051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 781a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (classLoader == null) ? BootClassLoader.getInstance() : classLoader; 78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 78351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code TypeVariable} objects that represent the 78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type variables declared by the generic declaration represented by this 78751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code GenericDeclaration} object, in declaration order. Returns an 78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if the underlying generic declaration declares no type 78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * variables. 79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of {@code TypeVariable} objects that represent 79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the type variables declared by this generic declaration 79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError if the generic 79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * signature of this generic declaration does not conform to 79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the format specified in 79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 7997accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller @Override 8007accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller public synchronized TypeVariable<Class<T>>[] getTypeParameters() { 801d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao String annotationSignature = getSignatureAttribute(); 802a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature == null) { 803a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return EmptyArray.TYPE_VARIABLE; 804a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 805a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 806a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 807a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return parser.formalTypeParameters; 80851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 80951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 8107accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 81151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 81251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} representing the superclass of the entity 81351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (class, interface, primitive type or void) represented by this 81451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class}. If this {@code Class} represents either the 81551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} class, an interface, a primitive type, or void, then 81651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null is returned. If this object represents an array class then the 81751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object representing the {@code Object} class is 81851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. 81951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 82051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the superclass of the class represented by this object. 82151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 822a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<? super T> getSuperclass() { 823a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // For interfaces superClass is Object (which agrees with the JNI spec) 824a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // but not with the expected behavior here. 825a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isInterface()) { 826a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 827a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 828a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return superClass; 829a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 830a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 83151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 83251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 83351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Type} representing the direct superclass of 83451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the entity (class, interface, primitive type or void) represented by 83551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class}. 83651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 83751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the superclass is a parameterized type, the {@code Type} 83851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object returned must accurately reflect the actual type 83951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameters used in the source code. The parameterized type 84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing the superclass is created if it had not been 84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * created before. See the declaration of {@link 84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.ParameterizedType ParameterizedType} for the 84351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * semantics of the creation process for parameterized types. If 84451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} represents either the {@code Object} 84551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, an interface, a primitive type, or void, then null is 84651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. If this object represents an array class then the 84751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object representing the {@code Object} class is 84851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. 84951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 85051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError if the generic 85151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class signature does not conform to the format specified in 85251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 85351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws TypeNotPresentException if the generic superclass 85451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * refers to a non-existent type declaration 85551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.MalformedParameterizedTypeException if the 85651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * generic superclass refers to a parameterized type that cannot be 85751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instantiated for any reason 85851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the superclass of the class represented by this object 85951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 86051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 86151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Type getGenericSuperclass() { 862a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Type genericSuperclass = getSuperclass(); 863a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // This method is specified to return null for all cases where getSuperclass 864a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // returns null, i.e, for primitives, interfaces, void and java.lang.Object. 865a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (genericSuperclass == null) { 866a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 867a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 868a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 869d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao String annotationSignature = getSignatureAttribute(); 870a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature != null) { 871a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 872a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 873a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski genericSuperclass = parser.superclassType; 874a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 875a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return Types.getType(genericSuperclass); 87651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 87751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 87851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 87951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Gets the package for this class. The class loader of this class is used 88051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to find the package. If the class was loaded by the bootstrap class 88151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader the set of packages loaded from CLASSPATH is searched to find the 88251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * package of the class. Null is returned if no package object was created 88351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the class loader of this class. 88451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 88551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Packages have attributes for versions and specifications only if the 88651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * information was defined in the manifests that accompany the classes, and 88751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the class loader created the package instance with the attributes 88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * from the manifest. 88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the package of the class, or null if no package 89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * information is available from the archive or codebase. 89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 89351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Package getPackage() { 894a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader loader = getClassLoader(); 895a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (loader != null) { 896a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName = getPackageName$(); 897a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName != null ? loader.getPackage(packageName) : null; 898a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 899a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 900a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 901a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 902a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 903a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns the package name of this class. This returns null for classes in 904a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * the default package. 905a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 906a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 907a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 908a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public String getPackageName$() { 909a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String name = getName(); 910a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int last = name.lastIndexOf('.'); 911a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return last == -1 ? null : name.substring(0, last); 91251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 91351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 91451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 91551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 91651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines the interfaces implemented by the class or interface 91751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 91851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 91951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class, the return value is an array 92051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * containing objects representing all interfaces implemented by the 92151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. The order of the interface objects in the array corresponds to 92251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the order of the interface names in the {@code implements} clause 92351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the declaration of the class represented by this object. For 92451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, given the declaration: 92551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 92651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code class Shimmer implements FloorWax, DessertTopping { ... }} 92751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 92851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * suppose the value of {@code s} is an instance of 92951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Shimmer}; the value of the expression: 93051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 93151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code s.getClass().getInterfaces()[0]} 93251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 93351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is the {@code Class} object that represents interface 93451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code FloorWax}; and the value of: 93551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 93651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code s.getClass().getInterfaces()[1]} 93751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 93851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is the {@code Class} object that represents interface 93951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code DessertTopping}. 94051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 94151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents an interface, the array contains objects 94251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing all interfaces extended by the interface. The order of the 94351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface objects in the array corresponds to the order of the interface 94451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * names in the {@code extends} clause of the declaration of the 94551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface represented by this object. 94651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 94751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class or interface that implements no 94851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces, the method returns an array of length 0. 94951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 95051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a primitive type or void, the method 95151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns an array of length 0. 95251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents an array type, the 9547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * interfaces {@code Cloneable} and {@code java.io.Serializable} are 9557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * returned in that order. 9567accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 95751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of interfaces implemented by this class. 95851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 959a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?>[] getInterfaces() { 960a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isArray()) { 961a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return new Class<?>[] { Cloneable.class, Serializable.class }; 962a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 963e77a467884a8b323a1ac6a229f06f9a032b141b5Narayan Kamath 964e77a467884a8b323a1ac6a229f06f9a032b141b5Narayan Kamath final Class<?>[] ifaces = getInterfacesInternal(); 965e77a467884a8b323a1ac6a229f06f9a032b141b5Narayan Kamath if (ifaces == null) { 966a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return EmptyArray.CLASS; 967a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 968e77a467884a8b323a1ac6a229f06f9a032b141b5Narayan Kamath 969e77a467884a8b323a1ac6a229f06f9a032b141b5Narayan Kamath return ifaces; 970a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 971a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 972fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 973e77a467884a8b323a1ac6a229f06f9a032b141b5Narayan Kamath private native Class<?>[] getInterfacesInternal(); 974e77a467884a8b323a1ac6a229f06f9a032b141b5Narayan Kamath 97551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 97651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 97751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Type}s representing the interfaces 97851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * directly implemented by the class or interface represented by 97951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this object. 98051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 98151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If a superinterface is a parameterized type, the 98251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Type} object returned for it must accurately reflect 98351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the actual type parameters used in the source code. The 98451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameterized type representing each superinterface is created 98551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if it had not been created before. See the declaration of 98651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link java.lang.reflect.ParameterizedType ParameterizedType} 98751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for the semantics of the creation process for parameterized 98851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types. 98951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 99051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class, the return value is an 99151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array containing objects representing all interfaces 99251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implemented by the class. The order of the interface objects in 99351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array corresponds to the order of the interface names in 99451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the {@code implements} clause of the declaration of the class 99551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. In the case of an array class, the 99651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces {@code Cloneable} and {@code Serializable} are 99751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned in that order. 99851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 99951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents an interface, the array contains 100051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects representing all interfaces directly extended by the 100151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface. The order of the interface objects in the array 100251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * corresponds to the order of the interface names in the 100351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code extends} clause of the declaration of the interface 100451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 100551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 100651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents a class or interface that 100751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implements no interfaces, the method returns an array of length 100851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 0. 100951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 101051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents a primitive type or void, the 101151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns an array of length 0. 101251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 101351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError 101451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the generic class signature does not conform to the format 101551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified in 101651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 101751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws TypeNotPresentException if any of the generic 101851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces refers to a non-existent type declaration 101951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.MalformedParameterizedTypeException 102051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if any of the generic superinterfaces refer to a parameterized 102151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type that cannot be instantiated for any reason 102251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of interfaces implemented by this class 102351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 102451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 102551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Type[] getGenericInterfaces() { 1026a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Type[] result; 1027a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized (Caches.genericInterfaces) { 1028a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = Caches.genericInterfaces.get(this); 1029a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null) { 1030d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao String annotationSignature = getSignatureAttribute(); 1031a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature == null) { 1032a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = getInterfaces(); 1033a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 1034a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 1035a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 1036a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = Types.getTypeArray(parser.interfaceTypes, false); 1037a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1038a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Caches.genericInterfaces.put(this, result); 1039a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1040a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1041a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (result.length == 0) ? result : result.clone(); 104251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 104351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 104451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 104551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 104651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} representing the component type of an 104751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. If this class does not represent an array class this method 104851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns null. 104951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 105051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Class} representing the component type of this 105151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class if this class is an array 105251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.reflect.Array 105351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 105451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1055a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?> getComponentType() { 1056a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return componentType; 1057a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 105851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 105951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 106051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the Java language modifiers for this class or interface, encoded 106151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in an integer. The modifiers consist of the Java Virtual Machine's 106251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constants for {@code public}, {@code protected}, 106351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code private}, {@code final}, {@code static}, 106451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code abstract} and {@code interface}; they should be decoded 106551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * using the methods of class {@code Modifier}. 106651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 106751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the underlying class is an array class, then its 106851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code public}, {@code private} and {@code protected} 106951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * modifiers are the same as those of its component type. If this 107051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} represents a primitive type or void, its 107151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code public} modifier is always {@code true}, and its 107251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code protected} and {@code private} modifiers are always 107351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. If this object represents an array class, a 107451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type or void, then its {@code final} modifier is always 107551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code true} and its interface modifier is always 107651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. The values of its other modifiers are not determined 107751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this specification. 107851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 107951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The modifier encodings are defined in <em>The Java Virtual Machine 108051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Specification</em>, table 4.1. 108151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 108251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code int} representing the modifiers for this class 108351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.reflect.Modifier 108451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 108551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1086a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getModifiers() { 1087a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Array classes inherit modifiers from their component types, but in the case of arrays 1088a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // of an inner class, the class file may contain "fake" access flags because it's not valid 1089a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // for a top-level class to private, say. The real access flags are stored in the InnerClass 1090a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // attribute, so we need to make sure we drill down to the inner class: the accessFlags 1091a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // field is not the value we want to return, and the synthesized array class does not itself 1092a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // have an InnerClass attribute. https://code.google.com/p/android/issues/detail?id=56267 1093a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isArray()) { 1094a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int componentModifiers = getComponentType().getModifiers(); 1095a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if ((componentModifiers & Modifier.INTERFACE) != 0) { 1096a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski componentModifiers &= ~(Modifier.INTERFACE | Modifier.STATIC); 1097a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1098a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return Modifier.ABSTRACT | Modifier.FINAL | componentModifiers; 1099a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1100a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int JAVA_FLAGS_MASK = 0xffff; 1101d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao int modifiers = this.getInnerClassFlags(accessFlags & JAVA_FLAGS_MASK); 1102a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return modifiers & JAVA_FLAGS_MASK; 1103a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 110451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 110551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 110651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Gets the signers of this class. 110751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 110851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the signers of this class, or null if there are no signers. In 110951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular, this method returns null if this object represents 111051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a primitive type or void. 111151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 111251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1113a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Object[] getSigners() { 1114a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 1115a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 111651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 1117fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 111870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native Method getEnclosingMethodNative(); 111951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 112051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 112151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents a local or anonymous 112251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class within a method, returns a {@link 112351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Method Method} object representing the 112451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * immediately enclosing method of the underlying class. Returns 112551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} otherwise. 112651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 112751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * In particular, this method returns {@code null} if the underlying 112851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class is a local or anonymous class immediately enclosed by a type 112951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declaration, instance initializer or static initializer. 113051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 113151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing method of the underlying class, if 113251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that class is a local or anonymous class; otherwise {@code null}. 113351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 113451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 11356975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed: Removed SecurityException 113651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getEnclosingMethod() { 1137a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classNameImpliesTopLevel()) { 113851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 113951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 114070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return getEnclosingMethodNative(); 114151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 114251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 114351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 114451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents a local or anonymous 114551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class within a constructor, returns a {@link 114651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Constructor Constructor} object representing 114751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the immediately enclosing constructor of the underlying 114851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. Returns {@code null} otherwise. In particular, this 114951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns {@code null} if the underlying class is a local 115051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or anonymous class immediately enclosed by a type declaration, 115151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instance initializer or static initializer. 115251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 115351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing constructor of the underlying class, if 115451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that class is a local or anonymous class; otherwise {@code null}. 115551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 115651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 11576975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed: Removed SecurityException 115851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?> getEnclosingConstructor() { 1159a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classNameImpliesTopLevel()) { 116051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 116151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 116270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return getEnclosingConstructorNative(); 1163a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1164a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1165fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 116670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native Constructor<?> getEnclosingConstructorNative(); 116770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 1168a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean classNameImpliesTopLevel() { 1169a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return !getName().contains("$"); 117051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 117151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 11727accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 117351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 117451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the class or interface represented by this {@code Class} object 117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a member of another class, returns the {@code Class} object 117651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing the class in which it was declared. This method returns 117751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null if this class or interface is not a member of any other class. If 117851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents an array class, a primitive 117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type, or void,then this method returns null. 118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 118151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the declaring class for this class 118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 11846975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed: Removed SecurityException 1185fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 118670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public native Class<?> getDeclaringClass(); 118751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 118851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 118951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the immediately enclosing class of the underlying 119051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. If the underlying class is a top level class this 119151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns {@code null}. 119251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing class of the underlying class 119351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 119451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 11956975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed: Removed SecurityException 1196fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 119770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public native Class<?> getEnclosingClass(); 119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the simple name of the underlying class as given in the 120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code. Returns an empty string if the underlying class is 120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * anonymous. 120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The simple name of an array is the simple name of the 120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * component type with "[]" appended. In particular the simple 120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name of an array whose component type is anonymous is "[]". 120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the simple name of the underlying class 120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getSimpleName() { 121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isArray()) 121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getComponentType().getSimpleName()+"[]"; 121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 121592d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath if (isAnonymousClass()) { 121692d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath return ""; 121792d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath } 121892d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath 121992d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath if (isMemberClass() || isLocalClass()) { 122092d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath // Note that we obtain this information from getInnerClassName(), which uses 122192d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath // dex system annotations to obtain the name. It is possible for this information 122292d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath // to disagree with the actual enclosing class name. For example, if dex 122392d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath // manipulation tools have renamed the enclosing class without adjusting 122492d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath // the system annotation to match. See http://b/28800927. 122592d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath return getInnerClassName(); 122692d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath } 122792d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath 122892d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath String simpleName = getName(); 122992d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath final int dot = simpleName.lastIndexOf("."); 123092d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath if (dot > 0) { 123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name 123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 123351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 123492d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath return simpleName; 123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 123651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 123720938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller /** 123820938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller * Return an informative string for the name of this type. 123920938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller * 124020938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller * @return an informative string for the name of this type 124120938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller * @since 1.8 124220938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller */ 124320938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller public String getTypeName() { 124420938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller if (isArray()) { 124520938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller try { 124620938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller Class<?> cl = this; 124720938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller int dimensions = 0; 124820938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller while (cl.isArray()) { 124920938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller dimensions++; 125020938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller cl = cl.getComponentType(); 125120938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller } 125220938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller StringBuilder sb = new StringBuilder(); 125320938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller sb.append(cl.getName()); 125420938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller for (int i = 0; i < dimensions; i++) { 125520938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller sb.append("[]"); 125620938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller } 125720938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller return sb.toString(); 125820938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller } catch (Throwable e) { /*FALLTHRU*/ } 125920938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller } 126020938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller return getName(); 126120938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller } 126220938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller 126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the canonical name of the underlying class as 126551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * defined by the Java Language Specification. Returns null if 126651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the underlying class does not have a canonical name (i.e., if 126751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is a local or anonymous class or an array whose component 126851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type does not have a canonical name). 126951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the canonical name of the underlying class if it exists, and 127051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} otherwise. 127151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 127251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 127351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getCanonicalName() { 127451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isArray()) { 127551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String canonicalName = getComponentType().getCanonicalName(); 127651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (canonicalName != null) 127751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return canonicalName + "[]"; 127851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 127951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 128051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 128151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isLocalOrAnonymousClass()) 128251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 128351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> enclosingClass = getEnclosingClass(); 128451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingClass == null) { // top level class 128551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getName(); 128651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 128751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String enclosingName = enclosingClass.getCanonicalName(); 128851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingName == null) 128951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 129051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return enclosingName + "." + getSimpleName(); 129151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 129251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 129351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 129451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 129551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 129651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is an anonymous class. 129751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 129851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is an anonymous class. 129951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 130051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1301fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 130270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public native boolean isAnonymousClass(); 130351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 130451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 130551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 130651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a local class. 130751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 130851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a local class. 130951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 131051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 131151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isLocalClass() { 1312d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao return (getEnclosingMethod() != null || getEnclosingConstructor() != null) 1313a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski && !isAnonymousClass(); 131451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 131551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 131651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 131751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 131851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a member class. 131951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 132051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a member class. 132151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 132251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 132351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isMemberClass() { 132492d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath return getDeclaringClass() != null; 132551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 132651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 132751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 132851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if this is a local class or an anonymous 132951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. Returns {@code false} otherwise. 133051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 133151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private boolean isLocalOrAnonymousClass() { 133251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // JVM Spec 4.8.6: A class must have an EnclosingMethod 133351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // attribute if and only if it is a local class or an 133451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // anonymous class. 1335a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return isLocalClass() || isAnonymousClass(); 133651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 133751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 133851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 133951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Class} objects representing all 134051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the public classes and interfaces that are members of the class 134151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object. This includes public 134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class and interface members inherited from superclasses and public class 134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and interface members declared by the class. This method returns an 134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if this {@code Class} object has no public member 134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * classes or interfaces. This method also returns an array of length 0 if 134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type, an array 134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, or void. 134851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Class} objects representing the public 13507accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * members of this class 135151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 135251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 135351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 135451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 135551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Class<?>[] getClasses() { 13561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath List<Class<?>> result = new ArrayList<Class<?>>(); 13571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath for (Class<?> c = this; c != null; c = c.superClass) { 13581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath for (Class<?> member : c.getDeclaredClasses()) { 13591ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath if (Modifier.isPublic(member.getModifiers())) { 13601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath result.add(member); 13611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 13621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 13631ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 13641ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return result.toArray(new Class[result.size()]); 136551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 136651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 136751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 136851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 136951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Field} objects reflecting all 137051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the accessible public fields of the class or interface represented by 13717accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * this {@code Class} object. 137251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13737accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents a class or interface with no 13747accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * no accessible public fields, then this method returns an array of length 13757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 0. 137651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13777accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents a class, then this method 13787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * returns the public fields of the class and of all its superclasses. 137951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13807accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents an interface, then this 13817accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * method returns the fields of the interface and of all its 13827accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * superinterfaces. 138351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13847accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents an array type, a primitive 13857accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * type, or void, then this method returns an array of length 0. 138651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13877accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> The elements in the returned array are not sorted and are not in any 13887accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * particular order. 138951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13907accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @return the array of {@code Field} objects representing the 13917accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * public fields 13927accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 13937accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and 13947accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * the caller's class loader is not the same as or an 13957accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 13967accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 13977accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 13987accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class. 139951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 140051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 14017accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.2 Class Members 14027accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.3 Field Declarations 140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 140451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 140551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Field[] getFields() throws SecurityException { 1406a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski List<Field> fields = new ArrayList<Field>(); 14071ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath getPublicFieldsRecursive(fields); 14081ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return fields.toArray(new Field[fields.size()]); 14091ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 14101ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 14111ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 14121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates {@code result} with public fields defined by this class, its 14131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * superclasses, and all implemented interfaces. 14141ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 14151ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private void getPublicFieldsRecursive(List<Field> result) { 1416a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search superclasses 1417a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = this; c != null; c = c.superClass) { 14181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, c.getPublicDeclaredFields()); 1419a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1420a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1421a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search iftable which has a flattened and uniqued list of interfaces 1422a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 1423a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 1424a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 14251ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, ((Class<?>) iftable[i]).getPublicDeclaredFields()); 1426a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1427a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 142851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 142951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 143051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 14317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * Returns an array containing {@code Method} objects reflecting all the 14327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * public methods of the class or interface represented by this {@code 14337accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * Class} object, including those declared by the class or interface and 14347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * those inherited from superclasses and superinterfaces. 143551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents a type that has multiple 14377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * public methods with the same name and parameter types, but different 14387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * return types, then the returned array has a {@code Method} object for 14397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * each such method. 144051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents a type with a class 14427accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * initialization method {@code <clinit>}, then the returned array does 14437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <em>not</em> have a corresponding {@code Method} object. 144451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14457accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents an array type, then the 14467accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * returned array has a {@code Method} object for each of the public 14477accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * methods inherited by the array type from {@code Object}. It does not 14487accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * contain a {@code Method} object for {@code clone()}. 144951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14507accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents an interface then the 14517accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * returned array does not contain any implicitly declared methods from 14527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@code Object}. Therefore, if no methods are explicitly declared in 14537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * this interface or any of its superinterfaces then the returned array 14547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * has length 0. (Note that a {@code Class} object which represents a class 14557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * always has public methods, inherited from {@code Object}.) 145651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14577accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents a primitive type or void, 14587accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * then the returned array has length 0. 145951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14607accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> Static methods declared in superinterfaces of the class or interface 14617accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * represented by this {@code Class} object are not considered members of 14627accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * the class or interface. 146351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14647accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> The elements in the returned array are not sorted and are not in any 14657accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * particular order. 146651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14677accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @return the array of {@code Method} objects representing the 14687accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * public methods of this class 14697accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 14707accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and 14717accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * the caller's class loader is not the same as or an 14727accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 14737accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 14747accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 14757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class. 14767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 14777accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.2 Class Members 14787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.4 Method Declarations 147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 148151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 148251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method[] getMethods() throws SecurityException { 1483a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski List<Method> methods = new ArrayList<Method>(); 14841ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath getPublicMethodsInternal(methods); 14851ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /* 14861ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Remove duplicate methods defined by superclasses and 14871ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * interfaces, preferring to keep methods declared by derived 14881ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * types. 14891ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 14901ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath CollectionUtils.removeDuplicates(methods, Method.ORDER_BY_SIGNATURE); 14911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return methods.toArray(new Method[methods.size()]); 14921ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 14931ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 14941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 14951ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates {@code result} with public methods defined by this class, its 14961ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * superclasses, and all implemented interfaces, including overridden methods. 14971ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 14981ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private void getPublicMethodsInternal(List<Method> result) { 14991ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, getDeclaredMethodsUnchecked(true)); 1500a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!isInterface()) { 1501a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search superclasses, for interfaces don't search java.lang.Object. 1502a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = superClass; c != null; c = c.superClass) { 15031ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, c.getDeclaredMethodsUnchecked(true)); 1504a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1505a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1506a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search iftable which has a flattened and uniqued list of interfaces. 1507a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 1508a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 1509a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 1510a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) iftable[i]; 15111ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, ifc.getDeclaredMethodsUnchecked(true)); 1512a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1513a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 151451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 151551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 151651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 151751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Constructor} objects reflecting 151851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * all the public constructors of the class represented by this 151951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. An array of length 0 is returned if the 152051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class has no public constructors, or if the class is an array class, or 152151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the class reflects a primitive type or void. 152251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 152351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that while this method returns an array of {@code 152451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor<T>} objects (that is an array of constructors from 152551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this class), the return type of this method is {@code 152651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as 152751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * might be expected. This less informative return type is 152851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * necessary since after being returned from this method, the 152951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array could be modified to hold {@code Constructor} objects for 153051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * different classes, which would violate the type guarantees of 153151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Constructor<T>[]}. 153251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 153351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Constructor} objects representing the 15347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * public constructors of this class 15357accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 15367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and 15377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * the caller's class loader is not the same as or an 15387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 15397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 15407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 15417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class. 154251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 154351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 154451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 154551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 154651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?>[] getConstructors() throws SecurityException { 15471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return getDeclaredConstructorsInternal(true); 154851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 154951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 15507accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 155151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 15527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * Returns a {@code Field} object that reflects the specified public member 15537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * field of the class or interface represented by this {@code Class} 15547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * object. The {@code name} parameter is a {@code String} specifying the 15557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * simple name of the desired field. 155651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 155751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The field to be reflected is determined by the algorithm that 15587accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * follows. Let C be the class or interface represented by this object: 15597accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 156051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <OL> 156151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If C declares a public field with the name specified, that is the 156251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * field to be reflected.</LI> 156351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no field was found in step 1 above, this algorithm is applied 156451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * recursively to each direct superinterface of C. The direct 156551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces are searched in the order they were declared.</LI> 156651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no field was found in steps 1 and 2 above, and C has a 156751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superclass S, then this algorithm is invoked recursively upon S. 156851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If C has no superclass, then a {@code NoSuchFieldException} 156951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is thrown.</LI> 157051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </OL> 157151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 15727accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents an array type, then this 15737accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * method does not find the {@code length} field of the array type. 157451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 157551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the field name 15767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @return the {@code Field} object of this class specified by 15777accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@code name} 15787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NoSuchFieldException if a field with the specified name is 15797accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * not found. 15807accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NullPointerException if {@code name} is {@code null} 15817accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 15827accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and 15837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * the caller's class loader is not the same as or an 15847accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 15857accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 15867accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 15877accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class. 158851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 158951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 15907accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.2 Class Members 15917accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.3 Field Declarations 159251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 15936975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed: Removed SecurityException 15947accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller public Field getField(String name) 15957accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller throws NoSuchFieldException { 1596fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski if (name == null) { 1597fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski throw new NullPointerException("name == null"); 1598fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski } 159970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak Field result = getPublicFieldRecursive(name); 160070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (result == null) { 160151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NoSuchFieldException(name); 160251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 160370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return result; 1604a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1605a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 160670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** 160770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * The native implementation of the {@code getField} method. 160870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 160970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * @throws NullPointerException 161070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * if name is null. 161170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * @see #getField(String) 161270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 1613fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 161470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native Field getPublicFieldRecursive(String name); 161551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 161651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 161751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Method} object that reflects the specified public 161851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member method of the class or interface represented by this 161951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 162051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} specifying the simple name of the desired method. The 162151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code parameterTypes} parameter is an array of {@code Class} 162251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects that identify the method's formal parameter types, in declared 162351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * order. If {@code parameterTypes} is {@code null}, it is 162451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * treated as if it were an empty array. 162551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 16267accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If the {@code name} is "{@code <init>}" or "{@code <clinit>}" a 162751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code NoSuchMethodException} is raised. Otherwise, the method to 162851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be reflected is determined by the algorithm that follows. Let C be the 16297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * class or interface represented by this object: 163051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <OL> 16317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <LI> C is searched for a <I>matching method</I>, as defined below. If a 16327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * matching method is found, it is reflected.</LI> 16337accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <LI> If no matching method is found by step 1 then: 16347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <OL TYPE="a"> 16357accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <LI> If C is a class other than {@code Object}, then this algorithm is 16367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invoked recursively on the superclass of C.</LI> 16377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <LI> If C is the class {@code Object}, or if C is an interface, then 16387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * the superinterfaces of C (if any) are searched for a matching 16397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * method. If any such method is found, it is reflected.</LI> 16407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * </OL></LI> 164151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </OL> 164251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 16437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> To find a matching method in a class or interface C: If C 16447accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * declares exactly one public method with the specified name and exactly 16457accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * the same formal parameter types, that is the method reflected. If more 16467accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * than one such method is found in C, and one of these methods has a 16477accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * return type that is more specific than any of the others, that method is 16487accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * reflected; otherwise one of the methods is chosen arbitrarily. 164951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 165051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that there may be more than one matching method in a 165151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class because while the Java language forbids a class to 165251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declare multiple methods with the same signature but different 165351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return types, the Java virtual machine does not. This 165451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * increased flexibility in the virtual machine can be used to 165551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implement various language features. For example, covariant 165651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns can be implemented with {@linkplain 165751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Method#isBridge bridge methods}; the bridge 165851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method and the method being overridden would have the same 165951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * signature but different return types. 166051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 16617accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents an array type, then this 16627accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * method does not find the {@code clone()} method. 16637accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 16647accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> Static methods declared in superinterfaces of the class or interface 16657accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * represented by this {@code Class} object are not considered members of 16667accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * the class or interface. 166751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 166851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the method 166951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the list of parameters 167051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Method} object that matches the specified 16717accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@code name} and {@code parameterTypes} 16727accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NoSuchMethodException if a matching method is not found 16737accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * or if the name is "<init>"or "<clinit>". 16747accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NullPointerException if {@code name} is {@code null} 16757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 16767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and 16777accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * the caller's class loader is not the same as or an 16787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 16797accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 16807accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 16817accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class. 16827accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 16837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.2 Class Members 16847accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.4 Method Declarations 168551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 168651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 168751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 168851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getMethod(String name, Class<?>... parameterTypes) 168951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 1690a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getMethod(name, parameterTypes, true); 169151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 169251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 169351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 169451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 169551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Constructor} object that reflects the specified 169651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public constructor of the class represented by this {@code Class} 169751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The {@code parameterTypes} parameter is an array of 169851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that identify the constructor's formal 169951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types, in declared order. 170051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents an inner class 170251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared in a non-static context, the formal parameter types 170351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * include the explicit enclosing instance as the first parameter. 170451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The constructor to reflect is the public constructor of the class 170651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object whose formal parameter 170751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types match those specified by {@code parameterTypes}. 170851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 171051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Constructor} object of the public constructor that 17117accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * matches the specified {@code parameterTypes} 17127accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NoSuchMethodException if a matching method is not found. 17137accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 17147accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and 17157accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * the caller's class loader is not the same as or an 17167accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 17177accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 17187accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 17197accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class. 172051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 172151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 172251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 172351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<T> getConstructor(Class<?>... parameterTypes) 172451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 172551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getConstructor0(parameterTypes, Member.PUBLIC); 172651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 172751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 17287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 172951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 173051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Class} objects reflecting all the 173151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * classes and interfaces declared as members of the class represented by 173251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object. This includes public, protected, default 173351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private classes and interfaces declared by the 173451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, but excludes inherited classes and interfaces. This method 173551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns an array of length 0 if the class declares no classes or 173651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces as members, or if this {@code Class} object represents a 173751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, an array class, or void. 173851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 173951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Class} objects representing all the 17407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * declared members of this class 17417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 17427accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and any of the 17437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * following conditions is met: 174451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17457accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <ul> 174651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17477accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as the 17487accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * class loader of this class and invocation of 17497accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@link SecurityManager#checkPermission 17507accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPermission} method with 17517accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@code RuntimePermission("accessDeclaredMembers")} 17527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * denies access to the declared classes within this class 175351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as or an 17557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 17567accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 17577accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 17587accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class 175951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17607accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * </ul> 176151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 176251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 176351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 17646975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed: Removed SecurityException 1765fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 176670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak public native Class<?>[] getDeclaredClasses(); 176751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 176851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 176951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Field} objects reflecting all the fields 177051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 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 fields, but excludes inherited fields. 177351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17747accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents a class or interface with no 17757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * declared fields, then this method returns an array of length 0. 17767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 17777accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents an array type, a primitive 17787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * type, or void, then this method returns an array of length 0. 17797accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 17807accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> The elements in the returned array are not sorted and are not in any 17817accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * particular order. 178251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @return the array of {@code Field} objects representing all the 17847accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * declared fields of this class 17857accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 17867accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and any of the 17877accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * following conditions is met: 178851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17897accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <ul> 179051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17917accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as the 17927accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * class loader of this class and invocation of 17937accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@link SecurityManager#checkPermission 17947accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPermission} method with 17957accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@code RuntimePermission("accessDeclaredMembers")} 17967accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * denies access to the declared fields within this class 179751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17987accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as or an 17997accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 18007accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 18017accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 18027accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class 180351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18047accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * </ul> 180551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 180651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 18077accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.2 Class Members 18087accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.3 Field Declarations 180951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 18106975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed: Removed SecurityException 1811fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 18121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Field[] getDeclaredFields(); 181351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 18141ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 18151ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates a list of fields without performing any security or type 18161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * resolution checks first. If no fields exist, the list is not modified. 18171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 18181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param publicOnly Whether to return only public fields. 18191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @hide 18201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1821fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 18221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Field[] getDeclaredFieldsUnchecked(boolean publicOnly); 182351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 182451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 182551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18267accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * Returns an array containing {@code Method} objects reflecting all the 18277accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * declared methods of the class or interface represented by this {@code 18287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * Class} object, including public, protected, default (package) 18297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * access, and private methods, but excluding inherited methods. 183051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents a type that has multiple 18327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * declared methods with the same name and parameter types, but different 18337accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * return types, then the returned array has a {@code Method} object for 18347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * each such method. 183551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents a type that has a class 18377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * initialization method {@code <clinit>}, then the returned array does 18387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <em>not</em> have a corresponding {@code Method} object. 183951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents a class or interface with no 18417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * declared methods, then the returned array has length 0. 184251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents an array type, a primitive 18447accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * type, or void, then the returned array has length 0. 184551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18467accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> The elements in the returned array are not sorted and are not in any 18477accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * particular order. 184851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18497accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @return the array of {@code Method} objects representing all the 18507accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * declared methods of this class 18517accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 18527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and any of the 18537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * following conditions is met: 18547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 18557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <ul> 18567accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 18577accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as the 18587accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * class loader of this class and invocation of 18597accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@link SecurityManager#checkPermission 18607accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPermission} method with 18617accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@code RuntimePermission("accessDeclaredMembers")} 18627accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * denies access to the declared methods within this class 18637accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 18647accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as or an 18657accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 18667accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 18677accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 18687accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class 18697accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 18707accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * </ul> 18717accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 18727accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.2 Class Members 18737accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.4 Method Declarations 187451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 187551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 187651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method[] getDeclaredMethods() throws SecurityException { 18771ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Method[] result = getDeclaredMethodsUnchecked(false); 1878a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Method m : result) { 1879a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Throw NoClassDefFoundError if types cannot be resolved. 1880a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski m.getReturnType(); 1881a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski m.getParameterTypes(); 1882a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1883a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 1884a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1885a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 18861ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 18871ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates a list of methods without performing any security or type 18881ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * resolution checks first. If no methods exist, the list is not modified. 18891ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 18901ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param publicOnly Whether to return only public methods. 18911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @hide 18921ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1893fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 18941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Method[] getDeclaredMethodsUnchecked(boolean publicOnly); 189551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 189651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 189751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Constructor} objects reflecting all the 189851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructors declared by the class represented by this 189951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. These are public, protected, default 190051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private constructors. The elements in the array 190151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned are not sorted and are not in any particular order. If the 190251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class has a default constructor, it is included in the returned array. 190351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method returns an array of length 0 if this {@code Class} 190451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object represents an interface, a primitive type, an array class, or 190551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * void. 190651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 190751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, section 8.2. 190851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 19097accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @return the array of {@code Constructor} objects representing all the 19107accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * declared constructors of this class 19117accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 19127accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and any of the 19137accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * following conditions is met: 191451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 19157accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <ul> 191651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 19177accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as the 19187accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * class loader of this class and invocation of 19197accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@link SecurityManager#checkPermission 19207accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPermission} method with 19217accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@code RuntimePermission("accessDeclaredMembers")} 19227accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * denies access to the declared constructors within this class 192351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 19247accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as or an 19257accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 19267accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 19277accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 19287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class 192951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 19307accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * </ul> 193151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 193251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 193351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 193451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?>[] getDeclaredConstructors() throws SecurityException { 19351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return getDeclaredConstructorsInternal(false); 193651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 193751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 19387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 193951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 19401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the constructor with the given parameters if it is defined by this class; 19411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * {@code null} otherwise. This may return a non-public member. 19421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1943fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 19441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Constructor<?>[] getDeclaredConstructorsInternal(boolean publicOnly); 19451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 19461ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 194751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Field} object that reflects the specified declared 194851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * field of the class or interface represented by this {@code Class} 19497accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * object. The {@code name} parameter is a {@code String} that specifies 19507accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * the simple name of the desired field. 19517accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 19527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents an array type, then this 19537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * method does not find the {@code length} field of the array type. 195451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 195551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the field 19567accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @return the {@code Field} object for the specified field in this 19577accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * class 19587accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NoSuchFieldException if a field with the specified name is 19597accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * not found. 19607accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NullPointerException if {@code name} is {@code null} 19617accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 19627accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and any of the 19637accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * following conditions is met: 19647accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 19657accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <ul> 19667accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 19677accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as the 19687accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * class loader of this class and invocation of 19697accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@link SecurityManager#checkPermission 19707accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPermission} method with 19717accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@code RuntimePermission("accessDeclaredMembers")} 19727accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * denies access to the declared field 19737accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 19747accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as or an 19757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 19767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 19777accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 19787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class 19797accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 19807accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * </ul> 198151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 198251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 19837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.2 Class Members 19847accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.3 Field Declarations 198551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 19866975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed: Removed SecurityException 1987fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 19881ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Field getDeclaredField(String name) throws NoSuchFieldException; 198951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 19901ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 19911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the subset of getDeclaredFields which are public. 19921ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1993fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 19941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Field[] getPublicDeclaredFields(); 1995a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 199651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 199751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Method} object that reflects the specified 199851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared method of the class or interface represented by this 199951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 200051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} that specifies the simple name of the desired 200151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method, and the {@code parameterTypes} parameter is an array of 200251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that identify the method's formal parameter 200351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types, in declared order. If more than one method with the same 200451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types is declared in a class, and one of these methods has a 200551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type that is more specific than any of the others, that method is 200651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned; otherwise one of the methods is chosen arbitrarily. If the 200751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name is "<init>"or "<clinit>" a {@code NoSuchMethodException} 200851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is raised. 200951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 20107accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <p> If this {@code Class} object represents an array type, then this 20117accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * method does not find the {@code clone()} method. 20127accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 201351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the method 201451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 20157accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @return the {@code Method} object for the method of this class 20167accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * matching the specified name and parameters 20177accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NoSuchMethodException if a matching method is not found. 20187accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NullPointerException if {@code name} is {@code null} 20197accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 20207accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and any of the 20217accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * following conditions is met: 20227accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 20237accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <ul> 20247accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 20257accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as the 20267accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * class loader of this class and invocation of 20277accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@link SecurityManager#checkPermission 20287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPermission} method with 20297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@code RuntimePermission("accessDeclaredMembers")} 20307accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * denies access to the declared method 20317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 20327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as or an 20337accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 20347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 20357accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 20367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class 20377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 20387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * </ul> 20397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 20407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.2 Class Members 20417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @jls 8.4 Method Declarations 204251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 204351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 204451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 204551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getDeclaredMethod(String name, Class<?>... parameterTypes) 204651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 2047a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getMethod(name, parameterTypes, false); 2048a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2049a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2050a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Method getMethod(String name, Class<?>[] parameterTypes, boolean recursivePublicMethods) 2051a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throws NoSuchMethodException { 2052a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (name == null) { 2053a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NullPointerException("name == null"); 2054a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2055a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parameterTypes == null) { 2056a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parameterTypes = EmptyArray.CLASS; 2057a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2058a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c : parameterTypes) { 2059a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == null) { 2060a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("parameter type is null"); 2061a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2062a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2063a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = recursivePublicMethods ? getPublicMethodRecursive(name, parameterTypes) 2064a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski : getDeclaredMethodInternal(name, parameterTypes); 2065a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Fail if we didn't find the method or it was expected to be public. 2066a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null || 2067a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski (recursivePublicMethods && !Modifier.isPublic(result.getAccessFlags()))) { 2068a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException(name + " " + Arrays.toString(parameterTypes)); 2069a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2070a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2071a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2072a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Method getPublicMethodRecursive(String name, Class<?>[] parameterTypes) { 2073a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search superclasses 2074a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = this; c != null; c = c.getSuperclass()) { 2075a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = c.getDeclaredMethodInternal(name, parameterTypes); 2076a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && Modifier.isPublic(result.getAccessFlags())) { 2077a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2078a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2079a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2080cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath 2081cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath return findInterfaceMethod(name, parameterTypes); 2082cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath } 2083cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath 2084cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath /** 2085cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath * Returns an instance method that's defined on this class or any super classes, regardless 2086cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath * of its access flags. Constructors are excluded. 2087cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath * 2088cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath * This function does not perform access checks and its semantics don't match any dex byte code 2089cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath * instruction or public reflection API. This is used by {@code MethodHandles.findVirtual} 2090cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath * which will perform access checks on the returned method. 2091cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath * 2092cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath * @hide 2093cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath */ 2094cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath public Method getInstanceMethod(String name, Class<?>[] parameterTypes) 2095cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath throws NoSuchMethodException, IllegalAccessException { 2096cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath for (Class<?> c = this; c != null; c = c.getSuperclass()) { 2097cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath Method result = c.getDeclaredMethodInternal(name, parameterTypes); 2098cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath if (result != null && !Modifier.isStatic(result.getModifiers())) { 2099cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath return result; 2100cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath } 2101cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath } 2102cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath 2103191e70e2ebae59054aa8737707bb9845b71b4820Narayan Kamath return findInterfaceMethod(name, parameterTypes); 2104cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath } 2105cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath 2106cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath private Method findInterfaceMethod(String name, Class<?>[] parameterTypes) { 2107a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 2108a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 2109d559f4f94cb0b9b4c80a3cc2040c0bd32276c225Alex Light // Search backwards so more specific interfaces are searched first. This ensures that 2110d559f4f94cb0b9b4c80a3cc2040c0bd32276c225Alex Light // the method we return is not overridden by one of it's subtypes that this class also 2111d559f4f94cb0b9b4c80a3cc2040c0bd32276c225Alex Light // implements. 2112d559f4f94cb0b9b4c80a3cc2040c0bd32276c225Alex Light for (int i = iftable.length - 2; i >= 0; i -= 2) { 2113a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) iftable[i]; 2114a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = ifc.getPublicMethodRecursive(name, parameterTypes); 2115a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && Modifier.isPublic(result.getAccessFlags())) { 2116a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2117a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2118a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 211951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2120cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath 2121a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 212251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 212351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 212451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 212551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 212651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Constructor} object that reflects the specified 212751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructor of the class or interface represented by this 212851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code parameterTypes} parameter is 212951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array of {@code Class} objects that identify the constructor's 213051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * formal parameter types, in declared order. 213151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 213251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents an inner class 213351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared in a non-static context, the formal parameter types 213451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * include the explicit enclosing instance as the first parameter. 213551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 213651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 21377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @return The {@code Constructor} object for the constructor with the 21387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * specified parameter list 21397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NoSuchMethodException if a matching method is not found. 21407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws SecurityException 21417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * If a security manager, <i>s</i>, is present and any of the 21427accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * following conditions is met: 21437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 21447accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <ul> 21457accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 21467accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as the 21477accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * class loader of this class and invocation of 21487accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@link SecurityManager#checkPermission 21497accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPermission} method with 21507accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@code RuntimePermission("accessDeclaredMembers")} 21517accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * denies access to the declared constructor 21527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 21537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <li> the caller's class loader is not the same as or an 21547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * ancestor of the class loader for the current class and 21557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * invocation of {@link SecurityManager#checkPackageAccess 21567accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * s.checkPackageAccess()} denies access to the package 21577accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * of this class 21587accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * 21597accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * </ul> 216051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 216151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 216251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 216351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 216451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 216551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 216651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getConstructor0(parameterTypes, Member.DECLARED); 216751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 216851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 216951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 217051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Finds a resource with a given name. The rules for searching resources 217151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * associated with a given class are implemented by the defining 217251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain ClassLoader class loader} of the class. This method 217351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * delegates to this object's class loader. If this object was loaded by 217451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the bootstrap class loader, the method delegates to {@link 217551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader#getSystemResourceAsStream}. 217651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 217751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Before delegation, an absolute resource name is constructed from the 217851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * given resource name using this algorithm: 217951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 218051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 218151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 218251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> If the {@code name} begins with a {@code '/'} 218351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 218451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * portion of the {@code name} following the {@code '/'}. 218551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 218651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> Otherwise, the absolute name is of the following form: 218751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 218851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 218951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code modified_package_name/name} 219051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 219151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 219251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Where the {@code modified_package_name} is the package name of this 219351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object with {@code '/'} substituted for {@code '.'} 219451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002e'</tt>). 219551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 219651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 219751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 219851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name name of the desired resource 219951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return A {@link java.io.InputStream} object or {@code null} if 220051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * no resource with this name is found 220151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException If {@code name} is {@code null} 220251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 220351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 220451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public InputStream getResourceAsStream(String name) { 220551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = resolveName(name); 2206a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader cl = getClassLoader(); 220751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cl==null) { 220851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // A system class. 220951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ClassLoader.getSystemResourceAsStream(name); 221051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 221151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return cl.getResourceAsStream(name); 221251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 221351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 221451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 221551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Finds a resource with a given name. The rules for searching resources 221651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * associated with a given class are implemented by the defining 221751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain ClassLoader class loader} of the class. This method 221851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * delegates to this object's class loader. If this object was loaded by 221951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the bootstrap class loader, the method delegates to {@link 222051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader#getSystemResource}. 222151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 222251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Before delegation, an absolute resource name is constructed from the 222351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * given resource name using this algorithm: 222451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 222551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 222651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 222751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> If the {@code name} begins with a {@code '/'} 222851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 222951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * portion of the {@code name} following the {@code '/'}. 223051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 223151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> Otherwise, the absolute name is of the following form: 223251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 223351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 223451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code modified_package_name/name} 223551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 223651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 223751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Where the {@code modified_package_name} is the package name of this 223851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object with {@code '/'} substituted for {@code '.'} 223951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002e'</tt>). 224051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 224151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 224251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 224351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name name of the desired resource 224451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return A {@link java.net.URL} object or {@code null} if no 224551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * resource with this name is found 224651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 224751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 224851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public java.net.URL getResource(String name) { 224951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = resolveName(name); 2250a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader cl = getClassLoader(); 225151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cl==null) { 225251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // A system class. 225351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ClassLoader.getSystemResource(name); 225451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 225551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return cl.getResource(name); 225651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 225751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 225851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 225951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code ProtectionDomain} of this class. If there is a 226051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * security manager installed, this method first calls the security 226151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager's {@code checkPermission} method with a 226251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code RuntimePermission("getProtectionDomain")} permission to 226351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ensure it's ok to get the 226451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code ProtectionDomain}. 226551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 226651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the ProtectionDomain of this class 226751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 226851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 226951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if a security manager exists and its 227051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code checkPermission} method doesn't allow 227151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * getting the ProtectionDomain. 227251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 227351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.security.ProtectionDomain 227451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 227551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.RuntimePermission 227651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 227751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 227851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public java.security.ProtectionDomain getProtectionDomain() { 2279a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 228051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 228151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 228251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 228351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Add a package name prefix if the name is not absolute Remove leading "/" 228451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if name is absolute 228551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 228651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String resolveName(String name) { 228751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (name == null) { 228851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 228951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 229051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (!name.startsWith("/")) { 229151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> c = this; 229251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (c.isArray()) { 229351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski c = c.getComponentType(); 229451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 229551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String baseName = c.getName(); 229651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int index = baseName.lastIndexOf('.'); 229751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (index != -1) { 229851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = baseName.substring(0, index).replace('.', '/') 229951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski +"/"+name; 230051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 230151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 230251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = name.substring(1); 230351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 230451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 230551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 230651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2307a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Constructor<T> getConstructor0(Class<?>[] parameterTypes, 2308a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int which) throws NoSuchMethodException 2309a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski { 2310a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parameterTypes == null) { 2311a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parameterTypes = EmptyArray.CLASS; 231251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2313a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c : parameterTypes) { 2314a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == null) { 2315a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("parameter type is null"); 2316a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 231751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2318a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Constructor<T> result = getDeclaredConstructorInternal(parameterTypes); 2319a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null || which == Member.PUBLIC && !Modifier.isPublic(result.getAccessFlags())) { 2320a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("<init> " + Arrays.toString(parameterTypes)); 2321a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2322a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 232351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 232451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 23257accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller /** use serialVersionUID from JDK 1.1 for interoperability */ 23267accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller private static final long serialVersionUID = 3206093459760846163L; 23277accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 23287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 23291ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 23301ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the constructor with the given parameters if it is defined by this class; 23311ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * {@code null} otherwise. This may return a non-public member. 23321ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 23331ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param args the types of the parameters to the constructor. 23341ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2335fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 23361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Constructor<T> getDeclaredConstructorInternal(Class<?>[] args); 233751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 233851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 233951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the assertion status that would be assigned to this 234051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class if it were to be initialized at the time this method is invoked. 234151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this class has had its assertion status set, the most recent 234251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * setting will be returned; otherwise, if any package default assertion 234351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * status pertains to this class, the most recent setting for the most 234451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specific pertinent package default assertion status is returned; 234551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, if this class is not a system class (i.e., it has a 234651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader) its class loader's default assertion status is returned; 234751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, the system class default assertion status is returned. 234851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 234951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Few programmers will have any need for this method; it is provided 235051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for the benefit of the JRE itself. (It allows a class to determine at 235151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the time that it is initialized whether assertions should be enabled.) 235251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method is not guaranteed to return the actual 235351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * assertion status that was (or will be) associated with the specified 235451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class when it was (or will be) initialized. 235551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 235651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the desired assertion status of the specified class. 235751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setClassAssertionStatus 235851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setPackageAssertionStatus 235951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setDefaultAssertionStatus 236051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 236151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 236251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean desiredAssertionStatus() { 2363a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 236451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 236551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 236651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 236770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * Returns the simple name of a member or local class, or {@code null} otherwise. 236870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 2369fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 237070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native String getInnerClassName(); 237170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 2372fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 237370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native int getInnerClassFlags(int defaultValue); 237470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 237570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** 237651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns true if and only if this class was declared as an enum in the 237751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code. 237851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 237951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if and only if this class was declared as an enum in the 238051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code 238151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 238251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 238351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isEnum() { 238451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // An enum must both directly extend java.lang.Enum and have 238551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // the ENUM bit set; classes for specialized enum constants 238651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // don't do the former. 238751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (this.getModifiers() & ENUM) != 0 && 238851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.getSuperclass() == java.lang.Enum.class; 238951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 239051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 239151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 239251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the elements of this enum class or null if this 239351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class object does not represent an enum type. 239451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 239551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array containing the values comprising the enum class 239651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this Class object in the order they're 239751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared, or null if this Class object does not 239851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represent an enum type 239951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 240051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 240151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public T[] getEnumConstants() { 240251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] values = getEnumConstantsShared(); 240351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (values != null) ? values.clone() : null; 240451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 240551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 240651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 240751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the elements of this enum class or null if this 240851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class object does not represent an enum type; 240951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * identical to getEnumConstants except that the result is 241051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * uncloned, cached, and shared by all callers. 241151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 241251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] getEnumConstantsShared() { 2413a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!isEnum()) return null; 2414a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (T[]) Enum.getSharedConstants((Class) this); 241551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 241651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 241751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 241851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Casts an object to the class or interface represented 241951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this {@code Class} object. 242051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 242151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param obj the object to be cast 242251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the object after casting, or null if obj is null 242351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 242451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the object is not 242551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null and is not assignable to the type T. 242651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 242751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 242851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 24297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller @SuppressWarnings("unchecked") 243051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public T cast(Object obj) { 243151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (obj != null && !isInstance(obj)) 243251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new ClassCastException(cannotCastMsg(obj)); 243351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (T) obj; 243451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 243551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 243651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String cannotCastMsg(Object obj) { 243751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "Cannot cast " + obj.getClass().getName() + " to " + getName(); 243851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 243951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 244051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 244151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Casts this {@code Class} object to represent a subclass of the class 24427accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * represented by the specified class object. Checks that the cast 244351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is valid, and throws a {@code ClassCastException} if it is not. If 244451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method succeeds, it always returns a reference to this class object. 244551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 244651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This method is useful when a client needs to "narrow" the type of 244751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a {@code Class} object to pass it to an API that restricts the 244851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that it is willing to accept. A cast would 244951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * generate a compile-time warning, as the correctness of the cast 245051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * could not be checked at runtime (because generic types are implemented 245151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by erasure). 245251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 24537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @param <U> the type to cast this class object to 24547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @param clazz the class of the type to cast this class object to 245551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return this {@code Class} object, cast to represent a subclass of 245651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified class object. 245751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if this {@code Class} object does not 245851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represent a subclass of the specified class (here "subclass" includes 245951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class itself). 246051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 246151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 24627accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller @SuppressWarnings("unchecked") 246351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public <U> Class<? extends U> asSubclass(Class<U> clazz) { 246451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (clazz.isAssignableFrom(this)) 246551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (Class<? extends U>) this; 246651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 246787778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak throw new ClassCastException(this.toString() + 246887778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak " cannot be cast to " + clazz.getName()); 246951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 247051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 247151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 247251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException {@inheritDoc} 247351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 247451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 24757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller @SuppressWarnings("unchecked") 247651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public <A extends Annotation> A getAnnotation(Class<A> annotationClass) { 24777accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller Objects.requireNonNull(annotationClass); 247851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 247970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak A annotation = getDeclaredAnnotation(annotationClass); 248070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotation != null) { 248170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return annotation; 248270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 248370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 248470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) { 248570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) { 248670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak annotation = sup.getDeclaredAnnotation(annotationClass); 248770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (annotation != null) { 248870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return annotation; 248970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 249070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 249170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 249270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 249370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return null; 249470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 249570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 24967accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller /** 24977accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * {@inheritDoc} 24987accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NullPointerException {@inheritDoc} 24997accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @since 1.5 25007accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller */ 25017accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller @Override 25027accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { 25037accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (annotationClass == null) { 25047accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller throw new NullPointerException("annotationClass == null"); 250570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 250670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 25077accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (isDeclaredAnnotationPresent(annotationClass)) { 250870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return true; 250970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 251070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 25117accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) { 251270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) { 25137accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (sup.isDeclaredAnnotationPresent(annotationClass)) { 251470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return true; 251570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 251670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 251770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 251870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak 251970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return false; 252051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 252151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 252251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 25237accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NullPointerException {@inheritDoc} 25247accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @since 1.8 252551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 25267accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller @Override 25277accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) { 25287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller // Find any associated annotations [directly or repeatably (indirectly) present on this]. 25297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller A[] annotations = GenericDeclaration.super.getAnnotationsByType(annotationClass); 25307accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 25317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (annotations.length != 0) { 25327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller return annotations; 25337accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } 25347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 25357accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller // Nothing was found, attempt looking for associated annotations recursively up to the root 25367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller // class if and only if: 25377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller // * The annotation class was marked with @Inherited. 25387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller // 25397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller // Inherited annotations are not coalesced into a single set: the first declaration found is 25407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller // returned. 25417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 25427accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) { 25437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller Class<?> superClass = getSuperclass(); // Returns null if klass's base is Object. 25447accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 25457accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller if (superClass != null) { 25467accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller return superClass.getAnnotationsByType(annotationClass); 25477accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } 25487accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } 25497accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 25507accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller // Annotated was not marked with @Inherited, or no superclass. 25517accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller return (A[]) Array.newInstance(annotationClass, 0); // Safe by construction. 25527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller } 25537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller 25547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller /** 25557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @since 1.5 25567accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller */ 25577accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller @Override 25587accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller public Annotation[] getAnnotations() { 255970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /* 256070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * We need to get the annotations declared on this class, plus the 256170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * annotations from superclasses that have the "@Inherited" annotation 256270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * set. We create a temporary map to use while we accumulate the 256370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * annotations and convert it to an array at the end. 256470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 256570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * It's possible to have duplicates when annotations are inherited. 256670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * We use a Map to filter those out. 256770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * 256870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * HashMap might be overkill here. 256970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak */ 257070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak HashMap<Class<?>, Annotation> map = new HashMap<Class<?>, Annotation>(); 257170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Annotation declaredAnnotation : getDeclaredAnnotations()) { 257270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak map.put(declaredAnnotation.annotationType(), declaredAnnotation); 257370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 257470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) { 257570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak for (Annotation declaredAnnotation : sup.getDeclaredAnnotations()) { 257670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak Class<? extends Annotation> clazz = declaredAnnotation.annotationType(); 257770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak if (!map.containsKey(clazz) && clazz.isDeclaredAnnotationPresent(Inherited.class)) { 257870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak map.put(clazz, declaredAnnotation); 257970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 258070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 258170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak } 258251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 258370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /* Convert annotation values from HashMap to array. */ 258470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak Collection<Annotation> coll = map.values(); 258570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak return coll.toArray(new Annotation[coll.size()]); 258651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 258751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 258870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak /** 25897accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @throws NullPointerException {@inheritDoc} 25909599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin * @since 1.8 259151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 25929599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin @Override 2593fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 25947accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller public native <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass); 25959599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin 25969599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin /** 25977accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * @since 1.5 25989599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin */ 25999599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin @Override 2600fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 26017accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller public native Annotation[] getDeclaredAnnotations(); 260251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 260351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 260470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak * Returns true if the annotation exists. 260551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2606fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 260770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak private native boolean isDeclaredAnnotationPresent(Class<? extends Annotation> annotationClass); 260851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2609d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao private String getSignatureAttribute() { 2610d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao String[] annotation = getSignatureAnnotation(); 2611d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao if (annotation == null) { 2612d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao return null; 2613d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao } 2614d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao StringBuilder result = new StringBuilder(); 2615d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao for (String s : annotation) { 2616d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao result.append(s); 2617d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao } 2618d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao return result.toString(); 2619d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao } 2620d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao 2621fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 2622d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao private native String[] getSignatureAnnotation(); 2623d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao 2624a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2625a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Is this a runtime created proxy class? 2626a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2627a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2628a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2629a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isProxy() { 2630a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (accessFlags & 0x00040000) != 0; 2631a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2632a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2633a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2634a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2635a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2636a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getAccessFlags() { 2637a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return accessFlags; 2638a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2639a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 26401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 26411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 26421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the method if it is defined by this class; {@code null} otherwise. This may return a 26431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * non-public member. 26441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 26451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param name the method name 26461ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param args the method's parameter types 26471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2648fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 26491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Method getDeclaredMethodInternal(String name, Class<?>[] args); 2650a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2651a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static class Caches { 2652a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2653a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Cache to avoid frequent recalculation of generic interfaces, which is generally uncommon. 2654a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Sized sufficient to allow ConcurrentHashMapTest to run without recalculating its generic 2655a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * interfaces (required to avoid time outs). Validated by running reflection heavy code 2656a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * such as applications using Guice-like frameworks. 2657a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2658a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static final BasicLruCache<Class, Type[]> genericInterfaces 2659a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski = new BasicLruCache<Class, Type[]>(8); 2660a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 266151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 2662