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