Class.java revision 87778fb754e6e9bf04e1d8e10fd13f49cc2aa608
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 java.lang.reflect.ArtField; 67a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport java.lang.reflect.ArtMethod; 68a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.AnnotationAccess; 69a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.InternalNames; 70a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.GenericSignatureParser; 71a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.reflect.Types; 72a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.BasicLruCache; 73a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.CollectionUtils; 74a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.EmptyArray; 75a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.SneakyThrow; 76a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Instances of the class {@code Class} represent classes and 7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces in a running Java application. An enum is a kind of 8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class and an annotation is a kind of interface. Every array also 8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * belongs to a class that is reflected as a {@code Class} object 8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that is shared by all arrays with the same element type and number 8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of dimensions. The primitive Java types ({@code boolean}, 8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code byte}, {@code char}, {@code short}, 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code int}, {@code long}, {@code float}, and 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code double}), and the keyword {@code void} are also 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented as {@code Class} objects. 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> {@code Class} has no public constructor. Instead {@code Class} 9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects are constructed automatically by the Java Virtual Machine as classes 9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are loaded and by calls to the {@code defineClass} method in the class 9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader. 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The following example uses a {@code Class} object to print the 9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class name of an object: 9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> <blockquote><pre> 9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * void printClassName(Object obj) { 9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * System.out.println("The class of " + obj + 10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * " is " + obj.getClass().getName()); 10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * } 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> It is also possible to get the {@code Class} object for a named 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type (or for void) using a class literal. See Section 15.8.2 of 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>. 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For example: 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> <blockquote> 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());} 11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param <T> the type of the class modeled by this {@code Class} 11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. For example, the type of {@code String.class} is {@code 11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class<String>}. Use {@code Class<?>} if the class being modeled is 11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * unknown. 11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author unascribed 11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#defineClass(byte[], int, int) 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0 12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic final 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski class Class<T> implements java.io.Serializable, 12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski java.lang.reflect.GenericDeclaration, 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski java.lang.reflect.Type, 12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski java.lang.reflect.AnnotatedElement { 12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static final int ANNOTATION= 0x00002000; 12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static final int ENUM = 0x00004000; 12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static final int SYNTHETIC = 0x00001000; 13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor. Only the Java Virtual Machine creates Class 13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects. 13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private Class() {} 13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 13751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Converts the object to a string. The string representation is the 14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * string "class" or "interface", followed by a space, and then by the 14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * fully qualified name of the class in the format returned by 14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code getName}. If this {@code Class} object represents a 14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, this method returns the name of the primitive type. If 14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents void this method returns 14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "void". 14651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of this class object. 14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String toString() { 15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) 15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski + getName(); 15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} object associated with the class or 15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface with the given string name. Invoking this method is 15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent to: 15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName(className, true, currentLoader)} 16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * where {@code currentLoader} denotes the defining class loader of 16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the current class. 16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> For example, the following code fragment returns the 16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * runtime {@code Class} descriptor for the class named 16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code java.lang.Thread}: 17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 17251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class t = Class.forName("java.lang.Thread")} 17351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 17451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 17551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A call to {@code forName("X")} causes the class named 17651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code X} to be initialized. 17751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param className the fully qualified name of the desired class. 17951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Class} object for the class with the 18051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified name. 18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception LinkageError if the linkage fails 18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization provoked 18351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this method fails 18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ClassNotFoundException if the class cannot be located 18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 18651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 18751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Class<?> forName(String className) 18851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws ClassNotFoundException { 189a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return forName(className, true, VMStack.getCallingClassLoader()); 19051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 19151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} object associated with the class or 19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface with the given string name, using the given class loader. 19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Given the fully qualified name for a class or interface (in the same 19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * format returned by {@code getName}) this method attempts to 19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * locate, load, and link the class or interface. The specified class 19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader is used to load the class or interface. If the parameter 20051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code loader} is null, the class is loaded through the bootstrap 20151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader. The class is initialized only if the 20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code initialize} parameter is {@code true} and if it has 20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not been initialized earlier. 20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If {@code name} denotes a primitive type or void, an attempt 20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will be made to locate a user-defined class in the unnamed package whose 20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name is {@code name}. Therefore, this method cannot be used to 20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtain any of the {@code Class} objects representing primitive 20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types or void. 21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If {@code name} denotes an array class, the component type of 21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array class is loaded but not initialized. 21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> For example, in an instance method the expression: 21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName("Foo")} 21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is equivalent to: 22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class.forName("Foo", true, this.getClass().getClassLoader())} 22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method throws errors related to loading, linking or 22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The 22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Java Language Specification</em>. 22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method does not check whether the requested class 23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is accessible to its caller. 23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the {@code loader} is {@code null}, and a security 23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager is present, and the caller's class loader is not null, then this 23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method calls the security manager's {@code checkPermission} method 23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with a {@code RuntimePermission("getClassLoader")} permission to 23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ensure it's ok to access the bootstrap class loader. 23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name fully qualified name of the desired class 23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param initialize whether the class must be initialized 24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param loader class loader from which the class must be loaded 24151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return class object representing the desired class 24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception LinkageError if the linkage fails 24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization provoked 24551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this method fails 24651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ClassNotFoundException if the class cannot be located by 24751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified class loader 24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 24951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Class#forName(String) 25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader 25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Class<?> forName(String name, boolean initialize, 25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ClassLoader loader) 25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws ClassNotFoundException 25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (loader == null) { 259a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski loader = ClassLoader.getSystemClassLoader(); 260a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 261a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 262a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> result; 263a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski try { 264a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = classForName(name, initialize, loader); 265a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } catch (ClassNotFoundException e) { 266a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Throwable cause = e.getCause(); 267a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (cause instanceof LinkageError) { 268a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw (LinkageError) cause; 26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 270a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw e; 27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 272a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** Called after security checks have been made. */ 276a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski static native Class<?> classForName(String className, boolean shouldInitialize, 277a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader classLoader) throws ClassNotFoundException; 27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Creates a new instance of the class represented by this {@code Class} 28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The class is instantiated as if by a {@code new} 28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * expression with an empty argument list. The class is initialized if it 28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has not already been initialized. 28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that this method propagates any exception thrown by the 28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * nullary constructor, including a checked exception. Use of 28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method effectively bypasses the compile-time exception 28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * checking that would otherwise be performed by the compiler. 28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The {@link 29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Constructor#newInstance(java.lang.Object...) 29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor.newInstance} method avoids this problem by wrapping 29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * any exception thrown by the constructor in a (checked) {@link 29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.InvocationTargetException}. 29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a newly allocated instance of the class represented by this 29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. 29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalAccessException if the class or its nullary 29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructor is not accessible. 29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception InstantiationException 30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if this {@code Class} represents an abstract class, 30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an interface, an array class, a primitive type, or void; 30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or if the class has no nullary constructor; 30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or if the instantiation fails for some other reason. 30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception ExceptionInInitializerError if the initialization 30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * provoked by this method fails. 30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * creation of new instances of this class 31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public T newInstance() 32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws InstantiationException, IllegalAccessException 32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 330a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isPrimitive() || isInterface() || isArray() || Modifier.isAbstract(accessFlags)) { 331a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new InstantiationException(this + " cannot be instantiated"); 33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 333a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> caller = VMStack.getStackClass1(); 334a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!caller.canAccess(this)) { 335a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new IllegalAccessException(this + " is not accessible from " + caller); 33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 337a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Constructor<T> init; 338a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski try { 339a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski init = getDeclaredConstructor(); 340a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } catch (NoSuchMethodException e) { 341a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski InstantiationException t = 342a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski new InstantiationException(this + " has no zero argument constructor"); 343a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski t.initCause(e); 344a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw t; 345a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 346a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!caller.canAccessMember(this, init.getAccessFlags())) { 347a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new IllegalAccessException(init + " is not accessible from " + caller); 34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski try { 350a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return init.newInstance(null, init.isAccessible()); 35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } catch (InvocationTargetException e) { 352a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski SneakyThrow.sneakyThrow(e.getCause()); 353a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; // Unreachable. 35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Object} is assignment-compatible 35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with the object represented by this {@code Class}. This method is 36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the dynamic equivalent of the Java language {@code instanceof} 36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * operator. The method returns {@code true} if the specified 36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} argument is non-null and can be cast to the 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * reference type represented by this {@code Class} object without 36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * raising a {@code ClassCastException.} It returns {@code false} 36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise. 36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, if this {@code Class} object represents a 36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared class, this method returns {@code true} if the specified 36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} argument is an instance of the represented class (or 37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of any of its subclasses); it returns {@code false} otherwise. If 37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents an array class, this method 37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code true} if the specified {@code Object} argument 37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * can be converted to an object of the array class by an identity 37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * conversion or by a widening reference conversion; it returns 37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. If this {@code Class} object 37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents an interface, this method returns {@code true} if the 37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class or any superclass of the specified {@code Object} argument 37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implements this interface; it returns {@code false} otherwise. If 37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type, this method 38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code false}. 38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param obj the object to check 38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if {@code obj} is an instance of this class 38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 387a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isInstance(Object object) { 388a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (object == null) { 389a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 390a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 391a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return isAssignableFrom(object.getClass()); 392a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the class or interface represented by this 39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object is either the same as, or is a superclass or 39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterface of, the class or interface represented by the specified 39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} parameter. It returns {@code true} if so; 40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise it returns {@code false}. If this {@code Class} 40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object represents a primitive type, this method returns 40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code true} if the specified {@code Class} parameter is 40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exactly this {@code Class} object; otherwise it returns 40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. 40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, this method tests whether the type represented by the 40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified {@code Class} parameter can be converted to the type 40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object via an identity conversion 40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or via a widening reference conversion. See <em>The Java Language 41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Specification</em>, sections 5.1.1 and 5.1.4 , for details. 41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param cls the {@code Class} object to be checked 41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code boolean} value indicating whether objects of the 41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type {@code cls} can be assigned to objects of this class 41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if the specified Class parameter is 41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null. 41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 419a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isAssignableFrom(Class<?> c) { 420a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (this == c) { 421a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; // Can always assign to things of the same type. 422a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (this == Object.class) { 423a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return !c.isPrimitive(); // Can assign any reference to java.lang.Object. 424a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isArray()) { 425a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return c.isArray() && componentType.isAssignableFrom(c.componentType); 426a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isInterface()) { 427a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search iftable which has a flattened and uniqued list of interfaces. 428a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = c.ifTable; 429a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 430a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 431a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) iftable[i]; 432a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (ifc == this) { 433a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 434a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 435a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 436a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 437a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 438a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 439a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!c.isInterface()) { 440a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (c = c.superClass; c != null; c = c.superClass) { 441a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == this) { 442a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 443a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 444a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 445a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 446a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 447a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 448a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 44951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 45151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 45251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Class} object represents an 45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface type. 45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this object represents an interface; 45651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 45751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 458a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isInterface() { 459a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (accessFlags & Modifier.INTERFACE) != 0; 460a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 46151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 46251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if this {@code Class} object represents an array class. 46551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 46651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this object represents an array class; 46751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false} otherwise. 46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 470a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isArray() { 471a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getComponentType() != null; 472a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the specified {@code Class} object represents a 47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type. 47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> There are nine predefined {@code Class} objects to represent 48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the eight primitive types and void. These are created by the Java 48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Virtual Machine, and have the same names as the primitive types that 48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * they represent, namely {@code boolean}, {@code byte}, 48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code char}, {@code short}, {@code int}, 48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code long}, {@code float}, and {@code double}. 48551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 48651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> These objects may only be accessed via the following public static 48751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * final variables, and are the only {@code Class} objects for which 48851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method returns {@code true}. 48951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 49051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if and only if this class represents a primitive type 49151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 49251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Boolean#TYPE 49351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Character#TYPE 49451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Byte#TYPE 49551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Short#TYPE 49651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Integer#TYPE 49751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Long#TYPE 49851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Float#TYPE 49951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Double#TYPE 50051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Void#TYPE 50151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 50251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 503a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isPrimitive() { 504a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return primitiveType != 0; 505a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns true if this {@code Class} object represents an annotation 50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type. Note that if this method returns true, {@link #isInterface()} 51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * would also return true, as all annotation types are also interfaces. 51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if this class object represents an annotation 51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type; {@code false} otherwise 51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isAnnotation() { 51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (getModifiers() & ANNOTATION) != 0; 51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 51951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if this class is a synthetic class; 52251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns {@code false} otherwise. 52351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a synthetic class as 52451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * defined by the Java Language Specification. 52551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 52651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 52751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isSynthetic() { 52851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (getModifiers() & SYNTHETIC) != 0; 52951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 53051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 53151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 53251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the name of the entity (class, interface, array class, 53351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, or void) represented by this {@code Class} object, 53451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as a {@code String}. 53551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 53651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a reference type that is not an 53751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array type then the binary name of the class is returned, as specified 53851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by 53951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>. 54051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 54151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a primitive type or void, then the 54251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name returned is a {@code String} equal to the Java language 54351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * keyword corresponding to the primitive type or void. 54451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 54551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this class object represents a class of arrays, then the internal 54651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * form of the name consists of the name of the element type preceded by 54751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one or more '{@code [}' characters representing the depth of the array 54851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * nesting. The encoding of element type names is as follows: 54951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 55051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><table summary="Element types and encodings"> 55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><th> Element Type <th> <th> Encoding 55251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> boolean <td> <td align=center> Z 55351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> byte <td> <td align=center> B 55451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> char <td> <td align=center> C 55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> class or interface 55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td> <td align=center> L<i>classname</i>; 55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> double <td> <td align=center> D 55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> float <td> <td align=center> F 55951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> int <td> <td align=center> I 56051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> long <td> <td align=center> J 56151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td> short <td> <td align=center> S 56251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </table></blockquote> 56351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 56451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The class or interface name <i>classname</i> is the binary name of 56551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class specified above. 56651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 56751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Examples: 56851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 56951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * String.class.getName() 57051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "java.lang.String" 57151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * byte.class.getName() 57251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "byte" 57351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (new Object[3]).getClass().getName() 57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "[Ljava.lang.Object;" 57551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (new int[3][4][5][6][7][8][9]).getClass().getName() 57651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns "[[[[[[[I" 57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 57951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the name of the class or interface 58051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 58251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getName() { 58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String name = this.name; 58451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (name == null) 585a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski this.name = name = getNameNative(); 58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 58751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 58851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 58951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // cache the name to reduce the number of calls into the VM 59051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private transient String name; 591a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 592a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native String getNameNative(); 59351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 59451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 59551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the class loader for the class. Some implementations may use 59651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null to represent the bootstrap class loader. This method will return 59751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null in such implementations if this class was loaded by the bootstrap 59851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader. 59951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 60051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If a security manager is present, and the caller's class loader is 60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not null and the caller's class loader is not the same as or an ancestor of 60251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class loader for the class whose class loader is requested, then 60351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method calls the security manager's {@code checkPermission} 60451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method with a {@code RuntimePermission("getClassLoader")} 60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * permission to ensure it's ok to access the class loader for the class. 60651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 60751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object 60851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type or void, null is returned. 60951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 61051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the class loader that loaded the class or interface 61151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 61251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 61351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if a security manager exists and its 61451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code checkPermission} method denies 61551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the class loader for the class. 61651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader 61751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 61851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.RuntimePermission 61951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 62051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 62151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public ClassLoader getClassLoader() { 622a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isPrimitive()) { 62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 62451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 625a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (classLoader == null) ? BootClassLoader.getInstance() : classLoader; 62651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 62751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 62851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 62951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 63051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 63151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code TypeVariable} objects that represent the 63251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type variables declared by the generic declaration represented by this 63351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code GenericDeclaration} object, in declaration order. Returns an 63451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if the underlying generic declaration declares no type 63551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * variables. 63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of {@code TypeVariable} objects that represent 63851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the type variables declared by this generic declaration 63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError if the generic 64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * signature of this generic declaration does not conform to 64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the format specified in 64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 645a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski @Override public synchronized TypeVariable<Class<T>>[] getTypeParameters() { 646a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String annotationSignature = AnnotationAccess.getSignature(this); 647a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature == null) { 648a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return EmptyArray.TYPE_VARIABLE; 649a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 650a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 651a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 652a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return parser.formalTypeParameters; 65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 65451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 65551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 65651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 65751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} representing the superclass of the entity 65851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (class, interface, primitive type or void) represented by this 65951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class}. If this {@code Class} represents either the 66051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Object} class, an interface, a primitive type, or void, then 66151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null is returned. If this object represents an array class then the 66251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object representing the {@code Object} class is 66351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. 66451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 66551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the superclass of the class represented by this object. 66651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 667a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<? super T> getSuperclass() { 668a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // For interfaces superClass is Object (which agrees with the JNI spec) 669a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // but not with the expected behavior here. 670a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isInterface()) { 671a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 672a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 673a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return superClass; 674a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 675a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 67651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Type} representing the direct superclass of 68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the entity (class, interface, primitive type or void) represented by 68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class}. 68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If the superclass is a parameterized type, the {@code Type} 68451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object returned must accurately reflect the actual type 68551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameters used in the source code. The parameterized type 68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing the superclass is created if it had not been 68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * created before. See the declaration of {@link 68851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.ParameterizedType ParameterizedType} for the 68951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * semantics of the creation process for parameterized types. If 69051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} represents either the {@code Object} 69151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, an interface, a primitive type, or void, then null is 69251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. If this object represents an array class then the 69351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object representing the {@code Object} class is 69451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned. 69551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 69651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError if the generic 69751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class signature does not conform to the format specified in 69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws TypeNotPresentException if the generic superclass 70051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * refers to a non-existent type declaration 70151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.MalformedParameterizedTypeException if the 70251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * generic superclass refers to a parameterized type that cannot be 70351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instantiated for any reason 70451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the superclass of the class represented by this object 70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 70651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 70751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Type getGenericSuperclass() { 708a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Type genericSuperclass = getSuperclass(); 709a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // This method is specified to return null for all cases where getSuperclass 710a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // returns null, i.e, for primitives, interfaces, void and java.lang.Object. 711a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (genericSuperclass == null) { 712a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 713a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 714a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 715a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String annotationSignature = AnnotationAccess.getSignature(this); 716a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature != null) { 717a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 718a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 719a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski genericSuperclass = parser.superclassType; 720a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 721a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return Types.getType(genericSuperclass); 72251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 72351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Gets the package for this class. The class loader of this class is used 72651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to find the package. If the class was loaded by the bootstrap class 72751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader the set of packages loaded from CLASSPATH is searched to find the 72851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * package of the class. Null is returned if no package object was created 72951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the class loader of this class. 73051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 73151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Packages have attributes for versions and specifications only if the 73251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * information was defined in the manifests that accompany the classes, and 73351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the class loader created the package instance with the attributes 73451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * from the manifest. 73551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 73651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the package of the class, or null if no package 73751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * information is available from the archive or codebase. 73851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Package getPackage() { 740a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader loader = getClassLoader(); 741a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (loader != null) { 742a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName = getPackageName$(); 743a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName != null ? loader.getPackage(packageName) : null; 744a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 745a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 746a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 747a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 748a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 749a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns the package name of this class. This returns null for classes in 750a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * the default package. 751a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 752a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 753a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 754a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public String getPackageName$() { 755a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String name = getName(); 756a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int last = name.lastIndexOf('.'); 757a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return last == -1 ? null : name.substring(0, last); 75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 76151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines the interfaces implemented by the class or interface 76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class, the return value is an array 76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * containing objects representing all interfaces implemented by the 76751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. The order of the interface objects in the array corresponds to 76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the order of the interface names in the {@code implements} clause 76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the declaration of the class represented by this object. For 77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, given the declaration: 77151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code class Shimmer implements FloorWax, DessertTopping { ... }} 77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * suppose the value of {@code s} is an instance of 77551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Shimmer}; the value of the expression: 77651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 77751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code s.getClass().getInterfaces()[0]} 77851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 77951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is the {@code Class} object that represents interface 78051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code FloorWax}; and the value of: 78151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code s.getClass().getInterfaces()[1]} 78351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is the {@code Class} object that represents interface 78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code DessertTopping}. 78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 78751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents an interface, the array contains objects 78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing all interfaces extended by the interface. The order of the 78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface objects in the array corresponds to the order of the interface 79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * names in the {@code extends} clause of the declaration of the 79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface represented by this object. 79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class or interface that implements no 79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces, the method returns an array of length 0. 79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a primitive type or void, the method 79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns an array of length 0. 79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 79951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of interfaces implemented by this class. 80051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 801a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?>[] getInterfaces() { 802a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isArray()) { 803a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return new Class<?>[] { Cloneable.class, Serializable.class }; 804a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (isProxy()) { 805a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getProxyInterfaces(); 806a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 807a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Dex dex = getDex(); 808a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dex == null) { 809a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return EmptyArray.CLASS; 810a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 811a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski short[] interfaces = dex.interfaceTypeIndicesFromClassDefIndex(dexClassDefIndex); 812a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?>[] result = new Class<?>[interfaces.length]; 813a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < interfaces.length; i++) { 814a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result[i] = getDexCacheType(dex, interfaces[i]); 815a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 816a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 817a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 818a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 819a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Returns the interfaces that this proxy class directly implements. 820a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native Class<?>[] getProxyInterfaces(); 82151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 82251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 82351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Type}s representing the interfaces 82451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * directly implemented by the class or interface represented by 82551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this object. 82651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 82751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If a superinterface is a parameterized type, the 82851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Type} object returned for it must accurately reflect 82951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the actual type parameters used in the source code. The 83051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameterized type representing each superinterface is created 83151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if it had not been created before. See the declaration of 83251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link java.lang.reflect.ParameterizedType ParameterizedType} 83351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for the semantics of the creation process for parameterized 83451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types. 83551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 83651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this object represents a class, the return value is an 83751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array containing objects representing all interfaces 83851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implemented by the class. The order of the interface objects in 83951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array corresponds to the order of the interface names in 84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the {@code implements} clause of the declaration of the class 84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. In the case of an array class, the 84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces {@code Cloneable} and {@code Serializable} are 84351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned in that order. 84451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 84551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents an interface, the array contains 84651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects representing all interfaces directly extended by the 84751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface. The order of the interface objects in the array 84851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * corresponds to the order of the interface names in the 84951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code extends} clause of the declaration of the interface 85051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this object. 85151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 85251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents a class or interface that 85351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implements no interfaces, the method returns an array of length 85451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 0. 85551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 85651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If this object represents a primitive type or void, the 85751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns an array of length 0. 85851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 85951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.GenericSignatureFormatError 86051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the generic class signature does not conform to the format 86151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified in 86251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Virtual Machine Specification</cite> 86351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws TypeNotPresentException if any of the generic 86451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces refers to a non-existent type declaration 86551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws java.lang.reflect.MalformedParameterizedTypeException 86651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if any of the generic superinterfaces refer to a parameterized 86751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type that cannot be instantiated for any reason 86851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of interfaces implemented by this class 86951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 87051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 87151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Type[] getGenericInterfaces() { 872a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Type[] result; 873a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized (Caches.genericInterfaces) { 874a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = Caches.genericInterfaces.get(this); 875a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null) { 876a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String annotationSignature = AnnotationAccess.getSignature(this); 877a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (annotationSignature == null) { 878a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = getInterfaces(); 879a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 880a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski GenericSignatureParser parser = new GenericSignatureParser(getClassLoader()); 881a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parser.parseForClass(this, annotationSignature); 882a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski result = Types.getTypeArray(parser.interfaceTypes, false); 883a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 884a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Caches.genericInterfaces.put(this, result); 885a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 886a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 887a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (result.length == 0) ? result : result.clone(); 88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code Class} representing the component type of an 89351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. If this class does not represent an array class this method 89451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns null. 89551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 89651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Class} representing the component type of this 89751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class if this class is an array 89851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.reflect.Array 89951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 90051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 901a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?> getComponentType() { 902a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return componentType; 903a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 90451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 90551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 90651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the Java language modifiers for this class or interface, encoded 90751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in an integer. The modifiers consist of the Java Virtual Machine's 90851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constants for {@code public}, {@code protected}, 90951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code private}, {@code final}, {@code static}, 91051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code abstract} and {@code interface}; they should be decoded 91151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * using the methods of class {@code Modifier}. 91251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 91351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the underlying class is an array class, then its 91451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code public}, {@code private} and {@code protected} 91551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * modifiers are the same as those of its component type. If this 91651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} represents a primitive type or void, its 91751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code public} modifier is always {@code true}, and its 91851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code protected} and {@code private} modifiers are always 91951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. If this object represents an array class, a 92051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type or void, then its {@code final} modifier is always 92151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code true} and its interface modifier is always 92251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code false}. The values of its other modifiers are not determined 92351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this specification. 92451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 92551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The modifier encodings are defined in <em>The Java Virtual Machine 92651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Specification</em>, table 4.1. 92751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 92851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code int} representing the modifiers for this class 92951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.reflect.Modifier 93051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 93151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 932a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getModifiers() { 933a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Array classes inherit modifiers from their component types, but in the case of arrays 934a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // of an inner class, the class file may contain "fake" access flags because it's not valid 935a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // for a top-level class to private, say. The real access flags are stored in the InnerClass 936a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // attribute, so we need to make sure we drill down to the inner class: the accessFlags 937a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // field is not the value we want to return, and the synthesized array class does not itself 938a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // have an InnerClass attribute. https://code.google.com/p/android/issues/detail?id=56267 939a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isArray()) { 940a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int componentModifiers = getComponentType().getModifiers(); 941a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if ((componentModifiers & Modifier.INTERFACE) != 0) { 942a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski componentModifiers &= ~(Modifier.INTERFACE | Modifier.STATIC); 943a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 944a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return Modifier.ABSTRACT | Modifier.FINAL | componentModifiers; 945a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 946a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int JAVA_FLAGS_MASK = 0xffff; 947a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int modifiers = AnnotationAccess.getInnerClassFlags(this, accessFlags & JAVA_FLAGS_MASK); 948a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return modifiers & JAVA_FLAGS_MASK; 949a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 95051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 95151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 95251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 95351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Gets the signers of this class. 95451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 95551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the signers of this class, or null if there are no signers. In 95651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular, this method returns null if this object represents 95751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a primitive type or void. 95851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 95951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 960a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Object[] getSigners() { 961a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 962a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 96351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 96451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 96551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 96651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents a local or anonymous 96751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class within a method, returns a {@link 96851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Method Method} object representing the 96951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * immediately enclosing method of the underlying class. Returns 97051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} otherwise. 97151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 97251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * In particular, this method returns {@code null} if the underlying 97351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class is a local or anonymous class immediately enclosed by a type 97451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declaration, instance initializer or static initializer. 97551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 97651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing method of the underlying class, if 97751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that class is a local or anonymous class; otherwise {@code null}. 97851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 97951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 98051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 98151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getEnclosingMethod() { 982a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classNameImpliesTopLevel()) { 98351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 98451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 985a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski AccessibleObject result = AnnotationAccess.getEnclosingMethodOrConstructor(this); 986a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result instanceof Method ? (Method) result : null; 98751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 98851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 98951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 99051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 99151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents a local or anonymous 99251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class within a constructor, returns a {@link 99351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Constructor Constructor} object representing 99451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the immediately enclosing constructor of the underlying 99551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. Returns {@code null} otherwise. In particular, this 99651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns {@code null} if the underlying class is a local 99751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or anonymous class immediately enclosed by a type declaration, 99851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instance initializer or static initializer. 99951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 100051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing constructor of the underlying class, if 100151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that class is a local or anonymous class; otherwise {@code null}. 100251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 100351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 100451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 100551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?> getEnclosingConstructor() { 1006a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classNameImpliesTopLevel()) { 100751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 100851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 1009a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski AccessibleObject result = AnnotationAccess.getEnclosingMethodOrConstructor(this); 1010a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result instanceof Constructor ? (Constructor<?>) result : null; 1011a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1012a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1013a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean classNameImpliesTopLevel() { 1014a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return !getName().contains("$"); 101551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 101651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 101751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 101851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 101951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the class or interface represented by this {@code Class} object 102051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a member of another class, returns the {@code Class} object 102151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representing the class in which it was declared. This method returns 102251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null if this class or interface is not a member of any other class. If 102351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents an array class, a primitive 102451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type, or void,then this method returns null. 102551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 102651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the declaring class for this class 102751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 102851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1029a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?> getDeclaringClass() { 1030a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (AnnotationAccess.isAnonymousClass(this)) { 1031a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 1032a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1033a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return AnnotationAccess.getEnclosingClass(this); 1034a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 103551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 103651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 103751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 103851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the immediately enclosing class of the underlying 103951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. If the underlying class is a top level class this 104051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns {@code null}. 104151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the immediately enclosing class of the underlying class 104251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 104351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 104451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 104551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Class<?> getEnclosingClass() { 1046a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> declaringClass = getDeclaringClass(); 1047a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (declaringClass != null) { 1048a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return declaringClass; 104951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 1050a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski AccessibleObject member = AnnotationAccess.getEnclosingMethodOrConstructor(this); 1051a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (member != null) { 1052a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return ((Member) member).getDeclaringClass(); 1053a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1054a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return AnnotationAccess.getEnclosingClass(this); 105551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 105651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 105751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 105851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the simple name of the underlying class as given in the 105951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code. Returns an empty string if the underlying class is 106051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * anonymous. 106151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 106251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The simple name of an array is the simple name of the 106351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * component type with "[]" appended. In particular the simple 106451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name of an array whose component type is anonymous is "[]". 106551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 106651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the simple name of the underlying class 106751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 106851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 106951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getSimpleName() { 107051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isArray()) 107151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getComponentType().getSimpleName()+"[]"; 107251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 107351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String simpleName = getSimpleBinaryName(); 107451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (simpleName == null) { // top level class 107551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski simpleName = getName(); 107651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name 107751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 107851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // According to JLS3 "Binary Compatibility" (13.1) the binary 107951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // name of non-package classes (not top level) is the binary 108051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // name of the immediately enclosing class followed by a '$' followed by: 108151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // (for nested and inner classes): the simple name. 108251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // (for local classes): 1 or more digits followed by the simple name. 108351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // (for anonymous classes): 1 or more digits. 108451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 108551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Since getSimpleBinaryName() will strip the binary name of 108651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // the immediatly enclosing class, we are now looking at a 108751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // string that matches the regular expression "\$[0-9]*" 108851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // followed by a simple name (considering the simple of an 108951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // anonymous class to be the empty string). 109051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 109151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Remove leading "\$[0-9]*" from the name 109251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = simpleName.length(); 109351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (length < 1 || simpleName.charAt(0) != '$') 109451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new InternalError("Malformed class name"); 109551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int index = 1; 109651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (index < length && isAsciiDigit(simpleName.charAt(index))) 109751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski index++; 109851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Eventually, this is the empty string iff this is an anonymous class 109951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return simpleName.substring(index); 110051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 110151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 110251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 110351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Character.isDigit answers {@code true} to some non-ascii 110451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * digits. This one does not. 110551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 110651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static boolean isAsciiDigit(char c) { 110751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return '0' <= c && c <= '9'; 110851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 110951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 111051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 111151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the canonical name of the underlying class as 111251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * defined by the Java Language Specification. Returns null if 111351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the underlying class does not have a canonical name (i.e., if 111451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is a local or anonymous class or an array whose component 111551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type does not have a canonical name). 111651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the canonical name of the underlying class if it exists, and 111751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} otherwise. 111851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 111951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 112051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String getCanonicalName() { 112151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isArray()) { 112251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String canonicalName = getComponentType().getCanonicalName(); 112351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (canonicalName != null) 112451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return canonicalName + "[]"; 112551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 112651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 112751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 112851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isLocalOrAnonymousClass()) 112951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 113051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> enclosingClass = getEnclosingClass(); 113151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingClass == null) { // top level class 113251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getName(); 113351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 113451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String enclosingName = enclosingClass.getCanonicalName(); 113551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingName == null) 113651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 113751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return enclosingName + "." + getSimpleName(); 113851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 113951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 114051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 114151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 114251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 114351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is an anonymous class. 114451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 114551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is an anonymous class. 114651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 114751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 114851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isAnonymousClass() { 114951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "".equals(getSimpleName()); 115051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 115151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 115251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 115351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 115451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a local class. 115551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 115651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a local class. 115751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 115851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 115951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isLocalClass() { 1160a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return !classNameImpliesTopLevel() 1161a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski && AnnotationAccess.getEnclosingMethodOrConstructor(this) != null 1162a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski && !isAnonymousClass(); 116351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 116451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 116551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 116651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if and only if the underlying class 116751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is a member class. 116851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 116951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return {@code true} if and only if this class is a member class. 117051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 117151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 117251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isMemberClass() { 117351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getSimpleBinaryName() != null && !isLocalOrAnonymousClass(); 117451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 117651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 117751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the "simple binary name" of the underlying class, i.e., 117851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary name without the leading enclosing class name. 117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code null} if the underlying class is a top level 118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. 118151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String getSimpleBinaryName() { 118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> enclosingClass = getEnclosingClass(); 118451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (enclosingClass == null) // top level class 118551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 118651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Otherwise, strip the enclosing class' name 118751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski try { 118851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getName().substring(enclosingClass.getName().length()); 118951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } catch (IndexOutOfBoundsException ex) { 119051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new InternalError("Malformed class name"); 119151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 119251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 119351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 119451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 119551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns {@code true} if this is a local class or an anonymous 119651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class. Returns {@code false} otherwise. 119751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private boolean isLocalOrAnonymousClass() { 119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // JVM Spec 4.8.6: A class must have an EnclosingMethod 120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // attribute if and only if it is a local class or an 120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // anonymous class. 1202a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return isLocalClass() || isAnonymousClass(); 120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Class} objects representing all 120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the public classes and interfaces that are members of the class 120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object. This includes public 120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class and interface members inherited from superclasses and public class 121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and interface members declared by the class. This method returns an 121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if this {@code Class} object has no public member 121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * classes or interfaces. This method also returns an array of length 0 if 121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type, an array 121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, or void. 121551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 121651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Class} objects representing the public 121751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * members of this class 121851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 121951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 122051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 122151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 122251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 122351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 122451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 122551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 122651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} method 122751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * denies access to the classes within this class 122851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 122951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 123051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 123351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 123451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 123651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 123751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 123851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 123951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 124051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Class<?>[] getClasses() { 124151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski List<Class<?>> list = new ArrayList<>(); 124251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> currentClass = Class.this; 124351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (currentClass != null) { 124451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?>[] members = currentClass.getDeclaredClasses(); 124551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; i < members.length; i++) { 124651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (Modifier.isPublic(members[i].getModifiers())) { 124751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski list.add(members[i]); 124851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 124951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 125051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski currentClass = currentClass.getSuperclass(); 125151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 125251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return list.toArray(new Class[0]); 125351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 125451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 125551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 125651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 125751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Field} objects reflecting all 125851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the accessible public fields of the class or interface represented by 125951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object. The elements in the array returned are 126051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not sorted and are not in any particular order. This method returns an 126151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of length 0 if the class or interface has no accessible public 126251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * fields, or if it represents an array class, a primitive type, or void. 126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Specifically, if this {@code Class} object represents a class, 126551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method returns the public fields of this class and of all its 126651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superclasses. If this {@code Class} object represents an 126751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface, this method returns the fields of this interface and of all 126851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * its superinterfaces. 126951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 127051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The implicit length field for array class is not reflected by this 127151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method. User code should use the methods of class {@code Array} to 127251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manipulate arrays. 127351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 127451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 127551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 127651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Field} objects representing the 127751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public fields 127851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 127951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 128051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 128151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 128251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 128351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 128451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 128551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 128651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 128751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the fields within this class 128851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 128951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 129051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 129151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 129251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 129351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 129451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 129551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 129651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 129751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 129851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 129951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 130051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Field[] getFields() throws SecurityException { 1301a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski List<Field> fields = new ArrayList<Field>(); 1302a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search superclasses 1303a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = this; c != null; c = c.superClass) { 1304de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski c.getDeclaredFieldsUnchecked(true, fields); 1305a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1306a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1307a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search iftable which has a flattened and uniqued list of interfaces 1308a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 1309a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 1310a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 1311a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) iftable[i]; 1312de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski ifc.getDeclaredFieldsUnchecked(true, fields); 1313a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1314a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1315a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Field[] result = fields.toArray(new Field[fields.size()]); 1316a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Field f : result) { 1317a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski f.getType(); // Throw NoClassDefFoundError if type cannot be resolved. 1318a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1319a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 132051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 132151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 132251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 132351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Method} objects reflecting all 132451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the public <em>member</em> methods of the class or interface represented 132551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this {@code Class} object, including those declared by the class 132651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or interface and those inherited from superclasses and 132751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces. Array classes return all the (public) member methods 132851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * inherited from the {@code Object} class. The elements in the array 132951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned are not sorted and are not in any particular order. This 133051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method returns an array of length 0 if this {@code Class} object 133151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a class or interface that has no public member methods, or if 133251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object represents a primitive type or void. 133351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 133451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The class initialization method {@code <clinit>} is not 133551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * included in the returned array. If the class declares multiple public 133651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member methods with the same parameter types, they are all included in 133751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the returned array. 133851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 133951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 134051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Method} objects representing the 134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public methods of this class 134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 134851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 135051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 135151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 135251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the methods within this class 135351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 135451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 135551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 135651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 135751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 135851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 135951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 136051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 136151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 136251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 136351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 136451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 136551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method[] getMethods() throws SecurityException { 1366a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski List<Method> methods = new ArrayList<Method>(); 1367de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski getDeclaredMethodsUnchecked(true, methods); 1368a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!isInterface()) { 1369a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search superclasses, for interfaces don't search java.lang.Object. 1370a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = superClass; c != null; c = c.superClass) { 1371de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski c.getDeclaredMethodsUnchecked(true, methods); 1372a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1373a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1374a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Search iftable which has a flattened and uniqued list of interfaces. 1375a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 1376a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 1377a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 1378a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) iftable[i]; 1379de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski ifc.getDeclaredMethodsUnchecked(true, methods); 1380a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1381a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1382a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /* 1383a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Remove duplicate methods defined by superclasses and 1384a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * interfaces, preferring to keep methods declared by derived 1385a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * types. 1386a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 1387a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski CollectionUtils.removeDuplicates(methods, Method.ORDER_BY_SIGNATURE); 1388a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return methods.toArray(new Method[methods.size()]); 138951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 139051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 139151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 139251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 139351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array containing {@code Constructor} objects reflecting 139451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * all the public constructors of the class represented by this 139551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. An array of length 0 is returned if the 139651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class has no public constructors, or if the class is an array class, or 139751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the class reflects a primitive type or void. 139851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 139951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that while this method returns an array of {@code 140051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor<T>} objects (that is an array of constructors from 140151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this class), the return type of this method is {@code 140251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as 140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * might be expected. This less informative return type is 140451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * necessary since after being returned from this method, the 140551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array could be modified to hold {@code Constructor} objects for 140651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * different classes, which would violate the type guarantees of 140751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Constructor<T>[]}. 140851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 140951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Constructor} objects representing the 141051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public constructors of this class 141151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 141251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 141351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 141451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 141551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 141651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 141751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 141851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 141951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 142051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the constructors within this class 142151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 142251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 142351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 142451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 142551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 142651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 142751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 142851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 142951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 143051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 143151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 143251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 143351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?>[] getConstructors() throws SecurityException { 1434a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ArrayList<Constructor<T>> constructors = new ArrayList(); 1435a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski getDeclaredConstructors(true, constructors); 1436a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return constructors.toArray(new Constructor[constructors.size()]); 143751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 143851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 143951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 144051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 144151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Field} object that reflects the specified public 144251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member field of the class or interface represented by this 144351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 144451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} specifying the simple name of the desired field. 144551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 144651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The field to be reflected is determined by the algorithm that 144751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * follows. Let C be the class represented by this object: 144851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <OL> 144951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If C declares a public field with the name specified, that is the 145051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * field to be reflected.</LI> 145151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no field was found in step 1 above, this algorithm is applied 145251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * recursively to each direct superinterface of C. The direct 145351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superinterfaces are searched in the order they were declared.</LI> 145451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no field was found in steps 1 and 2 above, and C has a 145551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * superclass S, then this algorithm is invoked recursively upon S. 145651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If C has no superclass, then a {@code NoSuchFieldException} 145751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is thrown.</LI> 145851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </OL> 145951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 146051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 146151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 146251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the field name 146351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Field} object of this class specified by 146451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code name} 146551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchFieldException if a field with the specified name is 146651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not found. 146751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 146851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 146951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 147051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 147151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 147251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 147351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 147451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 147551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 147651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 147751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the field 147851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 148151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 148251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 148351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 148451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 148551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 148651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 148751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 148851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 148951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 149051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Field getField(String name) 149151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchFieldException, SecurityException { 1492fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski if (name == null) { 1493fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski throw new NullPointerException("name == null"); 1494fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski } 1495a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Field field = getPublicFieldRecursive(name); 149651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (field == null) { 149751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NoSuchFieldException(name); 149851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 1499a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski field.getType(); // Throw NoClassDefFoundError if type cannot be resolved. 150051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return field; 150151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 150251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 1503a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Field getPublicFieldRecursive(String name) { 1504a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search superclasses 1505a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = this; c != null; c = c.superClass) { 1506a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Field result = c.getDeclaredFieldInternal(name); 1507a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && (result.getModifiers() & Modifier.PUBLIC) != 0) { 1508a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 1509a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1510a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1511a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1512a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search iftable which has a flattened and uniqued list of interfaces 1513a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (ifTable != null) { 1514a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < ifTable.length; i += 2) { 1515a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) ifTable[i]; 1516a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Field result = ifc.getPublicFieldRecursive(name); 1517a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && (result.getModifiers() & Modifier.PUBLIC) != 0) { 1518a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 1519a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1520a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1521a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1522a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1523a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 1524a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1525a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 152651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 152751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 152851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Method} object that reflects the specified public 152951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * member method of the class or interface represented by this 153051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 153151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} specifying the simple name of the desired method. The 153251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code parameterTypes} parameter is an array of {@code Class} 153351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * objects that identify the method's formal parameter types, in declared 153451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * order. If {@code parameterTypes} is {@code null}, it is 153551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * treated as if it were an empty array. 153651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 153751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a 153851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code NoSuchMethodException} is raised. Otherwise, the method to 153951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be reflected is determined by the algorithm that follows. Let C be the 154051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class represented by this object: 154151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <OL> 154251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> C is searched for any <I>matching methods</I>. If no matching 154351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method is found, the algorithm of step 1 is invoked recursively on 154451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the superclass of C.</LI> 154551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <LI> If no method was found in step 1 above, the superinterfaces of C 154651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are searched for a matching method. If any such method is found, it 154751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is reflected.</LI> 154851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </OL> 154951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 155051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * To find a matching method in a class C: If C declares exactly one 155151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public method with the specified name and exactly the same formal 155251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types, that is the method reflected. If more than one such 155351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method is found in C, and one of these methods has a return type that is 155451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * more specific than any of the others, that method is reflected; 155551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise one of the methods is chosen arbitrarily. 155651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 155751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that there may be more than one matching method in a 155851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class because while the Java language forbids a class to 155951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declare multiple methods with the same signature but different 156051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return types, the Java virtual machine does not. This 156151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * increased flexibility in the virtual machine can be used to 156251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implement various language features. For example, covariant 156351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns can be implemented with {@linkplain 156451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.lang.reflect.Method#isBridge bridge methods}; the bridge 156551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method and the method being overridden would have the same 156651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * signature but different return types. 156751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 156851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. 156951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 157051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the method 157151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the list of parameters 157251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Method} object that matches the specified 157351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code name} and {@code parameterTypes} 157451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found 157551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or if the name is "<init>"or "<clinit>". 157651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 157751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 157851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 157951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 158051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 158151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 158251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 158351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 158451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 158551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 158651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the method 158751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 158851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 158951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 159051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 159151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 159251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 159351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 159451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 159551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 159651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 159751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 159851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 159951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getMethod(String name, Class<?>... parameterTypes) 160051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 1601a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getMethod(name, parameterTypes, true); 160251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 160351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 160451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 160551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 160651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Constructor} object that reflects the specified 160751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public constructor of the class represented by this {@code Class} 160851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The {@code parameterTypes} parameter is an array of 160951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that identify the constructor's formal 161051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types, in declared order. 161151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 161251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents an inner class 161351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared in a non-static context, the formal parameter types 161451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * include the explicit enclosing instance as the first parameter. 161551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 161651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The constructor to reflect is the public constructor of the class 161751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this {@code Class} object whose formal parameter 161851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types match those specified by {@code parameterTypes}. 161951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 162051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 162151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Constructor} object of the public constructor that 162251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * matches the specified {@code parameterTypes} 162351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found. 162451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 162551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 162651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 162751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 162851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 162951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 163051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 163151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 163251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.PUBLIC)} denies 163351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the constructor 163451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 163551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 163651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 163751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 163851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 163951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 164051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 164151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 164251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 164351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 164451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 164551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 164651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<T> getConstructor(Class<?>... parameterTypes) 164751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 164851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getConstructor0(parameterTypes, Member.PUBLIC); 164951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 165051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 165151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 165251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 165351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Class} objects reflecting all the 165451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * classes and interfaces declared as members of the class represented by 165551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this {@code Class} object. This includes public, protected, default 165651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private classes and interfaces declared by the 165751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, but excludes inherited classes and interfaces. This method 165851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns an array of length 0 if the class declares no classes or 165951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interfaces as members, or if this {@code Class} object represents a 166051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primitive type, an array class, or void. 166151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 166251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Class} objects representing all the 166351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared members of this class 166451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 166551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 166651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 166751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 166851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 166951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 167051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 167151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 167251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 167351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared classes within this class 167451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 167551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 167651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 167751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 167851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 167951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 168051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 168151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 168251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 168351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 168451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 168551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 168651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Class<?>[] getDeclaredClasses() throws SecurityException { 1687a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return AnnotationAccess.getMemberClasses(this); 168851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 168951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 169051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 169151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 169251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Field} objects reflecting all the fields 169351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared by the class or interface represented by this 169451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. This includes public, protected, default 169551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private fields, but excludes inherited fields. 169651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The elements in the array returned are not sorted and are not in any 169751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular order. This method returns an array of length 0 if the class 169851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or interface declares no fields, or if this {@code Class} object 169951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type, an array class, or void. 170051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. 170251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Field} objects representing all the 170451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared fields of this class 170551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 170651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 170751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 170851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 171051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 171151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 171251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 171351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 171451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared fields within this class 171551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 171651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 171751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 171851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 171951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 172051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 172151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 172251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 172351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 172451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 172551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 172651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 172751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Field[] getDeclaredFields() throws SecurityException { 1728a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int initial_size = sFields == null ? 0 : sFields.length; 1729a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski initial_size += iFields == null ? 0 : iFields.length; 1730a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ArrayList<Field> fields = new ArrayList(initial_size); 1731de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski getDeclaredFieldsUnchecked(false, fields); 1732a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Field[] result = fields.toArray(new Field[fields.size()]); 1733a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Field f : result) { 1734a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski f.getType(); // Throw NoClassDefFoundError if type cannot be resolved. 1735a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1736a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 1737a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1738a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1739de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski /** @hide */ 1740de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski public void getDeclaredFieldsUnchecked(boolean publicOnly, List<Field> fields) { 1741a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iFields != null) { 1742a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (ArtField f : iFields) { 1743a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!publicOnly || Modifier.isPublic(f.getAccessFlags())) { 1744a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski fields.add(new Field(f)); 1745a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1746a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1747a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1748a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (sFields != null) { 1749a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (ArtField f : sFields) { 1750a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!publicOnly || Modifier.isPublic(f.getAccessFlags())) { 1751a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski fields.add(new Field(f)); 1752a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1753a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1754a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 175551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 175651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 175751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 175851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 175951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Method} objects reflecting all the 176051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * methods declared by the class or interface represented by this 176151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. This includes public, protected, default 176251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private methods, but excludes inherited methods. 176351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The elements in the array returned are not sorted and are not in any 176451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular order. This method returns an array of length 0 if the class 176551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or interface declares no methods, or if this {@code Class} object 176651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents a primitive type, an array class, or void. The class 176751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * initialization method {@code <clinit>} is not included in the 176851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned array. If the class declares multiple public member methods 176951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with the same parameter types, they are all included in the returned 177051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. 177151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 177251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, section 8.2. 177351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 177451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Method} objects representing all the 177551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared methods of this class 177651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 177751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 177851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 177951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 178051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 178151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 178251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 178351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 178451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 178551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared methods within this class 178651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 178751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 178851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 178951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 179051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 179151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 179251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 179351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 179451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 179551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 179651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 179751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 179851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method[] getDeclaredMethods() throws SecurityException { 1799a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int initial_size = virtualMethods == null ? 0 : virtualMethods.length; 1800a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski initial_size += directMethods == null ? 0 : directMethods.length; 1801a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ArrayList<Method> methods = new ArrayList<Method>(initial_size); 1802de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski getDeclaredMethodsUnchecked(false, methods); 1803a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method[] result = methods.toArray(new Method[methods.size()]); 1804a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Method m : result) { 1805a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Throw NoClassDefFoundError if types cannot be resolved. 1806a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski m.getReturnType(); 1807a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski m.getParameterTypes(); 1808a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1809a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 1810a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1811a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1812de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski /** @hide */ 1813de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski public void getDeclaredMethodsUnchecked(boolean publicOnly, List<Method> methods) { 1814a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (virtualMethods != null) { 1815a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (ArtMethod m : virtualMethods) { 1816a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int modifiers = m.getAccessFlags(); 1817a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!publicOnly || Modifier.isPublic(modifiers)) { 1818a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Add non-miranda virtual methods. 1819a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if ((modifiers & Modifier.MIRANDA) == 0) { 1820a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski methods.add(new Method(m)); 1821a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1822a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1823a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1824a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1825a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (directMethods != null) { 1826a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (ArtMethod m : directMethods) { 1827a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int modifiers = m.getAccessFlags(); 1828a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!publicOnly || Modifier.isPublic(modifiers)) { 1829a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Add non-constructor direct/static methods. 1830a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!Modifier.isConstructor(modifiers)) { 1831a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski methods.add(new Method(m)); 1832a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1833a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1834a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1835a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 183651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 183751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 183851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 183951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 184051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of {@code Constructor} objects reflecting all the 184151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructors declared by the class represented by this 184251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. These are public, protected, default 184351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (package) access, and private constructors. The elements in the array 184451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned are not sorted and are not in any particular order. If the 184551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class has a default constructor, it is included in the returned array. 184651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method returns an array of length 0 if this {@code Class} 184751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object represents an interface, a primitive type, an array class, or 184851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * void. 184951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 185051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> See <em>The Java Language Specification</em>, section 8.2. 185151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 185251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the array of {@code Constructor} objects representing all the 185351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared constructors of this class 185451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 185551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 185651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 185751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 185851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 185951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 186051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 186151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 186251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 186351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared constructors within this class 186451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 186551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 186651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 186751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 186851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 186951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 187051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 187151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 187251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 187351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 187451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 187551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 187651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<?>[] getDeclaredConstructors() throws SecurityException { 1877a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ArrayList<Constructor<T>> constructors = new ArrayList(); 1878a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski getDeclaredConstructors(false, constructors); 1879a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return constructors.toArray(new Constructor[constructors.size()]); 1880a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1881a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1882a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private void getDeclaredConstructors(boolean publicOnly, List<Constructor<T>> constructors) { 1883a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (directMethods != null) { 1884a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (ArtMethod m : directMethods) { 1885a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int modifiers = m.getAccessFlags(); 1886a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!publicOnly || Modifier.isPublic(modifiers)) { 1887a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Modifier.isStatic(modifiers)) { 1888a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // skip <clinit> which is a static constructor 1889a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski continue; 1890a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1891a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Modifier.isConstructor(modifiers)) { 1892a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski constructors.add(new Constructor<T>(m)); 1893a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1894a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1895a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1896a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 189751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 189851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 189951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 190051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 190151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Field} object that reflects the specified declared 190251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * field of the class or interface represented by this {@code Class} 190351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object. The {@code name} parameter is a {@code String} that 190451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specifies the simple name of the desired field. Note that this method 190551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will not reflect the {@code length} field of an array class. 190651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 190751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the field 190851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Field} object for the specified field in this 190951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class 191051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchFieldException if a field with the specified name is 191151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not found. 191251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 191351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 191451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 191551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 191651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 191751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 191851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 191951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 192051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 192151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 192251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared field 192351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 192451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 192551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 192651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 192751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 192851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 192951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 193051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 193151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 193251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 193351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 193451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 193551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Field getDeclaredField(String name) 193651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchFieldException, SecurityException { 1937fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski<<<<<<< HEAD 1938fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski======= 1939fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski /* ----- BEGIN android ----- 1940fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski // be very careful not to change the stack depth of this 1941fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski // checkMemberAccess call for security reasons 1942fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski // see java.lang.SecurityManager.checkMemberAccess 1943fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); 1944fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski Field field = searchFields(privateGetDeclaredFields(false), name);*/ 1945fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski if (name == null) { 1946fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski throw new NullPointerException(); 1947fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski } 1948fe26efa3f4d36ab6132ba2d01898cdf11c89667bPiotr Jastrzebski>>>>>>> a3f122d... Throw NullPointerException from reflection 1949a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Field field = getDeclaredFieldInternal(name); 195051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (field == null) { 195151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NoSuchFieldException(name); 195251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 195351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return field; 195451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 195551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 1956a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Field getDeclaredFieldInternal(String name) { 1957a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iFields != null) { 1958a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (ArtField f : iFields) { 1959a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (f.getName().equals(name)) { 1960a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return new Field(f); 1961a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1962a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1963a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1964a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (sFields != null) { 1965a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (ArtField f : sFields) { 1966a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (f.getName().equals(name)) { 1967a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return new Field(f); 1968a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1969a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1970a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1971a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 1972a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1973a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 197451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 197551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 197651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Method} object that reflects the specified 197751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared method of the class or interface represented by this 197851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code name} parameter is a 197951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code String} that specifies the simple name of the desired 198051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method, and the {@code parameterTypes} parameter is an array of 198151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that identify the method's formal parameter 198251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types, in declared order. If more than one method with the same 198351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter types is declared in a class, and one of these methods has a 198451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return type that is more specific than any of the others, that method is 198551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returned; otherwise one of the methods is chosen arbitrarily. If the 198651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name is "<init>"or "<clinit>" a {@code NoSuchMethodException} 198751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is raised. 198851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 198951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the method 199051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 199151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the {@code Method} object for the method of this class 199251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * matching the specified name and parameters 199351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found. 199451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NullPointerException if {@code name} is {@code null} 199551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 199651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 199751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 199851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 199951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 200051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 200151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 200251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 200351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 200451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared method 200551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 200651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 200751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 200851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 200951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 201051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 201151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 201251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 201351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 201451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 201551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 201651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 201751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Method getDeclaredMethod(String name, Class<?>... parameterTypes) 201851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 2019a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getMethod(name, parameterTypes, false); 2020a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2021a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2022a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Method getMethod(String name, Class<?>[] parameterTypes, boolean recursivePublicMethods) 2023a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throws NoSuchMethodException { 2024a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (name == null) { 2025a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NullPointerException("name == null"); 2026a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2027a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parameterTypes == null) { 2028a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parameterTypes = EmptyArray.CLASS; 2029a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2030a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c : parameterTypes) { 2031a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == null) { 2032a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("parameter type is null"); 2033a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2034a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2035a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = recursivePublicMethods ? getPublicMethodRecursive(name, parameterTypes) 2036a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski : getDeclaredMethodInternal(name, parameterTypes); 2037a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Fail if we didn't find the method or it was expected to be public. 2038a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null || 2039a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski (recursivePublicMethods && !Modifier.isPublic(result.getAccessFlags()))) { 2040a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException(name + " " + Arrays.toString(parameterTypes)); 2041a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2042a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2043a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2044a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2045a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Method getPublicMethodRecursive(String name, Class<?>[] parameterTypes) { 2046a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search superclasses 2047a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c = this; c != null; c = c.getSuperclass()) { 2048a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = c.getDeclaredMethodInternal(name, parameterTypes); 2049a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && Modifier.isPublic(result.getAccessFlags())) { 2050a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2051a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2052a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2053a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // search iftable which has a flattened and uniqued list of interfaces 2054a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object[] iftable = ifTable; 2055a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (iftable != null) { 2056a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < iftable.length; i += 2) { 2057a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> ifc = (Class<?>) iftable[i]; 2058a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Method result = ifc.getPublicMethodRecursive(name, parameterTypes); 2059a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result != null && Modifier.isPublic(result.getAccessFlags())) { 2060a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 2061a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2062a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 206351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2064a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 206551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 206651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 206751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 206851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 206951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a {@code Constructor} object that reflects the specified 207051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructor of the class or interface represented by this 207151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} object. The {@code parameterTypes} parameter is 207251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array of {@code Class} objects that identify the constructor's 207351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * formal parameter types, in declared order. 207451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 207551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this {@code Class} object represents an inner class 207651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared in a non-static context, the formal parameter types 207751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * include the explicit enclosing instance as the first parameter. 207851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 207951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param parameterTypes the parameter array 208051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return The {@code Constructor} object for the constructor with the 208151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified parameter list 208251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception NoSuchMethodException if a matching method is not found. 208351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException 208451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a security manager, <i>s</i>, is present and any of the 208551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following conditions is met: 208651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 208751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 208851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 208951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> invocation of 209051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkMemberAccess 209151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkMemberAccess(this, Member.DECLARED)} denies 209251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access to the declared constructor 209351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 209451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> the caller's class loader is not the same as or an 209551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ancestor of the class loader for the current class and 209651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation of {@link SecurityManager#checkPackageAccess 209751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * s.checkPackageAccess()} denies access to the package 209851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this class 209951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 210051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 210151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 210251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 210351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 210451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 210551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 210651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws NoSuchMethodException, SecurityException { 210751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getConstructor0(parameterTypes, Member.DECLARED); 210851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 210951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 211051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 211151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Finds a resource with a given name. The rules for searching resources 211251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * associated with a given class are implemented by the defining 211351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain ClassLoader class loader} of the class. This method 211451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * delegates to this object's class loader. If this object was loaded by 211551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the bootstrap class loader, the method delegates to {@link 211651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader#getSystemResourceAsStream}. 211751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 211851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Before delegation, an absolute resource name is constructed from the 211951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * given resource name using this algorithm: 212051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 212151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 212251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 212351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> If the {@code name} begins with a {@code '/'} 212451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 212551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * portion of the {@code name} following the {@code '/'}. 212651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 212751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> Otherwise, the absolute name is of the following form: 212851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 212951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 213051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code modified_package_name/name} 213151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 213251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 213351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Where the {@code modified_package_name} is the package name of this 213451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object with {@code '/'} substituted for {@code '.'} 213551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002e'</tt>). 213651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 213751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 213851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 213951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name name of the desired resource 214051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return A {@link java.io.InputStream} object or {@code null} if 214151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * no resource with this name is found 214251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException If {@code name} is {@code null} 214351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 214451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 214551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public InputStream getResourceAsStream(String name) { 214651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = resolveName(name); 2147a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader cl = getClassLoader(); 214851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cl==null) { 214951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // A system class. 215051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ClassLoader.getSystemResourceAsStream(name); 215151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 215251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return cl.getResourceAsStream(name); 215351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 215451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 215551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 215651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Finds a resource with a given name. The rules for searching resources 215751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * associated with a given class are implemented by the defining 215851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain ClassLoader class loader} of the class. This method 215951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * delegates to this object's class loader. If this object was loaded by 216051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the bootstrap class loader, the method delegates to {@link 216151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader#getSystemResource}. 216251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 216351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Before delegation, an absolute resource name is constructed from the 216451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * given resource name using this algorithm: 216551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 216651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 216751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 216851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> If the {@code name} begins with a {@code '/'} 216951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002f'</tt>), then the absolute name of the resource is the 217051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * portion of the {@code name} following the {@code '/'}. 217151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 217251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> Otherwise, the absolute name is of the following form: 217351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 217451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 217551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code modified_package_name/name} 217651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 217751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 217851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Where the {@code modified_package_name} is the package name of this 217951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object with {@code '/'} substituted for {@code '.'} 218051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>'\u002e'</tt>). 218151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 218251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 218351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 218451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name name of the desired resource 218551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return A {@link java.net.URL} object or {@code null} if no 218651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * resource with this name is found 218751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.1 218851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 218951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public java.net.URL getResource(String name) { 219051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = resolveName(name); 2191a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ClassLoader cl = getClassLoader(); 219251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cl==null) { 219351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // A system class. 219451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ClassLoader.getSystemResource(name); 219551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 219651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return cl.getResource(name); 219751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 219851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 219951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 220051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 220151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 220251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the {@code ProtectionDomain} of this class. If there is a 220351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * security manager installed, this method first calls the security 220451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager's {@code checkPermission} method with a 220551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code RuntimePermission("getProtectionDomain")} permission to 220651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ensure it's ok to get the 220751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code ProtectionDomain}. 220851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 220951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the ProtectionDomain of this class 221051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 221151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 221251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if a security manager exists and its 221351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code checkPermission} method doesn't allow 221451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * getting the ProtectionDomain. 221551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 221651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.security.ProtectionDomain 221751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 221851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.RuntimePermission 221951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 222051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 222151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public java.security.ProtectionDomain getProtectionDomain() { 2222a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 222351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 222451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 222551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 222651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Add a package name prefix if the name is not absolute Remove leading "/" 222751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if name is absolute 222851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 222951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String resolveName(String name) { 223051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (name == null) { 223151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 223251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 223351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (!name.startsWith("/")) { 223451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<?> c = this; 223551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (c.isArray()) { 223651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski c = c.getComponentType(); 223751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 223851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String baseName = c.getName(); 223951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int index = baseName.lastIndexOf('.'); 224051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (index != -1) { 224151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = baseName.substring(0, index).replace('.', '/') 224251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski +"/"+name; 224351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 224451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 224551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski name = name.substring(1); 224651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 224751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 224851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 224951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2250a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Constructor<T> getConstructor0(Class<?>[] parameterTypes, 2251a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int which) throws NoSuchMethodException 2252a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski { 2253a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parameterTypes == null) { 2254a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parameterTypes = EmptyArray.CLASS; 225551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2256a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> c : parameterTypes) { 2257a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (c == null) { 2258a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("parameter type is null"); 2259a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 226051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2261a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Constructor<T> result = getDeclaredConstructorInternal(parameterTypes); 2262a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (result == null || which == Member.PUBLIC && !Modifier.isPublic(result.getAccessFlags())) { 2263a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new NoSuchMethodException("<init> " + Arrays.toString(parameterTypes)); 2264a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2265a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result; 226651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 226751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2268a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Constructor<T> getDeclaredConstructorInternal(Class<?>[] args) { 2269a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (directMethods != null) { 2270a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (ArtMethod m : directMethods) { 2271a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int modifiers = m.getAccessFlags(); 2272a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Modifier.isStatic(modifiers)) { 2273a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // skip <clinit> which is a static constructor 2274a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski continue; 227551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2276a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!Modifier.isConstructor(modifiers)) { 2277a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski continue; 227851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2279a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!ArtMethod.equalConstructorParameters(m, args)) { 2280a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski continue; 228151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 2282a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return new Constructor<T>(m); 228351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 228451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 228551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 228651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 228751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 228851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 228951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** use serialVersionUID from JDK 1.1 for interoperability */ 229051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static final long serialVersionUID = 3206093459760846163L; 229151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 229251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 229351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the assertion status that would be assigned to this 229451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class if it were to be initialized at the time this method is invoked. 229551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this class has had its assertion status set, the most recent 229651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * setting will be returned; otherwise, if any package default assertion 229751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * status pertains to this class, the most recent setting for the most 229851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specific pertinent package default assertion status is returned; 229951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, if this class is not a system class (i.e., it has a 230051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader) its class loader's default assertion status is returned; 230151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, the system class default assertion status is returned. 230251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 230351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Few programmers will have any need for this method; it is provided 230451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for the benefit of the JRE itself. (It allows a class to determine at 230551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the time that it is initialized whether assertions should be enabled.) 230651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this method is not guaranteed to return the actual 230751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * assertion status that was (or will be) associated with the specified 230851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class when it was (or will be) initialized. 230951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 231051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the desired assertion status of the specified class. 231151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setClassAssertionStatus 231251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setPackageAssertionStatus 231351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.ClassLoader#setDefaultAssertionStatus 231451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 231551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 231651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean desiredAssertionStatus() { 2317a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 231851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 231951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 232051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 232151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns true if and only if this class was declared as an enum in the 232251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code. 232351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 232451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if and only if this class was declared as an enum in the 232551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * source code 232651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 232751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 232851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isEnum() { 232951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // An enum must both directly extend java.lang.Enum and have 233051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // the ENUM bit set; classes for specialized enum constants 233151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // don't do the former. 233251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (this.getModifiers() & ENUM) != 0 && 233351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.getSuperclass() == java.lang.Enum.class; 233451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 233551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 233651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 233751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the elements of this enum class or null if this 233851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class object does not represent an enum type. 233951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 234051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array containing the values comprising the enum class 234151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by this Class object in the order they're 234251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declared, or null if this Class object does not 234351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represent an enum type 234451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 234551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 234651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public T[] getEnumConstants() { 234751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] values = getEnumConstantsShared(); 234851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (values != null) ? values.clone() : null; 234951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 235051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 235151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 235251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the elements of this enum class or null if this 235351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Class object does not represent an enum type; 235451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * identical to getEnumConstants except that the result is 235551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * uncloned, cached, and shared by all callers. 235651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 235751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] getEnumConstantsShared() { 2358a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!isEnum()) return null; 2359a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (T[]) Enum.getSharedConstants((Class) this); 236051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 236151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 236251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 236351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Casts an object to the class or interface represented 236451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by this {@code Class} object. 236551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 236651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param obj the object to be cast 236751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the object after casting, or null if obj is null 236851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 236951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the object is not 237051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * null and is not assignable to the type T. 237151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 237251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 237351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 237451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public T cast(Object obj) { 237551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (obj != null && !isInstance(obj)) 237651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new ClassCastException(cannotCastMsg(obj)); 237751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (T) obj; 237851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 237951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 238051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private String cannotCastMsg(Object obj) { 238151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "Cannot cast " + obj.getClass().getName() + " to " + getName(); 238251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 238351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 238451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 238551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Casts this {@code Class} object to represent a subclass of the class 238651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represented by the specified class object. Checks that that the cast 238751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is valid, and throws a {@code ClassCastException} if it is not. If 238851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method succeeds, it always returns a reference to this class object. 238951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 239051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This method is useful when a client needs to "narrow" the type of 239151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a {@code Class} object to pass it to an API that restricts the 239251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Class} objects that it is willing to accept. A cast would 239351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * generate a compile-time warning, as the correctness of the cast 239451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * could not be checked at runtime (because generic types are implemented 239551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by erasure). 239651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 239751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return this {@code Class} object, cast to represent a subclass of 239851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified class object. 239951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if this {@code Class} object does not 240051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represent a subclass of the specified class (here "subclass" includes 240151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the class itself). 240251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 240351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 240451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public <U> Class<? extends U> asSubclass(Class<U> clazz) { 240551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (clazz.isAssignableFrom(this)) 240651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (Class<? extends U>) this; 240751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 240887778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak /* ----- BEGIN android ----- 240987778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak // Add more context 241087778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak throw new ClassCastException(this.toString());*/ 241187778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak throw new ClassCastException(this.toString() + 241287778fb754e6e9bf04e1d8e10fd13f49cc2aa608Przemyslaw Szczepaniak " cannot be cast to " + clazz.getName()); 241351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 241451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 241551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 241651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException {@inheritDoc} 241751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 241851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 241951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public <A extends Annotation> A getAnnotation(Class<A> annotationClass) { 242051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (annotationClass == null) 242151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NullPointerException(); 242251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2423a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return AnnotationAccess.getAnnotation(this, annotationClass); 242451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 242551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 242651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 242751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException {@inheritDoc} 242851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 242951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 243051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean isAnnotationPresent( 243151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class<? extends Annotation> annotationClass) { 243251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (annotationClass == null) 243351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new NullPointerException(); 243451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 243551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return getAnnotation(annotationClass) != null; 243651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 243751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 243851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 243951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 244051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 244151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 244251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Annotation[] getAnnotations() { 2443a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return AnnotationAccess.getAnnotations(this); 244451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 244551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 244651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 244751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 244851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 244951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Annotation[] getDeclaredAnnotations() { 2450a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski List<Annotation> result = AnnotationAccess.getDeclaredAnnotations(this); 2451a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return result.toArray(new Annotation[result.size()]); 245251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 245351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 245451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Annotation types cache their internal (AnnotationType) form 245551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 245651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private AnnotationType annotationType; 245751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2458a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** @hide */ 2459a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public void setAnnotationType(AnnotationType type) { 246051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski annotationType = type; 246151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 246251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2463a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** @hide */ 2464a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public AnnotationType getAnnotationType() { 246551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return annotationType; 246651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 246751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2468a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2469a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Is this a runtime created proxy class? 2470a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2471a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2472a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2473a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public boolean isProxy() { 2474a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (accessFlags & 0x00040000) != 0; 2475a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2476a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2477a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2478a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns the dex file from which this class was loaded. 2479a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2480a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2481a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2482a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Dex getDex() { 2483a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dexCache == null) { 2484a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 2485a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2486a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return dexCache.getDex(); 2487a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2488a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2489a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2490a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns a string from the dex cache, computing the string from the dex file if necessary. 2491a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2492a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2493a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2494a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public String getDexCacheString(Dex dex, int dexStringIndex) { 2495a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String s = dexCacheStrings[dexStringIndex]; 2496a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (s == null) { 2497a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski s = dex.strings().get(dexStringIndex).intern(); 2498a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski dexCacheStrings[dexStringIndex] = s; 2499a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2500a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return s; 2501a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2502a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2503a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2504a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Returns a resolved type from the dex cache, computing the type from the dex file if 2505a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * necessary. 2506a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2507a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2508a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2509a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public Class<?> getDexCacheType(Dex dex, int dexTypeIndex) { 2510a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?>[] dexCacheResolvedTypes = dexCache.resolvedTypes; 2511a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Class<?> resolvedType = dexCacheResolvedTypes[dexTypeIndex]; 2512a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (resolvedType == null) { 2513a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int descriptorIndex = dex.typeIds().get(dexTypeIndex); 2514a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String descriptor = getDexCacheString(dex, descriptorIndex); 2515a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski resolvedType = InternalNames.getClass(getClassLoader(), descriptor); 2516a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski dexCacheResolvedTypes[dexTypeIndex] = resolvedType; 2517a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2518a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return resolvedType; 2519a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2520a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2521a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2522a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The annotation directory offset of this class in its own Dex, or 0 if it 2523a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * is unknown. 2524a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2525a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * TODO: 0 is a sentinel that means 'no annotations directory'; this should be -1 if unknown 2526a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2527a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2528a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2529a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getDexAnnotationDirectoryOffset() { 2530a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Dex dex = getDex(); 2531a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dex == null) { 2532a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return 0; 2533a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2534a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int classDefIndex = getDexClassDefIndex(); 2535a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classDefIndex < 0) { 2536a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return 0; 2537a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2538a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return dex.annotationDirectoryOffsetFromClassDefIndex(classDefIndex); 2539a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2540a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2541a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2542a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The type index of this class in its own Dex, or -1 if it is unknown. If a class is referenced 2543a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * by multiple Dex files, it will have a different type index in each. Dex files support 65534 2544a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * type indices, with 65535 representing no index. 2545a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2546a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2547a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2548a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getDexTypeIndex() { 2549a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int typeIndex = dexTypeIndex; 2550a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (typeIndex != 65535) { 2551a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return typeIndex; 2552a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2553a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized (this) { 2554a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = dexTypeIndex; 2555a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (typeIndex == 65535) { 2556a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (dexClassDefIndex >= 0) { 2557a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = getDex().typeIndexFromClassDefIndex(dexClassDefIndex); 2558a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 2559a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = getDex().findTypeIndex(InternalNames.getInternalName(this)); 2560a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (typeIndex < 0) { 2561a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski typeIndex = -1; 2562a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2563a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2564a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski dexTypeIndex = typeIndex; 2565a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2566a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2567a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return typeIndex; 2568a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2569a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2570a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean canAccess(Class<?> c) { 2571a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if(Modifier.isPublic(c.accessFlags)) { 2572a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 2573a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2574a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return inSamePackage(c); 2575a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2576a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2577a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean canAccessMember(Class<?> memberClass, int memberModifiers) { 2578a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (memberClass == this || Modifier.isPublic(memberModifiers)) { 2579a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 2580a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2581a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Modifier.isPrivate(memberModifiers)) { 2582a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 2583a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2584a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Modifier.isProtected(memberModifiers)) { 2585a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (Class<?> parent = this.superClass; parent != null; parent = parent.superClass) { 2586a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parent == memberClass) { 2587a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return true; 2588a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2589a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2590a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2591a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return inSamePackage(memberClass); 2592a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2593a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2594a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private boolean inSamePackage(Class<?> c) { 2595a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (classLoader != c.classLoader) { 2596a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 2597a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2598a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName1 = getPackageName$(); 2599a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String packageName2 = c.getPackageName$(); 2600a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (packageName1 == null) { 2601a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName2 == null; 2602a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else if (packageName2 == null) { 2603a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return false; 2604a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 2605a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return packageName1.equals(packageName2); 2606a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2607a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2608a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2609a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2610a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2611a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2612a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getAccessFlags() { 2613a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return accessFlags; 2614a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2615a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2616a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private Method getDeclaredMethodInternal(String name, Class<?>[] args) { 2617a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Covariant return types permit the class to define multiple 2618a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // methods with the same name and parameter types. Prefer to 2619a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // return a non-synthetic method in such situations. We may 2620a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // still return a synthetic method to handle situations like 2621a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // escalated visibility. We never return miranda methods that 2622a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // were synthesized by the VM. 2623a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int skipModifiers = Modifier.MIRANDA | Modifier.SYNTHETIC; 2624a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski ArtMethod artMethodResult = null; 2625a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (virtualMethods != null) { 2626a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (ArtMethod m : virtualMethods) { 2627a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String methodName = ArtMethod.getMethodName(m); 2628a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!name.equals(methodName)) { 2629a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski continue; 2630a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2631a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!ArtMethod.equalMethodParameters(m, args)) { 2632a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski continue; 2633a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2634a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int modifiers = m.getAccessFlags(); 2635a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if ((modifiers & skipModifiers) == 0) { 2636a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return new Method(m); 2637a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2638a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if ((modifiers & Modifier.MIRANDA) == 0) { 2639a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Remember as potential result if it's not a miranda method. 2640a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski artMethodResult = m; 2641a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2642a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2643a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2644a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (artMethodResult == null) { 2645a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (directMethods != null) { 2646a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (ArtMethod m : directMethods) { 2647a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int modifiers = m.getAccessFlags(); 2648a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Modifier.isConstructor(modifiers)) { 2649a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski continue; 2650a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2651a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski String methodName = ArtMethod.getMethodName(m); 2652a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!name.equals(methodName)) { 2653a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski continue; 2654a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2655a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (!ArtMethod.equalMethodParameters(m, args)) { 2656a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski continue; 2657a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2658a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if ((modifiers & skipModifiers) == 0) { 2659a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return new Method(m); 2660a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2661a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Direct methods cannot be miranda methods, 2662a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // so this potential result must be synthetic. 2663a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski artMethodResult = m; 2664a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2665a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2666a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2667a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (artMethodResult == null) { 2668a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return null; 2669a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2670a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return new Method(artMethodResult); 2671a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2672a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2673a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2674a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The class def of this class in its own Dex, or -1 if there is no class def. 2675a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2676a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 2677a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2678a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int getDexClassDefIndex() { 2679a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return (dexClassDefIndex == 65535) ? -1 : dexClassDefIndex; 2680a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2681a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2682a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static class Caches { 2683a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2684a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Cache to avoid frequent recalculation of generic interfaces, which is generally uncommon. 2685a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Sized sufficient to allow ConcurrentHashMapTest to run without recalculating its generic 2686a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * interfaces (required to avoid time outs). Validated by running reflection heavy code 2687a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * such as applications using Guice-like frameworks. 2688a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2689a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static final BasicLruCache<Class, Type[]> genericInterfaces 2690a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski = new BasicLruCache<Class, Type[]>(8); 2691a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2692a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2693a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** defining class loader, or NULL for the "bootstrap" system loader. */ 2694a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient ClassLoader classLoader; 2695a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2696a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2697a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * For array classes, the component class object for instanceof/checkcast (for String[][][], 2698a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * this will be String[][]). NULL for non-array classes. 2699a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2700a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient Class<?> componentType; 2701a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2702a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2703a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * DexCache of resolved constant pool entries. Will be null for certain VM-generated classes 2704a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * e.g. arrays and primitive classes. 2705a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2706a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient DexCache dexCache; 2707a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2708de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski /** Short-cut to dexCache.strings */ 2709de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski private transient String[] dexCacheStrings; 2710de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski 2711a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** The superclass, or NULL if this is java.lang.Object, an interface or primitive type. */ 2712a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient Class<? super T> superClass; 2713a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2714a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** static, private, and <init> methods. */ 2715a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient ArtMethod[] directMethods; 2716a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2717a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** Virtual methods defined in this class; invoked through vtable. */ 2718a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient ArtMethod[] virtualMethods; 2719a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2720a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2721a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Instance fields. These describe the layout of the contents of an Object. Note that only the 2722a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * fields directly declared by this class are listed in iFields; fields declared by a 2723a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * superclass are listed in the superclass's Class.iFields. 2724a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2725a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * All instance fields that refer to objects are guaranteed to be at the beginning of the field 2726a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * list. {@link Class#numReferenceInstanceFields} specifies the number of reference fields. 2727a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2728a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient ArtField[] iFields; 2729a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2730a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2731a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The interface table (iftable_) contains pairs of a interface class and an array of the 2732a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * interface methods. There is one pair per interface supported by this class. That 2733a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * means one pair for each interface we support directly, indirectly via superclass, or 2734a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * indirectly via a superinterface. This will be null if neither we nor our superclass 2735a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * implement any interfaces. 2736a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2737a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()". 2738a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Invoke faceObj.blah(), where "blah" is part of the Face interface. We can't easily use a 2739a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * single vtable. 2740a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2741a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * For every interface a concrete class implements, we create an array of the concrete vtable_ 2742a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * methods for the methods in the interface. 2743a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2744a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient Object[] ifTable; 2745a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2746a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2747a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass 2748a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * is copied in, and virtual methods from our class either replace those from the super or are 2749a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * appended. For abstract classes, methods may be created in the vtable that aren't in 2750a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * virtual_ methods_ for miranda methods. 2751a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2752a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient ArtMethod[] vtable; 2753a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2754a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** Static fields */ 2755a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient ArtField[] sFields; 2756a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2757a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** access flags; low 16 bits are defined by VM spec */ 2758a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient int accessFlags; 2759a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2760a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2761a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Class def index from dex file. An index of 65535 indicates that there is no class definition, 2762a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * for example for an array type. 2763a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * TODO: really 16bits as type indices are 16bit. 2764a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2765a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient int dexClassDefIndex; 2766a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2767a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2768a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Class type index from dex file, lazily computed. An index of 65535 indicates that the type 2769a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * index isn't known. Volatile to avoid double-checked locking bugs. 2770a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * TODO: really 16bits as type indices are 16bit. 2771a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2772a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient volatile int dexTypeIndex; 2773a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2774a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** Primitive type value, or 0 if not a primitive type; set for generated primitive classes. */ 2775a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient int primitiveType; 2776a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2777a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2778a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Total size of the Class instance; used when allocating storage on GC heap. 2779a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * See also {@link Class#objectSize}. 2780a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2781a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient int classSize; 2782a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2783a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2784a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Total object size; used when allocating storage on GC heap. For interfaces and abstract 2785a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * classes this will be zero. See also {@link Class#classSize}. 2786a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2787a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient int objectSize; 2788a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2789a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** Number of instance fields that are object references. */ 2790a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient int numReferenceInstanceFields; 2791a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2792a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** Number of static fields that are object references. */ 2793a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient int numReferenceStaticFields; 2794a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2795a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** Bitmap of offsets of iFields. */ 2796a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient int referenceInstanceOffsets; 2797a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2798a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** Bitmap of offsets of sFields. */ 2799a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient int referenceStaticOffsets; 2800a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2801a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** If class verify fails, we must return same error on subsequent tries. */ 2802a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient Class<?> verifyErrorClass; 2803a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2804a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** State of class initialization */ 2805a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient int status; 2806a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2807a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2808a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * tid used to check for recursive static initializer invocation. 280951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2810a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private transient int clinitThreadId; 281151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 2812