Class.java revision bac8ef10e8a32316e4eedc6163dd3776cf5e5d7e
151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. 351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it 651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as 751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation. Oracle designates this 851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided 951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code. 1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT 1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that 1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code). 1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version 1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation, 1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any 2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions. 2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.lang; 2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Array; 2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.GenericArrayType; 3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Member; 3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Field; 3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Method; 3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Constructor; 3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.GenericDeclaration; 3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Modifier; 3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Type; 3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.TypeVariable; 3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.InvocationTargetException; 3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.ref.SoftReference; 4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.InputStream; 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.ObjectStreamField; 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.security.AccessController; 4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.security.PrivilegedAction; 4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.ArrayList; 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Arrays; 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Collection; 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.HashSet; 4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Iterator; 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.List; 5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.LinkedList; 5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.LinkedHashSet; 5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Set; 5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Map; 5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.HashMap; 5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.misc.Unsafe; 5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.reflect.CallerSensitive; 5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.annotation.Annotation; 5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.Proxy; 5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.reflect.annotation.*; 6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.reflect.misc.ReflectUtil; 6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 62a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport java.io.Serializable; 63a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport java.lang.reflect.AccessibleObject; 64a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport com.android.dex.Dex; 65a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport dalvik.system.VMStack; 66a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.AnnotationAccess; 67a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.InternalNames; 68a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.GenericSignatureParser; 69a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.Types; 70a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.BasicLruCache; 71a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.CollectionUtils; 72a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.EmptyArray; 73a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.SneakyThrow; 741ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamathimport java.util.Collections; 75a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Instances of the class {@code Class} represent classes and 7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces in a running Java application. An enum is a kind of 7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class and an annotation is a kind of interface. Every array also 8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * belongs to a class that is reflected as a {@code Class} object 8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that is shared by all arrays with the same element type and number 8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of dimensions. The primitive Java types ({@code boolean}, 8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code byte}, {@code char}, {@code short}, 8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int}, {@code long}, {@code float}, and 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code double}), and the keyword {@code void} are also 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented as {@code Class} objects. 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> {@code Class} has no public constructor. Instead {@code Class} 8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects are constructed automatically by the Java Virtual Machine as classes 9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are loaded and by calls to the {@code defineClass} method in the class 9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader. 9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The following example uses a {@code Class} object to print the 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class name of an object: 9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> <blockquote><pre> 9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * void printClassName(Object obj) { 9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * System.out.println("The class of " + obj + 9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * " is " + obj.getClass().getName()); 10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * } 10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> It is also possible to get the {@code Class} object for a named 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type (or for void) using a class literal. See Section 15.8.2 of 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>. 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For example: 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> <blockquote> 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());} 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param <T> the type of the class modeled by this {@code Class} 11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. For example, the type of {@code String.class} is {@code 11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class<String>}. Use {@code Class<?>} if the class being modeled is 11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * unknown. 11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author unascribed 11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#defineClass(byte[], int, int) 11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic final 12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski class Class<T> implements java.io.Serializable, 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski java.lang.reflect.GenericDeclaration, 12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski java.lang.reflect.Type, 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski java.lang.reflect.AnnotatedElement { 1261ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** use serialVersionUID from JDK 1.1 for interoperability */ 1271ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private static final long serialVersionUID = 3206093459760846163L; 1281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1291ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** defining class loader, or null for the "bootstrap" system loader. */ 1301ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient ClassLoader classLoader; 1311ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1321ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1331ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * For array classes, the component class object for instanceof/checkcast (for String[][][], 1341ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * this will be String[][]). null for non-array classes. 1351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Class<?> componentType; 1371ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * DexCache of resolved constant pool entries. Will be null for certain runtime-generated classes 1391ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * e.g. arrays and primitive classes. 1401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient DexCache dexCache; 1421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Short-cut to dexCache.strings */ 1441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient String[] dexCacheStrings; 1451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1461ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * The interface table (iftable_) contains pairs of a interface class and an array of the 1481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * interface methods. There is one pair per interface supported by this class. That 1491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * means one pair for each interface we support directly, indirectly via superclass, or 1501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * indirectly via a superinterface. This will be null if neither we nor our superclass 1511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * implement any interfaces. 1521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 1531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()". 1541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Invoke faceObj.blah(), where "blah" is part of the Face interface. We can't easily use a 1551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * single vtable. 1561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 1571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * For every interface a concrete class implements, we create an array of the concrete vtable_ 1581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * methods for the methods in the interface. 1591ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Object[] ifTable; 1611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Lazily computed name of this class; always prefer calling getName(). */ 1631ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient String name; 1641ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1651ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** The superclass, or null if this is java.lang.Object, an interface or primitive type. */ 1661ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Class<? super T> superClass; 1671ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1681ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** If class verify fails, we must return same error on subsequent tries. */ 1691ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Class<?> verifyErrorClass; 1701ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1711ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1721ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass 1731ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * is copied in, and virtual methods from our class either replace those from the super or are 1741ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * appended. For abstract classes, methods may be created in the vtable that aren't in 1751ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * virtual_ methods_ for miranda methods. 1761ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1771ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient Object vtable; 1781ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1791ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** access flags; low 16 bits are defined by VM spec */ 1801ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int accessFlags; 1811ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1821ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** static, private, and <init> methods. */ 1831ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient long directMethods; 1841ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1851ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 1861ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Instance fields. These describe the layout of the contents of an Object. Note that only the 1871ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * fields directly declared by this class are listed in iFields; fields declared by a 1881ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * superclass are listed in the superclass's Class.iFields. 1891ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 1901ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * All instance fields that refer to objects are guaranteed to be at the beginning of the field 1911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * list. {@link Class#numReferenceInstanceFields} specifies the number of reference fields. 1921ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 1931ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient long iFields; 1941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1951ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Static fields */ 1961ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient long sFields; 1971ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 1981ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Virtual methods defined in this class; invoked through vtable. */ 1991ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient long virtualMethods; 2001ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2011ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2021ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Total size of the Class instance; used when allocating storage on GC heap. 2031ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * See also {@link Class#objectSize}. 2041ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2051ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int classSize; 2061ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2071ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2081ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * tid used to check for recursive static initializer invocation. 2091ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2101ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int clinitThreadId; 2111ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Class def index from dex file. An index of 65535 indicates that there is no class definition, 2141ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * for example for an array type. 2151ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * TODO: really 16bits as type indices are 16bit. 2161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int dexClassDefIndex; 2181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Class type index from dex file, lazily computed. An index of 65535 indicates that the type 2211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * index isn't known. Volatile to avoid double-checked locking bugs. 2221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * TODO: really 16bits as type indices are 16bit. 2231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2241ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient volatile int dexTypeIndex; 2251ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2261ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Number of direct methods. */ 2271ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int numDirectMethods; 2281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2291ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Number of instance fields. */ 2301ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int numInstanceFields; 2311ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2321ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Number of instance fields that are object references. */ 2331ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int numReferenceInstanceFields; 2341ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Number of static fields that are object references. */ 2361ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int numReferenceStaticFields; 2371ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2381ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Number of static fields. */ 2391ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int numStaticFields; 2401ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Number of virtual methods. */ 2421ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int numVirtualMethods; 2431ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2441ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2451ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Total object size; used when allocating storage on GC heap. For interfaces and abstract 2461ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * classes this will be zero. See also {@link Class#classSize}. 2471ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int objectSize; 2491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 2511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * The lower 16 bits is the primitive type value, or 0 if not a primitive type; set for 2521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * generated primitive classes. 2531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 2541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int primitiveType; 2551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** Bitmap of offsets of iFields. */ 2571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int referenceInstanceOffsets; 2581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2591ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** State of class initialization */ 2601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private transient int status; 2611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private AnnotationType annotationType; 2631ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 2642ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong private static final int ANNOTATION = 0x00002000; 2652ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong private static final int ENUM = 0x00004000; 2662ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong private static final int SYNTHETIC = 0x00001000; 2672ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong private static final int FINALIZABLE = 0x80000000; 26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor. Only the Java Virtual Machine creates Class 27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects. 27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private Class() {} 27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Converts the object to a string. The string representation is the 27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * string "class" or "interface", followed by a space, and then by the 27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * fully qualified name of the class in the format returned by 28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code getName}. If this {@code Class} object represents a 28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, this method returns the name of the primitive type. If 28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents void this method returns 28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "void". 28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of this class object. 28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String toString() { 28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) 28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski + getName(); 29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} object associated with the class or 29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface with the given string name. Invoking this method is 29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent to: 29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName(className, true, currentLoader)} 30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * where {@code currentLoader} denotes the defining class loader of 30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the current class. 30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> For example, the following code fragment returns the 30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * runtime {@code Class} descriptor for the class named 30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code java.lang.Thread}: 30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class t = Class.forName("java.lang.Thread")} 31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A call to {@code forName("X")} causes the class named 31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code X} to be initialized. 31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param className the fully qualified name of the desired class. 31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Class} object for the class with the 31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified name. 31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception LinkageError if the linkage fails 32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization provoked 32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this method fails 32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ClassNotFoundException if the class cannot be located 32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Class<?> forName(String className) 32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws ClassNotFoundException { 327a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return forName(className, true, VMStack.getCallingClassLoader()); 32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} object associated with the class or 33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface with the given string name, using the given class loader. 33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Given the fully qualified name for a class or interface (in the same 33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * format returned by {@code getName}) this method attempts to 33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * locate, load, and link the class or interface. The specified class 33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader is used to load the class or interface. If the parameter 33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code loader} is null, the class is loaded through the bootstrap 33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader. The class is initialized only if the 34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code initialize} parameter is {@code true} and if it has 34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not been initialized earlier. 34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If {@code name} denotes a primitive type or void, an attempt 34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will be made to locate a user-defined class in the unnamed package whose 34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name is {@code name}. Therefore, this method cannot be used to 34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtain any of the {@code Class} objects representing primitive 34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types or void. 34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If {@code name} denotes an array class, the component type of 35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array class is loaded but not initialized. 35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> For example, in an instance method the expression: 35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName("Foo")} 35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is equivalent to: 35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName("Foo", true, this.getClass().getClassLoader())} 36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method throws errors related to loading, linking or 36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The 36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Java Language Specification</em>. 36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method does not check whether the requested class 36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is accessible to its caller. 36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the {@code loader} is {@code null}, and a security 37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager is present, and the caller's class loader is not null, then this 37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method calls the security manager's {@code checkPermission} method 37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with a {@code RuntimePermission("getClassLoader")} permission to 37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ensure it's ok to access the bootstrap class loader. 37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name fully qualified name of the desired class 37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param initialize whether the class must be initialized 37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param loader class loader from which the class must be loaded 37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return class object representing the desired class 38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception LinkageError if the linkage fails 38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization provoked 38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this method fails 38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ClassNotFoundException if the class cannot be located by 38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified class loader 38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Class#forName(String) 38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader 38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Class<?> forName(String name, boolean initialize, 39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ClassLoader loader) 39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws ClassNotFoundException 39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (loader == null) { 397bac8ef10e8a32316e4eedc6163dd3776cf5e5d7eNarayan Kamath loader = BootClassLoader.getInstance(); 398a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 399a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 400a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> result; 401a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski try { 402a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = classForName(name, initialize, loader); 403a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } catch (ClassNotFoundException e) { 404a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Throwable cause = e.getCause(); 405a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (cause instanceof LinkageError) { 406a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw (LinkageError) cause; 40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 408a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw e; 40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 410a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** Called after security checks have been made. */ 414a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski static native Class<?> classForName(String className, boolean shouldInitialize, 415a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader classLoader) throws ClassNotFoundException; 41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Creates a new instance of the class represented by this {@code Class} 41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The class is instantiated as if by a {@code new} 42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * expression with an empty argument list. The class is initialized if it 42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has not already been initialized. 42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that this method propagates any exception thrown by the 42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * nullary constructor, including a checked exception. Use of 42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method effectively bypasses the compile-time exception 42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * checking that would otherwise be performed by the compiler. 42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The {@link 42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Constructor#newInstance(java.lang.Object...) 42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor.newInstance} method avoids this problem by wrapping 43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * any exception thrown by the constructor in a (checked) {@link 43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.InvocationTargetException}. 43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a newly allocated instance of the class represented by this 43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. 43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalAccessException if the class or its nullary 43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructor is not accessible. 43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception InstantiationException 43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if this {@code Class} represents an abstract class, 43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an interface, an array class, a primitive type, or void; 44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or if the class has no nullary constructor; 44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or if the instantiation fails for some other reason. 44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization 44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * provoked by this method fails. 44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 44551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 44651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 44751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 44951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 45151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 45251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * creation of new instances of this class 45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 45651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 45751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 45851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 45951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 46151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 46251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 4651ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native T newInstance() throws InstantiationException, IllegalAccessException; 46651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 46751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Object} is assignment-compatible 46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with the object represented by this {@code Class}. This method is 47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the dynamic equivalent of the Java language {@code instanceof} 47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * operator. The method returns {@code true} if the specified 47251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} argument is non-null and can be cast to the 47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * reference type represented by this {@code Class} object without 47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * raising a {@code ClassCastException.} It returns {@code false} 47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise. 47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, if this {@code Class} object represents a 47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared class, this method returns {@code true} if the specified 47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} argument is an instance of the represented class (or 48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of any of its subclasses); it returns {@code false} otherwise. If 48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents an array class, this method 48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code true} if the specified {@code Object} argument 48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * can be converted to an object of the array class by an identity 48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * conversion or by a widening reference conversion; it returns 48551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. If this {@code Class} object 48651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents an interface, this method returns {@code true} if the 48751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class or any superclass of the specified {@code Object} argument 48851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implements this interface; it returns {@code false} otherwise. If 48951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type, this method 49051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code false}. 49151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 4929e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param object the object to check 49351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if {@code obj} is an instance of this class 49451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 49551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 49651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 497a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isInstance(Object object) { 498a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (object == null) { 499a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 500a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 501a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return isAssignableFrom(object.getClass()); 502a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the class or interface represented by this 50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object is either the same as, or is a superclass or 50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterface of, the class or interface represented by the specified 50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} parameter. It returns {@code true} if so; 51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise it returns {@code false}. If this {@code Class} 51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object represents a primitive type, this method returns 51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code true} if the specified {@code Class} parameter is 51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exactly this {@code Class} object; otherwise it returns 51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. 51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, this method tests whether the type represented by the 51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified {@code Class} parameter can be converted to the type 51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object via an identity conversion 51951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or via a widening reference conversion. See <em>The Java Language 52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Specification</em>, sections 5.1.1 and 5.1.4 , for details. 52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 5229e78cee3f3edf84254174717f475605d712aad1cNarayan Kamath * @param c the {@code Class} object to be checked 52351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code boolean} value indicating whether objects of the 52451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type {@code cls} can be assigned to objects of this class 52551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if the specified Class parameter is 52651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null. 52751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 52851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 529a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isAssignableFrom(Class<?> c) { 530a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (this == c) { 531a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; // Can always assign to things of the same type. 532a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (this == Object.class) { 533a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return !c.isPrimitive(); // Can assign any reference to java.lang.Object. 534a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isArray()) { 535a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return c.isArray() && componentType.isAssignableFrom(c.componentType); 536a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isInterface()) { 537a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search iftable which has a flattened and uniqued list of interfaces. 538a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = c.ifTable; 539a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 540a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 5411ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath if (iftable[i] == this) { 542a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 543a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 544a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 545a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 546a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 547a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 548a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!c.isInterface()) { 549a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (c = c.superClass; c != null; c = c.superClass) { 550a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == this) { 551a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 552a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 553a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 554a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 555a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 556a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 557a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 55951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 56051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 56151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Class} object represents an 56251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface type. 56351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 56451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this object represents an interface; 56551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 56651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 567a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isInterface() { 568a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (accessFlags & Modifier.INTERFACE) != 0; 569a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 57051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 57151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 57251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 57351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if this {@code Class} object represents an array class. 57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 57551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this object represents an array class; 57651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 579a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isArray() { 580a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getComponentType() != null; 581a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 58251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 58451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 58551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Class} object represents a 58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type. 58751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 58851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> There are nine predefined {@code Class} objects to represent 58951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the eight primitive types and void. These are created by the Java 59051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Virtual Machine, and have the same names as the primitive types that 59151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * they represent, namely {@code boolean}, {@code byte}, 59251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code char}, {@code short}, {@code int}, 59351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code long}, {@code float}, and {@code double}. 59451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 59551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> These objects may only be accessed via the following public static 59651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * final variables, and are the only {@code Class} objects for which 59751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method returns {@code true}. 59851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 59951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if and only if this class represents a primitive type 60051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Boolean#TYPE 60251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Character#TYPE 60351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Byte#TYPE 60451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Short#TYPE 60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Integer#TYPE 60651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Long#TYPE 60751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Float#TYPE 60851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Double#TYPE 60951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Void#TYPE 61051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 61151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 612a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isPrimitive() { 6131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return (primitiveType & 0xFFFF) != 0; 614a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 61551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 61651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 6172ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * Indicates whether this {@code Class} or its parents override finalize. 6182ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * 6192ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * @return {@code true} if and if this class or its parents override 6202ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * finalize; 6212ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * 6222ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong * @hide 6232ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong */ 6242ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong public boolean isFinalizable() { 6252ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong return (getModifiers() & FINALIZABLE) != 0; 6262ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong } 6272ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong 6282ebb445676e3f0170930e5c47e7a86d20332e5a8Yi Kong /** 62951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns true if this {@code Class} object represents an annotation 63051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type. Note that if this method returns true, {@link #isInterface()} 63151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * would also return true, as all annotation types are also interfaces. 63251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 63351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this class object represents an annotation 63451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type; {@code false} otherwise 63551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isAnnotation() { 63851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (getModifiers() & ANNOTATION) != 0; 63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if this class is a synthetic class; 64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code false} otherwise. 64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a synthetic class as 64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * defined by the Java Language Specification. 64651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 64751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 64851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isSynthetic() { 64951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (getModifiers() & SYNTHETIC) != 0; 65051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 65151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 65251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the name of the entity (class, interface, array class, 65451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, or void) represented by this {@code Class} object, 65551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as a {@code String}. 65651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 65751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a reference type that is not an 65851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array type then the binary name of the class is returned, as specified 65951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by 66051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>. 66151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 66251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a primitive type or void, then the 66351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name returned is a {@code String} equal to the Java language 66451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * keyword corresponding to the primitive type or void. 66551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 66651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a class of arrays, then the internal 66751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * form of the name consists of the name of the element type preceded by 66851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one or more '{@code [}' characters representing the depth of the array 66951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * nesting. The encoding of element type names is as follows: 67051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 67151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><table summary="Element types and encodings"> 67251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><th> Element Type <th> <th> Encoding 67351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> boolean <td> <td align=center> Z 67451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> byte <td> <td align=center> B 67551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> char <td> <td align=center> C 67651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> class or interface 67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td> <td align=center> L<i>classname</i>; 67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> double <td> <td align=center> D 67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> float <td> <td align=center> F 68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> int <td> <td align=center> I 68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> long <td> <td align=center> J 68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> short <td> <td align=center> S 68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </table></blockquote> 68451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 68551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The class or interface name <i>classname</i> is the binary name of 68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class specified above. 68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 68851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Examples: 68951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 69051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * String.class.getName() 69151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "java.lang.String" 69251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * byte.class.getName() 69351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "byte" 69451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (new Object[3]).getClass().getName() 69551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "[Ljava.lang.Object;" 69651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (new int[3][4][5][6][7][8][9]).getClass().getName() 69751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "[[[[[[[I" 69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 70051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the name of the class or interface 70151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 70251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 70351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getName() { 70451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String name = this.name; 70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (name == null) 706a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski this.name = name = getNameNative(); 70751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 70851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 70951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 710a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native String getNameNative(); 71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the class loader for the class. Some implementations may use 71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null to represent the bootstrap class loader. This method will return 71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null in such implementations if this class was loaded by the bootstrap 71651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader. 71751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 71851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If a security manager is present, and the caller's class loader is 71951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not null and the caller's class loader is not the same as or an ancestor of 72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class loader for the class whose class loader is requested, then 72151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method calls the security manager's {@code checkPermission} 72251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method with a {@code RuntimePermission("getClassLoader")} 72351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * permission to ensure it's ok to access the class loader for the class. 72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object 72651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type or void, null is returned. 72751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 72851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the class loader that loaded the class or interface 72951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 73051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 73151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if a security manager exists and its 73251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code checkPermission} method denies 73351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the class loader for the class. 73451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader 73551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 73651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.RuntimePermission 73751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 73851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public ClassLoader getClassLoader() { 740a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isPrimitive()) { 74151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 74251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 743a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (classLoader == null) ? BootClassLoader.getInstance() : classLoader; 74451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 74551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 74651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 74751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 74851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 74951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code TypeVariable} objects that represent the 75051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type variables declared by the generic declaration represented by this 75151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code GenericDeclaration} object, in declaration order. Returns an 75251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if the underlying generic declaration declares no type 75351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * variables. 75451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 75551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of {@code TypeVariable} objects that represent 75651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the type variables declared by this generic declaration 75751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError if the generic 75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * signature of this generic declaration does not conform to 75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the format specified in 76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 76151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 763a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski @Override public synchronized TypeVariable<Class<T>>[] getTypeParameters() { 764a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String annotationSignature = AnnotationAccess.getSignature(this); 765a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature == null) { 766a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return EmptyArray.TYPE_VARIABLE; 767a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 768a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 769a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 770a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return parser.formalTypeParameters; 77151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 77551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} representing the superclass of the entity 77651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (class, interface, primitive type or void) represented by this 77751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class}. If this {@code Class} represents either the 77851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} class, an interface, a primitive type, or void, then 77951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null is returned. If this object represents an array class then the 78051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object representing the {@code Object} class is 78151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. 78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 78351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the superclass of the class represented by this object. 78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 785a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<? super T> getSuperclass() { 786a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // For interfaces superClass is Object (which agrees with the JNI spec) 787a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // but not with the expected behavior here. 788a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isInterface()) { 789a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 790a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 791a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return superClass; 792a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 793a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Type} representing the direct superclass of 79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the entity (class, interface, primitive type or void) represented by 79951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class}. 80051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 80151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the superclass is a parameterized type, the {@code Type} 80251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object returned must accurately reflect the actual type 80351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameters used in the source code. The parameterized type 80451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing the superclass is created if it had not been 80551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * created before. See the declaration of {@link 80651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.ParameterizedType ParameterizedType} for the 80751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * semantics of the creation process for parameterized types. If 80851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} represents either the {@code Object} 80951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, an interface, a primitive type, or void, then null is 81051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. If this object represents an array class then the 81151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object representing the {@code Object} class is 81251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. 81351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 81451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError if the generic 81551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class signature does not conform to the format specified in 81651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 81751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws TypeNotPresentException if the generic superclass 81851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * refers to a non-existent type declaration 81951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.MalformedParameterizedTypeException if the 82051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * generic superclass refers to a parameterized type that cannot be 82151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instantiated for any reason 82251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the superclass of the class represented by this object 82351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 82451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 82551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Type getGenericSuperclass() { 826a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Type genericSuperclass = getSuperclass(); 827a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // This method is specified to return null for all cases where getSuperclass 828a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // returns null, i.e, for primitives, interfaces, void and java.lang.Object. 829a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (genericSuperclass == null) { 830a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 831a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 832a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 833a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String annotationSignature = AnnotationAccess.getSignature(this); 834a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature != null) { 835a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 836a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 837a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski genericSuperclass = parser.superclassType; 838a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 839a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return Types.getType(genericSuperclass); 84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 84351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Gets the package for this class. The class loader of this class is used 84451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to find the package. If the class was loaded by the bootstrap class 84551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader the set of packages loaded from CLASSPATH is searched to find the 84651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * package of the class. Null is returned if no package object was created 84751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the class loader of this class. 84851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 84951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Packages have attributes for versions and specifications only if the 85051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * information was defined in the manifests that accompany the classes, and 85151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the class loader created the package instance with the attributes 85251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * from the manifest. 85351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 85451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the package of the class, or null if no package 85551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * information is available from the archive or codebase. 85651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 85751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Package getPackage() { 858a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader loader = getClassLoader(); 859a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (loader != null) { 860a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName = getPackageName$(); 861a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName != null ? loader.getPackage(packageName) : null; 862a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 863a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 864a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 865a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 866a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 867a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns the package name of this class. This returns null for classes in 868a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * the default package. 869a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 870a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 871a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 872a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public String getPackageName$() { 873a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String name = getName(); 874a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int last = name.lastIndexOf('.'); 875a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return last == -1 ? null : name.substring(0, last); 87651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 87751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 87851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 87951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 88051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines the interfaces implemented by the class or interface 88151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 88251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 88351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class, the return value is an array 88451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * containing objects representing all interfaces implemented by the 88551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. The order of the interface objects in the array corresponds to 88651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the order of the interface names in the {@code implements} clause 88751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the declaration of the class represented by this object. For 88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, given the declaration: 88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code class Shimmer implements FloorWax, DessertTopping { ... }} 89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * suppose the value of {@code s} is an instance of 89351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Shimmer}; the value of the expression: 89451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 89551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code s.getClass().getInterfaces()[0]} 89651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 89751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is the {@code Class} object that represents interface 89851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code FloorWax}; and the value of: 89951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 90051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code s.getClass().getInterfaces()[1]} 90151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 90251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is the {@code Class} object that represents interface 90351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code DessertTopping}. 90451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 90551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents an interface, the array contains objects 90651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing all interfaces extended by the interface. The order of the 90751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface objects in the array corresponds to the order of the interface 90851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * names in the {@code extends} clause of the declaration of the 90951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface represented by this object. 91051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 91151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class or interface that implements no 91251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces, the method returns an array of length 0. 91351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 91451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a primitive type or void, the method 91551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns an array of length 0. 91651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 91751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of interfaces implemented by this class. 91851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 919a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?>[] getInterfaces() { 920a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isArray()) { 921a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return new Class<?>[] { Cloneable.class, Serializable.class }; 922a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isProxy()) { 923a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getProxyInterfaces(); 924a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 925a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Dex dex = getDex(); 926a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dex == null) { 927a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return EmptyArray.CLASS; 928a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 929a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski short[] interfaces = dex.interfaceTypeIndicesFromClassDefIndex(dexClassDefIndex); 930a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?>[] result = new Class<?>[interfaces.length]; 931a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < interfaces.length; i++) { 932a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result[i] = getDexCacheType(dex, interfaces[i]); 933a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 934a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 935a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 936a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 937a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Returns the interfaces that this proxy class directly implements. 938a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native Class<?>[] getProxyInterfaces(); 93951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 94051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 94151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Type}s representing the interfaces 94251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * directly implemented by the class or interface represented by 94351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this object. 94451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 94551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If a superinterface is a parameterized type, the 94651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Type} object returned for it must accurately reflect 94751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the actual type parameters used in the source code. The 94851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameterized type representing each superinterface is created 94951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if it had not been created before. See the declaration of 95051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link java.lang.reflect.ParameterizedType ParameterizedType} 95151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for the semantics of the creation process for parameterized 95251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types. 95351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 95451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class, the return value is an 95551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array containing objects representing all interfaces 95651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implemented by the class. The order of the interface objects in 95751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array corresponds to the order of the interface names in 95851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the {@code implements} clause of the declaration of the class 95951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. In the case of an array class, the 96051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces {@code Cloneable} and {@code Serializable} are 96151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned in that order. 96251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 96351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents an interface, the array contains 96451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects representing all interfaces directly extended by the 96551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface. The order of the interface objects in the array 96651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * corresponds to the order of the interface names in the 96751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code extends} clause of the declaration of the interface 96851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 96951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 97051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents a class or interface that 97151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implements no interfaces, the method returns an array of length 97251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 0. 97351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 97451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents a primitive type or void, the 97551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns an array of length 0. 97651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 97751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError 97851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the generic class signature does not conform to the format 97951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified in 98051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 98151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws TypeNotPresentException if any of the generic 98251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces refers to a non-existent type declaration 98351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.MalformedParameterizedTypeException 98451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if any of the generic superinterfaces refer to a parameterized 98551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type that cannot be instantiated for any reason 98651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of interfaces implemented by this class 98751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 98851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 98951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Type[] getGenericInterfaces() { 990a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Type[] result; 991a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized (Caches.genericInterfaces) { 992a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = Caches.genericInterfaces.get(this); 993a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null) { 994a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String annotationSignature = AnnotationAccess.getSignature(this); 995a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature == null) { 996a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = getInterfaces(); 997a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 998a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 999a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 1000a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = Types.getTypeArray(parser.interfaceTypes, false); 1001a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1002a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Caches.genericInterfaces.put(this, result); 1003a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1004a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1005a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (result.length == 0) ? result : result.clone(); 100651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 100751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 100851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 100951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 101051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} representing the component type of an 101151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. If this class does not represent an array class this method 101251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns null. 101351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 101451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Class} representing the component type of this 101551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class if this class is an array 101651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.reflect.Array 101751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 101851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1019a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?> getComponentType() { 1020a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return componentType; 1021a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 102251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 102351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 102451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the Java language modifiers for this class or interface, encoded 102551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in an integer. The modifiers consist of the Java Virtual Machine's 102651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constants for {@code public}, {@code protected}, 102751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code private}, {@code final}, {@code static}, 102851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code abstract} and {@code interface}; they should be decoded 102951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * using the methods of class {@code Modifier}. 103051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 103151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the underlying class is an array class, then its 103251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code public}, {@code private} and {@code protected} 103351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * modifiers are the same as those of its component type. If this 103451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} represents a primitive type or void, its 103551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code public} modifier is always {@code true}, and its 103651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code protected} and {@code private} modifiers are always 103751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. If this object represents an array class, a 103851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type or void, then its {@code final} modifier is always 103951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code true} and its interface modifier is always 104051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. The values of its other modifiers are not determined 104151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this specification. 104251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 104351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The modifier encodings are defined in <em>The Java Virtual Machine 104451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Specification</em>, table 4.1. 104551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 104651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code int} representing the modifiers for this class 104751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.reflect.Modifier 104851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 104951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1050a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getModifiers() { 1051a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Array classes inherit modifiers from their component types, but in the case of arrays 1052a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // of an inner class, the class file may contain "fake" access flags because it's not valid 1053a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // for a top-level class to private, say. The real access flags are stored in the InnerClass 1054a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // attribute, so we need to make sure we drill down to the inner class: the accessFlags 1055a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // field is not the value we want to return, and the synthesized array class does not itself 1056a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // have an InnerClass attribute. https://code.google.com/p/android/issues/detail?id=56267 1057a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isArray()) { 1058a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int componentModifiers = getComponentType().getModifiers(); 1059a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if ((componentModifiers & Modifier.INTERFACE) != 0) { 1060a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski componentModifiers &= ~(Modifier.INTERFACE | Modifier.STATIC); 1061a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1062a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return Modifier.ABSTRACT | Modifier.FINAL | componentModifiers; 1063a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1064a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int JAVA_FLAGS_MASK = 0xffff; 1065a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int modifiers = AnnotationAccess.getInnerClassFlags(this, accessFlags & JAVA_FLAGS_MASK); 1066a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return modifiers & JAVA_FLAGS_MASK; 1067a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 106851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 106951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 107051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 107151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Gets the signers of this class. 107251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 107351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the signers of this class, or null if there are no signers. In 107451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular, this method returns null if this object represents 107551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a primitive type or void. 107651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 107751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1078a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Object[] getSigners() { 1079a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 1080a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 108151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 108251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 108351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 108451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents a local or anonymous 108551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class within a method, returns a {@link 108651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Method Method} object representing the 108751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * immediately enclosing method of the underlying class. Returns 108851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} otherwise. 108951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 109051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * In particular, this method returns {@code null} if the underlying 109151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class is a local or anonymous class immediately enclosed by a type 109251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declaration, instance initializer or static initializer. 109351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 109451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing method of the underlying class, if 109551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that class is a local or anonymous class; otherwise {@code null}. 109651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 109751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 109851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 109951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getEnclosingMethod() { 1100a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classNameImpliesTopLevel()) { 110151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 110251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 1103a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski AccessibleObject result = AnnotationAccess.getEnclosingMethodOrConstructor(this); 1104a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result instanceof Method ? (Method) result : null; 110551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 110651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 110751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 110851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 110951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents a local or anonymous 111051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class within a constructor, returns a {@link 111151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Constructor Constructor} object representing 111251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the immediately enclosing constructor of the underlying 111351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. Returns {@code null} otherwise. In particular, this 111451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns {@code null} if the underlying class is a local 111551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or anonymous class immediately enclosed by a type declaration, 111651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instance initializer or static initializer. 111751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 111851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing constructor of the underlying class, if 111951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that class is a local or anonymous class; otherwise {@code null}. 112051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 112151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 112251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 112351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?> getEnclosingConstructor() { 1124a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classNameImpliesTopLevel()) { 112551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 112651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 1127a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski AccessibleObject result = AnnotationAccess.getEnclosingMethodOrConstructor(this); 1128a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result instanceof Constructor ? (Constructor<?>) result : null; 1129a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1130a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1131a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean classNameImpliesTopLevel() { 1132a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return !getName().contains("$"); 113351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 113451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 113551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 113651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 113751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the class or interface represented by this {@code Class} object 113851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a member of another class, returns the {@code Class} object 113951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing the class in which it was declared. This method returns 114051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null if this class or interface is not a member of any other class. If 114151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents an array class, a primitive 114251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type, or void,then this method returns null. 114351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 114451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the declaring class for this class 114551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 114651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1147a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?> getDeclaringClass() { 1148a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (AnnotationAccess.isAnonymousClass(this)) { 1149a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 1150a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1151a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return AnnotationAccess.getEnclosingClass(this); 1152a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 115351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 115451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 115551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 115651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the immediately enclosing class of the underlying 115751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. If the underlying class is a top level class this 115851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns {@code null}. 115951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing class of the underlying class 116051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 116151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 116251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 116351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Class<?> getEnclosingClass() { 1164a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> declaringClass = getDeclaringClass(); 1165a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (declaringClass != null) { 1166a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return declaringClass; 116751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 1168a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski AccessibleObject member = AnnotationAccess.getEnclosingMethodOrConstructor(this); 1169a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (member != null) { 1170a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return ((Member) member).getDeclaringClass(); 1171a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1172a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return AnnotationAccess.getEnclosingClass(this); 117351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 117451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 117651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the simple name of the underlying class as given in the 117751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code. Returns an empty string if the underlying class is 117851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * anonymous. 117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The simple name of an array is the simple name of the 118151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * component type with "[]" appended. In particular the simple 118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name of an array whose component type is anonymous is "[]". 118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 118451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the simple name of the underlying class 118551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 118651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 118751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getSimpleName() { 118851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isArray()) 118951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getComponentType().getSimpleName()+"[]"; 119051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 119151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String simpleName = getSimpleBinaryName(); 119251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (simpleName == null) { // top level class 119351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski simpleName = getName(); 119451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name 119551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 119651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // According to JLS3 "Binary Compatibility" (13.1) the binary 119751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // name of non-package classes (not top level) is the binary 119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // name of the immediately enclosing class followed by a '$' followed by: 119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // (for nested and inner classes): the simple name. 120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // (for local classes): 1 or more digits followed by the simple name. 120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // (for anonymous classes): 1 or more digits. 120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Since getSimpleBinaryName() will strip the binary name of 120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // the immediatly enclosing class, we are now looking at a 120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // string that matches the regular expression "\$[0-9]*" 120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // followed by a simple name (considering the simple of an 120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // anonymous class to be the empty string). 120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Remove leading "\$[0-9]*" from the name 121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = simpleName.length(); 121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (length < 1 || simpleName.charAt(0) != '$') 121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new InternalError("Malformed class name"); 121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int index = 1; 121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (index < length && isAsciiDigit(simpleName.charAt(index))) 121551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski index++; 121651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Eventually, this is the empty string iff this is an anonymous class 121751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return simpleName.substring(index); 121851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 121951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 122051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 122151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Character.isDigit answers {@code true} to some non-ascii 122251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * digits. This one does not. 122351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 122451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static boolean isAsciiDigit(char c) { 122551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return '0' <= c && c <= '9'; 122651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 122751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 122851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 122951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the canonical name of the underlying class as 123051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * defined by the Java Language Specification. Returns null if 123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the underlying class does not have a canonical name (i.e., if 123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is a local or anonymous class or an array whose component 123351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type does not have a canonical name). 123451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the canonical name of the underlying class if it exists, and 123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} otherwise. 123651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 123751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 123851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getCanonicalName() { 123951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isArray()) { 124051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String canonicalName = getComponentType().getCanonicalName(); 124151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (canonicalName != null) 124251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return canonicalName + "[]"; 124351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 124451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 124551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 124651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isLocalOrAnonymousClass()) 124751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 124851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> enclosingClass = getEnclosingClass(); 124951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingClass == null) { // top level class 125051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getName(); 125151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 125251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String enclosingName = enclosingClass.getCanonicalName(); 125351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingName == null) 125451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 125551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return enclosingName + "." + getSimpleName(); 125651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 125751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 125851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 125951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 126051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 126151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is an anonymous class. 126251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is an anonymous class. 126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 126551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 126651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isAnonymousClass() { 126751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "".equals(getSimpleName()); 126851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 126951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 127051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 127151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 127251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a local class. 127351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 127451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a local class. 127551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 127651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 127751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isLocalClass() { 1278a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return !classNameImpliesTopLevel() 1279a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski && AnnotationAccess.getEnclosingMethodOrConstructor(this) != null 1280a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski && !isAnonymousClass(); 128151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 128251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 128351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 128451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 128551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a member class. 128651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 128751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a member class. 128851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 128951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 129051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isMemberClass() { 129151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getSimpleBinaryName() != null && !isLocalOrAnonymousClass(); 129251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 129351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 129451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 129551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the "simple binary name" of the underlying class, i.e., 129651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary name without the leading enclosing class name. 129751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code null} if the underlying class is a top level 129851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. 129951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 130051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String getSimpleBinaryName() { 130151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> enclosingClass = getEnclosingClass(); 130251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingClass == null) // top level class 130351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 130451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Otherwise, strip the enclosing class' name 130551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski try { 130651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getName().substring(enclosingClass.getName().length()); 130751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } catch (IndexOutOfBoundsException ex) { 130851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new InternalError("Malformed class name"); 130951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 131051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 131151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 131251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 131351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if this is a local class or an anonymous 131451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. Returns {@code false} otherwise. 131551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 131651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private boolean isLocalOrAnonymousClass() { 131751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // JVM Spec 4.8.6: A class must have an EnclosingMethod 131851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // attribute if and only if it is a local class or an 131951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // anonymous class. 1320a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return isLocalClass() || isAnonymousClass(); 132151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 132251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 132351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 132451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Class} objects representing all 132551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the public classes and interfaces that are members of the class 132651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object. This includes public 132751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class and interface members inherited from superclasses and public class 132851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and interface members declared by the class. This method returns an 132951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if this {@code Class} object has no public member 133051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * classes or interfaces. This method also returns an array of length 0 if 133151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type, an array 133251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, or void. 133351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 133451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Class} objects representing the public 133551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * members of this class 133651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 133751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 133851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 133951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 134151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} method 134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * denies access to the classes within this class 134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 134851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 134951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 135051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 135151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 135251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 135351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 135451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 135551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 135651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 135751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 135851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Class<?>[] getClasses() { 13591ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath List<Class<?>> result = new ArrayList<Class<?>>(); 13601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath for (Class<?> c = this; c != null; c = c.superClass) { 13611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath for (Class<?> member : c.getDeclaredClasses()) { 13621ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath if (Modifier.isPublic(member.getModifiers())) { 13631ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath result.add(member); 13641ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 13651ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 13661ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 13671ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return result.toArray(new Class[result.size()]); 136851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 136951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 137051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 137151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 137251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Field} objects reflecting all 137351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the accessible public fields of the class or interface represented by 137451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object. The elements in the array returned are 137551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not sorted and are not in any particular order. This method returns an 137651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if the class or interface has no accessible public 137751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * fields, or if it represents an array class, a primitive type, or void. 137851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 137951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, if this {@code Class} object represents a class, 138051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method returns the public fields of this class and of all its 138151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superclasses. If this {@code Class} object represents an 138251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface, this method returns the fields of this interface and of all 138351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * its superinterfaces. 138451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 138551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The implicit length field for array class is not reflected by this 138651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method. User code should use the methods of class {@code Array} to 138751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manipulate arrays. 138851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 138951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 139051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 139151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Field} objects representing the 139251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public fields 139351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 139451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 139551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 139651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 139751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 139851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 139951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 140051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 140151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 140251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the fields within this class 140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 140451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 140551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 140651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 140751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 140851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 140951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 141051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 141151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 141251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 141351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 141451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 141551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Field[] getFields() throws SecurityException { 1416a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski List<Field> fields = new ArrayList<Field>(); 14171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath getPublicFieldsRecursive(fields); 14181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return fields.toArray(new Field[fields.size()]); 14191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 14201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 14211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 14221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates {@code result} with public fields defined by this class, its 14231ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * superclasses, and all implemented interfaces. 14241ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 14251ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private void getPublicFieldsRecursive(List<Field> result) { 1426a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search superclasses 1427a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = this; c != null; c = c.superClass) { 14281ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, c.getPublicDeclaredFields()); 1429a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1430a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1431a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search iftable which has a flattened and uniqued list of interfaces 1432a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 1433a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 1434a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 14351ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, ((Class<?>) iftable[i]).getPublicDeclaredFields()); 1436a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1437a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 143851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 143951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 144051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 144151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Method} objects reflecting all 144251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the public <em>member</em> methods of the class or interface represented 144351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this {@code Class} object, including those declared by the class 144451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or interface and those inherited from superclasses and 144551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces. Array classes return all the (public) member methods 144651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * inherited from the {@code Object} class. The elements in the array 144751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned are not sorted and are not in any particular order. This 144851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns an array of length 0 if this {@code Class} object 144951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a class or interface that has no public member methods, or if 145051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type or void. 145151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 145251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The class initialization method {@code <clinit>} is not 145351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * included in the returned array. If the class declares multiple public 145451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member methods with the same parameter types, they are all included in 145551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the returned array. 145651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 145751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 145851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 145951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Method} objects representing the 146051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public methods of this class 146151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 146251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 146351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 146451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 146551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 146651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 146751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 146851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 146951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 147051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the methods within this class 147151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 147251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 147351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 147451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 147551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 147651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 147751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 147851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 148151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 148251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 148351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method[] getMethods() throws SecurityException { 1484a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski List<Method> methods = new ArrayList<Method>(); 14851ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath getPublicMethodsInternal(methods); 14861ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /* 14871ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Remove duplicate methods defined by superclasses and 14881ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * interfaces, preferring to keep methods declared by derived 14891ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * types. 14901ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 14911ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath CollectionUtils.removeDuplicates(methods, Method.ORDER_BY_SIGNATURE); 14921ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return methods.toArray(new Method[methods.size()]); 14931ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath } 14941ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 14951ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 14961ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates {@code result} with public methods defined by this class, its 14971ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * superclasses, and all implemented interfaces, including overridden methods. 14981ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 14991ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private void getPublicMethodsInternal(List<Method> result) { 15001ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, getDeclaredMethodsUnchecked(true)); 1501a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!isInterface()) { 1502a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search superclasses, for interfaces don't search java.lang.Object. 1503a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = superClass; c != null; c = c.superClass) { 15041ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, c.getDeclaredMethodsUnchecked(true)); 1505a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1506a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1507a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search iftable which has a flattened and uniqued list of interfaces. 1508a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 1509a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 1510a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 1511a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) iftable[i]; 15121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Collections.addAll(result, ifc.getDeclaredMethodsUnchecked(true)); 1513a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1514a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 151551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 151651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 151751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 151851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 151951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Constructor} objects reflecting 152051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * all the public constructors of the class represented by this 152151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. An array of length 0 is returned if the 152251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class has no public constructors, or if the class is an array class, or 152351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the class reflects a primitive type or void. 152451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 152551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that while this method returns an array of {@code 152651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor<T>} objects (that is an array of constructors from 152751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this class), the return type of this method is {@code 152851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as 152951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * might be expected. This less informative return type is 153051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * necessary since after being returned from this method, the 153151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array could be modified to hold {@code Constructor} objects for 153251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * different classes, which would violate the type guarantees of 153351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Constructor<T>[]}. 153451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 153551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Constructor} objects representing the 153651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public constructors of this class 153751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 153851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 153951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 154051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 154151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 154251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 154351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 154451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 154551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 154651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the constructors within this class 154751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 154851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 154951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 155051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 155151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 155251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 155351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 155451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 155551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 155651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 155751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 155851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 155951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?>[] getConstructors() throws SecurityException { 15601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return getDeclaredConstructorsInternal(true); 156151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 156251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 156351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 156451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 156551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Field} object that reflects the specified public 156651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member field of the class or interface represented by this 156751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 156851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} specifying the simple name of the desired field. 156951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 157051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The field to be reflected is determined by the algorithm that 157151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * follows. Let C be the class represented by this object: 157251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <OL> 157351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If C declares a public field with the name specified, that is the 157451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * field to be reflected.</LI> 157551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no field was found in step 1 above, this algorithm is applied 157651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * recursively to each direct superinterface of C. The direct 157751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces are searched in the order they were declared.</LI> 157851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no field was found in steps 1 and 2 above, and C has a 157951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superclass S, then this algorithm is invoked recursively upon S. 158051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If C has no superclass, then a {@code NoSuchFieldException} 158151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is thrown.</LI> 158251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </OL> 158351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 158451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 158551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 158651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the field name 158751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Field} object of this class specified by 158851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code name} 158951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchFieldException if a field with the specified name is 159051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not found. 159151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 159251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 159351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 159451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 159551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 159651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 159751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 159851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 159951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 160051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 160151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the field 160251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 160351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 160451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 160551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 160651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 160751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 160851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 160951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 161051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 161151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 161251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 161351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 161451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Field getField(String name) 161551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchFieldException, SecurityException { 1616fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski if (name == null) { 1617fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski throw new NullPointerException("name == null"); 1618fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski } 1619a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Field field = getPublicFieldRecursive(name); 162051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (field == null) { 162151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NoSuchFieldException(name); 162251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 1623a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski field.getType(); // Throw NoClassDefFoundError if type cannot be resolved. 162451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return field; 162551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 162651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 1627a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Field getPublicFieldRecursive(String name) { 1628a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search superclasses 1629a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = this; c != null; c = c.superClass) { 1630a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Field result = c.getDeclaredFieldInternal(name); 1631a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && (result.getModifiers() & Modifier.PUBLIC) != 0) { 1632a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 1633a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1634a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1635a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1636a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search iftable which has a flattened and uniqued list of interfaces 1637a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (ifTable != null) { 1638a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < ifTable.length; i += 2) { 1639a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) ifTable[i]; 1640a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Field result = ifc.getPublicFieldRecursive(name); 1641a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && (result.getModifiers() & Modifier.PUBLIC) != 0) { 1642a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 1643a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1644a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1645a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1646a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1647a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 1648a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1649a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 165051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 165151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 165251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Method} object that reflects the specified public 165351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member method of the class or interface represented by this 165451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 165551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} specifying the simple name of the desired method. The 165651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code parameterTypes} parameter is an array of {@code Class} 165751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects that identify the method's formal parameter types, in declared 165851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * order. If {@code parameterTypes} is {@code null}, it is 165951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * treated as if it were an empty array. 166051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 166151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a 166251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code NoSuchMethodException} is raised. Otherwise, the method to 166351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be reflected is determined by the algorithm that follows. Let C be the 166451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class represented by this object: 166551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <OL> 166651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> C is searched for any <I>matching methods</I>. If no matching 166751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method is found, the algorithm of step 1 is invoked recursively on 166851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the superclass of C.</LI> 166951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no method was found in step 1 above, the superinterfaces of C 167051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are searched for a matching method. If any such method is found, it 167151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is reflected.</LI> 167251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </OL> 167351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 167451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * To find a matching method in a class C: If C declares exactly one 167551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public method with the specified name and exactly the same formal 167651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types, that is the method reflected. If more than one such 167751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method is found in C, and one of these methods has a return type that is 167851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * more specific than any of the others, that method is reflected; 167951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise one of the methods is chosen arbitrarily. 168051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 168151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that there may be more than one matching method in a 168251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class because while the Java language forbids a class to 168351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declare multiple methods with the same signature but different 168451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return types, the Java virtual machine does not. This 168551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * increased flexibility in the virtual machine can be used to 168651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implement various language features. For example, covariant 168751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns can be implemented with {@linkplain 168851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Method#isBridge bridge methods}; the bridge 168951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method and the method being overridden would have the same 169051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * signature but different return types. 169151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 169251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 169351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 169451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the method 169551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the list of parameters 169651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Method} object that matches the specified 169751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code name} and {@code parameterTypes} 169851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found 169951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or if the name is "<init>"or "<clinit>". 170051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 170151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 170251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 170351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 170451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 170651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 170851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 170951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 171051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the method 171151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 171251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 171351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 171451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 171551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 171651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 171751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 171851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 171951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 172051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 172151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 172251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 172351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getMethod(String name, Class<?>... parameterTypes) 172451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 1725a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getMethod(name, parameterTypes, true); 172651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 172751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 172851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 172951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 173051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Constructor} object that reflects the specified 173151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public constructor of the class represented by this {@code Class} 173251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The {@code parameterTypes} parameter is an array of 173351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that identify the constructor's formal 173451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types, in declared order. 173551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 173651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents an inner class 173751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared in a non-static context, the formal parameter types 173851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * include the explicit enclosing instance as the first parameter. 173951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 174051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The constructor to reflect is the public constructor of the class 174151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object whose formal parameter 174251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types match those specified by {@code parameterTypes}. 174351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 174451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 174551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Constructor} object of the public constructor that 174651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * matches the specified {@code parameterTypes} 174751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found. 174851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 174951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 175051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 175151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 175251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 175351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 175451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 175551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 175651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 175751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the constructor 175851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 175951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 176051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 176151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 176251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 176351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 176451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 176551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 176651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 176751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 176851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 176951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 177051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<T> getConstructor(Class<?>... parameterTypes) 177151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 177251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getConstructor0(parameterTypes, Member.PUBLIC); 177351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 177451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 177551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 177651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 177751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Class} objects reflecting all the 177851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * classes and interfaces declared as members of the class represented by 177951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object. This includes public, protected, default 178051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private classes and interfaces declared by the 178151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, but excludes inherited classes and interfaces. This method 178251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns an array of length 0 if the class declares no classes or 178351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces as members, or if this {@code Class} object represents a 178451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, an array class, or void. 178551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 178651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Class} objects representing all the 178751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared members of this class 178851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 178951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 179051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 179151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 179251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 179351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 179451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 179551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 179651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 179751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared classes within this class 179851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 179951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 180051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 180151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 180251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 180351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 180451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 180551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 180651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 180751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 180851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 180951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 181051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Class<?>[] getDeclaredClasses() throws SecurityException { 1811a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return AnnotationAccess.getMemberClasses(this); 181251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 181351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 181451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 181551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 181651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Field} objects reflecting all the fields 181751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared by the class or interface represented by this 181851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. This includes public, protected, default 181951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private fields, but excludes inherited fields. 182051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The elements in the array returned are not sorted and are not in any 182151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular order. This method returns an array of length 0 if the class 182251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or interface declares no fields, or if this {@code Class} object 182351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type, an array class, or void. 182451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 182551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 182651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 182751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Field} objects representing all the 182851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared fields of this class 182951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 183051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 183151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 183251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 183351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 183451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 183551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 183651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 183751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 183851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared fields within this class 183951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 184051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 184151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 184251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 184351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 184451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 184551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 184651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 184751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 184851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 184951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 18501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Field[] getDeclaredFields(); 185151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 18521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 18531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates a list of fields without performing any security or type 18541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * resolution checks first. If no fields exist, the list is not modified. 18551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 18561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param publicOnly Whether to return only public fields. 18571ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param fields A list to populate with declared fields. 18581ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @hide 18591ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 18601ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Field[] getDeclaredFieldsUnchecked(boolean publicOnly); 186151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 186251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 186351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Method} objects reflecting all the 186451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * methods declared by the class or interface represented by this 186551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. This includes public, protected, default 186651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private methods, but excludes inherited methods. 186751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The elements in the array returned are not sorted and are not in any 186851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular order. This method returns an array of length 0 if the class 186951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or interface declares no methods, or if this {@code Class} object 187051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type, an array class, or void. The class 187151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * initialization method {@code <clinit>} is not included in the 187251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned array. If the class declares multiple public member methods 187351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with the same parameter types, they are all included in the returned 187451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. 187551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 187651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, section 8.2. 187751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 187851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Method} objects representing all the 187951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared methods of this class 188051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 188151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 188251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 188351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 188451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 188551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 188651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 188751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 188851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 188951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared methods within this class 189051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 189151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 189251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 189351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 189451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 189551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 189651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 189751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 189851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 189951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 190051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 190151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 190251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method[] getDeclaredMethods() throws SecurityException { 19031ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath Method[] result = getDeclaredMethodsUnchecked(false); 1904a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Method m : result) { 1905a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Throw NoClassDefFoundError if types cannot be resolved. 1906a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski m.getReturnType(); 1907a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski m.getParameterTypes(); 1908a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1909a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 1910a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1911a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 19121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 19131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Populates a list of methods without performing any security or type 19141ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * resolution checks first. If no methods exist, the list is not modified. 19151ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 19161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param publicOnly Whether to return only public methods. 19171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param methods A list to populate with declared methods. 19181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @hide 19191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 19201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Method[] getDeclaredMethodsUnchecked(boolean publicOnly); 192151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 192251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 192351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 192451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Constructor} objects reflecting all the 192551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructors declared by the class represented by this 192651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. These are public, protected, default 192751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private constructors. The elements in the array 192851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned are not sorted and are not in any particular order. If the 192951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class has a default constructor, it is included in the returned array. 193051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method returns an array of length 0 if this {@code Class} 193151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object represents an interface, a primitive type, an array class, or 193251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * void. 193351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 193451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, section 8.2. 193551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 193651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Constructor} objects representing all the 193751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared constructors of this class 193851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 193951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 194051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 194151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 194251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 194351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 194451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 194551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 194651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 194751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared constructors within this class 194851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 194951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 195051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 195151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 195251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 195351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 195451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 195551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 195651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 195751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 195851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 195951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 196051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?>[] getDeclaredConstructors() throws SecurityException { 19611ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath return getDeclaredConstructorsInternal(false); 196251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 196351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 196451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 196551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 19661ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the constructor with the given parameters if it is defined by this class; 19671ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * {@code null} otherwise. This may return a non-public member. 19681ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 19691ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param args the types of the parameters to the constructor. 19701ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 19711ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Constructor<?>[] getDeclaredConstructorsInternal(boolean publicOnly); 19721ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 19731ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 19741ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 197551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Field} object that reflects the specified declared 197651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * field of the class or interface represented by this {@code Class} 197751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The {@code name} parameter is a {@code String} that 197851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specifies the simple name of the desired field. Note that this method 197951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will not reflect the {@code length} field of an array class. 198051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 198151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the field 198251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Field} object for the specified field in this 198351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class 198451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchFieldException if a field with the specified name is 198551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not found. 198651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 198751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 198851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 198951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 199051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 199151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 199251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 199351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 199451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 199551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 199651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared field 199751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 199851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 199951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 200051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 200151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 200251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 200351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 200451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 200551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 200651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 200751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 200851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 20091ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath public native Field getDeclaredField(String name) throws NoSuchFieldException; 201051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 20111ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 20121ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the field if it is defined by this class; {@code null} otherwise. This 20131ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * may return a non-public member. 20141ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 20151ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Field getDeclaredFieldInternal(String name); 20161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 20171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 20181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the subset of getDeclaredFields which are public. 20191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 20201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Field[] getPublicDeclaredFields(); 2021a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 202251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 202351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 202451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Method} object that reflects the specified 202551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared method of the class or interface represented by this 202651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 202751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} that specifies the simple name of the desired 202851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method, and the {@code parameterTypes} parameter is an array of 202951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that identify the method's formal parameter 203051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types, in declared order. If more than one method with the same 203151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types is declared in a class, and one of these methods has a 203251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type that is more specific than any of the others, that method is 203351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned; otherwise one of the methods is chosen arbitrarily. If the 203451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name is "<init>"or "<clinit>" a {@code NoSuchMethodException} 203551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is raised. 203651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 203751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the method 203851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 203951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Method} object for the method of this class 204051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * matching the specified name and parameters 204151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found. 204251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 204351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 204451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 204551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 204651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 204751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 204851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 204951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 205051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 205151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 205251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared method 205351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 205451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 205551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 205651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 205751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 205851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 205951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 206051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 206151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 206251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 206351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 206451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 206551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getDeclaredMethod(String name, Class<?>... parameterTypes) 206651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 2067a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getMethod(name, parameterTypes, false); 2068a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2069a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2070a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Method getMethod(String name, Class<?>[] parameterTypes, boolean recursivePublicMethods) 2071a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throws NoSuchMethodException { 2072a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (name == null) { 2073a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NullPointerException("name == null"); 2074a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2075a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parameterTypes == null) { 2076a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parameterTypes = EmptyArray.CLASS; 2077a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2078a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c : parameterTypes) { 2079a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == null) { 2080a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("parameter type is null"); 2081a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2082a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2083a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = recursivePublicMethods ? getPublicMethodRecursive(name, parameterTypes) 2084a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski : getDeclaredMethodInternal(name, parameterTypes); 2085a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Fail if we didn't find the method or it was expected to be public. 2086a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null || 2087a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski (recursivePublicMethods && !Modifier.isPublic(result.getAccessFlags()))) { 2088a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException(name + " " + Arrays.toString(parameterTypes)); 2089a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2090a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2091a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2092a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2093a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Method getPublicMethodRecursive(String name, Class<?>[] parameterTypes) { 2094a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search superclasses 2095a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = this; c != null; c = c.getSuperclass()) { 2096a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = c.getDeclaredMethodInternal(name, parameterTypes); 2097a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && Modifier.isPublic(result.getAccessFlags())) { 2098a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2099a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2100a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2101a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search iftable which has a flattened and uniqued list of interfaces 2102a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 2103a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 2104a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 2105a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) iftable[i]; 2106a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = ifc.getPublicMethodRecursive(name, parameterTypes); 2107a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && Modifier.isPublic(result.getAccessFlags())) { 2108a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2109a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2110a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 211151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2112a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 211351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 211451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 211551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 211651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 211751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Constructor} object that reflects the specified 211851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructor of the class or interface represented by this 211951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code parameterTypes} parameter is 212051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array of {@code Class} objects that identify the constructor's 212151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * formal parameter types, in declared order. 212251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 212351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents an inner class 212451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared in a non-static context, the formal parameter types 212551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * include the explicit enclosing instance as the first parameter. 212651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 212751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 212851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return The {@code Constructor} object for the constructor with the 212951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified parameter list 213051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found. 213151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 213251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 213351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 213451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 213551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 213651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 213751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 213851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 213951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 214051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared constructor 214151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 214251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 214351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 214451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 214551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 214651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 214751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 214851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 214951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 215051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 215151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 215251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 215351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 215451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 215551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getConstructor0(parameterTypes, Member.DECLARED); 215651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 215751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 215851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 215951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Finds a resource with a given name. The rules for searching resources 216051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * associated with a given class are implemented by the defining 216151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain ClassLoader class loader} of the class. This method 216251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * delegates to this object's class loader. If this object was loaded by 216351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the bootstrap class loader, the method delegates to {@link 216451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader#getSystemResourceAsStream}. 216551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 216651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Before delegation, an absolute resource name is constructed from the 216751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * given resource name using this algorithm: 216851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 216951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 217051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 217151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> If the {@code name} begins with a {@code '/'} 217251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 217351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * portion of the {@code name} following the {@code '/'}. 217451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 217551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> Otherwise, the absolute name is of the following form: 217651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 217751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 217851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code modified_package_name/name} 217951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 218051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 218151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Where the {@code modified_package_name} is the package name of this 218251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object with {@code '/'} substituted for {@code '.'} 218351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002e'</tt>). 218451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 218551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 218651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 218751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name name of the desired resource 218851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return A {@link java.io.InputStream} object or {@code null} if 218951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * no resource with this name is found 219051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException If {@code name} is {@code null} 219151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 219251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 219351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public InputStream getResourceAsStream(String name) { 219451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = resolveName(name); 2195a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader cl = getClassLoader(); 219651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cl==null) { 219751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // A system class. 219851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ClassLoader.getSystemResourceAsStream(name); 219951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 220051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return cl.getResourceAsStream(name); 220151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 220251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 220351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 220451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Finds a resource with a given name. The rules for searching resources 220551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * associated with a given class are implemented by the defining 220651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain ClassLoader class loader} of the class. This method 220751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * delegates to this object's class loader. If this object was loaded by 220851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the bootstrap class loader, the method delegates to {@link 220951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader#getSystemResource}. 221051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 221151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Before delegation, an absolute resource name is constructed from the 221251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * given resource name using this algorithm: 221351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 221451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 221551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 221651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> If the {@code name} begins with a {@code '/'} 221751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 221851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * portion of the {@code name} following the {@code '/'}. 221951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 222051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> Otherwise, the absolute name is of the following form: 222151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 222251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 222351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code modified_package_name/name} 222451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 222551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 222651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Where the {@code modified_package_name} is the package name of this 222751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object with {@code '/'} substituted for {@code '.'} 222851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002e'</tt>). 222951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 223051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 223151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 223251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name name of the desired resource 223351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return A {@link java.net.URL} object or {@code null} if no 223451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * resource with this name is found 223551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 223651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 223751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public java.net.URL getResource(String name) { 223851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = resolveName(name); 2239a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader cl = getClassLoader(); 224051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cl==null) { 224151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // A system class. 224251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ClassLoader.getSystemResource(name); 224351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 224451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return cl.getResource(name); 224551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 224651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 224751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 224851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 224951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 225051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code ProtectionDomain} of this class. If there is a 225151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * security manager installed, this method first calls the security 225251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager's {@code checkPermission} method with a 225351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code RuntimePermission("getProtectionDomain")} permission to 225451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ensure it's ok to get the 225551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code ProtectionDomain}. 225651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 225751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the ProtectionDomain of this class 225851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 225951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 226051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if a security manager exists and its 226151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code checkPermission} method doesn't allow 226251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * getting the ProtectionDomain. 226351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 226451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.security.ProtectionDomain 226551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 226651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.RuntimePermission 226751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 226851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 226951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public java.security.ProtectionDomain getProtectionDomain() { 2270a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 227151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 227251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 227351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 227451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Add a package name prefix if the name is not absolute Remove leading "/" 227551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if name is absolute 227651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 227751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String resolveName(String name) { 227851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (name == null) { 227951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 228051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 228151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (!name.startsWith("/")) { 228251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> c = this; 228351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (c.isArray()) { 228451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski c = c.getComponentType(); 228551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 228651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String baseName = c.getName(); 228751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int index = baseName.lastIndexOf('.'); 228851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (index != -1) { 228951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = baseName.substring(0, index).replace('.', '/') 229051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski +"/"+name; 229151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 229251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 229351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = name.substring(1); 229451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 229551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 229651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 229751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2298a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Constructor<T> getConstructor0(Class<?>[] parameterTypes, 2299a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int which) throws NoSuchMethodException 2300a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski { 2301a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parameterTypes == null) { 2302a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parameterTypes = EmptyArray.CLASS; 230351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2304a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c : parameterTypes) { 2305a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == null) { 2306a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("parameter type is null"); 2307a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 230851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2309a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Constructor<T> result = getDeclaredConstructorInternal(parameterTypes); 2310a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null || which == Member.PUBLIC && !Modifier.isPublic(result.getAccessFlags())) { 2311a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("<init> " + Arrays.toString(parameterTypes)); 2312a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2313a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 231451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 231551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 23161ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 23171ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the constructor with the given parameters if it is defined by this class; 23181ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * {@code null} otherwise. This may return a non-public member. 23191ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 23201ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param args the types of the parameters to the constructor. 23211ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 23221ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Constructor<T> getDeclaredConstructorInternal(Class<?>[] args); 232351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 232451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 232551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 232651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 232751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the assertion status that would be assigned to this 232851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class if it were to be initialized at the time this method is invoked. 232951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this class has had its assertion status set, the most recent 233051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * setting will be returned; otherwise, if any package default assertion 233151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * status pertains to this class, the most recent setting for the most 233251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specific pertinent package default assertion status is returned; 233351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, if this class is not a system class (i.e., it has a 233451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader) its class loader's default assertion status is returned; 233551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, the system class default assertion status is returned. 233651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 233751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Few programmers will have any need for this method; it is provided 233851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for the benefit of the JRE itself. (It allows a class to determine at 233951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the time that it is initialized whether assertions should be enabled.) 234051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method is not guaranteed to return the actual 234151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * assertion status that was (or will be) associated with the specified 234251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class when it was (or will be) initialized. 234351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 234451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the desired assertion status of the specified class. 234551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setClassAssertionStatus 234651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setPackageAssertionStatus 234751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setDefaultAssertionStatus 234851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 234951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 235051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean desiredAssertionStatus() { 2351a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 235251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 235351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 235451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 235551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns true if and only if this class was declared as an enum in the 235651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code. 235751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 235851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if and only if this class was declared as an enum in the 235951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code 236051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 236151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 236251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isEnum() { 236351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // An enum must both directly extend java.lang.Enum and have 236451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // the ENUM bit set; classes for specialized enum constants 236551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // don't do the former. 236651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (this.getModifiers() & ENUM) != 0 && 236751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.getSuperclass() == java.lang.Enum.class; 236851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 236951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 237051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 237151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the elements of this enum class or null if this 237251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class object does not represent an enum type. 237351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 237451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array containing the values comprising the enum class 237551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this Class object in the order they're 237651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared, or null if this Class object does not 237751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represent an enum type 237851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 237951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 238051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public T[] getEnumConstants() { 238151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] values = getEnumConstantsShared(); 238251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (values != null) ? values.clone() : null; 238351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 238451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 238551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 238651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the elements of this enum class or null if this 238751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class object does not represent an enum type; 238851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * identical to getEnumConstants except that the result is 238951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * uncloned, cached, and shared by all callers. 239051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 239151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] getEnumConstantsShared() { 2392a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!isEnum()) return null; 2393a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (T[]) Enum.getSharedConstants((Class) this); 239451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 239551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 239651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 239751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Casts an object to the class or interface represented 239851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this {@code Class} object. 239951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 240051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param obj the object to be cast 240151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the object after casting, or null if obj is null 240251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 240351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the object is not 240451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null and is not assignable to the type T. 240551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 240651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 240751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 240851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public T cast(Object obj) { 240951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (obj != null && !isInstance(obj)) 241051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new ClassCastException(cannotCastMsg(obj)); 241151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (T) obj; 241251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 241351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 241451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String cannotCastMsg(Object obj) { 241551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "Cannot cast " + obj.getClass().getName() + " to " + getName(); 241651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 241751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 241851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 241951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Casts this {@code Class} object to represent a subclass of the class 242051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the specified class object. Checks that that the cast 242151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is valid, and throws a {@code ClassCastException} if it is not. If 242251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method succeeds, it always returns a reference to this class object. 242351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 242451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This method is useful when a client needs to "narrow" the type of 242551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a {@code Class} object to pass it to an API that restricts the 242651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that it is willing to accept. A cast would 242751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * generate a compile-time warning, as the correctness of the cast 242851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * could not be checked at runtime (because generic types are implemented 242951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by erasure). 243051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 243151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return this {@code Class} object, cast to represent a subclass of 243251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified class object. 243351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if this {@code Class} object does not 243451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represent a subclass of the specified class (here "subclass" includes 243551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class itself). 243651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 243751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 243851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public <U> Class<? extends U> asSubclass(Class<U> clazz) { 243951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (clazz.isAssignableFrom(this)) 244051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (Class<? extends U>) this; 244151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 244287778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak /* ----- BEGIN android ----- 244387778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak // Add more context 244487778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak throw new ClassCastException(this.toString());*/ 244587778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak throw new ClassCastException(this.toString() + 244687778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak " cannot be cast to " + clazz.getName()); 244751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 244851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 244951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 245051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException {@inheritDoc} 245151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 245251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 245351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public <A extends Annotation> A getAnnotation(Class<A> annotationClass) { 245451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (annotationClass == null) 245551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NullPointerException(); 245651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2457a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return AnnotationAccess.getAnnotation(this, annotationClass); 245851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 245951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 246051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 246151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException {@inheritDoc} 246251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 246351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 246451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isAnnotationPresent( 246551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<? extends Annotation> annotationClass) { 246651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (annotationClass == null) 246751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NullPointerException(); 246851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 246951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getAnnotation(annotationClass) != null; 247051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 247151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 247251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 247351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 247451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 247551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 247651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Annotation[] getAnnotations() { 2477a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return AnnotationAccess.getAnnotations(this); 247851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 247951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 248051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 248151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 248251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 248351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Annotation[] getDeclaredAnnotations() { 2484a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski List<Annotation> result = AnnotationAccess.getDeclaredAnnotations(this); 2485a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result.toArray(new Annotation[result.size()]); 248651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 248751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 248851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Annotation types cache their internal (AnnotationType) form 248951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2490a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** @hide */ 2491a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public void setAnnotationType(AnnotationType type) { 249251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski annotationType = type; 249351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 249451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2495a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** @hide */ 2496a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public AnnotationType getAnnotationType() { 249751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return annotationType; 249851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 249951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2500a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2501a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Is this a runtime created proxy class? 2502a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2503a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2504a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2505a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isProxy() { 2506a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (accessFlags & 0x00040000) != 0; 2507a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2508a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2509a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2510a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns the dex file from which this class was loaded. 2511a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2512a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2513a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2514a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Dex getDex() { 2515a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dexCache == null) { 2516a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 2517a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2518a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return dexCache.getDex(); 2519a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2520a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2521a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2522a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns a string from the dex cache, computing the string from the dex file if necessary. 2523a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2524a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2525a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2526a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public String getDexCacheString(Dex dex, int dexStringIndex) { 2527a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String s = dexCacheStrings[dexStringIndex]; 2528a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (s == null) { 2529a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski s = dex.strings().get(dexStringIndex).intern(); 2530a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski dexCacheStrings[dexStringIndex] = s; 2531a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2532a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return s; 2533a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2534a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2535a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2536a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns a resolved type from the dex cache, computing the type from the dex file if 2537a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * necessary. 2538a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2539a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2540a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2541a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?> getDexCacheType(Dex dex, int dexTypeIndex) { 2542a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?>[] dexCacheResolvedTypes = dexCache.resolvedTypes; 2543a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> resolvedType = dexCacheResolvedTypes[dexTypeIndex]; 2544a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (resolvedType == null) { 2545a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int descriptorIndex = dex.typeIds().get(dexTypeIndex); 2546a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String descriptor = getDexCacheString(dex, descriptorIndex); 2547a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski resolvedType = InternalNames.getClass(getClassLoader(), descriptor); 2548a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski dexCacheResolvedTypes[dexTypeIndex] = resolvedType; 2549a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2550a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return resolvedType; 2551a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2552a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2553a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2554a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The annotation directory offset of this class in its own Dex, or 0 if it 2555a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * is unknown. 2556a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2557a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * TODO: 0 is a sentinel that means 'no annotations directory'; this should be -1 if unknown 2558a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2559a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2560a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2561a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getDexAnnotationDirectoryOffset() { 2562a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Dex dex = getDex(); 2563a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dex == null) { 2564a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return 0; 2565a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2566a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int classDefIndex = getDexClassDefIndex(); 2567a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classDefIndex < 0) { 2568a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return 0; 2569a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2570a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return dex.annotationDirectoryOffsetFromClassDefIndex(classDefIndex); 2571a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2572a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2573a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2574a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The type index of this class in its own Dex, or -1 if it is unknown. If a class is referenced 2575a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * by multiple Dex files, it will have a different type index in each. Dex files support 65534 2576a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * type indices, with 65535 representing no index. 2577a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2578a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2579a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2580a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getDexTypeIndex() { 2581a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int typeIndex = dexTypeIndex; 2582a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (typeIndex != 65535) { 2583a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return typeIndex; 2584a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2585a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized (this) { 2586a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = dexTypeIndex; 2587a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (typeIndex == 65535) { 2588a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dexClassDefIndex >= 0) { 2589a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = getDex().typeIndexFromClassDefIndex(dexClassDefIndex); 2590a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 2591a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = getDex().findTypeIndex(InternalNames.getInternalName(this)); 2592a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (typeIndex < 0) { 2593a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = -1; 2594a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2595a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2596a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski dexTypeIndex = typeIndex; 2597a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2598a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2599a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return typeIndex; 2600a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2601a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2602a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean canAccess(Class<?> c) { 2603a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if(Modifier.isPublic(c.accessFlags)) { 2604a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 2605a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2606a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return inSamePackage(c); 2607a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2608a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2609a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean canAccessMember(Class<?> memberClass, int memberModifiers) { 2610a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (memberClass == this || Modifier.isPublic(memberModifiers)) { 2611a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 2612a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2613a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Modifier.isPrivate(memberModifiers)) { 2614a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 2615a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2616a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Modifier.isProtected(memberModifiers)) { 2617a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> parent = this.superClass; parent != null; parent = parent.superClass) { 2618a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parent == memberClass) { 2619a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 2620a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2621a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2622a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2623a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return inSamePackage(memberClass); 2624a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2625a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2626a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean inSamePackage(Class<?> c) { 2627a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classLoader != c.classLoader) { 2628a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 2629a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2630a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName1 = getPackageName$(); 2631a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName2 = c.getPackageName$(); 2632a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (packageName1 == null) { 2633a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName2 == null; 2634a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (packageName2 == null) { 2635a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 2636a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 2637a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName1.equals(packageName2); 2638a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2639a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2640a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2641a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2642a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2643a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2644a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getAccessFlags() { 2645a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return accessFlags; 2646a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2647a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 26481ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath 26491ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath /** 26501ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * Returns the method if it is defined by this class; {@code null} otherwise. This may return a 26511ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * non-public member. 26521ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * 26531ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param name the method name 26541ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath * @param args the method's parameter types 26551ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath */ 26561ee709428b0987348980749b7fb7be90d8b225b5Narayan Kamath private native Method getDeclaredMethodInternal(String name, Class<?>[] args); 2657a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2658a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2659a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The class def of this class in its own Dex, or -1 if there is no class def. 2660a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2661a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2662a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2663a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getDexClassDefIndex() { 2664a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (dexClassDefIndex == 65535) ? -1 : dexClassDefIndex; 2665a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2666a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2667a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static class Caches { 2668a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2669a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Cache to avoid frequent recalculation of generic interfaces, which is generally uncommon. 2670a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Sized sufficient to allow ConcurrentHashMapTest to run without recalculating its generic 2671a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * interfaces (required to avoid time outs). Validated by running reflection heavy code 2672a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * such as applications using Guice-like frameworks. 2673a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2674a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static final BasicLruCache<Class, Type[]> genericInterfaces 2675a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski = new BasicLruCache<Class, Type[]>(8); 2676a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 267751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 2678