Class.java revision 191e70e2ebae59054aa8737707bb9845b71b4820
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
2920938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport com.android.dex.Dex;
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
5920938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport dalvik.system.VMStack;
6020938c5ed2bc8f5de8047a47caddb146f730868fNeil Fullerimport sun.reflect.CallerSensitive;
61a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/**
6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Instances of the class {@code Class} represent classes and
6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces in a running Java application.  An enum is a kind of
6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class and an annotation is a kind of interface.  Every array also
6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * belongs to a class that is reflected as a {@code Class} object
6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that is shared by all arrays with the same element type and number
6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of dimensions.  The primitive Java types ({@code boolean},
6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code byte}, {@code char}, {@code short},
7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int}, {@code long}, {@code float}, and
7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code double}), and the keyword {@code void} are also
7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented as {@code Class} objects.
7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> {@code Class} has no public constructor. Instead {@code Class}
7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects are constructed automatically by the Java Virtual Machine as classes
7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are loaded and by calls to the {@code defineClass} method in the class
7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader.
7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The following example uses a {@code Class} object to print the
8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class name of an object:
8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
827accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <blockquote><pre>
8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *     void printClassName(Object obj) {
8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         System.out.println("The class of " + obj +
8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *                            " is " + obj.getClass().getName());
8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *     }
8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote>
8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> It is also possible to get the {@code Class} object for a named
9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type (or for void) using a class literal.  See Section 15.8.2 of
9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java&trade; Language Specification</cite>.
9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For example:
9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
947accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller * <blockquote>
9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote>
9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param <T> the type of the class modeled by this {@code Class}
9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object.  For example, the type of {@code String.class} is {@code
10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class<String>}.  Use {@code Class<?>} if the class being modeled is
10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * unknown.
10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author  unascribed
10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since   JDK1.0
10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
1077accc5fb09911a1dcc027f48089845f617f2912eNeil Fullerpublic final class Class<T> implements java.io.Serializable,
1087accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                              GenericDeclaration,
1097accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                              Type,
1107accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                              AnnotatedElement {
1117accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    private static final int ANNOTATION= 0x00002000;
1127accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    private static final int ENUM      = 0x00004000;
1137accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    private static final int SYNTHETIC = 0x00001000;
1147accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    private static final int FINALIZABLE = 0x80000000;
1151ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
1161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /** defining class loader, or null for the "bootstrap" system loader. */
1171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient ClassLoader classLoader;
1181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
1191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
1201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * For array classes, the component class object for instanceof/checkcast (for String[][][],
1211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * this will be String[][]). null for non-array classes.
1221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
1231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient Class<?> componentType;
1241ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
1251ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * DexCache of resolved constant pool entries. Will be null for certain runtime-generated classes
1261ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * e.g. arrays and primitive classes.
1271ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
1281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient DexCache dexCache;
1291ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
1301ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
1311ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * The interface table (iftable_) contains pairs of a interface class and an array of the
1321ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * interface methods. There is one pair per interface supported by this class.  That
1331ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * means one pair for each interface we support directly, indirectly via superclass, or
1341ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * indirectly via a superinterface.  This will be null if neither we nor our superclass
1351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * implement any interfaces.
1361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     *
1371ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()".
1381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Invoke faceObj.blah(), where "blah" is part of the Face interface.  We can't easily use a
1391ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * single vtable.
1401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     *
1411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * For every interface a concrete class implements, we create an array of the concrete vtable_
1421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * methods for the methods in the interface.
1431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
1441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient Object[] ifTable;
1451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
1461ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /** Lazily computed name of this class; always prefer calling getName(). */
1471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient String name;
1481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
1491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /** The superclass, or null if this is java.lang.Object, an interface or primitive type. */
1501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient Class<? super T> superClass;
1511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
15270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    /**
15370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak     * If class verify fails, we must return same error on subsequent tries. We may store either
15470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak     * the class of the error, or an actual instance of Throwable here.
15570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak     */
15670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    private transient Object verifyError;
1571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
1581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
1591ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass
1601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * is copied in, and virtual methods from our class either replace those from the super or are
1611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * appended. For abstract classes, methods may be created in the vtable that aren't in
1621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * virtual_ methods_ for miranda methods.
1631ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
1641ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient Object vtable;
1651ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
16670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    /** Short-cut to dexCache.strings */
16770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    private transient long dexCacheStrings;
16870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak
1691ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /** access flags; low 16 bits are defined by VM spec */
1701ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient int accessFlags;
1711ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
1721ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
1731ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Instance fields. These describe the layout of the contents of an Object. Note that only the
1741ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * fields directly declared by this class are listed in iFields; fields declared by a
1751ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * superclass are listed in the superclass's Class.iFields.
1761ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     *
1771ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * All instance fields that refer to objects are guaranteed to be at the beginning of the field
1781ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * list.  {@link Class#numReferenceInstanceFields} specifies the number of reference fields.
1791ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
1801ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient long iFields;
1811ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
182551112f15a648b739f842b2466b5fdae3e80d6deAlex Light    /** All methods with this class as the base for virtual dispatch. */
183551112f15a648b739f842b2466b5fdae3e80d6deAlex Light    private transient long methods;
184551112f15a648b739f842b2466b5fdae3e80d6deAlex Light
1851ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /** Static fields */
1861ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient long sFields;
1871ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
18870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    /** Class flags to help the GC with object scanning. */
18970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    private transient int classFlags;
19070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak
1911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
1921ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Total size of the Class instance; used when allocating storage on GC heap.
1931ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * See also {@link Class#objectSize}.
1941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
1951ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient int classSize;
1961ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
1971ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
1981ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * tid used to check for recursive static initializer invocation.
1991ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
2001ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient int clinitThreadId;
2011ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
2021ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
2031ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Class def index from dex file. An index of 65535 indicates that there is no class definition,
2041ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * for example for an array type.
2051ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * TODO: really 16bits as type indices are 16bit.
2061ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
2071ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient int dexClassDefIndex;
2081ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
2091ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
2101ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Class type index from dex file, lazily computed. An index of 65535 indicates that the type
2111ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * index isn't known. Volatile to avoid double-checked locking bugs.
2121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * TODO: really 16bits as type indices are 16bit.
2131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
2141ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient volatile int dexTypeIndex;
2151ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
2161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /** Number of instance fields that are object references. */
2171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient int numReferenceInstanceFields;
2181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
2191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /** Number of static fields that are object references. */
2201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient int numReferenceStaticFields;
2211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
2221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
2231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Total object size; used when allocating storage on GC heap. For interfaces and abstract
2241ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * classes this will be zero. See also {@link Class#classSize}.
2251ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
2261ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient int objectSize;
2271ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
2281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
229265a876206ebe30470777d444d7fc5087087913aMathieu Chartier     * Aligned object size for allocation fast path. The value is max int if the object is
230265a876206ebe30470777d444d7fc5087087913aMathieu Chartier     * uninitialized or finalizable, otherwise the aligned object size.
231265a876206ebe30470777d444d7fc5087087913aMathieu Chartier     */
232265a876206ebe30470777d444d7fc5087087913aMathieu Chartier    private transient int objectSizeAllocFastPath;
233265a876206ebe30470777d444d7fc5087087913aMathieu Chartier
234265a876206ebe30470777d444d7fc5087087913aMathieu Chartier    /**
2351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * The lower 16 bits is the primitive type value, or 0 if not a primitive type; set for
2361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * generated primitive classes.
2371ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
2381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient int primitiveType;
2391ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
2401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /** Bitmap of offsets of iFields. */
2411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient int referenceInstanceOffsets;
2421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
2431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /** State of class initialization */
2441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private transient int status;
2451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
246551112f15a648b739f842b2466b5fdae3e80d6deAlex Light    /** Offset of the first virtual method copied from an interface in the methods array. */
247551112f15a648b739f842b2466b5fdae3e80d6deAlex Light    private transient short copiedMethodsOffset;
248551112f15a648b739f842b2466b5fdae3e80d6deAlex Light
249551112f15a648b739f842b2466b5fdae3e80d6deAlex Light    /** Offset of the first virtual method defined in this class in the methods array. */
250551112f15a648b739f842b2466b5fdae3e80d6deAlex Light    private transient short virtualMethodsOffset;
251551112f15a648b739f842b2466b5fdae3e80d6deAlex Light
25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
2537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * Private constructor. Only the Java Virtual Machine creates Class objects.
2547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * This constructor is not used and prevents the default constructor being
2557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * generated.
25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private Class() {}
25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Converts the object to a string. The string representation is the
26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * string "class" or "interface", followed by a space, and then by the
26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * fully qualified name of the class in the format returned by
26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code getName}.  If this {@code Class} object represents a
26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * primitive type, this method returns the name of the primitive type.  If
26651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this {@code Class} object represents void this method returns
26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * "void".
26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of this class object.
27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public String toString() {
27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            + getName();
27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    /**
2777accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * Returns a string describing this {@code Class}, including
2787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * information about modifiers and type parameters.
2797accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
2807accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * The string is formatted as a list of type modifiers, if any,
2817accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * followed by the kind of type (empty string for primitive types
2827accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * and {@code class}, {@code enum}, {@code interface}, or
2837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <code>&#64;</code>{@code interface}, as appropriate), followed
2847accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * by the type's name, followed by an angle-bracketed
2857accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * comma-separated list of the type's type parameters, if any.
2867accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
2877accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * A space is used to separate modifiers from one another and to
2887accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * separate any modifiers from the kind of type. The modifiers
2897accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * occur in canonical order. If there are no type parameters, the
2907accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * type parameter list is elided.
2917accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
2927accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p>Note that since information about the runtime representation
2937accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * of a type is being generated, modifiers not present on the
2947accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * originating source code or illegal on the originating source
2957accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * code may be present.
2967accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
2977accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @return a string describing this {@code Class}, including
2987accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * information about modifiers and type parameters
2997accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
3007accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @since 1.8
3017accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     */
3027accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    public String toGenericString() {
3037accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller        if (isPrimitive()) {
3047accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            return toString();
3057accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller        } else {
3067accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            StringBuilder sb = new StringBuilder();
3077accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
3087accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            // Class modifiers are a superset of interface modifiers
3097accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            int modifiers = getModifiers() & Modifier.classModifiers();
3107accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            if (modifiers != 0) {
3117accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                sb.append(Modifier.toString(modifiers));
3127accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                sb.append(' ');
3137accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            }
3147accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
3157accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            if (isAnnotation()) {
3167accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                sb.append('@');
3177accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            }
3187accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            if (isInterface()) { // Note: all annotation types are interfaces
3197accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                sb.append("interface");
3207accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            } else {
3217accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                if (isEnum())
3227accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                    sb.append("enum");
3237accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                else
3247accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                    sb.append("class");
3257accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            }
3267accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            sb.append(' ');
3277accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            sb.append(getName());
3287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
3297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            TypeVariable<?>[] typeparms = getTypeParameters();
3307accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            if (typeparms.length > 0) {
3317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                boolean first = true;
3327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                sb.append('<');
3337accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                for(TypeVariable<?> typeparm: typeparms) {
3347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                    if (!first)
3357accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                        sb.append(',');
3367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                    sb.append(typeparm.getTypeName());
3377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                    first = false;
3387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                }
3397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                sb.append('>');
3407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            }
3417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
3427accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            return sb.toString();
3437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller        }
3447accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    }
34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the {@code Class} object associated with the class or
34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interface with the given string name.  Invoking this method is
34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent to:
35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <blockquote>
35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *  {@code Class.forName(className, true, currentLoader)}
35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </blockquote>
35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * where {@code currentLoader} denotes the defining class loader of
35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the current class.
35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> For example, the following code fragment returns the
35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * runtime {@code Class} descriptor for the class named
36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code java.lang.Thread}:
36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <blockquote>
36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *   {@code Class t = Class.forName("java.lang.Thread")}
36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </blockquote>
36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>
36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * A call to {@code forName("X")} causes the class named
36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code X} to be initialized.
36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param      className   the fully qualified name of the desired class.
37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return     the {@code Class} object for the class with the
37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *             specified name.
37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception LinkageError if the linkage fails
37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception ExceptionInInitializerError if the initialization provoked
37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *            by this method fails
37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception ClassNotFoundException if the class cannot be located
37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @CallerSensitive
37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static Class<?> forName(String className)
37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                throws ClassNotFoundException {
380a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return forName(className, true, VMStack.getCallingClassLoader());
38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the {@code Class} object associated with the class or
38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interface with the given string name, using the given class loader.
38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Given the fully qualified name for a class or interface (in the same
38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * format returned by {@code getName}) this method attempts to
38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * locate, load, and link the class or interface.  The specified class
39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * loader is used to load the class or interface.  If the parameter
39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code loader} is null, the class is loaded through the bootstrap
39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class loader.  The class is initialized only if the
39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code initialize} parameter is {@code true} and if it has
39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not been initialized earlier.
39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> If {@code name} denotes a primitive type or void, an attempt
39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * will be made to locate a user-defined class in the unnamed package whose
39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * name is {@code name}. Therefore, this method cannot be used to
39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtain any of the {@code Class} objects representing primitive
40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * types or void.
40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> If {@code name} denotes an array class, the component type of
40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the array class is loaded but not initialized.
40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> For example, in an instance method the expression:
40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <blockquote>
40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *  {@code Class.forName("Foo")}
40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </blockquote>
41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is equivalent to:
41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <blockquote>
41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *  {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </blockquote>
41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Note that this method throws errors related to loading, linking or
41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Java Language Specification</em>.
42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Note that this method does not check whether the requested class
42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is accessible to its caller.
42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> If the {@code loader} is {@code null}, and a security
42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * manager is present, and the caller's class loader is not null, then this
42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method calls the security manager's {@code checkPermission} method
42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * with a {@code RuntimePermission("getClassLoader")} permission to
42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ensure it's ok to access the bootstrap class loader.
42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param name       fully qualified name of the desired class
4307accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @param initialize if {@code true} the class will be initialized.
4317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *                   See Section 12.4 of <em>The Java Language Specification</em>.
43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param loader     class loader from which the class must be loaded
43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return           class object representing the desired class
43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception LinkageError if the linkage fails
43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception ExceptionInInitializerError if the initialization provoked
43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *            by this method fails
43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception ClassNotFoundException if the class cannot be located by
43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *            the specified class loader
44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see       java.lang.Class#forName(String)
44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see       java.lang.ClassLoader
44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since     1.2
44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
44551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @CallerSensitive
44651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static Class<?> forName(String name, boolean initialize,
44751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   ClassLoader loader)
44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws ClassNotFoundException
44951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    {
45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (loader == null) {
451bac8ef10e8a32316e4eedc6163dd3776cf5e5d7eNarayan Kamath            loader = BootClassLoader.getInstance();
452a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
453a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        Class<?> result;
454a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        try {
455a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            result = classForName(name, initialize, loader);
456a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        } catch (ClassNotFoundException e) {
457a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            Throwable cause = e.getCause();
458a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            if (cause instanceof LinkageError) {
459a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                throw (LinkageError) cause;
46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
461a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            throw e;
46251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
463a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return result;
46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
46551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
46651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /** Called after security checks have been made. */
467a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    static native Class<?> classForName(String className, boolean shouldInitialize,
468a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            ClassLoader classLoader) throws ClassNotFoundException;
46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Creates a new instance of the class represented by this {@code Class}
47251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * object.  The class is instantiated as if by a {@code new}
47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * expression with an empty argument list.  The class is initialized if it
47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * has not already been initialized.
47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Note that this method propagates any exception thrown by the
47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * nullary constructor, including a checked exception.  Use of
47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this method effectively bypasses the compile-time exception
47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * checking that would otherwise be performed by the compiler.
48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The {@link
48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Constructor.newInstance} method avoids this problem by wrapping
48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * any exception thrown by the constructor in a (checked) {@link
48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * java.lang.reflect.InvocationTargetException}.
48551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
4867accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @return  a newly allocated instance of the class represented by this
4877accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          object.
4887accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  IllegalAccessException  if the class or its nullary
4897accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          constructor is not accessible.
4907accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  InstantiationException
4917accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          if this {@code Class} represents an abstract class,
4927accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          an interface, an array class, a primitive type, or void;
4937accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          or if the class has no nullary constructor;
4947accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          or if the instantiation fails for some other reason.
4957accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  ExceptionInInitializerError if the initialization
4967accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          provoked by this method fails.
4977accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  SecurityException
4987accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          If a security manager, <i>s</i>, is present and
4997accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          the caller's class loader is not the same as or an
5007accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          ancestor of the class loader for the current class and
5017accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          invocation of {@link SecurityManager#checkPackageAccess
5027accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPackageAccess()} denies access to the package
5037accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          of this class.
50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
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&trade; 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> &nbsp;&nbsp;&nbsp; <th> Encoding
71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tr><td> boolean      <td> &nbsp;&nbsp;&nbsp; <td align=center> Z
71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tr><td> byte         <td> &nbsp;&nbsp;&nbsp; <td align=center> B
71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tr><td> char         <td> &nbsp;&nbsp;&nbsp; <td align=center> C
71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tr><td> class or interface
71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *                       <td> &nbsp;&nbsp;&nbsp; <td align=center> L<i>classname</i>;
71651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tr><td> double       <td> &nbsp;&nbsp;&nbsp; <td align=center> D
71751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tr><td> float        <td> &nbsp;&nbsp;&nbsp; <td align=center> F
71851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tr><td> int          <td> &nbsp;&nbsp;&nbsp; <td align=center> I
71951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tr><td> long         <td> &nbsp;&nbsp;&nbsp; <td align=center> J
72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tr><td> short        <td> &nbsp;&nbsp;&nbsp; <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
748a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    private native String getNameNative();
74951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
75051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
75151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the class loader for the class.  Some implementations may use
75251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * null to represent the bootstrap class loader. This method will return
75351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * null in such implementations if this class was loaded by the bootstrap
75451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class loader.
75551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
75651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> If a security manager is present, and the caller's class loader is
75751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not null and the caller's class loader is not the same as or an ancestor of
75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the class loader for the class whose class loader is requested, then
75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this method calls the security manager's {@code checkPermission}
76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method with a {@code RuntimePermission("getClassLoader")}
76151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * permission to ensure it's ok to access the class loader for the class.
76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If this object
76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * represents a primitive type or void, null is returned.
76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the class loader that loaded the class or interface
76751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          represented by this object.
76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws SecurityException
76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    if a security manager exists and its
77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    {@code checkPermission} method denies
77151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    access to the class loader for the class.
77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see java.lang.ClassLoader
77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see SecurityManager#checkPermission
77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see java.lang.RuntimePermission
77551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
77651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public ClassLoader getClassLoader() {
777a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (isPrimitive()) {
77851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return null;
77951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
780a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return (classLoader == null) ? BootClassLoader.getInstance() : classLoader;
78151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
78351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns an array of {@code TypeVariable} objects that represent the
78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * type variables declared by the generic declaration represented by this
78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code GenericDeclaration} object, in declaration order.  Returns an
78751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array of length 0 if the underlying generic declaration declares no type
78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * variables.
78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return an array of {@code TypeVariable} objects that represent
79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     the type variables declared by this generic declaration
79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws java.lang.reflect.GenericSignatureFormatError if the generic
79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     signature of this generic declaration does not conform to
79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     the format specified in
79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     <cite>The Java&trade; Virtual Machine Specification</cite>
79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
7987accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    @Override
7997accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    public synchronized TypeVariable<Class<T>>[] getTypeParameters() {
800d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao        String annotationSignature = getSignatureAttribute();
801a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (annotationSignature == null) {
802a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return EmptyArray.TYPE_VARIABLE;
803a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
804a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        GenericSignatureParser parser = new GenericSignatureParser(getClassLoader());
805a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        parser.parseForClass(this, annotationSignature);
806a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return parser.formalTypeParameters;
80751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
80851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
8097accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
81051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
81151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the {@code Class} representing the superclass of the entity
81251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (class, interface, primitive type or void) represented by this
81351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class}.  If this {@code Class} represents either the
81451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Object} class, an interface, a primitive type, or void, then
81551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * null is returned.  If this object represents an array class then the
81651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class} object representing the {@code Object} class is
81751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returned.
81851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
81951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the superclass of the class represented by this object.
82051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
821a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public Class<? super T> getSuperclass() {
822a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // For interfaces superClass is Object (which agrees with the JNI spec)
823a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // but not with the expected behavior here.
824a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (isInterface()) {
825a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return null;
826a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        } else {
827a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return superClass;
828a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
829a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
83051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
83151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
83251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the {@code Type} representing the direct superclass of
83351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the entity (class, interface, primitive type or void) represented by
83451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this {@code Class}.
83551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
83651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If the superclass is a parameterized type, the {@code Type}
83751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * object returned must accurately reflect the actual type
83851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * parameters used in the source code. The parameterized type
83951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * representing the superclass is created if it had not been
84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * created before. See the declaration of {@link
84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * java.lang.reflect.ParameterizedType ParameterizedType} for the
84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * semantics of the creation process for parameterized types.  If
84351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this {@code Class} represents either the {@code Object}
84451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class, an interface, a primitive type, or void, then null is
84551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returned.  If this object represents an array class then the
84651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class} object representing the {@code Object} class is
84751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returned.
84851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
84951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws java.lang.reflect.GenericSignatureFormatError if the generic
85051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     class signature does not conform to the format specified in
85151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     <cite>The Java&trade; Virtual Machine Specification</cite>
85251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws TypeNotPresentException if the generic superclass
85351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     refers to a non-existent type declaration
85451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws java.lang.reflect.MalformedParameterizedTypeException if the
85551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     generic superclass refers to a parameterized type that cannot be
85651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     instantiated  for any reason
85751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the superclass of the class represented by this object
85851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
85951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
86051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Type getGenericSuperclass() {
861a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        Type genericSuperclass = getSuperclass();
862a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // This method is specified to return null for all cases where getSuperclass
863a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // returns null, i.e, for primitives, interfaces, void and java.lang.Object.
864a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (genericSuperclass == null) {
865a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return null;
866a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
867a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
868d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao        String annotationSignature = getSignatureAttribute();
869a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (annotationSignature != null) {
870a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            GenericSignatureParser parser = new GenericSignatureParser(getClassLoader());
871a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            parser.parseForClass(this, annotationSignature);
872a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            genericSuperclass = parser.superclassType;
873a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
874a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return Types.getType(genericSuperclass);
87551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
87651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
87751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
87851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Gets the package for this class.  The class loader of this class is used
87951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to find the package.  If the class was loaded by the bootstrap class
88051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * loader the set of packages loaded from CLASSPATH is searched to find the
88151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * package of the class. Null is returned if no package object was created
88251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the class loader of this class.
88351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
88451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> Packages have attributes for versions and specifications only if the
88551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * information was defined in the manifests that accompany the classes, and
88651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * if the class loader created the package instance with the attributes
88751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * from the manifest.
88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the package of the class, or null if no package
89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         information is available from the archive or codebase.
89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Package getPackage() {
893a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        ClassLoader loader = getClassLoader();
894a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (loader != null) {
895a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            String packageName = getPackageName$();
896a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return packageName != null ? loader.getPackage(packageName) : null;
897a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
898a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return null;
899a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
900a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
901a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    /**
902a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * Returns the package name of this class. This returns null for classes in
903a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * the default package.
904a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     *
905a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * @hide
906a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     */
907a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public String getPackageName$() {
908a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        String name = getName();
909a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        int last = name.lastIndexOf('.');
910a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return last == -1 ? null : name.substring(0, last);
91151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
91251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
91351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
91451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
91551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Determines the interfaces implemented by the class or interface
91651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * represented by this object.
91751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
91851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> If this object represents a class, the return value is an array
91951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * containing objects representing all interfaces implemented by the
92051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class. The order of the interface objects in the array corresponds to
92151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the order of the interface names in the {@code implements} clause
92251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the declaration of the class represented by this object. For
92351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * example, given the declaration:
92451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <blockquote>
92551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
92651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </blockquote>
92751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * suppose the value of {@code s} is an instance of
92851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Shimmer}; the value of the expression:
92951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <blockquote>
93051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code s.getClass().getInterfaces()[0]}
93151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </blockquote>
93251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is the {@code Class} object that represents interface
93351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code FloorWax}; and the value of:
93451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <blockquote>
93551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code s.getClass().getInterfaces()[1]}
93651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </blockquote>
93751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is the {@code Class} object that represents interface
93851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code DessertTopping}.
93951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
94051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> If this object represents an interface, the array contains objects
94151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * representing all interfaces extended by the interface. The order of the
94251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interface objects in the array corresponds to the order of the interface
94351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * names in the {@code extends} clause of the declaration of the
94451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interface represented by this object.
94551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
94651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> If this object represents a class or interface that implements no
94751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interfaces, the method returns an array of length 0.
94851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
94951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> If this object represents a primitive type or void, the method
95051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returns an array of length 0.
95151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
9527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents an array type, the
9537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * interfaces {@code Cloneable} and {@code java.io.Serializable} are
9547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * returned in that order.
9557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
95651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return an array of interfaces implemented by this class.
95751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
958a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public Class<?>[] getInterfaces() {
959a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (isArray()) {
960a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return new Class<?>[] { Cloneable.class, Serializable.class };
961a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        } else if (isProxy()) {
962a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return getProxyInterfaces();
963a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
964a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        Dex dex = getDex();
965a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (dex == null) {
966a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return EmptyArray.CLASS;
967a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
968a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        short[] interfaces = dex.interfaceTypeIndicesFromClassDefIndex(dexClassDefIndex);
969a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        Class<?>[] result = new Class<?>[interfaces.length];
970a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        for (int i = 0; i < interfaces.length; i++) {
971a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            result[i] = getDexCacheType(dex, interfaces[i]);
972a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
973a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return result;
974a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
975a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
976a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    // Returns the interfaces that this proxy class directly implements.
977a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    private native Class<?>[] getProxyInterfaces();
97851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
97951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
98051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the {@code Type}s representing the interfaces
98151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * directly implemented by the class or interface represented by
98251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this object.
98351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
98451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If a superinterface is a parameterized type, the
98551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Type} object returned for it must accurately reflect
98651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the actual type parameters used in the source code. The
98751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * parameterized type representing each superinterface is created
98851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * if it had not been created before. See the declaration of
98951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link java.lang.reflect.ParameterizedType ParameterizedType}
99051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * for the semantics of the creation process for parameterized
99151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * types.
99251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
99351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> If this object represents a class, the return value is an
99451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array containing objects representing all interfaces
99551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implemented by the class. The order of the interface objects in
99651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the array corresponds to the order of the interface names in
99751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the {@code implements} clause of the declaration of the class
99851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * represented by this object.  In the case of an array class, the
99951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interfaces {@code Cloneable} and {@code Serializable} are
100051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returned in that order.
100151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
100251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If this object represents an interface, the array contains
100351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * objects representing all interfaces directly extended by the
100451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interface.  The order of the interface objects in the array
100551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * corresponds to the order of the interface names in the
100651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code extends} clause of the declaration of the interface
100751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * represented by this object.
100851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
100951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If this object represents a class or interface that
101051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implements no interfaces, the method returns an array of length
101151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * 0.
101251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
101351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If this object represents a primitive type or void, the
101451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method returns an array of length 0.
101551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
101651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws java.lang.reflect.GenericSignatureFormatError
101751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if the generic class signature does not conform to the format
101851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     specified in
101951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     <cite>The Java&trade; Virtual Machine Specification</cite>
102051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws TypeNotPresentException if any of the generic
102151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     superinterfaces refers to a non-existent type declaration
102251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws java.lang.reflect.MalformedParameterizedTypeException
102351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if any of the generic superinterfaces refer to a parameterized
102451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     type that cannot be instantiated for any reason
102551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return an array of interfaces implemented by this class
102651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
102751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
102851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Type[] getGenericInterfaces() {
1029a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        Type[] result;
1030a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        synchronized (Caches.genericInterfaces) {
1031a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            result = Caches.genericInterfaces.get(this);
1032a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            if (result == null) {
1033d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao                String annotationSignature = getSignatureAttribute();
1034a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                if (annotationSignature == null) {
1035a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                    result = getInterfaces();
1036a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                } else {
1037a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                    GenericSignatureParser parser = new GenericSignatureParser(getClassLoader());
1038a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                    parser.parseForClass(this, annotationSignature);
1039a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                    result = Types.getTypeArray(parser.interfaceTypes, false);
1040a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                }
1041a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                Caches.genericInterfaces.put(this, result);
1042a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            }
1043a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
1044a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return (result.length == 0) ? result : result.clone();
104551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
104651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
104751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
104851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
104951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the {@code Class} representing the component type of an
105051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array.  If this class does not represent an array class this method
105151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returns null.
105251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
105351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the {@code Class} representing the component type of this
105451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class if this class is an array
105551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see     java.lang.reflect.Array
105651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
105751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
1058a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public Class<?> getComponentType() {
1059a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski      return componentType;
1060a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
106151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
106251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
106351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the Java language modifiers for this class or interface, encoded
106451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * in an integer. The modifiers consist of the Java Virtual Machine's
106551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * constants for {@code public}, {@code protected},
106651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code private}, {@code final}, {@code static},
106751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code abstract} and {@code interface}; they should be decoded
106851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * using the methods of class {@code Modifier}.
106951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
107051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> If the underlying class is an array class, then its
107151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code public}, {@code private} and {@code protected}
107251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * modifiers are the same as those of its component type.  If this
107351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class} represents a primitive type or void, its
107451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code public} modifier is always {@code true}, and its
107551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code protected} and {@code private} modifiers are always
107651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code false}. If this object represents an array class, a
107751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * primitive type or void, then its {@code final} modifier is always
107851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code true} and its interface modifier is always
107951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code false}. The values of its other modifiers are not determined
108051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by this specification.
108151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
108251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> The modifier encodings are defined in <em>The Java Virtual Machine
108351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Specification</em>, table 4.1.
108451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
108551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the {@code int} representing the modifiers for this class
108651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see     java.lang.reflect.Modifier
108751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
108851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
1089a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public int getModifiers() {
1090a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // Array classes inherit modifiers from their component types, but in the case of arrays
1091a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // of an inner class, the class file may contain "fake" access flags because it's not valid
1092a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // for a top-level class to private, say. The real access flags are stored in the InnerClass
1093a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // attribute, so we need to make sure we drill down to the inner class: the accessFlags
1094a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // field is not the value we want to return, and the synthesized array class does not itself
1095a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // have an InnerClass attribute. https://code.google.com/p/android/issues/detail?id=56267
1096a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (isArray()) {
1097a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            int componentModifiers = getComponentType().getModifiers();
1098a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            if ((componentModifiers & Modifier.INTERFACE) != 0) {
1099a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                componentModifiers &= ~(Modifier.INTERFACE | Modifier.STATIC);
1100a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            }
1101a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return Modifier.ABSTRACT | Modifier.FINAL | componentModifiers;
1102a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
1103a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        int JAVA_FLAGS_MASK = 0xffff;
1104d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao        int modifiers = this.getInnerClassFlags(accessFlags & JAVA_FLAGS_MASK);
1105a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return modifiers & JAVA_FLAGS_MASK;
1106a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
110751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
110851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
110951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Gets the signers of this class.
111051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
111151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the signers of this class, or null if there are no signers.  In
111251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          particular, this method returns null if this object represents
111351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          a primitive type or void.
111451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since   JDK1.1
111551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
1116a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public Object[] getSigners() {
1117a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return null;
1118a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
111951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
112070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    private native Method getEnclosingMethodNative();
112151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
112251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
112351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If this {@code Class} object represents a local or anonymous
112451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class within a method, returns a {@link
112551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * java.lang.reflect.Method Method} object representing the
112651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * immediately enclosing method of the underlying class. Returns
112751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code null} otherwise.
112851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
112951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * In particular, this method returns {@code null} if the underlying
113051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class is a local or anonymous class immediately enclosed by a type
113151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * declaration, instance initializer or static initializer.
113251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
113351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the immediately enclosing method of the underlying class, if
113451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     that class is a local or anonymous class; otherwise {@code null}.
113551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
113651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
11377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    // ANDROID-CHANGED: Removed SecurityException
113851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Method getEnclosingMethod() {
1139a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (classNameImpliesTopLevel()) {
114051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return null;
114151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
114270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        return getEnclosingMethodNative();
114351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
114451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
114551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
114651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If this {@code Class} object represents a local or anonymous
114751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class within a constructor, returns a {@link
114851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * java.lang.reflect.Constructor Constructor} object representing
114951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the immediately enclosing constructor of the underlying
115051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class. Returns {@code null} otherwise.  In particular, this
115151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method returns {@code null} if the underlying class is a local
115251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * or anonymous class immediately enclosed by a type declaration,
115351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instance initializer or static initializer.
115451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
115551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the immediately enclosing constructor of the underlying class, if
115651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     that class is a local or anonymous class; otherwise {@code null}.
115751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
115851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
11597accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    // ANDROID-CHANGED: Removed SecurityException
116051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Constructor<?> getEnclosingConstructor() {
1161a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (classNameImpliesTopLevel()) {
116251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return null;
116351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
116470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        return getEnclosingConstructorNative();
1165a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
1166a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
116770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    private native Constructor<?> getEnclosingConstructorNative();
116870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak
1169a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    private boolean classNameImpliesTopLevel() {
1170a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return !getName().contains("$");
117151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
117251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
11737accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
117451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the class or interface represented by this {@code Class} object
117651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is a member of another class, returns the {@code Class} object
117751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * representing the class in which it was declared.  This method returns
117851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * null if this class or interface is not a member of any other class.  If
117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this {@code Class} object represents an array class, a primitive
118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * type, or void,then this method returns null.
118151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the declaring class for this class
118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
118451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
11857accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    // ANDROID-CHANGED: Removed SecurityException
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     */
11957accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    // ANDROID-CHANGED: Removed SecurityException
119670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    public native Class<?> getEnclosingClass();
119751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the simple name of the underlying class as given in the
120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * source code. Returns an empty string if the underlying class is
120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * anonymous.
120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The simple name of an array is the simple name of the
120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * component type with "[]" appended.  In particular the simple
120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * name of an array whose component type is anonymous is "[]".
120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the simple name of the underlying class
120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public String getSimpleName() {
121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (isArray())
121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return getComponentType().getSimpleName()+"[]";
121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
121492d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath        if (isAnonymousClass()) {
121592d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath            return "";
121692d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath        }
121792d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath
121892d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath        if (isMemberClass() || isLocalClass()) {
121992d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath            // Note that we obtain this information from getInnerClassName(), which uses
122092d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath            // dex system annotations to obtain the name. It is possible for this information
122192d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath            // to disagree with the actual enclosing class name. For example, if dex
122292d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath            // manipulation tools have renamed the enclosing class without adjusting
122392d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath            // the system annotation to match. See http://b/28800927.
122492d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath            return getInnerClassName();
122592d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath        }
122692d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath
122792d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath        String simpleName = getName();
122892d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath        final int dot = simpleName.lastIndexOf(".");
122992d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath        if (dot > 0) {
123051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
123392d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath        return simpleName;
123451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
123620938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller    /**
123720938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller     * Return an informative string for the name of this type.
123820938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller     *
123920938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller     * @return an informative string for the name of this type
124020938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller     * @since 1.8
124120938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller     */
124220938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller    public String getTypeName() {
124320938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller        if (isArray()) {
124420938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller            try {
124520938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller                Class<?> cl = this;
124620938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller                int dimensions = 0;
124720938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller                while (cl.isArray()) {
124820938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller                    dimensions++;
124920938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller                    cl = cl.getComponentType();
125020938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller                }
125120938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller                StringBuilder sb = new StringBuilder();
125220938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller                sb.append(cl.getName());
125320938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller                for (int i = 0; i < dimensions; i++) {
125420938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller                    sb.append("[]");
125520938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller                }
125620938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller                return sb.toString();
125720938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller            } catch (Throwable e) { /*FALLTHRU*/ }
125820938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller        }
125920938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller        return getName();
126020938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller    }
126120938c5ed2bc8f5de8047a47caddb146f730868fNeil Fuller
126251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the canonical name of the underlying class as
126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * defined by the Java Language Specification.  Returns null if
126551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the underlying class does not have a canonical name (i.e., if
126651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is a local or anonymous class or an array whose component
126751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * type does not have a canonical name).
126851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the canonical name of the underlying class if it exists, and
126951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code null} otherwise.
127051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
127151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
127251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public String getCanonicalName() {
127351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (isArray()) {
127451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            String canonicalName = getComponentType().getCanonicalName();
127551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (canonicalName != null)
127651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return canonicalName + "[]";
127751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
127851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return null;
127951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
128051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (isLocalOrAnonymousClass())
128151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return null;
128251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        Class<?> enclosingClass = getEnclosingClass();
128351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (enclosingClass == null) { // top level class
128451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return getName();
128551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        } else {
128651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            String enclosingName = enclosingClass.getCanonicalName();
128751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (enclosingName == null)
128851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return null;
128951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return enclosingName + "." + getSimpleName();
129051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
129151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
129251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
129351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
129451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns {@code true} if and only if the underlying class
129551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is an anonymous class.
129651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
129751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return {@code true} if and only if this class is an anonymous class.
129851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
129951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
130070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    public native boolean isAnonymousClass();
130151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
130251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
130351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns {@code true} if and only if the underlying class
130451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is a local class.
130551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
130651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return {@code true} if and only if this class is a local class.
130751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
130851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
130951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isLocalClass() {
1310d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao        return (getEnclosingMethod() != null || getEnclosingConstructor() != null)
1311a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                && !isAnonymousClass();
131251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
131351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
131451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
131551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns {@code true} if and only if the underlying class
131651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is a member class.
131751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
131851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return {@code true} if and only if this class is a member class.
131951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
132051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
132151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMemberClass() {
132292d7211db6d0d1afd981f8e4a51857c88cee0e03Narayan Kamath        return getDeclaringClass() != null;
132351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
132451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
132551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
132651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns {@code true} if this is a local class or an anonymous
132751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class.  Returns {@code false} otherwise.
132851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
132951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private boolean isLocalOrAnonymousClass() {
133051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // JVM Spec 4.8.6: A class must have an EnclosingMethod
133151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // attribute if and only if it is a local class or an
133251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // anonymous class.
1333a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return isLocalClass() || isAnonymousClass();
133451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
133551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
133651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
133751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns an array containing {@code Class} objects representing all
133851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the public classes and interfaces that are members of the class
133951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * represented by this {@code Class} object.  This includes public
134051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class and interface members inherited from superclasses and public class
134151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and interface members declared by the class.  This method returns an
134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array of length 0 if this {@code Class} object has no public member
134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * classes or interfaces.  This method also returns an array of length 0 if
134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this {@code Class} object represents a primitive type, an array
134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class, or void.
134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the array of {@code Class} objects representing the public
13487accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         members of this class
134951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
135051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
135151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
135251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @CallerSensitive
135351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Class<?>[] getClasses() {
13541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        List<Class<?>> result = new ArrayList<Class<?>>();
13551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        for (Class<?> c = this; c != null; c = c.superClass) {
13561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath            for (Class<?> member : c.getDeclaredClasses()) {
13571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath                if (Modifier.isPublic(member.getModifiers())) {
13581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath                    result.add(member);
13591ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath                }
13601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath            }
13611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        }
13621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        return result.toArray(new Class[result.size()]);
136351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
136451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
136551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
136651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
136751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns an array containing {@code Field} objects reflecting all
136851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the accessible public fields of the class or interface represented by
13697accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * this {@code Class} object.
137051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
13717accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents a class or interface with no
13727accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * no accessible public fields, then this method returns an array of length
13737accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * 0.
137451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
13757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents a class, then this method
13767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * returns the public fields of the class and of all its superclasses.
137751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
13787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents an interface, then this
13797accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * method returns the fields of the interface and of all its
13807accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * superinterfaces.
138151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
13827accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents an array type, a primitive
13837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * type, or void, then this method returns an array of length 0.
138451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
13857accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> The elements in the returned array are not sorted and are not in any
13867accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * particular order.
138751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
13887accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @return the array of {@code Field} objects representing the
13897accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         public fields
13907accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws SecurityException
13917accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         If a security manager, <i>s</i>, is present and
13927accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         the caller's class loader is not the same as or an
13937accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         ancestor of the class loader for the current class and
13947accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         invocation of {@link SecurityManager#checkPackageAccess
13957accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         s.checkPackageAccess()} denies access to the package
13967accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         of this class.
139751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
139851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
13997accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.2 Class Members
14007accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.3 Field Declarations
140151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
140251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @CallerSensitive
140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Field[] getFields() throws SecurityException {
1404a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        List<Field> fields = new ArrayList<Field>();
14051ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        getPublicFieldsRecursive(fields);
14061ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        return fields.toArray(new Field[fields.size()]);
14071ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    }
14081ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
14091ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
14101ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Populates {@code result} with public fields defined by this class, its
14111ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * superclasses, and all implemented interfaces.
14121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
14131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private void getPublicFieldsRecursive(List<Field> result) {
1414a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // search superclasses
1415a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        for (Class<?> c = this; c != null; c = c.superClass) {
14161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath            Collections.addAll(result, c.getPublicDeclaredFields());
1417a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
1418a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
1419a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // search iftable which has a flattened and uniqued list of interfaces
1420a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        Object[] iftable = ifTable;
1421a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (iftable != null) {
1422a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            for (int i = 0; i < iftable.length; i += 2) {
14231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath                Collections.addAll(result, ((Class<?>) iftable[i]).getPublicDeclaredFields());
1424a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            }
1425a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
142651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
142751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
142851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
14297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * Returns an array containing {@code Method} objects reflecting all the
14307accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * public methods of the class or interface represented by this {@code
14317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * Class} object, including those declared by the class or interface and
14327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * those inherited from superclasses and superinterfaces.
143351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
14347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents a type that has multiple
14357accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * public methods with the same name and parameter types, but different
14367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * return types, then the returned array has a {@code Method} object for
14377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * each such method.
143851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
14397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents a type with a class
14407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * initialization method {@code <clinit>}, then the returned array does
14417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <em>not</em> have a corresponding {@code Method} object.
144251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
14437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents an array type, then the
14447accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * returned array has a {@code Method} object for each of the public
14457accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * methods inherited by the array type from {@code Object}. It does not
14467accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * contain a {@code Method} object for {@code clone()}.
144751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
14487accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents an interface then the
14497accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * returned array does not contain any implicitly declared methods from
14507accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * {@code Object}. Therefore, if no methods are explicitly declared in
14517accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * this interface or any of its superinterfaces then the returned array
14527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * has length 0. (Note that a {@code Class} object which represents a class
14537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * always has public methods, inherited from {@code Object}.)
145451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
14557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents a primitive type or void,
14567accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * then the returned array has length 0.
145751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
14587accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> Static methods declared in superinterfaces of the class or interface
14597accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * represented by this {@code Class} object are not considered members of
14607accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * the class or interface.
146151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
14627accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> The elements in the returned array are not sorted and are not in any
14637accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * particular order.
146451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
14657accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @return the array of {@code Method} objects representing the
14667accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         public methods of this class
14677accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws SecurityException
14687accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         If a security manager, <i>s</i>, is present and
14697accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         the caller's class loader is not the same as or an
14707accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         ancestor of the class loader for the current class and
14717accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         invocation of {@link SecurityManager#checkPackageAccess
14727accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         s.checkPackageAccess()} denies access to the package
14737accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         of this class.
14747accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
14757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.2 Class Members
14767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.4 Method Declarations
147751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
147851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @CallerSensitive
148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Method[] getMethods() throws SecurityException {
1481a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        List<Method> methods = new ArrayList<Method>();
14821ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        getPublicMethodsInternal(methods);
14831ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        /*
14841ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath         * Remove duplicate methods defined by superclasses and
14851ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath         * interfaces, preferring to keep methods declared by derived
14861ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath         * types.
14871ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath         */
14881ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        CollectionUtils.removeDuplicates(methods, Method.ORDER_BY_SIGNATURE);
14891ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        return methods.toArray(new Method[methods.size()]);
14901ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    }
14911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
14921ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
14931ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Populates {@code result} with public methods defined by this class, its
14941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * superclasses, and all implemented interfaces, including overridden methods.
14951ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
14961ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private void getPublicMethodsInternal(List<Method> result) {
14971ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        Collections.addAll(result, getDeclaredMethodsUnchecked(true));
1498a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (!isInterface()) {
1499a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            // Search superclasses, for interfaces don't search java.lang.Object.
1500a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            for (Class<?> c = superClass; c != null; c = c.superClass) {
15011ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath                Collections.addAll(result, c.getDeclaredMethodsUnchecked(true));
1502a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            }
1503a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
1504a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // Search iftable which has a flattened and uniqued list of interfaces.
1505a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        Object[] iftable = ifTable;
1506a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (iftable != null) {
1507a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            for (int i = 0; i < iftable.length; i += 2) {
1508a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                Class<?> ifc = (Class<?>) iftable[i];
15091ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath                Collections.addAll(result, ifc.getDeclaredMethodsUnchecked(true));
1510a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            }
1511a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
151251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
151351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
151451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
151551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns an array containing {@code Constructor} objects reflecting
151651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * all the public constructors of the class represented by this
151751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class} object.  An array of length 0 is returned if the
151851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class has no public constructors, or if the class is an array class, or
151951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * if the class reflects a primitive type or void.
152051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
152151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Note that while this method returns an array of {@code
152251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Constructor<T>} objects (that is an array of constructors from
152351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this class), the return type of this method is {@code
152451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
152551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * might be expected.  This less informative return type is
152651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * necessary since after being returned from this method, the
152751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array could be modified to hold {@code Constructor} objects for
152851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * different classes, which would violate the type guarantees of
152951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Constructor<T>[]}.
153051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
153151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the array of {@code Constructor} objects representing the
15327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         public constructors of this class
15337accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws SecurityException
15347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         If a security manager, <i>s</i>, is present and
15357accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         the caller's class loader is not the same as or an
15367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         ancestor of the class loader for the current class and
15377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         invocation of {@link SecurityManager#checkPackageAccess
15387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         s.checkPackageAccess()} denies access to the package
15397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         of this class.
154051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
154151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
154251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
154351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @CallerSensitive
154451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Constructor<?>[] getConstructors() throws SecurityException {
15451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        return getDeclaredConstructorsInternal(true);
154651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
154751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
15487accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
154951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
15507accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * Returns a {@code Field} object that reflects the specified public member
15517accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * field of the class or interface represented by this {@code Class}
15527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * object. The {@code name} parameter is a {@code String} specifying the
15537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * simple name of the desired field.
155451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
155551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> The field to be reflected is determined by the algorithm that
15567accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * follows.  Let C be the class or interface represented by this object:
15577accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
155851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <OL>
155951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <LI> If C declares a public field with the name specified, that is the
156051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *      field to be reflected.</LI>
156151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <LI> If no field was found in step 1 above, this algorithm is applied
156251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *      recursively to each direct superinterface of C. The direct
156351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *      superinterfaces are searched in the order they were declared.</LI>
156451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <LI> If no field was found in steps 1 and 2 above, and C has a
156551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *      superclass S, then this algorithm is invoked recursively upon S.
156651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *      If C has no superclass, then a {@code NoSuchFieldException}
156751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *      is thrown.</LI>
156851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </OL>
156951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
15707accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents an array type, then this
15717accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * method does not find the {@code length} field of the array type.
157251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
157351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param name the field name
15747accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @return the {@code Field} object of this class specified by
15757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         {@code name}
15767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws NoSuchFieldException if a field with the specified name is
15777accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         not found.
15787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws NullPointerException if {@code name} is {@code null}
15797accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws SecurityException
15807accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         If a security manager, <i>s</i>, is present and
15817accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         the caller's class loader is not the same as or an
15827accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         ancestor of the class loader for the current class and
15837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         invocation of {@link SecurityManager#checkPackageAccess
15847accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         s.checkPackageAccess()} denies access to the package
15857accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         of this class.
158651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
158751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
15887accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.2 Class Members
15897accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.3 Field Declarations
159051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
15917accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    // ANDROID-CHANGED: Removed SecurityException
15927accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    public Field getField(String name)
15937accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller        throws NoSuchFieldException {
1594fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski        if (name == null) {
1595fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski            throw new NullPointerException("name == null");
1596fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski        }
159770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        Field result = getPublicFieldRecursive(name);
159870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        if (result == null) {
159951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new NoSuchFieldException(name);
160051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
160170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        return result;
1602a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
1603a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
160470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    /**
160570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak     * The native implementation of the {@code getField} method.
160670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak     *
160770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak     * @throws NullPointerException
160870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak     *            if name is null.
160970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak     * @see #getField(String)
161070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak     */
161170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    private native Field getPublicFieldRecursive(String name);
161251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
161351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
161451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a {@code Method} object that reflects the specified public
161551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * member method of the class or interface represented by this
161651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class} object. The {@code name} parameter is a
161751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code String} specifying the simple name of the desired method. The
161851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code parameterTypes} parameter is an array of {@code Class}
161951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * objects that identify the method's formal parameter types, in declared
162051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * order. If {@code parameterTypes} is {@code null}, it is
162151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * treated as if it were an empty array.
162251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
16237accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If the {@code name} is "{@code <init>}" or "{@code <clinit>}" a
162451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code NoSuchMethodException} is raised. Otherwise, the method to
162551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * be reflected is determined by the algorithm that follows.  Let C be the
16267accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * class or interface represented by this object:
162751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <OL>
16287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <LI> C is searched for a <I>matching method</I>, as defined below. If a
16297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *      matching method is found, it is reflected.</LI>
16307accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <LI> If no matching method is found by step 1 then:
16317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *   <OL TYPE="a">
16327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *   <LI> If C is a class other than {@code Object}, then this algorithm is
16337accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *        invoked recursively on the superclass of C.</LI>
16347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *   <LI> If C is the class {@code Object}, or if C is an interface, then
16357accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *        the superinterfaces of C (if any) are searched for a matching
16367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *        method. If any such method is found, it is reflected.</LI>
16377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *   </OL></LI>
163851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </OL>
163951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
16407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> To find a matching method in a class or interface C:&nbsp; If C
16417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * declares exactly one public method with the specified name and exactly
16427accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * the same formal parameter types, that is the method reflected. If more
16437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * than one such method is found in C, and one of these methods has a
16447accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * return type that is more specific than any of the others, that method is
16457accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * reflected; otherwise one of the methods is chosen arbitrarily.
164651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
164751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Note that there may be more than one matching method in a
164851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class because while the Java language forbids a class to
164951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * declare multiple methods with the same signature but different
165051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * return types, the Java virtual machine does not.  This
165151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * increased flexibility in the virtual machine can be used to
165251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implement various language features.  For example, covariant
165351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returns can be implemented with {@linkplain
165451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * java.lang.reflect.Method#isBridge bridge methods}; the bridge
165551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method and the method being overridden would have the same
165651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * signature but different return types.
165751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
16587accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents an array type, then this
16597accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * method does not find the {@code clone()} method.
16607accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
16617accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> Static methods declared in superinterfaces of the class or interface
16627accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * represented by this {@code Class} object are not considered members of
16637accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * the class or interface.
166451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
166551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param name the name of the method
166651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param parameterTypes the list of parameters
166751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the {@code Method} object that matches the specified
16687accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         {@code name} and {@code parameterTypes}
16697accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws NoSuchMethodException if a matching method is not found
16707accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
16717accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws NullPointerException if {@code name} is {@code null}
16727accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws SecurityException
16737accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         If a security manager, <i>s</i>, is present and
16747accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         the caller's class loader is not the same as or an
16757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         ancestor of the class loader for the current class and
16767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         invocation of {@link SecurityManager#checkPackageAccess
16777accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         s.checkPackageAccess()} denies access to the package
16787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         of this class.
16797accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
16807accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.2 Class Members
16817accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.4 Method Declarations
168251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
168351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
168451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @CallerSensitive
168551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Method getMethod(String name, Class<?>... parameterTypes)
168651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws NoSuchMethodException, SecurityException {
1687a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return getMethod(name, parameterTypes, true);
168851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
168951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
169051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
169151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
169251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a {@code Constructor} object that reflects the specified
169351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * public constructor of the class represented by this {@code Class}
169451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * object. The {@code parameterTypes} parameter is an array of
169551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class} objects that identify the constructor's formal
169651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * parameter types, in declared order.
169751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
169851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If this {@code Class} object represents an inner class
169951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * declared in a non-static context, the formal parameter types
170051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * include the explicit enclosing instance as the first parameter.
170151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
170251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> The constructor to reflect is the public constructor of the class
170351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * represented by this {@code Class} object whose formal parameter
170451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * types match those specified by {@code parameterTypes}.
170551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
170651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param parameterTypes the parameter array
170751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the {@code Constructor} object of the public constructor that
17087accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         matches the specified {@code parameterTypes}
17097accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws NoSuchMethodException if a matching method is not found.
17107accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws SecurityException
17117accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         If a security manager, <i>s</i>, is present and
17127accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         the caller's class loader is not the same as or an
17137accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         ancestor of the class loader for the current class and
17147accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         invocation of {@link SecurityManager#checkPackageAccess
17157accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         s.checkPackageAccess()} denies access to the package
17167accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         of this class.
171751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
171851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
171951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
172051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Constructor<T> getConstructor(Class<?>... parameterTypes)
172151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws NoSuchMethodException, SecurityException {
172251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return getConstructor0(parameterTypes, Member.PUBLIC);
172351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
172451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
17257accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
172651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
172751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns an array of {@code Class} objects reflecting all the
172851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * classes and interfaces declared as members of the class represented by
172951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this {@code Class} object. This includes public, protected, default
173051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (package) access, and private classes and interfaces declared by the
173151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class, but excludes inherited classes and interfaces.  This method
173251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returns an array of length 0 if the class declares no classes or
173351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interfaces as members, or if this {@code Class} object represents a
173451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * primitive type, an array class, or void.
173551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
173651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the array of {@code Class} objects representing all the
17377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         declared members of this class
17387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws SecurityException
17397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         If a security manager, <i>s</i>, is present and any of the
17407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         following conditions is met:
174151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17427accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         <ul>
174351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17447accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         <li> the caller's class loader is not the same as the
17457accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         class loader of this class and invocation of
17467accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         {@link SecurityManager#checkPermission
17477accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         s.checkPermission} method with
17487accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         {@code RuntimePermission("accessDeclaredMembers")}
17497accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         denies access to the declared classes within this class
175051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17517accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         <li> the caller's class loader is not the same as or an
17527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         ancestor of the class loader for the current class and
17537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         invocation of {@link SecurityManager#checkPackageAccess
17547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         s.checkPackageAccess()} denies access to the package
17557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         of this class
175651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17577accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *         </ul>
175851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
175951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
176051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
17617accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    // ANDROID-CHANGED: Removed SecurityException
176270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    public native Class<?>[] getDeclaredClasses();
176351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
176451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
176551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns an array of {@code Field} objects reflecting all the fields
176651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * declared by the class or interface represented by this
176751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class} object. This includes public, protected, default
176851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (package) access, and private fields, but excludes inherited fields.
176951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17707accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents a class or interface with no
17717accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * declared fields, then this method returns an array of length 0.
17727accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
17737accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents an array type, a primitive
17747accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * type, or void, then this method returns an array of length 0.
17757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
17767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> The elements in the returned array are not sorted and are not in any
17777accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * particular order.
177851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17797accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @return  the array of {@code Field} objects representing all the
17807accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          declared fields of this class
17817accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  SecurityException
17827accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          If a security manager, <i>s</i>, is present and any of the
17837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          following conditions is met:
178451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17857accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <ul>
178651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17877accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <li> the caller's class loader is not the same as the
17887accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          class loader of this class and invocation of
17897accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          {@link SecurityManager#checkPermission
17907accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPermission} method with
17917accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          {@code RuntimePermission("accessDeclaredMembers")}
17927accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          denies access to the declared fields within this class
179351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17947accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <li> the caller's class loader is not the same as or an
17957accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          ancestor of the class loader for the current class and
17967accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          invocation of {@link SecurityManager#checkPackageAccess
17977accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPackageAccess()} denies access to the package
17987accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          of this class
179951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
18007accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          </ul>
180151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
180251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
18037accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.2 Class Members
18047accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.3 Field Declarations
180551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
18067accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    // ANDROID-CHANGED: Removed SecurityException
18071ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    public native Field[] getDeclaredFields();
180851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
18091ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
18101ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Populates a list of fields without performing any security or type
18111ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * resolution checks first. If no fields exist, the list is not modified.
18121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     *
18131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * @param publicOnly Whether to return only public fields.
18141ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * @hide
18151ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
18161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    public native Field[] getDeclaredFieldsUnchecked(boolean publicOnly);
181751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
181851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
181951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
18207accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * Returns an array containing {@code Method} objects reflecting all the
18217accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * declared methods of the class or interface represented by this {@code
18227accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * Class} object, including public, protected, default (package)
18237accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * access, and private methods, but excluding inherited methods.
182451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
18257accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents a type that has multiple
18267accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * declared methods with the same name and parameter types, but different
18277accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * return types, then the returned array has a {@code Method} object for
18287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * each such method.
182951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
18307accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents a type that has a class
18317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * initialization method {@code <clinit>}, then the returned array does
18327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <em>not</em> have a corresponding {@code Method} object.
183351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
18347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents a class or interface with no
18357accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * declared methods, then the returned array has length 0.
183651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
18377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents an array type, a primitive
18387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * type, or void, then the returned array has length 0.
183951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
18407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> The elements in the returned array are not sorted and are not in any
18417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * particular order.
184251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
18437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @return  the array of {@code Method} objects representing all the
18447accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          declared methods of this class
18457accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  SecurityException
18467accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          If a security manager, <i>s</i>, is present and any of the
18477accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          following conditions is met:
18487accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
18497accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <ul>
18507accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
18517accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <li> the caller's class loader is not the same as the
18527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          class loader of this class and invocation of
18537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          {@link SecurityManager#checkPermission
18547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPermission} method with
18557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          {@code RuntimePermission("accessDeclaredMembers")}
18567accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          denies access to the declared methods within this class
18577accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
18587accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <li> the caller's class loader is not the same as or an
18597accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          ancestor of the class loader for the current class and
18607accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          invocation of {@link SecurityManager#checkPackageAccess
18617accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPackageAccess()} denies access to the package
18627accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          of this class
18637accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
18647accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          </ul>
18657accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
18667accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.2 Class Members
18677accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.4 Method Declarations
186851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
186951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
187051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Method[] getDeclaredMethods() throws SecurityException {
18711ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        Method[] result = getDeclaredMethodsUnchecked(false);
1872a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        for (Method m : result) {
1873a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            // Throw NoClassDefFoundError if types cannot be resolved.
1874a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            m.getReturnType();
1875a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            m.getParameterTypes();
1876a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
1877a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return result;
1878a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
1879a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
18801ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
18811ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Populates a list of methods without performing any security or type
18821ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * resolution checks first. If no methods exist, the list is not modified.
18831ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     *
18841ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * @param publicOnly Whether to return only public methods.
18851ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * @hide
18861ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
18871ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    public native Method[] getDeclaredMethodsUnchecked(boolean publicOnly);
188851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
188951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
189051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns an array of {@code Constructor} objects reflecting all the
189151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * constructors declared by the class represented by this
189251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class} object. These are public, protected, default
189351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (package) access, and private constructors.  The elements in the array
189451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returned are not sorted and are not in any particular order.  If the
189551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class has a default constructor, it is included in the returned array.
189651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * This method returns an array of length 0 if this {@code Class}
189751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * object represents an interface, a primitive type, an array class, or
189851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * void.
189951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
190051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> See <em>The Java Language Specification</em>, section 8.2.
190151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
19027accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @return  the array of {@code Constructor} objects representing all the
19037accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          declared constructors of this class
19047accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  SecurityException
19057accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          If a security manager, <i>s</i>, is present and any of the
19067accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          following conditions is met:
190751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
19087accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <ul>
190951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
19107accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <li> the caller's class loader is not the same as the
19117accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          class loader of this class and invocation of
19127accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          {@link SecurityManager#checkPermission
19137accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPermission} method with
19147accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          {@code RuntimePermission("accessDeclaredMembers")}
19157accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          denies access to the declared constructors within this class
191651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
19177accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <li> the caller's class loader is not the same as or an
19187accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          ancestor of the class loader for the current class and
19197accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          invocation of {@link SecurityManager#checkPackageAccess
19207accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPackageAccess()} denies access to the package
19217accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          of this class
192251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
19237accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          </ul>
192451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
192551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
192651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
192751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
19281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath        return getDeclaredConstructorsInternal(false);
192951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
193051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
19317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
193251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
19331ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Returns the constructor with the given parameters if it is defined by this class;
19341ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * {@code null} otherwise. This may return a non-public member.
19351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
19361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private native Constructor<?>[] getDeclaredConstructorsInternal(boolean publicOnly);
19371ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
19381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
193951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a {@code Field} object that reflects the specified declared
194051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * field of the class or interface represented by this {@code Class}
19417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * object. The {@code name} parameter is a {@code String} that specifies
19427accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * the simple name of the desired field.
19437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
19447accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents an array type, then this
19457accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * method does not find the {@code length} field of the array type.
194651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
194751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param name the name of the field
19487accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @return  the {@code Field} object for the specified field in this
19497accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          class
19507accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  NoSuchFieldException if a field with the specified name is
19517accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          not found.
19527accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  NullPointerException if {@code name} is {@code null}
19537accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  SecurityException
19547accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          If a security manager, <i>s</i>, is present and any of the
19557accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          following conditions is met:
19567accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
19577accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <ul>
19587accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
19597accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <li> the caller's class loader is not the same as the
19607accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          class loader of this class and invocation of
19617accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          {@link SecurityManager#checkPermission
19627accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPermission} method with
19637accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          {@code RuntimePermission("accessDeclaredMembers")}
19647accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          denies access to the declared field
19657accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
19667accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <li> the caller's class loader is not the same as or an
19677accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          ancestor of the class loader for the current class and
19687accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          invocation of {@link SecurityManager#checkPackageAccess
19697accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPackageAccess()} denies access to the package
19707accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          of this class
19717accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
19727accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          </ul>
197351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
197451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
19757accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.2 Class Members
19767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.3 Field Declarations
197751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
19787accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    // ANDROID-CHANGED: Removed SecurityException
19791ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    public native Field getDeclaredField(String name) throws NoSuchFieldException;
198051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
19811ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
19821ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Returns the subset of getDeclaredFields which are public.
19831ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
19841ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private native Field[] getPublicDeclaredFields();
1985a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
198651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
198751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a {@code Method} object that reflects the specified
198851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * declared method of the class or interface represented by this
198951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class} object. The {@code name} parameter is a
199051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code String} that specifies the simple name of the desired
199151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method, and the {@code parameterTypes} parameter is an array of
199251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class} objects that identify the method's formal parameter
199351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * types, in declared order.  If more than one method with the same
199451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * parameter types is declared in a class, and one of these methods has a
199551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * return type that is more specific than any of the others, that method is
199651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returned; otherwise one of the methods is chosen arbitrarily.  If the
199751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * name is "&lt;init&gt;"or "&lt;clinit&gt;" a {@code NoSuchMethodException}
199851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is raised.
199951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
20007accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * <p> If this {@code Class} object represents an array type, then this
20017accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * method does not find the {@code clone()} method.
20027accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
200351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param name the name of the method
200451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param parameterTypes the parameter array
20057accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @return  the {@code Method} object for the method of this class
20067accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          matching the specified name and parameters
20077accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  NoSuchMethodException if a matching method is not found.
20087accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  NullPointerException if {@code name} is {@code null}
20097accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  SecurityException
20107accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          If a security manager, <i>s</i>, is present and any of the
20117accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          following conditions is met:
20127accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
20137accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <ul>
20147accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
20157accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <li> the caller's class loader is not the same as the
20167accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          class loader of this class and invocation of
20177accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          {@link SecurityManager#checkPermission
20187accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPermission} method with
20197accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          {@code RuntimePermission("accessDeclaredMembers")}
20207accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          denies access to the declared method
20217accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
20227accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <li> the caller's class loader is not the same as or an
20237accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          ancestor of the class loader for the current class and
20247accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          invocation of {@link SecurityManager#checkPackageAccess
20257accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPackageAccess()} denies access to the package
20267accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          of this class
20277accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
20287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          </ul>
20297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
20307accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.2 Class Members
20317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @jls 8.4 Method Declarations
203251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
203351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
203451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @CallerSensitive
203551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
203651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws NoSuchMethodException, SecurityException {
2037a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return getMethod(name, parameterTypes, false);
2038a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2039a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
2040a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    private Method getMethod(String name, Class<?>[] parameterTypes, boolean recursivePublicMethods)
2041a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            throws NoSuchMethodException {
2042a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (name == null) {
2043a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            throw new NullPointerException("name == null");
2044a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2045a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (parameterTypes == null) {
2046a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            parameterTypes = EmptyArray.CLASS;
2047a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2048a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        for (Class<?> c : parameterTypes) {
2049a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            if (c == null) {
2050a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                throw new NoSuchMethodException("parameter type is null");
2051a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            }
2052a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2053a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        Method result = recursivePublicMethods ? getPublicMethodRecursive(name, parameterTypes)
2054a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                                               : getDeclaredMethodInternal(name, parameterTypes);
2055a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // Fail if we didn't find the method or it was expected to be public.
2056a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (result == null ||
2057a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            (recursivePublicMethods && !Modifier.isPublic(result.getAccessFlags()))) {
2058a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            throw new NoSuchMethodException(name + " " + Arrays.toString(parameterTypes));
2059a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2060a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return result;
2061a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2062a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    private Method getPublicMethodRecursive(String name, Class<?>[] parameterTypes) {
2063a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        // search superclasses
2064a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        for (Class<?> c = this; c != null; c = c.getSuperclass()) {
2065a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            Method result = c.getDeclaredMethodInternal(name, parameterTypes);
2066a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            if (result != null && Modifier.isPublic(result.getAccessFlags())) {
2067a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                return result;
2068a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            }
2069a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2070cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath
2071cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath        return findInterfaceMethod(name, parameterTypes);
2072cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath    }
2073cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath
2074cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath    /**
2075cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath     * Returns an instance method that's defined on this class or any super classes, regardless
2076cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath     * of its access flags. Constructors are excluded.
2077cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath     *
2078cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath     * This function does not perform access checks and its semantics don't match any dex byte code
2079cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath     * instruction or public reflection API. This is used by {@code MethodHandles.findVirtual}
2080cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath     * which will perform access checks on the returned method.
2081cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath     *
2082cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath     * @hide
2083cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath     */
2084cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath    public Method getInstanceMethod(String name, Class<?>[] parameterTypes)
2085cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath            throws NoSuchMethodException, IllegalAccessException {
2086cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath        for (Class<?> c = this; c != null; c = c.getSuperclass()) {
2087cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath            Method result = c.getDeclaredMethodInternal(name, parameterTypes);
2088cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath            if (result != null && !Modifier.isStatic(result.getModifiers())) {
2089cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath                return result;
2090cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath            }
2091cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath        }
2092cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath
2093191e70e2ebae59054aa8737707bb9845b71b4820Narayan Kamath        return findInterfaceMethod(name, parameterTypes);
2094cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath    }
2095cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath
2096cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath    private Method findInterfaceMethod(String name, Class<?>[] parameterTypes) {
2097a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        Object[] iftable = ifTable;
2098a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (iftable != null) {
2099d559f4f94cb0b9b4c80a3cc2040c0bd32276c225Alex Light            // Search backwards so more specific interfaces are searched first. This ensures that
2100d559f4f94cb0b9b4c80a3cc2040c0bd32276c225Alex Light            // the method we return is not overridden by one of it's subtypes that this class also
2101d559f4f94cb0b9b4c80a3cc2040c0bd32276c225Alex Light            // implements.
2102d559f4f94cb0b9b4c80a3cc2040c0bd32276c225Alex Light            for (int i = iftable.length - 2; i >= 0; i -= 2) {
2103a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                Class<?> ifc = (Class<?>) iftable[i];
2104a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                Method result = ifc.getPublicMethodRecursive(name, parameterTypes);
2105a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                if (result != null && Modifier.isPublic(result.getAccessFlags())) {
2106a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                    return result;
2107a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                }
2108a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            }
210951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
2110cc44ded0807d59a7c6059bdefdaeb89a1543228dNarayan Kamath
2111a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return null;
211251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
211351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
211451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
211551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
211651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a {@code Constructor} object that reflects the specified
211751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * constructor of the class or interface represented by this
211851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class} object.  The {@code parameterTypes} parameter is
211951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * an array of {@code Class} objects that identify the constructor's
212051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * formal parameter types, in declared order.
212151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
212251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If this {@code Class} object represents an inner class
212351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * declared in a non-static context, the formal parameter types
212451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * include the explicit enclosing instance as the first parameter.
212551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
212651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param parameterTypes the parameter array
21277accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @return  The {@code Constructor} object for the constructor with the
21287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          specified parameter list
21297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  NoSuchMethodException if a matching method is not found.
21307accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws  SecurityException
21317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          If a security manager, <i>s</i>, is present and any of the
21327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          following conditions is met:
21337accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
21347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <ul>
21357accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
21367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <li> the caller's class loader is not the same as the
21377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          class loader of this class and invocation of
21387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          {@link SecurityManager#checkPermission
21397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPermission} method with
21407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          {@code RuntimePermission("accessDeclaredMembers")}
21417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          denies access to the declared constructor
21427accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
21437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          <li> the caller's class loader is not the same as or an
21447accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          ancestor of the class loader for the current class and
21457accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          invocation of {@link SecurityManager#checkPackageAccess
21467accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          s.checkPackageAccess()} denies access to the package
21477accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          of this class
21487accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *
21497accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     *          </ul>
215051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
215151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
215251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
215351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @CallerSensitive
215451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
215551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws NoSuchMethodException, SecurityException {
215651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return getConstructor0(parameterTypes, Member.DECLARED);
215751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
215851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
215951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
216051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Finds a resource with a given name.  The rules for searching resources
216151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * associated with a given class are implemented by the defining
216251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@linkplain ClassLoader class loader} of the class.  This method
216351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * delegates to this object's class loader.  If this object was loaded by
216451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the bootstrap class loader, the method delegates to {@link
216551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ClassLoader#getSystemResourceAsStream}.
216651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
216751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> Before delegation, an absolute resource name is constructed from the
216851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * given resource name using this algorithm:
216951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
217051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <ul>
217151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
217251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <li> If the {@code name} begins with a {@code '/'}
217351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
217451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * portion of the {@code name} following the {@code '/'}.
217551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
217651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <li> Otherwise, the absolute name is of the following form:
217751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
217851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <blockquote>
217951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *   {@code modified_package_name/name}
218051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </blockquote>
218151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
218251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> Where the {@code modified_package_name} is the package name of this
218351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * object with {@code '/'} substituted for {@code '.'}
218451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>'&#92;u002e'</tt>).
218551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
218651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </ul>
218751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
218851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param  name name of the desired resource
218951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return      A {@link java.io.InputStream} object or {@code null} if
219051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *              no resource with this name is found
219151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws  NullPointerException If {@code name} is {@code null}
219251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since  JDK1.1
219351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
219451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     public InputStream getResourceAsStream(String name) {
219551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        name = resolveName(name);
2196a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        ClassLoader cl = getClassLoader();
219751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (cl==null) {
219851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            // A system class.
219951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return ClassLoader.getSystemResourceAsStream(name);
220051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
220151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return cl.getResourceAsStream(name);
220251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
220351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
220451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
220551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Finds a resource with a given name.  The rules for searching resources
220651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * associated with a given class are implemented by the defining
220751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@linkplain ClassLoader class loader} of the class.  This method
220851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * delegates to this object's class loader.  If this object was loaded by
220951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the bootstrap class loader, the method delegates to {@link
221051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ClassLoader#getSystemResource}.
221151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
221251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> Before delegation, an absolute resource name is constructed from the
221351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * given resource name using this algorithm:
221451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
221551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <ul>
221651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
221751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <li> If the {@code name} begins with a {@code '/'}
221851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
221951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * portion of the {@code name} following the {@code '/'}.
222051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
222151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <li> Otherwise, the absolute name is of the following form:
222251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
222351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <blockquote>
222451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *   {@code modified_package_name/name}
222551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </blockquote>
222651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
222751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> Where the {@code modified_package_name} is the package name of this
222851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * object with {@code '/'} substituted for {@code '.'}
222951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>'&#92;u002e'</tt>).
223051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
223151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </ul>
223251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
223351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param  name name of the desired resource
223451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return      A  {@link java.net.URL} object or {@code null} if no
223551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *              resource with this name is found
223651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since  JDK1.1
223751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
223851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public java.net.URL getResource(String name) {
223951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        name = resolveName(name);
2240a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        ClassLoader cl = getClassLoader();
224151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (cl==null) {
224251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            // A system class.
224351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return ClassLoader.getSystemResource(name);
224451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
224551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return cl.getResource(name);
224651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
224751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
224851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
224951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the {@code ProtectionDomain} of this class.  If there is a
225051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * security manager installed, this method first calls the security
225151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * manager's {@code checkPermission} method with a
225251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code RuntimePermission("getProtectionDomain")} permission to
225351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ensure it's ok to get the
225451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code ProtectionDomain}.
225551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
225651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the ProtectionDomain of this class
225751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
225851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws SecurityException
225951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        if a security manager exists and its
226051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@code checkPermission} method doesn't allow
226151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        getting the ProtectionDomain.
226251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
226351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see java.security.ProtectionDomain
226451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see SecurityManager#checkPermission
226551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see java.lang.RuntimePermission
226651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.2
226751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
226851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public java.security.ProtectionDomain getProtectionDomain() {
2269a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return null;
227051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
227151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
227251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
227351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Add a package name prefix if the name is not absolute Remove leading "/"
227451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * if name is absolute
227551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
227651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private String resolveName(String name) {
227751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (name == null) {
227851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return name;
227951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
228051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (!name.startsWith("/")) {
228151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Class<?> c = this;
228251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            while (c.isArray()) {
228351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                c = c.getComponentType();
228451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
228551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            String baseName = c.getName();
228651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int index = baseName.lastIndexOf('.');
228751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (index != -1) {
228851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                name = baseName.substring(0, index).replace('.', '/')
228951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    +"/"+name;
229051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
229151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        } else {
229251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            name = name.substring(1);
229351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
229451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return name;
229551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
229651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2297a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    private Constructor<T> getConstructor0(Class<?>[] parameterTypes,
2298a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                                        int which) throws NoSuchMethodException
2299a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    {
2300a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (parameterTypes == null) {
2301a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            parameterTypes = EmptyArray.CLASS;
230251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
2303a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        for (Class<?> c : parameterTypes) {
2304a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            if (c == null) {
2305a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                throw new NoSuchMethodException("parameter type is null");
2306a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            }
230751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
2308a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        Constructor<T> result = getDeclaredConstructorInternal(parameterTypes);
2309a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (result == null || which == Member.PUBLIC && !Modifier.isPublic(result.getAccessFlags())) {
2310a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            throw new NoSuchMethodException("<init> " + Arrays.toString(parameterTypes));
2311a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2312a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return result;
231351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
231451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
23157accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    /** use serialVersionUID from JDK 1.1 for interoperability */
23167accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    private static final long serialVersionUID = 3206093459760846163L;
23177accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
23187accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
23191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
23201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Returns the constructor with the given parameters if it is defined by this class;
23211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * {@code null} otherwise. This may return a non-public member.
23221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     *
23231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * @param args the types of the parameters to the constructor.
23241ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
23251ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private native Constructor<T> getDeclaredConstructorInternal(Class<?>[] args);
232651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
232751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
232851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the assertion status that would be assigned to this
232951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class if it were to be initialized at the time this method is invoked.
233051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If this class has had its assertion status set, the most recent
233151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * setting will be returned; otherwise, if any package default assertion
233251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * status pertains to this class, the most recent setting for the most
233351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * specific pertinent package default assertion status is returned;
233451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * otherwise, if this class is not a system class (i.e., it has a
233551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class loader) its class loader's default assertion status is returned;
233651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * otherwise, the system class default assertion status is returned.
233751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>
233851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Few programmers will have any need for this method; it is provided
233951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * for the benefit of the JRE itself.  (It allows a class to determine at
234051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the time that it is initialized whether assertions should be enabled.)
234151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Note that this method is not guaranteed to return the actual
234251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * assertion status that was (or will be) associated with the specified
234351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * class when it was (or will be) initialized.
234451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
234551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the desired assertion status of the specified class.
234651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see    java.lang.ClassLoader#setClassAssertionStatus
234751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see    java.lang.ClassLoader#setPackageAssertionStatus
234851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see    java.lang.ClassLoader#setDefaultAssertionStatus
234951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since  1.4
235051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
235151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean desiredAssertionStatus() {
2352a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return false;
235351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
235451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
235551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
235670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak     * Returns the simple name of a member or local class, or {@code null} otherwise.
235770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak     */
235870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    private native String getInnerClassName();
235970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak
236070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    private native int getInnerClassFlags(int defaultValue);
236170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak
236270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    /**
236351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns true if and only if this class was declared as an enum in the
236451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * source code.
236551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
236651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return true if and only if this class was declared as an enum in the
236751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     source code
236851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
236951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
237051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isEnum() {
237151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // An enum must both directly extend java.lang.Enum and have
237251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // the ENUM bit set; classes for specialized enum constants
237351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // don't do the former.
237451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return (this.getModifiers() & ENUM) != 0 &&
237551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        this.getSuperclass() == java.lang.Enum.class;
237651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
237751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
237851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
237951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the elements of this enum class or null if this
238051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Class object does not represent an enum type.
238151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
238251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return an array containing the values comprising the enum class
238351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     represented by this Class object in the order they're
238451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     declared, or null if this Class object does not
238551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     represent an enum type
238651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
238751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
238851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public T[] getEnumConstants() {
238951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        T[] values = getEnumConstantsShared();
239051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return (values != null) ? values.clone() : null;
239151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
239251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
239351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
239451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the elements of this enum class or null if this
239551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Class object does not represent an enum type;
239651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * identical to getEnumConstants except that the result is
239751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * uncloned, cached, and shared by all callers.
239851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
239951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    T[] getEnumConstantsShared() {
2400a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (!isEnum()) return null;
2401a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return (T[]) Enum.getSharedConstants((Class) this);
240251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
240351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
240451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
240551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Casts an object to the class or interface represented
240651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by this {@code Class} object.
240751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
240851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param obj the object to be cast
240951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the object after casting, or null if obj is null
241051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
241151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the object is not
241251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * null and is not assignable to the type T.
241351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
241451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
241551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
24167accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    @SuppressWarnings("unchecked")
241751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public T cast(Object obj) {
241851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (obj != null && !isInstance(obj))
241951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new ClassCastException(cannotCastMsg(obj));
242051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return (T) obj;
242151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
242251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
242351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private String cannotCastMsg(Object obj) {
242451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return "Cannot cast " + obj.getClass().getName() + " to " + getName();
242551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
242651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
242751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
242851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Casts this {@code Class} object to represent a subclass of the class
24297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * represented by the specified class object.  Checks that the cast
243051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is valid, and throws a {@code ClassCastException} if it is not.  If
243151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this method succeeds, it always returns a reference to this class object.
243251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
243351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This method is useful when a client needs to "narrow" the type of
243451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * a {@code Class} object to pass it to an API that restricts the
243551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code Class} objects that it is willing to accept.  A cast would
243651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * generate a compile-time warning, as the correctness of the cast
243751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * could not be checked at runtime (because generic types are implemented
243851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by erasure).
243951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
24407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @param <U> the type to cast this class object to
24417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @param clazz the class of the type to cast this class object to
244251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return this {@code Class} object, cast to represent a subclass of
244351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    the specified class object.
244451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if this {@code Class} object does not
244551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    represent a subclass of the specified class (here "subclass" includes
244651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    the class itself).
244751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
244851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
24497accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    @SuppressWarnings("unchecked")
245051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public <U> Class<? extends U> asSubclass(Class<U> clazz) {
245151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (clazz.isAssignableFrom(this))
245251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return (Class<? extends U>) this;
245351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
245487778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak            throw new ClassCastException(this.toString() +
245587778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak                " cannot be cast to " + clazz.getName());
245651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
245751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
245851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
245951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException {@inheritDoc}
246051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
246151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
24627accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    @SuppressWarnings("unchecked")
246351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
24647accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller        Objects.requireNonNull(annotationClass);
246551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
246670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        A annotation = getDeclaredAnnotation(annotationClass);
246770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        if (annotation != null) {
246870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak            return annotation;
246970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        }
247070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak
247170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) {
247270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak            for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) {
247370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak                annotation = sup.getDeclaredAnnotation(annotationClass);
247470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak                if (annotation != null) {
247570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak                    return annotation;
247670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak                }
247770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak            }
247870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        }
247970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak
248070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        return null;
248170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    }
248270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak
24837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    /**
24847accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * {@inheritDoc}
24857accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws NullPointerException {@inheritDoc}
24867accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @since 1.5
24877accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     */
24887accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    @Override
24897accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
24907accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller        if (annotationClass == null) {
24917accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller            throw new NullPointerException("annotationClass == null");
249270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        }
249370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak
24947accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller        if (isDeclaredAnnotationPresent(annotationClass)) {
249570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak            return true;
249670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        }
249770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak
24987accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller        if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) {
249970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak            for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) {
25007accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller                if (sup.isDeclaredAnnotationPresent(annotationClass)) {
250170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak                    return true;
250270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak                }
250370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak            }
250470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        }
250570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak
250670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        return false;
250751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
250851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
250951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
25107accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws NullPointerException {@inheritDoc}
25117accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @since 1.8
251251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
25137accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    @Override
25147accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
25157accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      // Find any associated annotations [directly or repeatably (indirectly) present on this].
25167accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      A[] annotations = GenericDeclaration.super.getAnnotationsByType(annotationClass);
25177accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
25187accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      if (annotations.length != 0) {
25197accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller        return annotations;
25207accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      }
25217accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
25227accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      // Nothing was found, attempt looking for associated annotations recursively up to the root
25237accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      // class if and only if:
25247accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      // * The annotation class was marked with @Inherited.
25257accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      //
25267accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      // Inherited annotations are not coalesced into a single set: the first declaration found is
25277accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      // returned.
25287accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
25297accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) {
25307accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller        Class<?> superClass = getSuperclass();  // Returns null if klass's base is Object.
25317accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
25327accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller        if (superClass != null) {
25337accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller          return superClass.getAnnotationsByType(annotationClass);
25347accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller        }
25357accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      }
25367accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
25377accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      // Annotated was not marked with @Inherited, or no superclass.
25387accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller      return (A[]) Array.newInstance(annotationClass, 0);  // Safe by construction.
25397accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    }
25407accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller
25417accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    /**
25427accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @since 1.5
25437accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     */
25447accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    @Override
25457accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    public Annotation[] getAnnotations() {
254670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        /*
254770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak         * We need to get the annotations declared on this class, plus the
254870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak         * annotations from superclasses that have the "@Inherited" annotation
254970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak         * set.  We create a temporary map to use while we accumulate the
255070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak         * annotations and convert it to an array at the end.
255170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak         *
255270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak         * It's possible to have duplicates when annotations are inherited.
255370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak         * We use a Map to filter those out.
255470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak         *
255570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak         * HashMap might be overkill here.
255670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak         */
255770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        HashMap<Class<?>, Annotation> map = new HashMap<Class<?>, Annotation>();
255870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        for (Annotation declaredAnnotation : getDeclaredAnnotations()) {
255970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak            map.put(declaredAnnotation.annotationType(), declaredAnnotation);
256070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        }
256170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) {
256270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak            for (Annotation declaredAnnotation : sup.getDeclaredAnnotations()) {
256370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak                Class<? extends Annotation> clazz = declaredAnnotation.annotationType();
256470b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak                if (!map.containsKey(clazz) && clazz.isDeclaredAnnotationPresent(Inherited.class)) {
256570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak                    map.put(clazz, declaredAnnotation);
256670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak                }
256770b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak            }
256870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        }
256951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
257070b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        /* Convert annotation values from HashMap to array. */
257170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        Collection<Annotation> coll = map.values();
257270b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        return coll.toArray(new Annotation[coll.size()]);
257351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
257451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
257570b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    /**
25767accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @throws NullPointerException {@inheritDoc}
25779599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin     * @since 1.8
257851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
25799599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin    @Override
25807accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    public native <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass);
25819599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin
25829599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin    /**
25837accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller     * @since 1.5
25849599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin     */
25859599ec54b164da29db4e3386a9839aca73caf8eeIgor Murashkin    @Override
25867accc5fb09911a1dcc027f48089845f617f2912eNeil Fuller    public native Annotation[] getDeclaredAnnotations();
258751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
258851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
258970b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak     * Returns true if the annotation exists.
259051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
259170b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak    private native boolean isDeclaredAnnotationPresent(Class<? extends Annotation> annotationClass);
259251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2593d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao    private String getSignatureAttribute() {
2594d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao        String[] annotation = getSignatureAnnotation();
2595d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao        if (annotation == null) {
2596d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao            return null;
2597d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao        }
2598d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao        StringBuilder result = new StringBuilder();
2599d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao        for (String s : annotation) {
2600d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao            result.append(s);
2601d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao        }
2602d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao        return result.toString();
2603d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao    }
2604d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao
2605d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao    private native String[] getSignatureAnnotation();
2606d890de26ec16f7fc32b58a00725aff1b2a79a169Jeff Hao
2607a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    /**
2608a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * Is this a runtime created proxy class?
2609a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     *
2610a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * @hide
2611a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     */
2612a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public boolean isProxy() {
2613a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return (accessFlags & 0x00040000) != 0;
2614a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2615a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
2616a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    /**
2617a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * Returns the dex file from which this class was loaded.
2618a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     *
2619a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * @hide
2620a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     */
2621a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public Dex getDex() {
2622a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (dexCache == null) {
2623a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return null;
2624a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2625a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return dexCache.getDex();
2626a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2627a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    /**
2628a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * Returns a string from the dex cache, computing the string from the dex file if necessary.
2629a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     *
2630a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * @hide
2631a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     */
2632a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public String getDexCacheString(Dex dex, int dexStringIndex) {
263370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        String s = dexCache.getResolvedString(dexStringIndex);
2634a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (s == null) {
2635a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            s = dex.strings().get(dexStringIndex).intern();
263670b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak            dexCache.setResolvedString(dexStringIndex, s);
2637a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2638a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return s;
2639a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2640a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
2641a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    /**
2642a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * Returns a resolved type from the dex cache, computing the type from the dex file if
2643a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * necessary.
2644a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     *
2645a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * @hide
2646a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     */
2647a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public Class<?> getDexCacheType(Dex dex, int dexTypeIndex) {
264870b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak        Class<?> resolvedType = dexCache.getResolvedType(dexTypeIndex);
2649a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (resolvedType == null) {
2650a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            int descriptorIndex = dex.typeIds().get(dexTypeIndex);
2651a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            String descriptor = getDexCacheString(dex, descriptorIndex);
2652a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            resolvedType = InternalNames.getClass(getClassLoader(), descriptor);
265370b617fe562806bc3e15452a2792fec4355bd54fPrzemyslaw Szczepaniak            dexCache.setResolvedType(dexTypeIndex, resolvedType);
2654a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2655a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return resolvedType;
2656a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2657a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    /**
2658a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * The annotation directory offset of this class in its own Dex, or 0 if it
2659a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * is unknown.
2660a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     *
2661a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * TODO: 0 is a sentinel that means 'no annotations directory'; this should be -1 if unknown
2662a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     *
2663a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * @hide
2664a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     */
2665a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public int getDexAnnotationDirectoryOffset() {
2666a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        Dex dex = getDex();
2667a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (dex == null) {
2668a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return 0;
2669a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2670a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        int classDefIndex = getDexClassDefIndex();
2671a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (classDefIndex < 0) {
2672a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return 0;
2673a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2674a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return dex.annotationDirectoryOffsetFromClassDefIndex(classDefIndex);
2675a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2676a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    /**
2677a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * The type index of this class in its own Dex, or -1 if it is unknown. If a class is referenced
2678a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * by multiple Dex files, it will have a different type index in each. Dex files support 65534
2679a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * type indices, with 65535 representing no index.
2680a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     *
2681a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * @hide
2682a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     */
2683a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public int getDexTypeIndex() {
2684a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        int typeIndex = dexTypeIndex;
2685a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (typeIndex != 65535) {
2686a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return typeIndex;
2687a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2688a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        synchronized (this) {
2689a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            typeIndex = dexTypeIndex;
2690a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            if (typeIndex == 65535) {
2691a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                if (dexClassDefIndex >= 0) {
2692a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                    typeIndex = getDex().typeIndexFromClassDefIndex(dexClassDefIndex);
2693a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                } else {
2694a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                    typeIndex = getDex().findTypeIndex(InternalNames.getInternalName(this));
2695a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                    if (typeIndex < 0) {
2696a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                        typeIndex = -1;
2697a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                    }
2698a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                }
2699a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                dexTypeIndex = typeIndex;
2700a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            }
2701a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2702a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return typeIndex;
2703a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2704a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    private boolean canAccess(Class<?> c) {
2705a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if(Modifier.isPublic(c.accessFlags)) {
2706a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return true;
2707a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2708a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return inSamePackage(c);
2709a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2710a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
2711a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    private boolean canAccessMember(Class<?> memberClass, int memberModifiers) {
2712a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (memberClass == this || Modifier.isPublic(memberModifiers)) {
2713a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return true;
2714a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2715a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (Modifier.isPrivate(memberModifiers)) {
2716a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return false;
2717a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2718a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (Modifier.isProtected(memberModifiers)) {
2719a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            for (Class<?> parent = this.superClass; parent != null; parent = parent.superClass) {
2720a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                if (parent == memberClass) {
2721a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                    return true;
2722a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski                }
2723a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            }
2724a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2725a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return inSamePackage(memberClass);
2726a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2727a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
2728a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    private boolean inSamePackage(Class<?> c) {
2729a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (classLoader != c.classLoader) {
2730a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return false;
2731a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2732a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        String packageName1 = getPackageName$();
2733a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        String packageName2 = c.getPackageName$();
2734a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        if (packageName1 == null) {
2735a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return packageName2 == null;
2736a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        } else if (packageName2 == null) {
2737a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return false;
2738a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        } else {
2739a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            return packageName1.equals(packageName2);
2740a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        }
2741a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2742a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    /**
2743a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * @hide
2744a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     */
2745a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public int getAccessFlags() {
2746a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return accessFlags;
2747a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2748a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
27491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath
27501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    /**
27511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * Returns the method if it is defined by this class; {@code null} otherwise. This may return a
27521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * non-public member.
27531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     *
27541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * @param name the method name
27551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     * @param args the method's parameter types
27561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath     */
27571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath    private native Method getDeclaredMethodInternal(String name, Class<?>[] args);
2758a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski
2759a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    /**
2760a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * The class def of this class in its own Dex, or -1 if there is no class def.
2761a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     *
2762a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     * @hide
2763a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski     */
2764a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    public int getDexClassDefIndex() {
2765a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        return (dexClassDefIndex == 65535) ? -1 : dexClassDefIndex;
2766a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
2767a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    private static class Caches {
2768a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        /**
2769a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski         * Cache to avoid frequent recalculation of generic interfaces, which is generally uncommon.
2770a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski         * Sized sufficient to allow ConcurrentHashMapTest to run without recalculating its generic
2771a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski         * interfaces (required to avoid time outs). Validated by running reflection heavy code
2772a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski         * such as applications using Guice-like frameworks.
2773a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski         */
2774a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski        private static final BasicLruCache<Class, Type[]> genericInterfaces
2775a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski            = new BasicLruCache<Class, Type[]>(8);
2776a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski    }
277751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski}
2778