1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/src/main/java/java/io/IOException.java Sat Sep 29 06:47:05 2012 +0200
1.3 @@ -0,0 +1,101 @@
1.4 +/*
1.5 + * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.io;
1.30 +
1.31 +/**
1.32 + * Signals that an I/O exception of some sort has occurred. This
1.33 + * class is the general class of exceptions produced by failed or
1.34 + * interrupted I/O operations.
1.35 + *
1.36 + * @author unascribed
1.37 + * @see java.io.InputStream
1.38 + * @see java.io.OutputStream
1.39 + * @since JDK1.0
1.40 + */
1.41 +public
1.42 +class IOException extends Exception {
1.43 + static final long serialVersionUID = 7818375828146090155L;
1.44 +
1.45 + /**
1.46 + * Constructs an {@code IOException} with {@code null}
1.47 + * as its error detail message.
1.48 + */
1.49 + public IOException() {
1.50 + super();
1.51 + }
1.52 +
1.53 + /**
1.54 + * Constructs an {@code IOException} with the specified detail message.
1.55 + *
1.56 + * @param message
1.57 + * The detail message (which is saved for later retrieval
1.58 + * by the {@link #getMessage()} method)
1.59 + */
1.60 + public IOException(String message) {
1.61 + super(message);
1.62 + }
1.63 +
1.64 + /**
1.65 + * Constructs an {@code IOException} with the specified detail message
1.66 + * and cause.
1.67 + *
1.68 + * <p> Note that the detail message associated with {@code cause} is
1.69 + * <i>not</i> automatically incorporated into this exception's detail
1.70 + * message.
1.71 + *
1.72 + * @param message
1.73 + * The detail message (which is saved for later retrieval
1.74 + * by the {@link #getMessage()} method)
1.75 + *
1.76 + * @param cause
1.77 + * The cause (which is saved for later retrieval by the
1.78 + * {@link #getCause()} method). (A null value is permitted,
1.79 + * and indicates that the cause is nonexistent or unknown.)
1.80 + *
1.81 + * @since 1.6
1.82 + */
1.83 + public IOException(String message, Throwable cause) {
1.84 + super(message, cause);
1.85 + }
1.86 +
1.87 + /**
1.88 + * Constructs an {@code IOException} with the specified cause and a
1.89 + * detail message of {@code (cause==null ? null : cause.toString())}
1.90 + * (which typically contains the class and detail message of {@code cause}).
1.91 + * This constructor is useful for IO exceptions that are little more
1.92 + * than wrappers for other throwables.
1.93 + *
1.94 + * @param cause
1.95 + * The cause (which is saved for later retrieval by the
1.96 + * {@link #getCause()} method). (A null value is permitted,
1.97 + * and indicates that the cause is nonexistent or unknown.)
1.98 + *
1.99 + * @since 1.6
1.100 + */
1.101 + public IOException(Throwable cause) {
1.102 + super(cause);
1.103 + }
1.104 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/emul/src/main/java/java/io/ObjectStreamField.java Sat Sep 29 06:47:05 2012 +0200
2.3 @@ -0,0 +1,314 @@
2.4 +/*
2.5 + * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2.7 + *
2.8 + * This code is free software; you can redistribute it and/or modify it
2.9 + * under the terms of the GNU General Public License version 2 only, as
2.10 + * published by the Free Software Foundation. Oracle designates this
2.11 + * particular file as subject to the "Classpath" exception as provided
2.12 + * by Oracle in the LICENSE file that accompanied this code.
2.13 + *
2.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
2.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2.17 + * version 2 for more details (a copy is included in the LICENSE file that
2.18 + * accompanied this code).
2.19 + *
2.20 + * You should have received a copy of the GNU General Public License version
2.21 + * 2 along with this work; if not, write to the Free Software Foundation,
2.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2.23 + *
2.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2.25 + * or visit www.oracle.com if you need additional information or have any
2.26 + * questions.
2.27 + */
2.28 +
2.29 +package java.io;
2.30 +
2.31 +import java.lang.reflect.Field;
2.32 +
2.33 +/**
2.34 + * A description of a Serializable field from a Serializable class. An array
2.35 + * of ObjectStreamFields is used to declare the Serializable fields of a class.
2.36 + *
2.37 + * @author Mike Warres
2.38 + * @author Roger Riggs
2.39 + * @see ObjectStreamClass
2.40 + * @since 1.2
2.41 + */
2.42 +public class ObjectStreamField
2.43 + implements Comparable<Object>
2.44 +{
2.45 +
2.46 + /** field name */
2.47 + private final String name;
2.48 + /** canonical JVM signature of field type */
2.49 + private final String signature;
2.50 + /** field type (Object.class if unknown non-primitive type) */
2.51 + private final Class<?> type;
2.52 + /** whether or not to (de)serialize field values as unshared */
2.53 + private final boolean unshared;
2.54 + /** corresponding reflective field object, if any */
2.55 + private final Field field;
2.56 + /** offset of field value in enclosing field group */
2.57 + private int offset = 0;
2.58 +
2.59 + /**
2.60 + * Create a Serializable field with the specified type. This field should
2.61 + * be documented with a <code>serialField</code> tag.
2.62 + *
2.63 + * @param name the name of the serializable field
2.64 + * @param type the <code>Class</code> object of the serializable field
2.65 + */
2.66 + public ObjectStreamField(String name, Class<?> type) {
2.67 + this(name, type, false);
2.68 + }
2.69 +
2.70 + /**
2.71 + * Creates an ObjectStreamField representing a serializable field with the
2.72 + * given name and type. If unshared is false, values of the represented
2.73 + * field are serialized and deserialized in the default manner--if the
2.74 + * field is non-primitive, object values are serialized and deserialized as
2.75 + * if they had been written and read by calls to writeObject and
2.76 + * readObject. If unshared is true, values of the represented field are
2.77 + * serialized and deserialized as if they had been written and read by
2.78 + * calls to writeUnshared and readUnshared.
2.79 + *
2.80 + * @param name field name
2.81 + * @param type field type
2.82 + * @param unshared if false, write/read field values in the same manner
2.83 + * as writeObject/readObject; if true, write/read in the same
2.84 + * manner as writeUnshared/readUnshared
2.85 + * @since 1.4
2.86 + */
2.87 + public ObjectStreamField(String name, Class<?> type, boolean unshared) {
2.88 + if (name == null) {
2.89 + throw new NullPointerException();
2.90 + }
2.91 + this.name = name;
2.92 + this.type = type;
2.93 + this.unshared = unshared;
2.94 + signature = getClassSignature(type).intern();
2.95 + field = null;
2.96 + }
2.97 +
2.98 + /**
2.99 + * Creates an ObjectStreamField representing a field with the given name,
2.100 + * signature and unshared setting.
2.101 + */
2.102 + ObjectStreamField(String name, String signature, boolean unshared) {
2.103 + if (name == null) {
2.104 + throw new NullPointerException();
2.105 + }
2.106 + this.name = name;
2.107 + this.signature = signature.intern();
2.108 + this.unshared = unshared;
2.109 + field = null;
2.110 +
2.111 + switch (signature.charAt(0)) {
2.112 + case 'Z': type = Boolean.TYPE; break;
2.113 + case 'B': type = Byte.TYPE; break;
2.114 + case 'C': type = Character.TYPE; break;
2.115 + case 'S': type = Short.TYPE; break;
2.116 + case 'I': type = Integer.TYPE; break;
2.117 + case 'J': type = Long.TYPE; break;
2.118 + case 'F': type = Float.TYPE; break;
2.119 + case 'D': type = Double.TYPE; break;
2.120 + case 'L':
2.121 + case '[': type = Object.class; break;
2.122 + default: throw new IllegalArgumentException("illegal signature");
2.123 + }
2.124 + }
2.125 +
2.126 + /**
2.127 + * Creates an ObjectStreamField representing the given field with the
2.128 + * specified unshared setting. For compatibility with the behavior of
2.129 + * earlier serialization implementations, a "showType" parameter is
2.130 + * necessary to govern whether or not a getType() call on this
2.131 + * ObjectStreamField (if non-primitive) will return Object.class (as
2.132 + * opposed to a more specific reference type).
2.133 + */
2.134 + ObjectStreamField(Field field, boolean unshared, boolean showType) {
2.135 + this.field = field;
2.136 + this.unshared = unshared;
2.137 + name = field.getName();
2.138 + Class<?> ftype = field.getType();
2.139 + type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
2.140 + signature = getClassSignature(ftype).intern();
2.141 + }
2.142 +
2.143 + /**
2.144 + * Get the name of this field.
2.145 + *
2.146 + * @return a <code>String</code> representing the name of the serializable
2.147 + * field
2.148 + */
2.149 + public String getName() {
2.150 + return name;
2.151 + }
2.152 +
2.153 + /**
2.154 + * Get the type of the field. If the type is non-primitive and this
2.155 + * <code>ObjectStreamField</code> was obtained from a deserialized {@link
2.156 + * ObjectStreamClass} instance, then <code>Object.class</code> is returned.
2.157 + * Otherwise, the <code>Class</code> object for the type of the field is
2.158 + * returned.
2.159 + *
2.160 + * @return a <code>Class</code> object representing the type of the
2.161 + * serializable field
2.162 + */
2.163 + public Class<?> getType() {
2.164 + return type;
2.165 + }
2.166 +
2.167 + /**
2.168 + * Returns character encoding of field type. The encoding is as follows:
2.169 + * <blockquote><pre>
2.170 + * B byte
2.171 + * C char
2.172 + * D double
2.173 + * F float
2.174 + * I int
2.175 + * J long
2.176 + * L class or interface
2.177 + * S short
2.178 + * Z boolean
2.179 + * [ array
2.180 + * </pre></blockquote>
2.181 + *
2.182 + * @return the typecode of the serializable field
2.183 + */
2.184 + // REMIND: deprecate?
2.185 + public char getTypeCode() {
2.186 + return signature.charAt(0);
2.187 + }
2.188 +
2.189 + /**
2.190 + * Return the JVM type signature.
2.191 + *
2.192 + * @return null if this field has a primitive type.
2.193 + */
2.194 + // REMIND: deprecate?
2.195 + public String getTypeString() {
2.196 + return isPrimitive() ? null : signature;
2.197 + }
2.198 +
2.199 + /**
2.200 + * Offset of field within instance data.
2.201 + *
2.202 + * @return the offset of this field
2.203 + * @see #setOffset
2.204 + */
2.205 + // REMIND: deprecate?
2.206 + public int getOffset() {
2.207 + return offset;
2.208 + }
2.209 +
2.210 + /**
2.211 + * Offset within instance data.
2.212 + *
2.213 + * @param offset the offset of the field
2.214 + * @see #getOffset
2.215 + */
2.216 + // REMIND: deprecate?
2.217 + protected void setOffset(int offset) {
2.218 + this.offset = offset;
2.219 + }
2.220 +
2.221 + /**
2.222 + * Return true if this field has a primitive type.
2.223 + *
2.224 + * @return true if and only if this field corresponds to a primitive type
2.225 + */
2.226 + // REMIND: deprecate?
2.227 + public boolean isPrimitive() {
2.228 + char tcode = signature.charAt(0);
2.229 + return ((tcode != 'L') && (tcode != '['));
2.230 + }
2.231 +
2.232 + /**
2.233 + * Returns boolean value indicating whether or not the serializable field
2.234 + * represented by this ObjectStreamField instance is unshared.
2.235 + *
2.236 + * @since 1.4
2.237 + */
2.238 + public boolean isUnshared() {
2.239 + return unshared;
2.240 + }
2.241 +
2.242 + /**
2.243 + * Compare this field with another <code>ObjectStreamField</code>. Return
2.244 + * -1 if this is smaller, 0 if equal, 1 if greater. Types that are
2.245 + * primitives are "smaller" than object types. If equal, the field names
2.246 + * are compared.
2.247 + */
2.248 + // REMIND: deprecate?
2.249 + public int compareTo(Object obj) {
2.250 + ObjectStreamField other = (ObjectStreamField) obj;
2.251 + boolean isPrim = isPrimitive();
2.252 + if (isPrim != other.isPrimitive()) {
2.253 + return isPrim ? -1 : 1;
2.254 + }
2.255 + return name.compareTo(other.name);
2.256 + }
2.257 +
2.258 + /**
2.259 + * Return a string that describes this field.
2.260 + */
2.261 + public String toString() {
2.262 + return signature + ' ' + name;
2.263 + }
2.264 +
2.265 + /**
2.266 + * Returns field represented by this ObjectStreamField, or null if
2.267 + * ObjectStreamField is not associated with an actual field.
2.268 + */
2.269 + Field getField() {
2.270 + return field;
2.271 + }
2.272 +
2.273 + /**
2.274 + * Returns JVM type signature of field (similar to getTypeString, except
2.275 + * that signature strings are returned for primitive fields as well).
2.276 + */
2.277 + String getSignature() {
2.278 + return signature;
2.279 + }
2.280 +
2.281 + /**
2.282 + * Returns JVM type signature for given class.
2.283 + */
2.284 + private static String getClassSignature(Class<?> cl) {
2.285 + StringBuilder sbuf = new StringBuilder();
2.286 + while (cl.isArray()) {
2.287 + sbuf.append('[');
2.288 + cl = cl.getComponentType();
2.289 + }
2.290 + if (cl.isPrimitive()) {
2.291 + if (cl == Integer.TYPE) {
2.292 + sbuf.append('I');
2.293 + } else if (cl == Byte.TYPE) {
2.294 + sbuf.append('B');
2.295 + } else if (cl == Long.TYPE) {
2.296 + sbuf.append('J');
2.297 + } else if (cl == Float.TYPE) {
2.298 + sbuf.append('F');
2.299 + } else if (cl == Double.TYPE) {
2.300 + sbuf.append('D');
2.301 + } else if (cl == Short.TYPE) {
2.302 + sbuf.append('S');
2.303 + } else if (cl == Character.TYPE) {
2.304 + sbuf.append('C');
2.305 + } else if (cl == Boolean.TYPE) {
2.306 + sbuf.append('Z');
2.307 + } else if (cl == Void.TYPE) {
2.308 + sbuf.append('V');
2.309 + } else {
2.310 + throw new InternalError();
2.311 + }
2.312 + } else {
2.313 + sbuf.append('L' + cl.getName().replace('.', '/') + ';');
2.314 + }
2.315 + return sbuf.toString();
2.316 + }
2.317 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/emul/src/main/java/java/io/Serializable.java Sat Sep 29 06:47:05 2012 +0200
3.3 @@ -0,0 +1,170 @@
3.4 +/*
3.5 + * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3.7 + *
3.8 + * This code is free software; you can redistribute it and/or modify it
3.9 + * under the terms of the GNU General Public License version 2 only, as
3.10 + * published by the Free Software Foundation. Oracle designates this
3.11 + * particular file as subject to the "Classpath" exception as provided
3.12 + * by Oracle in the LICENSE file that accompanied this code.
3.13 + *
3.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
3.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3.17 + * version 2 for more details (a copy is included in the LICENSE file that
3.18 + * accompanied this code).
3.19 + *
3.20 + * You should have received a copy of the GNU General Public License version
3.21 + * 2 along with this work; if not, write to the Free Software Foundation,
3.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3.23 + *
3.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3.25 + * or visit www.oracle.com if you need additional information or have any
3.26 + * questions.
3.27 + */
3.28 +
3.29 +package java.io;
3.30 +
3.31 +/**
3.32 + * Serializability of a class is enabled by the class implementing the
3.33 + * java.io.Serializable interface. Classes that do not implement this
3.34 + * interface will not have any of their state serialized or
3.35 + * deserialized. All subtypes of a serializable class are themselves
3.36 + * serializable. The serialization interface has no methods or fields
3.37 + * and serves only to identify the semantics of being serializable. <p>
3.38 + *
3.39 + * To allow subtypes of non-serializable classes to be serialized, the
3.40 + * subtype may assume responsibility for saving and restoring the
3.41 + * state of the supertype's public, protected, and (if accessible)
3.42 + * package fields. The subtype may assume this responsibility only if
3.43 + * the class it extends has an accessible no-arg constructor to
3.44 + * initialize the class's state. It is an error to declare a class
3.45 + * Serializable if this is not the case. The error will be detected at
3.46 + * runtime. <p>
3.47 + *
3.48 + * During deserialization, the fields of non-serializable classes will
3.49 + * be initialized using the public or protected no-arg constructor of
3.50 + * the class. A no-arg constructor must be accessible to the subclass
3.51 + * that is serializable. The fields of serializable subclasses will
3.52 + * be restored from the stream. <p>
3.53 + *
3.54 + * When traversing a graph, an object may be encountered that does not
3.55 + * support the Serializable interface. In this case the
3.56 + * NotSerializableException will be thrown and will identify the class
3.57 + * of the non-serializable object. <p>
3.58 + *
3.59 + * Classes that require special handling during the serialization and
3.60 + * deserialization process must implement special methods with these exact
3.61 + * signatures: <p>
3.62 + *
3.63 + * <PRE>
3.64 + * private void writeObject(java.io.ObjectOutputStream out)
3.65 + * throws IOException
3.66 + * private void readObject(java.io.ObjectInputStream in)
3.67 + * throws IOException, ClassNotFoundException;
3.68 + * private void readObjectNoData()
3.69 + * throws ObjectStreamException;
3.70 + * </PRE>
3.71 + *
3.72 + * <p>The writeObject method is responsible for writing the state of the
3.73 + * object for its particular class so that the corresponding
3.74 + * readObject method can restore it. The default mechanism for saving
3.75 + * the Object's fields can be invoked by calling
3.76 + * out.defaultWriteObject. The method does not need to concern
3.77 + * itself with the state belonging to its superclasses or subclasses.
3.78 + * State is saved by writing the individual fields to the
3.79 + * ObjectOutputStream using the writeObject method or by using the
3.80 + * methods for primitive data types supported by DataOutput.
3.81 + *
3.82 + * <p>The readObject method is responsible for reading from the stream and
3.83 + * restoring the classes fields. It may call in.defaultReadObject to invoke
3.84 + * the default mechanism for restoring the object's non-static and
3.85 + * non-transient fields. The defaultReadObject method uses information in
3.86 + * the stream to assign the fields of the object saved in the stream with the
3.87 + * correspondingly named fields in the current object. This handles the case
3.88 + * when the class has evolved to add new fields. The method does not need to
3.89 + * concern itself with the state belonging to its superclasses or subclasses.
3.90 + * State is saved by writing the individual fields to the
3.91 + * ObjectOutputStream using the writeObject method or by using the
3.92 + * methods for primitive data types supported by DataOutput.
3.93 + *
3.94 + * <p>The readObjectNoData method is responsible for initializing the state of
3.95 + * the object for its particular class in the event that the serialization
3.96 + * stream does not list the given class as a superclass of the object being
3.97 + * deserialized. This may occur in cases where the receiving party uses a
3.98 + * different version of the deserialized instance's class than the sending
3.99 + * party, and the receiver's version extends classes that are not extended by
3.100 + * the sender's version. This may also occur if the serialization stream has
3.101 + * been tampered; hence, readObjectNoData is useful for initializing
3.102 + * deserialized objects properly despite a "hostile" or incomplete source
3.103 + * stream.
3.104 + *
3.105 + * <p>Serializable classes that need to designate an alternative object to be
3.106 + * used when writing an object to the stream should implement this
3.107 + * special method with the exact signature: <p>
3.108 + *
3.109 + * <PRE>
3.110 + * ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
3.111 + * </PRE><p>
3.112 + *
3.113 + * This writeReplace method is invoked by serialization if the method
3.114 + * exists and it would be accessible from a method defined within the
3.115 + * class of the object being serialized. Thus, the method can have private,
3.116 + * protected and package-private access. Subclass access to this method
3.117 + * follows java accessibility rules. <p>
3.118 + *
3.119 + * Classes that need to designate a replacement when an instance of it
3.120 + * is read from the stream should implement this special method with the
3.121 + * exact signature.<p>
3.122 + *
3.123 + * <PRE>
3.124 + * ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
3.125 + * </PRE><p>
3.126 + *
3.127 + * This readResolve method follows the same invocation rules and
3.128 + * accessibility rules as writeReplace.<p>
3.129 + *
3.130 + * The serialization runtime associates with each serializable class a version
3.131 + * number, called a serialVersionUID, which is used during deserialization to
3.132 + * verify that the sender and receiver of a serialized object have loaded
3.133 + * classes for that object that are compatible with respect to serialization.
3.134 + * If the receiver has loaded a class for the object that has a different
3.135 + * serialVersionUID than that of the corresponding sender's class, then
3.136 + * deserialization will result in an {@link InvalidClassException}. A
3.137 + * serializable class can declare its own serialVersionUID explicitly by
3.138 + * declaring a field named <code>"serialVersionUID"</code> that must be static,
3.139 + * final, and of type <code>long</code>:<p>
3.140 + *
3.141 + * <PRE>
3.142 + * ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
3.143 + * </PRE>
3.144 + *
3.145 + * If a serializable class does not explicitly declare a serialVersionUID, then
3.146 + * the serialization runtime will calculate a default serialVersionUID value
3.147 + * for that class based on various aspects of the class, as described in the
3.148 + * Java(TM) Object Serialization Specification. However, it is <em>strongly
3.149 + * recommended</em> that all serializable classes explicitly declare
3.150 + * serialVersionUID values, since the default serialVersionUID computation is
3.151 + * highly sensitive to class details that may vary depending on compiler
3.152 + * implementations, and can thus result in unexpected
3.153 + * <code>InvalidClassException</code>s during deserialization. Therefore, to
3.154 + * guarantee a consistent serialVersionUID value across different java compiler
3.155 + * implementations, a serializable class must declare an explicit
3.156 + * serialVersionUID value. It is also strongly advised that explicit
3.157 + * serialVersionUID declarations use the <code>private</code> modifier where
3.158 + * possible, since such declarations apply only to the immediately declaring
3.159 + * class--serialVersionUID fields are not useful as inherited members. Array
3.160 + * classes cannot declare an explicit serialVersionUID, so they always have
3.161 + * the default computed value, but the requirement for matching
3.162 + * serialVersionUID values is waived for array classes.
3.163 + *
3.164 + * @author unascribed
3.165 + * @see java.io.ObjectOutputStream
3.166 + * @see java.io.ObjectInputStream
3.167 + * @see java.io.ObjectOutput
3.168 + * @see java.io.ObjectInput
3.169 + * @see java.io.Externalizable
3.170 + * @since JDK1.1
3.171 + */
3.172 +public interface Serializable {
3.173 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/emul/src/main/java/java/io/UnsupportedEncodingException.java Sat Sep 29 06:47:05 2012 +0200
4.3 @@ -0,0 +1,52 @@
4.4 +/*
4.5 + * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4.7 + *
4.8 + * This code is free software; you can redistribute it and/or modify it
4.9 + * under the terms of the GNU General Public License version 2 only, as
4.10 + * published by the Free Software Foundation. Oracle designates this
4.11 + * particular file as subject to the "Classpath" exception as provided
4.12 + * by Oracle in the LICENSE file that accompanied this code.
4.13 + *
4.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
4.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4.17 + * version 2 for more details (a copy is included in the LICENSE file that
4.18 + * accompanied this code).
4.19 + *
4.20 + * You should have received a copy of the GNU General Public License version
4.21 + * 2 along with this work; if not, write to the Free Software Foundation,
4.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4.23 + *
4.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4.25 + * or visit www.oracle.com if you need additional information or have any
4.26 + * questions.
4.27 + */
4.28 +package java.io;
4.29 +
4.30 +/**
4.31 + * The Character Encoding is not supported.
4.32 + *
4.33 + * @author Asmus Freytag
4.34 + * @since JDK1.1
4.35 + */
4.36 +public class UnsupportedEncodingException
4.37 + extends IOException
4.38 +{
4.39 + private static final long serialVersionUID = -4274276298326136670L;
4.40 +
4.41 + /**
4.42 + * Constructs an UnsupportedEncodingException without a detail message.
4.43 + */
4.44 + public UnsupportedEncodingException() {
4.45 + super();
4.46 + }
4.47 +
4.48 + /**
4.49 + * Constructs an UnsupportedEncodingException with a detail message.
4.50 + * @param s Describes the reason for the exception.
4.51 + */
4.52 + public UnsupportedEncodingException(String s) {
4.53 + super(s);
4.54 + }
4.55 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/emul/src/main/java/java/lang/AbstractStringBuilder.java Sat Sep 29 06:47:05 2012 +0200
5.3 @@ -0,0 +1,1407 @@
5.4 +/*
5.5 + * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5.7 + *
5.8 + * This code is free software; you can redistribute it and/or modify it
5.9 + * under the terms of the GNU General Public License version 2 only, as
5.10 + * published by the Free Software Foundation. Oracle designates this
5.11 + * particular file as subject to the "Classpath" exception as provided
5.12 + * by Oracle in the LICENSE file that accompanied this code.
5.13 + *
5.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
5.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5.17 + * version 2 for more details (a copy is included in the LICENSE file that
5.18 + * accompanied this code).
5.19 + *
5.20 + * You should have received a copy of the GNU General Public License version
5.21 + * 2 along with this work; if not, write to the Free Software Foundation,
5.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5.23 + *
5.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5.25 + * or visit www.oracle.com if you need additional information or have any
5.26 + * questions.
5.27 + */
5.28 +
5.29 +package java.lang;
5.30 +
5.31 +import sun.misc.FloatingDecimal;
5.32 +import java.util.Arrays;
5.33 +
5.34 +/**
5.35 + * A mutable sequence of characters.
5.36 + * <p>
5.37 + * Implements a modifiable string. At any point in time it contains some
5.38 + * particular sequence of characters, but the length and content of the
5.39 + * sequence can be changed through certain method calls.
5.40 + *
5.41 + * @author Michael McCloskey
5.42 + * @author Martin Buchholz
5.43 + * @author Ulf Zibis
5.44 + * @since 1.5
5.45 + */
5.46 +abstract class AbstractStringBuilder implements Appendable, CharSequence {
5.47 + /**
5.48 + * The value is used for character storage.
5.49 + */
5.50 + char[] value;
5.51 +
5.52 + /**
5.53 + * The count is the number of characters used.
5.54 + */
5.55 + int count;
5.56 +
5.57 + /**
5.58 + * This no-arg constructor is necessary for serialization of subclasses.
5.59 + */
5.60 + AbstractStringBuilder() {
5.61 + }
5.62 +
5.63 + /**
5.64 + * Creates an AbstractStringBuilder of the specified capacity.
5.65 + */
5.66 + AbstractStringBuilder(int capacity) {
5.67 + value = new char[capacity];
5.68 + }
5.69 +
5.70 + /**
5.71 + * Returns the length (character count).
5.72 + *
5.73 + * @return the length of the sequence of characters currently
5.74 + * represented by this object
5.75 + */
5.76 + public int length() {
5.77 + return count;
5.78 + }
5.79 +
5.80 + /**
5.81 + * Returns the current capacity. The capacity is the amount of storage
5.82 + * available for newly inserted characters, beyond which an allocation
5.83 + * will occur.
5.84 + *
5.85 + * @return the current capacity
5.86 + */
5.87 + public int capacity() {
5.88 + return value.length;
5.89 + }
5.90 +
5.91 + /**
5.92 + * Ensures that the capacity is at least equal to the specified minimum.
5.93 + * If the current capacity is less than the argument, then a new internal
5.94 + * array is allocated with greater capacity. The new capacity is the
5.95 + * larger of:
5.96 + * <ul>
5.97 + * <li>The <code>minimumCapacity</code> argument.
5.98 + * <li>Twice the old capacity, plus <code>2</code>.
5.99 + * </ul>
5.100 + * If the <code>minimumCapacity</code> argument is nonpositive, this
5.101 + * method takes no action and simply returns.
5.102 + *
5.103 + * @param minimumCapacity the minimum desired capacity.
5.104 + */
5.105 + public void ensureCapacity(int minimumCapacity) {
5.106 + if (minimumCapacity > 0)
5.107 + ensureCapacityInternal(minimumCapacity);
5.108 + }
5.109 +
5.110 + /**
5.111 + * This method has the same contract as ensureCapacity, but is
5.112 + * never synchronized.
5.113 + */
5.114 + private void ensureCapacityInternal(int minimumCapacity) {
5.115 + // overflow-conscious code
5.116 + if (minimumCapacity - value.length > 0)
5.117 + expandCapacity(minimumCapacity);
5.118 + }
5.119 +
5.120 + /**
5.121 + * This implements the expansion semantics of ensureCapacity with no
5.122 + * size check or synchronization.
5.123 + */
5.124 + void expandCapacity(int minimumCapacity) {
5.125 + int newCapacity = value.length * 2 + 2;
5.126 + if (newCapacity - minimumCapacity < 0)
5.127 + newCapacity = minimumCapacity;
5.128 + if (newCapacity < 0) {
5.129 + if (minimumCapacity < 0) // overflow
5.130 + throw new OutOfMemoryError();
5.131 + newCapacity = Integer.MAX_VALUE;
5.132 + }
5.133 + value = Arrays.copyOf(value, newCapacity);
5.134 + }
5.135 +
5.136 + /**
5.137 + * Attempts to reduce storage used for the character sequence.
5.138 + * If the buffer is larger than necessary to hold its current sequence of
5.139 + * characters, then it may be resized to become more space efficient.
5.140 + * Calling this method may, but is not required to, affect the value
5.141 + * returned by a subsequent call to the {@link #capacity()} method.
5.142 + */
5.143 + public void trimToSize() {
5.144 + if (count < value.length) {
5.145 + value = Arrays.copyOf(value, count);
5.146 + }
5.147 + }
5.148 +
5.149 + /**
5.150 + * Sets the length of the character sequence.
5.151 + * The sequence is changed to a new character sequence
5.152 + * whose length is specified by the argument. For every nonnegative
5.153 + * index <i>k</i> less than <code>newLength</code>, the character at
5.154 + * index <i>k</i> in the new character sequence is the same as the
5.155 + * character at index <i>k</i> in the old sequence if <i>k</i> is less
5.156 + * than the length of the old character sequence; otherwise, it is the
5.157 + * null character <code>'\u0000'</code>.
5.158 + *
5.159 + * In other words, if the <code>newLength</code> argument is less than
5.160 + * the current length, the length is changed to the specified length.
5.161 + * <p>
5.162 + * If the <code>newLength</code> argument is greater than or equal
5.163 + * to the current length, sufficient null characters
5.164 + * (<code>'\u0000'</code>) are appended so that
5.165 + * length becomes the <code>newLength</code> argument.
5.166 + * <p>
5.167 + * The <code>newLength</code> argument must be greater than or equal
5.168 + * to <code>0</code>.
5.169 + *
5.170 + * @param newLength the new length
5.171 + * @throws IndexOutOfBoundsException if the
5.172 + * <code>newLength</code> argument is negative.
5.173 + */
5.174 + public void setLength(int newLength) {
5.175 + if (newLength < 0)
5.176 + throw new StringIndexOutOfBoundsException(newLength);
5.177 + ensureCapacityInternal(newLength);
5.178 +
5.179 + if (count < newLength) {
5.180 + for (; count < newLength; count++)
5.181 + value[count] = '\0';
5.182 + } else {
5.183 + count = newLength;
5.184 + }
5.185 + }
5.186 +
5.187 + /**
5.188 + * Returns the <code>char</code> value in this sequence at the specified index.
5.189 + * The first <code>char</code> value is at index <code>0</code>, the next at index
5.190 + * <code>1</code>, and so on, as in array indexing.
5.191 + * <p>
5.192 + * The index argument must be greater than or equal to
5.193 + * <code>0</code>, and less than the length of this sequence.
5.194 + *
5.195 + * <p>If the <code>char</code> value specified by the index is a
5.196 + * <a href="Character.html#unicode">surrogate</a>, the surrogate
5.197 + * value is returned.
5.198 + *
5.199 + * @param index the index of the desired <code>char</code> value.
5.200 + * @return the <code>char</code> value at the specified index.
5.201 + * @throws IndexOutOfBoundsException if <code>index</code> is
5.202 + * negative or greater than or equal to <code>length()</code>.
5.203 + */
5.204 + public char charAt(int index) {
5.205 + if ((index < 0) || (index >= count))
5.206 + throw new StringIndexOutOfBoundsException(index);
5.207 + return value[index];
5.208 + }
5.209 +
5.210 + /**
5.211 + * Returns the character (Unicode code point) at the specified
5.212 + * index. The index refers to <code>char</code> values
5.213 + * (Unicode code units) and ranges from <code>0</code> to
5.214 + * {@link #length()}<code> - 1</code>.
5.215 + *
5.216 + * <p> If the <code>char</code> value specified at the given index
5.217 + * is in the high-surrogate range, the following index is less
5.218 + * than the length of this sequence, and the
5.219 + * <code>char</code> value at the following index is in the
5.220 + * low-surrogate range, then the supplementary code point
5.221 + * corresponding to this surrogate pair is returned. Otherwise,
5.222 + * the <code>char</code> value at the given index is returned.
5.223 + *
5.224 + * @param index the index to the <code>char</code> values
5.225 + * @return the code point value of the character at the
5.226 + * <code>index</code>
5.227 + * @exception IndexOutOfBoundsException if the <code>index</code>
5.228 + * argument is negative or not less than the length of this
5.229 + * sequence.
5.230 + */
5.231 + public int codePointAt(int index) {
5.232 + if ((index < 0) || (index >= count)) {
5.233 + throw new StringIndexOutOfBoundsException(index);
5.234 + }
5.235 + return Character.codePointAt(value, index);
5.236 + }
5.237 +
5.238 + /**
5.239 + * Returns the character (Unicode code point) before the specified
5.240 + * index. The index refers to <code>char</code> values
5.241 + * (Unicode code units) and ranges from <code>1</code> to {@link
5.242 + * #length()}.
5.243 + *
5.244 + * <p> If the <code>char</code> value at <code>(index - 1)</code>
5.245 + * is in the low-surrogate range, <code>(index - 2)</code> is not
5.246 + * negative, and the <code>char</code> value at <code>(index -
5.247 + * 2)</code> is in the high-surrogate range, then the
5.248 + * supplementary code point value of the surrogate pair is
5.249 + * returned. If the <code>char</code> value at <code>index -
5.250 + * 1</code> is an unpaired low-surrogate or a high-surrogate, the
5.251 + * surrogate value is returned.
5.252 + *
5.253 + * @param index the index following the code point that should be returned
5.254 + * @return the Unicode code point value before the given index.
5.255 + * @exception IndexOutOfBoundsException if the <code>index</code>
5.256 + * argument is less than 1 or greater than the length
5.257 + * of this sequence.
5.258 + */
5.259 + public int codePointBefore(int index) {
5.260 + int i = index - 1;
5.261 + if ((i < 0) || (i >= count)) {
5.262 + throw new StringIndexOutOfBoundsException(index);
5.263 + }
5.264 + return Character.codePointBefore(value, index);
5.265 + }
5.266 +
5.267 + /**
5.268 + * Returns the number of Unicode code points in the specified text
5.269 + * range of this sequence. The text range begins at the specified
5.270 + * <code>beginIndex</code> and extends to the <code>char</code> at
5.271 + * index <code>endIndex - 1</code>. Thus the length (in
5.272 + * <code>char</code>s) of the text range is
5.273 + * <code>endIndex-beginIndex</code>. Unpaired surrogates within
5.274 + * this sequence count as one code point each.
5.275 + *
5.276 + * @param beginIndex the index to the first <code>char</code> of
5.277 + * the text range.
5.278 + * @param endIndex the index after the last <code>char</code> of
5.279 + * the text range.
5.280 + * @return the number of Unicode code points in the specified text
5.281 + * range
5.282 + * @exception IndexOutOfBoundsException if the
5.283 + * <code>beginIndex</code> is negative, or <code>endIndex</code>
5.284 + * is larger than the length of this sequence, or
5.285 + * <code>beginIndex</code> is larger than <code>endIndex</code>.
5.286 + */
5.287 + public int codePointCount(int beginIndex, int endIndex) {
5.288 + if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
5.289 + throw new IndexOutOfBoundsException();
5.290 + }
5.291 + return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);
5.292 + }
5.293 +
5.294 + /**
5.295 + * Returns the index within this sequence that is offset from the
5.296 + * given <code>index</code> by <code>codePointOffset</code> code
5.297 + * points. Unpaired surrogates within the text range given by
5.298 + * <code>index</code> and <code>codePointOffset</code> count as
5.299 + * one code point each.
5.300 + *
5.301 + * @param index the index to be offset
5.302 + * @param codePointOffset the offset in code points
5.303 + * @return the index within this sequence
5.304 + * @exception IndexOutOfBoundsException if <code>index</code>
5.305 + * is negative or larger then the length of this sequence,
5.306 + * or if <code>codePointOffset</code> is positive and the subsequence
5.307 + * starting with <code>index</code> has fewer than
5.308 + * <code>codePointOffset</code> code points,
5.309 + * or if <code>codePointOffset</code> is negative and the subsequence
5.310 + * before <code>index</code> has fewer than the absolute value of
5.311 + * <code>codePointOffset</code> code points.
5.312 + */
5.313 + public int offsetByCodePoints(int index, int codePointOffset) {
5.314 + if (index < 0 || index > count) {
5.315 + throw new IndexOutOfBoundsException();
5.316 + }
5.317 + return Character.offsetByCodePointsImpl(value, 0, count,
5.318 + index, codePointOffset);
5.319 + }
5.320 +
5.321 + /**
5.322 + * Characters are copied from this sequence into the
5.323 + * destination character array <code>dst</code>. The first character to
5.324 + * be copied is at index <code>srcBegin</code>; the last character to
5.325 + * be copied is at index <code>srcEnd-1</code>. The total number of
5.326 + * characters to be copied is <code>srcEnd-srcBegin</code>. The
5.327 + * characters are copied into the subarray of <code>dst</code> starting
5.328 + * at index <code>dstBegin</code> and ending at index:
5.329 + * <p><blockquote><pre>
5.330 + * dstbegin + (srcEnd-srcBegin) - 1
5.331 + * </pre></blockquote>
5.332 + *
5.333 + * @param srcBegin start copying at this offset.
5.334 + * @param srcEnd stop copying at this offset.
5.335 + * @param dst the array to copy the data into.
5.336 + * @param dstBegin offset into <code>dst</code>.
5.337 + * @throws NullPointerException if <code>dst</code> is
5.338 + * <code>null</code>.
5.339 + * @throws IndexOutOfBoundsException if any of the following is true:
5.340 + * <ul>
5.341 + * <li><code>srcBegin</code> is negative
5.342 + * <li><code>dstBegin</code> is negative
5.343 + * <li>the <code>srcBegin</code> argument is greater than
5.344 + * the <code>srcEnd</code> argument.
5.345 + * <li><code>srcEnd</code> is greater than
5.346 + * <code>this.length()</code>.
5.347 + * <li><code>dstBegin+srcEnd-srcBegin</code> is greater than
5.348 + * <code>dst.length</code>
5.349 + * </ul>
5.350 + */
5.351 + public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
5.352 + {
5.353 + if (srcBegin < 0)
5.354 + throw new StringIndexOutOfBoundsException(srcBegin);
5.355 + if ((srcEnd < 0) || (srcEnd > count))
5.356 + throw new StringIndexOutOfBoundsException(srcEnd);
5.357 + if (srcBegin > srcEnd)
5.358 + throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
5.359 + System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
5.360 + }
5.361 +
5.362 + /**
5.363 + * The character at the specified index is set to <code>ch</code>. This
5.364 + * sequence is altered to represent a new character sequence that is
5.365 + * identical to the old character sequence, except that it contains the
5.366 + * character <code>ch</code> at position <code>index</code>.
5.367 + * <p>
5.368 + * The index argument must be greater than or equal to
5.369 + * <code>0</code>, and less than the length of this sequence.
5.370 + *
5.371 + * @param index the index of the character to modify.
5.372 + * @param ch the new character.
5.373 + * @throws IndexOutOfBoundsException if <code>index</code> is
5.374 + * negative or greater than or equal to <code>length()</code>.
5.375 + */
5.376 + public void setCharAt(int index, char ch) {
5.377 + if ((index < 0) || (index >= count))
5.378 + throw new StringIndexOutOfBoundsException(index);
5.379 + value[index] = ch;
5.380 + }
5.381 +
5.382 + /**
5.383 + * Appends the string representation of the {@code Object} argument.
5.384 + * <p>
5.385 + * The overall effect is exactly as if the argument were converted
5.386 + * to a string by the method {@link String#valueOf(Object)},
5.387 + * and the characters of that string were then
5.388 + * {@link #append(String) appended} to this character sequence.
5.389 + *
5.390 + * @param obj an {@code Object}.
5.391 + * @return a reference to this object.
5.392 + */
5.393 + public AbstractStringBuilder append(Object obj) {
5.394 + return append(String.valueOf(obj));
5.395 + }
5.396 +
5.397 + /**
5.398 + * Appends the specified string to this character sequence.
5.399 + * <p>
5.400 + * The characters of the {@code String} argument are appended, in
5.401 + * order, increasing the length of this sequence by the length of the
5.402 + * argument. If {@code str} is {@code null}, then the four
5.403 + * characters {@code "null"} are appended.
5.404 + * <p>
5.405 + * Let <i>n</i> be the length of this character sequence just prior to
5.406 + * execution of the {@code append} method. Then the character at
5.407 + * index <i>k</i> in the new character sequence is equal to the character
5.408 + * at index <i>k</i> in the old character sequence, if <i>k</i> is less
5.409 + * than <i>n</i>; otherwise, it is equal to the character at index
5.410 + * <i>k-n</i> in the argument {@code str}.
5.411 + *
5.412 + * @param str a string.
5.413 + * @return a reference to this object.
5.414 + */
5.415 + public AbstractStringBuilder append(String str) {
5.416 + if (str == null) str = "null";
5.417 + int len = str.length();
5.418 + ensureCapacityInternal(count + len);
5.419 + str.getChars(0, len, value, count);
5.420 + count += len;
5.421 + return this;
5.422 + }
5.423 +
5.424 + // Documentation in subclasses because of synchro difference
5.425 + public AbstractStringBuilder append(StringBuffer sb) {
5.426 + if (sb == null)
5.427 + return append("null");
5.428 + int len = sb.length();
5.429 + ensureCapacityInternal(count + len);
5.430 + sb.getChars(0, len, value, count);
5.431 + count += len;
5.432 + return this;
5.433 + }
5.434 +
5.435 + // Documentation in subclasses because of synchro difference
5.436 + public AbstractStringBuilder append(CharSequence s) {
5.437 + if (s == null)
5.438 + s = "null";
5.439 + if (s instanceof String)
5.440 + return this.append((String)s);
5.441 + if (s instanceof StringBuffer)
5.442 + return this.append((StringBuffer)s);
5.443 + return this.append(s, 0, s.length());
5.444 + }
5.445 +
5.446 + /**
5.447 + * Appends a subsequence of the specified {@code CharSequence} to this
5.448 + * sequence.
5.449 + * <p>
5.450 + * Characters of the argument {@code s}, starting at
5.451 + * index {@code start}, are appended, in order, to the contents of
5.452 + * this sequence up to the (exclusive) index {@code end}. The length
5.453 + * of this sequence is increased by the value of {@code end - start}.
5.454 + * <p>
5.455 + * Let <i>n</i> be the length of this character sequence just prior to
5.456 + * execution of the {@code append} method. Then the character at
5.457 + * index <i>k</i> in this character sequence becomes equal to the
5.458 + * character at index <i>k</i> in this sequence, if <i>k</i> is less than
5.459 + * <i>n</i>; otherwise, it is equal to the character at index
5.460 + * <i>k+start-n</i> in the argument {@code s}.
5.461 + * <p>
5.462 + * If {@code s} is {@code null}, then this method appends
5.463 + * characters as if the s parameter was a sequence containing the four
5.464 + * characters {@code "null"}.
5.465 + *
5.466 + * @param s the sequence to append.
5.467 + * @param start the starting index of the subsequence to be appended.
5.468 + * @param end the end index of the subsequence to be appended.
5.469 + * @return a reference to this object.
5.470 + * @throws IndexOutOfBoundsException if
5.471 + * {@code start} is negative, or
5.472 + * {@code start} is greater than {@code end} or
5.473 + * {@code end} is greater than {@code s.length()}
5.474 + */
5.475 + public AbstractStringBuilder append(CharSequence s, int start, int end) {
5.476 + if (s == null)
5.477 + s = "null";
5.478 + if ((start < 0) || (start > end) || (end > s.length()))
5.479 + throw new IndexOutOfBoundsException(
5.480 + "start " + start + ", end " + end + ", s.length() "
5.481 + + s.length());
5.482 + int len = end - start;
5.483 + ensureCapacityInternal(count + len);
5.484 + for (int i = start, j = count; i < end; i++, j++)
5.485 + value[j] = s.charAt(i);
5.486 + count += len;
5.487 + return this;
5.488 + }
5.489 +
5.490 + /**
5.491 + * Appends the string representation of the {@code char} array
5.492 + * argument to this sequence.
5.493 + * <p>
5.494 + * The characters of the array argument are appended, in order, to
5.495 + * the contents of this sequence. The length of this sequence
5.496 + * increases by the length of the argument.
5.497 + * <p>
5.498 + * The overall effect is exactly as if the argument were converted
5.499 + * to a string by the method {@link String#valueOf(char[])},
5.500 + * and the characters of that string were then
5.501 + * {@link #append(String) appended} to this character sequence.
5.502 + *
5.503 + * @param str the characters to be appended.
5.504 + * @return a reference to this object.
5.505 + */
5.506 + public AbstractStringBuilder append(char[] str) {
5.507 + int len = str.length;
5.508 + ensureCapacityInternal(count + len);
5.509 + System.arraycopy(str, 0, value, count, len);
5.510 + count += len;
5.511 + return this;
5.512 + }
5.513 +
5.514 + /**
5.515 + * Appends the string representation of a subarray of the
5.516 + * {@code char} array argument to this sequence.
5.517 + * <p>
5.518 + * Characters of the {@code char} array {@code str}, starting at
5.519 + * index {@code offset}, are appended, in order, to the contents
5.520 + * of this sequence. The length of this sequence increases
5.521 + * by the value of {@code len}.
5.522 + * <p>
5.523 + * The overall effect is exactly as if the arguments were converted
5.524 + * to a string by the method {@link String#valueOf(char[],int,int)},
5.525 + * and the characters of that string were then
5.526 + * {@link #append(String) appended} to this character sequence.
5.527 + *
5.528 + * @param str the characters to be appended.
5.529 + * @param offset the index of the first {@code char} to append.
5.530 + * @param len the number of {@code char}s to append.
5.531 + * @return a reference to this object.
5.532 + * @throws IndexOutOfBoundsException
5.533 + * if {@code offset < 0} or {@code len < 0}
5.534 + * or {@code offset+len > str.length}
5.535 + */
5.536 + public AbstractStringBuilder append(char str[], int offset, int len) {
5.537 + if (len > 0) // let arraycopy report AIOOBE for len < 0
5.538 + ensureCapacityInternal(count + len);
5.539 + System.arraycopy(str, offset, value, count, len);
5.540 + count += len;
5.541 + return this;
5.542 + }
5.543 +
5.544 + /**
5.545 + * Appends the string representation of the {@code boolean}
5.546 + * argument to the sequence.
5.547 + * <p>
5.548 + * The overall effect is exactly as if the argument were converted
5.549 + * to a string by the method {@link String#valueOf(boolean)},
5.550 + * and the characters of that string were then
5.551 + * {@link #append(String) appended} to this character sequence.
5.552 + *
5.553 + * @param b a {@code boolean}.
5.554 + * @return a reference to this object.
5.555 + */
5.556 + public AbstractStringBuilder append(boolean b) {
5.557 + if (b) {
5.558 + ensureCapacityInternal(count + 4);
5.559 + value[count++] = 't';
5.560 + value[count++] = 'r';
5.561 + value[count++] = 'u';
5.562 + value[count++] = 'e';
5.563 + } else {
5.564 + ensureCapacityInternal(count + 5);
5.565 + value[count++] = 'f';
5.566 + value[count++] = 'a';
5.567 + value[count++] = 'l';
5.568 + value[count++] = 's';
5.569 + value[count++] = 'e';
5.570 + }
5.571 + return this;
5.572 + }
5.573 +
5.574 + /**
5.575 + * Appends the string representation of the {@code char}
5.576 + * argument to this sequence.
5.577 + * <p>
5.578 + * The argument is appended to the contents of this sequence.
5.579 + * The length of this sequence increases by {@code 1}.
5.580 + * <p>
5.581 + * The overall effect is exactly as if the argument were converted
5.582 + * to a string by the method {@link String#valueOf(char)},
5.583 + * and the character in that string were then
5.584 + * {@link #append(String) appended} to this character sequence.
5.585 + *
5.586 + * @param c a {@code char}.
5.587 + * @return a reference to this object.
5.588 + */
5.589 + public AbstractStringBuilder append(char c) {
5.590 + ensureCapacityInternal(count + 1);
5.591 + value[count++] = c;
5.592 + return this;
5.593 + }
5.594 +
5.595 + /**
5.596 + * Appends the string representation of the {@code int}
5.597 + * argument to this sequence.
5.598 + * <p>
5.599 + * The overall effect is exactly as if the argument were converted
5.600 + * to a string by the method {@link String#valueOf(int)},
5.601 + * and the characters of that string were then
5.602 + * {@link #append(String) appended} to this character sequence.
5.603 + *
5.604 + * @param i an {@code int}.
5.605 + * @return a reference to this object.
5.606 + */
5.607 + public AbstractStringBuilder append(int i) {
5.608 + if (i == Integer.MIN_VALUE) {
5.609 + append("-2147483648");
5.610 + return this;
5.611 + }
5.612 + int appendedLength = (i < 0) ? Integer.stringSize(-i) + 1
5.613 + : Integer.stringSize(i);
5.614 + int spaceNeeded = count + appendedLength;
5.615 + ensureCapacityInternal(spaceNeeded);
5.616 + Integer.getChars(i, spaceNeeded, value);
5.617 + count = spaceNeeded;
5.618 + return this;
5.619 + }
5.620 +
5.621 + /**
5.622 + * Appends the string representation of the {@code long}
5.623 + * argument to this sequence.
5.624 + * <p>
5.625 + * The overall effect is exactly as if the argument were converted
5.626 + * to a string by the method {@link String#valueOf(long)},
5.627 + * and the characters of that string were then
5.628 + * {@link #append(String) appended} to this character sequence.
5.629 + *
5.630 + * @param l a {@code long}.
5.631 + * @return a reference to this object.
5.632 + */
5.633 + public AbstractStringBuilder append(long l) {
5.634 + if (l == Long.MIN_VALUE) {
5.635 + append("-9223372036854775808");
5.636 + return this;
5.637 + }
5.638 + int appendedLength = (l < 0) ? Long.stringSize(-l) + 1
5.639 + : Long.stringSize(l);
5.640 + int spaceNeeded = count + appendedLength;
5.641 + ensureCapacityInternal(spaceNeeded);
5.642 + Long.getChars(l, spaceNeeded, value);
5.643 + count = spaceNeeded;
5.644 + return this;
5.645 + }
5.646 +
5.647 + /**
5.648 + * Appends the string representation of the {@code float}
5.649 + * argument to this sequence.
5.650 + * <p>
5.651 + * The overall effect is exactly as if the argument were converted
5.652 + * to a string by the method {@link String#valueOf(float)},
5.653 + * and the characters of that string were then
5.654 + * {@link #append(String) appended} to this character sequence.
5.655 + *
5.656 + * @param f a {@code float}.
5.657 + * @return a reference to this object.
5.658 + */
5.659 + public AbstractStringBuilder append(float f) {
5.660 + new FloatingDecimal(f).appendTo(this);
5.661 + return this;
5.662 + }
5.663 +
5.664 + /**
5.665 + * Appends the string representation of the {@code double}
5.666 + * argument to this sequence.
5.667 + * <p>
5.668 + * The overall effect is exactly as if the argument were converted
5.669 + * to a string by the method {@link String#valueOf(double)},
5.670 + * and the characters of that string were then
5.671 + * {@link #append(String) appended} to this character sequence.
5.672 + *
5.673 + * @param d a {@code double}.
5.674 + * @return a reference to this object.
5.675 + */
5.676 + public AbstractStringBuilder append(double d) {
5.677 + new FloatingDecimal(d).appendTo(this);
5.678 + return this;
5.679 + }
5.680 +
5.681 + /**
5.682 + * Removes the characters in a substring of this sequence.
5.683 + * The substring begins at the specified {@code start} and extends to
5.684 + * the character at index {@code end - 1} or to the end of the
5.685 + * sequence if no such character exists. If
5.686 + * {@code start} is equal to {@code end}, no changes are made.
5.687 + *
5.688 + * @param start The beginning index, inclusive.
5.689 + * @param end The ending index, exclusive.
5.690 + * @return This object.
5.691 + * @throws StringIndexOutOfBoundsException if {@code start}
5.692 + * is negative, greater than {@code length()}, or
5.693 + * greater than {@code end}.
5.694 + */
5.695 + public AbstractStringBuilder delete(int start, int end) {
5.696 + if (start < 0)
5.697 + throw new StringIndexOutOfBoundsException(start);
5.698 + if (end > count)
5.699 + end = count;
5.700 + if (start > end)
5.701 + throw new StringIndexOutOfBoundsException();
5.702 + int len = end - start;
5.703 + if (len > 0) {
5.704 + System.arraycopy(value, start+len, value, start, count-end);
5.705 + count -= len;
5.706 + }
5.707 + return this;
5.708 + }
5.709 +
5.710 + /**
5.711 + * Appends the string representation of the {@code codePoint}
5.712 + * argument to this sequence.
5.713 + *
5.714 + * <p> The argument is appended to the contents of this sequence.
5.715 + * The length of this sequence increases by
5.716 + * {@link Character#charCount(int) Character.charCount(codePoint)}.
5.717 + *
5.718 + * <p> The overall effect is exactly as if the argument were
5.719 + * converted to a {@code char} array by the method
5.720 + * {@link Character#toChars(int)} and the character in that array
5.721 + * were then {@link #append(char[]) appended} to this character
5.722 + * sequence.
5.723 + *
5.724 + * @param codePoint a Unicode code point
5.725 + * @return a reference to this object.
5.726 + * @exception IllegalArgumentException if the specified
5.727 + * {@code codePoint} isn't a valid Unicode code point
5.728 + */
5.729 + public AbstractStringBuilder appendCodePoint(int codePoint) {
5.730 + final int count = this.count;
5.731 +
5.732 + if (Character.isBmpCodePoint(codePoint)) {
5.733 + ensureCapacityInternal(count + 1);
5.734 + value[count] = (char) codePoint;
5.735 + this.count = count + 1;
5.736 + } else if (Character.isValidCodePoint(codePoint)) {
5.737 + ensureCapacityInternal(count + 2);
5.738 + Character.toSurrogates(codePoint, value, count);
5.739 + this.count = count + 2;
5.740 + } else {
5.741 + throw new IllegalArgumentException();
5.742 + }
5.743 + return this;
5.744 + }
5.745 +
5.746 + /**
5.747 + * Removes the <code>char</code> at the specified position in this
5.748 + * sequence. This sequence is shortened by one <code>char</code>.
5.749 + *
5.750 + * <p>Note: If the character at the given index is a supplementary
5.751 + * character, this method does not remove the entire character. If
5.752 + * correct handling of supplementary characters is required,
5.753 + * determine the number of <code>char</code>s to remove by calling
5.754 + * <code>Character.charCount(thisSequence.codePointAt(index))</code>,
5.755 + * where <code>thisSequence</code> is this sequence.
5.756 + *
5.757 + * @param index Index of <code>char</code> to remove
5.758 + * @return This object.
5.759 + * @throws StringIndexOutOfBoundsException if the <code>index</code>
5.760 + * is negative or greater than or equal to
5.761 + * <code>length()</code>.
5.762 + */
5.763 + public AbstractStringBuilder deleteCharAt(int index) {
5.764 + if ((index < 0) || (index >= count))
5.765 + throw new StringIndexOutOfBoundsException(index);
5.766 + System.arraycopy(value, index+1, value, index, count-index-1);
5.767 + count--;
5.768 + return this;
5.769 + }
5.770 +
5.771 + /**
5.772 + * Replaces the characters in a substring of this sequence
5.773 + * with characters in the specified <code>String</code>. The substring
5.774 + * begins at the specified <code>start</code> and extends to the character
5.775 + * at index <code>end - 1</code> or to the end of the
5.776 + * sequence if no such character exists. First the
5.777 + * characters in the substring are removed and then the specified
5.778 + * <code>String</code> is inserted at <code>start</code>. (This
5.779 + * sequence will be lengthened to accommodate the
5.780 + * specified String if necessary.)
5.781 + *
5.782 + * @param start The beginning index, inclusive.
5.783 + * @param end The ending index, exclusive.
5.784 + * @param str String that will replace previous contents.
5.785 + * @return This object.
5.786 + * @throws StringIndexOutOfBoundsException if <code>start</code>
5.787 + * is negative, greater than <code>length()</code>, or
5.788 + * greater than <code>end</code>.
5.789 + */
5.790 + public AbstractStringBuilder replace(int start, int end, String str) {
5.791 + if (start < 0)
5.792 + throw new StringIndexOutOfBoundsException(start);
5.793 + if (start > count)
5.794 + throw new StringIndexOutOfBoundsException("start > length()");
5.795 + if (start > end)
5.796 + throw new StringIndexOutOfBoundsException("start > end");
5.797 +
5.798 + if (end > count)
5.799 + end = count;
5.800 + int len = str.length();
5.801 + int newCount = count + len - (end - start);
5.802 + ensureCapacityInternal(newCount);
5.803 +
5.804 + System.arraycopy(value, end, value, start + len, count - end);
5.805 + str.getChars(value, start);
5.806 + count = newCount;
5.807 + return this;
5.808 + }
5.809 +
5.810 + /**
5.811 + * Returns a new <code>String</code> that contains a subsequence of
5.812 + * characters currently contained in this character sequence. The
5.813 + * substring begins at the specified index and extends to the end of
5.814 + * this sequence.
5.815 + *
5.816 + * @param start The beginning index, inclusive.
5.817 + * @return The new string.
5.818 + * @throws StringIndexOutOfBoundsException if <code>start</code> is
5.819 + * less than zero, or greater than the length of this object.
5.820 + */
5.821 + public String substring(int start) {
5.822 + return substring(start, count);
5.823 + }
5.824 +
5.825 + /**
5.826 + * Returns a new character sequence that is a subsequence of this sequence.
5.827 + *
5.828 + * <p> An invocation of this method of the form
5.829 + *
5.830 + * <blockquote><pre>
5.831 + * sb.subSequence(begin, end)</pre></blockquote>
5.832 + *
5.833 + * behaves in exactly the same way as the invocation
5.834 + *
5.835 + * <blockquote><pre>
5.836 + * sb.substring(begin, end)</pre></blockquote>
5.837 + *
5.838 + * This method is provided so that this class can
5.839 + * implement the {@link CharSequence} interface. </p>
5.840 + *
5.841 + * @param start the start index, inclusive.
5.842 + * @param end the end index, exclusive.
5.843 + * @return the specified subsequence.
5.844 + *
5.845 + * @throws IndexOutOfBoundsException
5.846 + * if <tt>start</tt> or <tt>end</tt> are negative,
5.847 + * if <tt>end</tt> is greater than <tt>length()</tt>,
5.848 + * or if <tt>start</tt> is greater than <tt>end</tt>
5.849 + * @spec JSR-51
5.850 + */
5.851 + public CharSequence subSequence(int start, int end) {
5.852 + return substring(start, end);
5.853 + }
5.854 +
5.855 + /**
5.856 + * Returns a new <code>String</code> that contains a subsequence of
5.857 + * characters currently contained in this sequence. The
5.858 + * substring begins at the specified <code>start</code> and
5.859 + * extends to the character at index <code>end - 1</code>.
5.860 + *
5.861 + * @param start The beginning index, inclusive.
5.862 + * @param end The ending index, exclusive.
5.863 + * @return The new string.
5.864 + * @throws StringIndexOutOfBoundsException if <code>start</code>
5.865 + * or <code>end</code> are negative or greater than
5.866 + * <code>length()</code>, or <code>start</code> is
5.867 + * greater than <code>end</code>.
5.868 + */
5.869 + public String substring(int start, int end) {
5.870 + if (start < 0)
5.871 + throw new StringIndexOutOfBoundsException(start);
5.872 + if (end > count)
5.873 + throw new StringIndexOutOfBoundsException(end);
5.874 + if (start > end)
5.875 + throw new StringIndexOutOfBoundsException(end - start);
5.876 + return new String(value, start, end - start);
5.877 + }
5.878 +
5.879 + /**
5.880 + * Inserts the string representation of a subarray of the {@code str}
5.881 + * array argument into this sequence. The subarray begins at the
5.882 + * specified {@code offset} and extends {@code len} {@code char}s.
5.883 + * The characters of the subarray are inserted into this sequence at
5.884 + * the position indicated by {@code index}. The length of this
5.885 + * sequence increases by {@code len} {@code char}s.
5.886 + *
5.887 + * @param index position at which to insert subarray.
5.888 + * @param str A {@code char} array.
5.889 + * @param offset the index of the first {@code char} in subarray to
5.890 + * be inserted.
5.891 + * @param len the number of {@code char}s in the subarray to
5.892 + * be inserted.
5.893 + * @return This object
5.894 + * @throws StringIndexOutOfBoundsException if {@code index}
5.895 + * is negative or greater than {@code length()}, or
5.896 + * {@code offset} or {@code len} are negative, or
5.897 + * {@code (offset+len)} is greater than
5.898 + * {@code str.length}.
5.899 + */
5.900 + public AbstractStringBuilder insert(int index, char[] str, int offset,
5.901 + int len)
5.902 + {
5.903 + if ((index < 0) || (index > length()))
5.904 + throw new StringIndexOutOfBoundsException(index);
5.905 + if ((offset < 0) || (len < 0) || (offset > str.length - len))
5.906 + throw new StringIndexOutOfBoundsException(
5.907 + "offset " + offset + ", len " + len + ", str.length "
5.908 + + str.length);
5.909 + ensureCapacityInternal(count + len);
5.910 + System.arraycopy(value, index, value, index + len, count - index);
5.911 + System.arraycopy(str, offset, value, index, len);
5.912 + count += len;
5.913 + return this;
5.914 + }
5.915 +
5.916 + /**
5.917 + * Inserts the string representation of the {@code Object}
5.918 + * argument into this character sequence.
5.919 + * <p>
5.920 + * The overall effect is exactly as if the second argument were
5.921 + * converted to a string by the method {@link String#valueOf(Object)},
5.922 + * and the characters of that string were then
5.923 + * {@link #insert(int,String) inserted} into this character
5.924 + * sequence at the indicated offset.
5.925 + * <p>
5.926 + * The {@code offset} argument must be greater than or equal to
5.927 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
5.928 + * of this sequence.
5.929 + *
5.930 + * @param offset the offset.
5.931 + * @param obj an {@code Object}.
5.932 + * @return a reference to this object.
5.933 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
5.934 + */
5.935 + public AbstractStringBuilder insert(int offset, Object obj) {
5.936 + return insert(offset, String.valueOf(obj));
5.937 + }
5.938 +
5.939 + /**
5.940 + * Inserts the string into this character sequence.
5.941 + * <p>
5.942 + * The characters of the {@code String} argument are inserted, in
5.943 + * order, into this sequence at the indicated offset, moving up any
5.944 + * characters originally above that position and increasing the length
5.945 + * of this sequence by the length of the argument. If
5.946 + * {@code str} is {@code null}, then the four characters
5.947 + * {@code "null"} are inserted into this sequence.
5.948 + * <p>
5.949 + * The character at index <i>k</i> in the new character sequence is
5.950 + * equal to:
5.951 + * <ul>
5.952 + * <li>the character at index <i>k</i> in the old character sequence, if
5.953 + * <i>k</i> is less than {@code offset}
5.954 + * <li>the character at index <i>k</i>{@code -offset} in the
5.955 + * argument {@code str}, if <i>k</i> is not less than
5.956 + * {@code offset} but is less than {@code offset+str.length()}
5.957 + * <li>the character at index <i>k</i>{@code -str.length()} in the
5.958 + * old character sequence, if <i>k</i> is not less than
5.959 + * {@code offset+str.length()}
5.960 + * </ul><p>
5.961 + * The {@code offset} argument must be greater than or equal to
5.962 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
5.963 + * of this sequence.
5.964 + *
5.965 + * @param offset the offset.
5.966 + * @param str a string.
5.967 + * @return a reference to this object.
5.968 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
5.969 + */
5.970 + public AbstractStringBuilder insert(int offset, String str) {
5.971 + if ((offset < 0) || (offset > length()))
5.972 + throw new StringIndexOutOfBoundsException(offset);
5.973 + if (str == null)
5.974 + str = "null";
5.975 + int len = str.length();
5.976 + ensureCapacityInternal(count + len);
5.977 + System.arraycopy(value, offset, value, offset + len, count - offset);
5.978 + str.getChars(value, offset);
5.979 + count += len;
5.980 + return this;
5.981 + }
5.982 +
5.983 + /**
5.984 + * Inserts the string representation of the {@code char} array
5.985 + * argument into this sequence.
5.986 + * <p>
5.987 + * The characters of the array argument are inserted into the
5.988 + * contents of this sequence at the position indicated by
5.989 + * {@code offset}. The length of this sequence increases by
5.990 + * the length of the argument.
5.991 + * <p>
5.992 + * The overall effect is exactly as if the second argument were
5.993 + * converted to a string by the method {@link String#valueOf(char[])},
5.994 + * and the characters of that string were then
5.995 + * {@link #insert(int,String) inserted} into this character
5.996 + * sequence at the indicated offset.
5.997 + * <p>
5.998 + * The {@code offset} argument must be greater than or equal to
5.999 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
5.1000 + * of this sequence.
5.1001 + *
5.1002 + * @param offset the offset.
5.1003 + * @param str a character array.
5.1004 + * @return a reference to this object.
5.1005 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
5.1006 + */
5.1007 + public AbstractStringBuilder insert(int offset, char[] str) {
5.1008 + if ((offset < 0) || (offset > length()))
5.1009 + throw new StringIndexOutOfBoundsException(offset);
5.1010 + int len = str.length;
5.1011 + ensureCapacityInternal(count + len);
5.1012 + System.arraycopy(value, offset, value, offset + len, count - offset);
5.1013 + System.arraycopy(str, 0, value, offset, len);
5.1014 + count += len;
5.1015 + return this;
5.1016 + }
5.1017 +
5.1018 + /**
5.1019 + * Inserts the specified {@code CharSequence} into this sequence.
5.1020 + * <p>
5.1021 + * The characters of the {@code CharSequence} argument are inserted,
5.1022 + * in order, into this sequence at the indicated offset, moving up
5.1023 + * any characters originally above that position and increasing the length
5.1024 + * of this sequence by the length of the argument s.
5.1025 + * <p>
5.1026 + * The result of this method is exactly the same as if it were an
5.1027 + * invocation of this object's
5.1028 + * {@link #insert(int,CharSequence,int,int) insert}(dstOffset, s, 0, s.length())
5.1029 + * method.
5.1030 + *
5.1031 + * <p>If {@code s} is {@code null}, then the four characters
5.1032 + * {@code "null"} are inserted into this sequence.
5.1033 + *
5.1034 + * @param dstOffset the offset.
5.1035 + * @param s the sequence to be inserted
5.1036 + * @return a reference to this object.
5.1037 + * @throws IndexOutOfBoundsException if the offset is invalid.
5.1038 + */
5.1039 + public AbstractStringBuilder insert(int dstOffset, CharSequence s) {
5.1040 + if (s == null)
5.1041 + s = "null";
5.1042 + if (s instanceof String)
5.1043 + return this.insert(dstOffset, (String)s);
5.1044 + return this.insert(dstOffset, s, 0, s.length());
5.1045 + }
5.1046 +
5.1047 + /**
5.1048 + * Inserts a subsequence of the specified {@code CharSequence} into
5.1049 + * this sequence.
5.1050 + * <p>
5.1051 + * The subsequence of the argument {@code s} specified by
5.1052 + * {@code start} and {@code end} are inserted,
5.1053 + * in order, into this sequence at the specified destination offset, moving
5.1054 + * up any characters originally above that position. The length of this
5.1055 + * sequence is increased by {@code end - start}.
5.1056 + * <p>
5.1057 + * The character at index <i>k</i> in this sequence becomes equal to:
5.1058 + * <ul>
5.1059 + * <li>the character at index <i>k</i> in this sequence, if
5.1060 + * <i>k</i> is less than {@code dstOffset}
5.1061 + * <li>the character at index <i>k</i>{@code +start-dstOffset} in
5.1062 + * the argument {@code s}, if <i>k</i> is greater than or equal to
5.1063 + * {@code dstOffset} but is less than {@code dstOffset+end-start}
5.1064 + * <li>the character at index <i>k</i>{@code -(end-start)} in this
5.1065 + * sequence, if <i>k</i> is greater than or equal to
5.1066 + * {@code dstOffset+end-start}
5.1067 + * </ul><p>
5.1068 + * The {@code dstOffset} argument must be greater than or equal to
5.1069 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
5.1070 + * of this sequence.
5.1071 + * <p>The start argument must be nonnegative, and not greater than
5.1072 + * {@code end}.
5.1073 + * <p>The end argument must be greater than or equal to
5.1074 + * {@code start}, and less than or equal to the length of s.
5.1075 + *
5.1076 + * <p>If {@code s} is {@code null}, then this method inserts
5.1077 + * characters as if the s parameter was a sequence containing the four
5.1078 + * characters {@code "null"}.
5.1079 + *
5.1080 + * @param dstOffset the offset in this sequence.
5.1081 + * @param s the sequence to be inserted.
5.1082 + * @param start the starting index of the subsequence to be inserted.
5.1083 + * @param end the end index of the subsequence to be inserted.
5.1084 + * @return a reference to this object.
5.1085 + * @throws IndexOutOfBoundsException if {@code dstOffset}
5.1086 + * is negative or greater than {@code this.length()}, or
5.1087 + * {@code start} or {@code end} are negative, or
5.1088 + * {@code start} is greater than {@code end} or
5.1089 + * {@code end} is greater than {@code s.length()}
5.1090 + */
5.1091 + public AbstractStringBuilder insert(int dstOffset, CharSequence s,
5.1092 + int start, int end) {
5.1093 + if (s == null)
5.1094 + s = "null";
5.1095 + if ((dstOffset < 0) || (dstOffset > this.length()))
5.1096 + throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
5.1097 + if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
5.1098 + throw new IndexOutOfBoundsException(
5.1099 + "start " + start + ", end " + end + ", s.length() "
5.1100 + + s.length());
5.1101 + int len = end - start;
5.1102 + ensureCapacityInternal(count + len);
5.1103 + System.arraycopy(value, dstOffset, value, dstOffset + len,
5.1104 + count - dstOffset);
5.1105 + for (int i=start; i<end; i++)
5.1106 + value[dstOffset++] = s.charAt(i);
5.1107 + count += len;
5.1108 + return this;
5.1109 + }
5.1110 +
5.1111 + /**
5.1112 + * Inserts the string representation of the {@code boolean}
5.1113 + * argument into this sequence.
5.1114 + * <p>
5.1115 + * The overall effect is exactly as if the second argument were
5.1116 + * converted to a string by the method {@link String#valueOf(boolean)},
5.1117 + * and the characters of that string were then
5.1118 + * {@link #insert(int,String) inserted} into this character
5.1119 + * sequence at the indicated offset.
5.1120 + * <p>
5.1121 + * The {@code offset} argument must be greater than or equal to
5.1122 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
5.1123 + * of this sequence.
5.1124 + *
5.1125 + * @param offset the offset.
5.1126 + * @param b a {@code boolean}.
5.1127 + * @return a reference to this object.
5.1128 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
5.1129 + */
5.1130 + public AbstractStringBuilder insert(int offset, boolean b) {
5.1131 + return insert(offset, String.valueOf(b));
5.1132 + }
5.1133 +
5.1134 + /**
5.1135 + * Inserts the string representation of the {@code char}
5.1136 + * argument into this sequence.
5.1137 + * <p>
5.1138 + * The overall effect is exactly as if the second argument were
5.1139 + * converted to a string by the method {@link String#valueOf(char)},
5.1140 + * and the character in that string were then
5.1141 + * {@link #insert(int,String) inserted} into this character
5.1142 + * sequence at the indicated offset.
5.1143 + * <p>
5.1144 + * The {@code offset} argument must be greater than or equal to
5.1145 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
5.1146 + * of this sequence.
5.1147 + *
5.1148 + * @param offset the offset.
5.1149 + * @param c a {@code char}.
5.1150 + * @return a reference to this object.
5.1151 + * @throws IndexOutOfBoundsException if the offset is invalid.
5.1152 + */
5.1153 + public AbstractStringBuilder insert(int offset, char c) {
5.1154 + ensureCapacityInternal(count + 1);
5.1155 + System.arraycopy(value, offset, value, offset + 1, count - offset);
5.1156 + value[offset] = c;
5.1157 + count += 1;
5.1158 + return this;
5.1159 + }
5.1160 +
5.1161 + /**
5.1162 + * Inserts the string representation of the second {@code int}
5.1163 + * argument into this sequence.
5.1164 + * <p>
5.1165 + * The overall effect is exactly as if the second argument were
5.1166 + * converted to a string by the method {@link String#valueOf(int)},
5.1167 + * and the characters of that string were then
5.1168 + * {@link #insert(int,String) inserted} into this character
5.1169 + * sequence at the indicated offset.
5.1170 + * <p>
5.1171 + * The {@code offset} argument must be greater than or equal to
5.1172 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
5.1173 + * of this sequence.
5.1174 + *
5.1175 + * @param offset the offset.
5.1176 + * @param i an {@code int}.
5.1177 + * @return a reference to this object.
5.1178 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
5.1179 + */
5.1180 + public AbstractStringBuilder insert(int offset, int i) {
5.1181 + return insert(offset, String.valueOf(i));
5.1182 + }
5.1183 +
5.1184 + /**
5.1185 + * Inserts the string representation of the {@code long}
5.1186 + * argument into this sequence.
5.1187 + * <p>
5.1188 + * The overall effect is exactly as if the second argument were
5.1189 + * converted to a string by the method {@link String#valueOf(long)},
5.1190 + * and the characters of that string were then
5.1191 + * {@link #insert(int,String) inserted} into this character
5.1192 + * sequence at the indicated offset.
5.1193 + * <p>
5.1194 + * The {@code offset} argument must be greater than or equal to
5.1195 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
5.1196 + * of this sequence.
5.1197 + *
5.1198 + * @param offset the offset.
5.1199 + * @param l a {@code long}.
5.1200 + * @return a reference to this object.
5.1201 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
5.1202 + */
5.1203 + public AbstractStringBuilder insert(int offset, long l) {
5.1204 + return insert(offset, String.valueOf(l));
5.1205 + }
5.1206 +
5.1207 + /**
5.1208 + * Inserts the string representation of the {@code float}
5.1209 + * argument into this sequence.
5.1210 + * <p>
5.1211 + * The overall effect is exactly as if the second argument were
5.1212 + * converted to a string by the method {@link String#valueOf(float)},
5.1213 + * and the characters of that string were then
5.1214 + * {@link #insert(int,String) inserted} into this character
5.1215 + * sequence at the indicated offset.
5.1216 + * <p>
5.1217 + * The {@code offset} argument must be greater than or equal to
5.1218 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
5.1219 + * of this sequence.
5.1220 + *
5.1221 + * @param offset the offset.
5.1222 + * @param f a {@code float}.
5.1223 + * @return a reference to this object.
5.1224 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
5.1225 + */
5.1226 + public AbstractStringBuilder insert(int offset, float f) {
5.1227 + return insert(offset, String.valueOf(f));
5.1228 + }
5.1229 +
5.1230 + /**
5.1231 + * Inserts the string representation of the {@code double}
5.1232 + * argument into this sequence.
5.1233 + * <p>
5.1234 + * The overall effect is exactly as if the second argument were
5.1235 + * converted to a string by the method {@link String#valueOf(double)},
5.1236 + * and the characters of that string were then
5.1237 + * {@link #insert(int,String) inserted} into this character
5.1238 + * sequence at the indicated offset.
5.1239 + * <p>
5.1240 + * The {@code offset} argument must be greater than or equal to
5.1241 + * {@code 0}, and less than or equal to the {@linkplain #length() length}
5.1242 + * of this sequence.
5.1243 + *
5.1244 + * @param offset the offset.
5.1245 + * @param d a {@code double}.
5.1246 + * @return a reference to this object.
5.1247 + * @throws StringIndexOutOfBoundsException if the offset is invalid.
5.1248 + */
5.1249 + public AbstractStringBuilder insert(int offset, double d) {
5.1250 + return insert(offset, String.valueOf(d));
5.1251 + }
5.1252 +
5.1253 + /**
5.1254 + * Returns the index within this string of the first occurrence of the
5.1255 + * specified substring. The integer returned is the smallest value
5.1256 + * <i>k</i> such that:
5.1257 + * <blockquote><pre>
5.1258 + * this.toString().startsWith(str, <i>k</i>)
5.1259 + * </pre></blockquote>
5.1260 + * is <code>true</code>.
5.1261 + *
5.1262 + * @param str any string.
5.1263 + * @return if the string argument occurs as a substring within this
5.1264 + * object, then the index of the first character of the first
5.1265 + * such substring is returned; if it does not occur as a
5.1266 + * substring, <code>-1</code> is returned.
5.1267 + * @throws java.lang.NullPointerException if <code>str</code> is
5.1268 + * <code>null</code>.
5.1269 + */
5.1270 + public int indexOf(String str) {
5.1271 + return indexOf(str, 0);
5.1272 + }
5.1273 +
5.1274 + /**
5.1275 + * Returns the index within this string of the first occurrence of the
5.1276 + * specified substring, starting at the specified index. The integer
5.1277 + * returned is the smallest value <tt>k</tt> for which:
5.1278 + * <blockquote><pre>
5.1279 + * k >= Math.min(fromIndex, str.length()) &&
5.1280 + * this.toString().startsWith(str, k)
5.1281 + * </pre></blockquote>
5.1282 + * If no such value of <i>k</i> exists, then -1 is returned.
5.1283 + *
5.1284 + * @param str the substring for which to search.
5.1285 + * @param fromIndex the index from which to start the search.
5.1286 + * @return the index within this string of the first occurrence of the
5.1287 + * specified substring, starting at the specified index.
5.1288 + * @throws java.lang.NullPointerException if <code>str</code> is
5.1289 + * <code>null</code>.
5.1290 + */
5.1291 + public int indexOf(String str, int fromIndex) {
5.1292 + return String.indexOf(value, 0, count,
5.1293 + str.toCharArray(), 0, str.length(), fromIndex);
5.1294 + }
5.1295 +
5.1296 + /**
5.1297 + * Returns the index within this string of the rightmost occurrence
5.1298 + * of the specified substring. The rightmost empty string "" is
5.1299 + * considered to occur at the index value <code>this.length()</code>.
5.1300 + * The returned index is the largest value <i>k</i> such that
5.1301 + * <blockquote><pre>
5.1302 + * this.toString().startsWith(str, k)
5.1303 + * </pre></blockquote>
5.1304 + * is true.
5.1305 + *
5.1306 + * @param str the substring to search for.
5.1307 + * @return if the string argument occurs one or more times as a substring
5.1308 + * within this object, then the index of the first character of
5.1309 + * the last such substring is returned. If it does not occur as
5.1310 + * a substring, <code>-1</code> is returned.
5.1311 + * @throws java.lang.NullPointerException if <code>str</code> is
5.1312 + * <code>null</code>.
5.1313 + */
5.1314 + public int lastIndexOf(String str) {
5.1315 + return lastIndexOf(str, count);
5.1316 + }
5.1317 +
5.1318 + /**
5.1319 + * Returns the index within this string of the last occurrence of the
5.1320 + * specified substring. The integer returned is the largest value <i>k</i>
5.1321 + * such that:
5.1322 + * <blockquote><pre>
5.1323 + * k <= Math.min(fromIndex, str.length()) &&
5.1324 + * this.toString().startsWith(str, k)
5.1325 + * </pre></blockquote>
5.1326 + * If no such value of <i>k</i> exists, then -1 is returned.
5.1327 + *
5.1328 + * @param str the substring to search for.
5.1329 + * @param fromIndex the index to start the search from.
5.1330 + * @return the index within this sequence of the last occurrence of the
5.1331 + * specified substring.
5.1332 + * @throws java.lang.NullPointerException if <code>str</code> is
5.1333 + * <code>null</code>.
5.1334 + */
5.1335 + public int lastIndexOf(String str, int fromIndex) {
5.1336 + return String.lastIndexOf(value, 0, count,
5.1337 + str.toCharArray(), 0, str.length(), fromIndex);
5.1338 + }
5.1339 +
5.1340 + /**
5.1341 + * Causes this character sequence to be replaced by the reverse of
5.1342 + * the sequence. If there are any surrogate pairs included in the
5.1343 + * sequence, these are treated as single characters for the
5.1344 + * reverse operation. Thus, the order of the high-low surrogates
5.1345 + * is never reversed.
5.1346 + *
5.1347 + * Let <i>n</i> be the character length of this character sequence
5.1348 + * (not the length in <code>char</code> values) just prior to
5.1349 + * execution of the <code>reverse</code> method. Then the
5.1350 + * character at index <i>k</i> in the new character sequence is
5.1351 + * equal to the character at index <i>n-k-1</i> in the old
5.1352 + * character sequence.
5.1353 + *
5.1354 + * <p>Note that the reverse operation may result in producing
5.1355 + * surrogate pairs that were unpaired low-surrogates and
5.1356 + * high-surrogates before the operation. For example, reversing
5.1357 + * "\uDC00\uD800" produces "\uD800\uDC00" which is
5.1358 + * a valid surrogate pair.
5.1359 + *
5.1360 + * @return a reference to this object.
5.1361 + */
5.1362 + public AbstractStringBuilder reverse() {
5.1363 + boolean hasSurrogate = false;
5.1364 + int n = count - 1;
5.1365 + for (int j = (n-1) >> 1; j >= 0; --j) {
5.1366 + char temp = value[j];
5.1367 + char temp2 = value[n - j];
5.1368 + if (!hasSurrogate) {
5.1369 + hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <= Character.MAX_SURROGATE)
5.1370 + || (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE);
5.1371 + }
5.1372 + value[j] = temp2;
5.1373 + value[n - j] = temp;
5.1374 + }
5.1375 + if (hasSurrogate) {
5.1376 + // Reverse back all valid surrogate pairs
5.1377 + for (int i = 0; i < count - 1; i++) {
5.1378 + char c2 = value[i];
5.1379 + if (Character.isLowSurrogate(c2)) {
5.1380 + char c1 = value[i + 1];
5.1381 + if (Character.isHighSurrogate(c1)) {
5.1382 + value[i++] = c1;
5.1383 + value[i] = c2;
5.1384 + }
5.1385 + }
5.1386 + }
5.1387 + }
5.1388 + return this;
5.1389 + }
5.1390 +
5.1391 + /**
5.1392 + * Returns a string representing the data in this sequence.
5.1393 + * A new <code>String</code> object is allocated and initialized to
5.1394 + * contain the character sequence currently represented by this
5.1395 + * object. This <code>String</code> is then returned. Subsequent
5.1396 + * changes to this sequence do not affect the contents of the
5.1397 + * <code>String</code>.
5.1398 + *
5.1399 + * @return a string representation of this sequence of characters.
5.1400 + */
5.1401 + public abstract String toString();
5.1402 +
5.1403 + /**
5.1404 + * Needed by <tt>String</tt> for the contentEquals method.
5.1405 + */
5.1406 + final char[] getValue() {
5.1407 + return value;
5.1408 + }
5.1409 +
5.1410 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/emul/src/main/java/java/lang/Class.java Sat Sep 29 06:47:05 2012 +0200
6.3 @@ -0,0 +1,3117 @@
6.4 +/*
6.5 + * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6.7 + *
6.8 + * This code is free software; you can redistribute it and/or modify it
6.9 + * under the terms of the GNU General Public License version 2 only, as
6.10 + * published by the Free Software Foundation. Oracle designates this
6.11 + * particular file as subject to the "Classpath" exception as provided
6.12 + * by Oracle in the LICENSE file that accompanied this code.
6.13 + *
6.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
6.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6.17 + * version 2 for more details (a copy is included in the LICENSE file that
6.18 + * accompanied this code).
6.19 + *
6.20 + * You should have received a copy of the GNU General Public License version
6.21 + * 2 along with this work; if not, write to the Free Software Foundation,
6.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6.23 + *
6.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6.25 + * or visit www.oracle.com if you need additional information or have any
6.26 + * questions.
6.27 + */
6.28 +
6.29 +package java.lang;
6.30 +
6.31 +import java.lang.reflect.Array;
6.32 +import java.lang.reflect.GenericArrayType;
6.33 +import java.lang.reflect.Member;
6.34 +import java.lang.reflect.Field;
6.35 +import java.lang.reflect.Method;
6.36 +import java.lang.reflect.Constructor;
6.37 +import java.lang.reflect.GenericDeclaration;
6.38 +import java.lang.reflect.Modifier;
6.39 +import java.lang.reflect.Type;
6.40 +import java.lang.reflect.TypeVariable;
6.41 +import java.lang.reflect.InvocationTargetException;
6.42 +import java.lang.ref.SoftReference;
6.43 +import java.io.InputStream;
6.44 +import java.io.ObjectStreamField;
6.45 +import java.security.AccessController;
6.46 +import java.security.PrivilegedAction;
6.47 +import java.util.ArrayList;
6.48 +import java.util.Arrays;
6.49 +import java.util.Collection;
6.50 +import java.util.HashSet;
6.51 +import java.util.Iterator;
6.52 +import java.util.List;
6.53 +import java.util.LinkedList;
6.54 +import java.util.LinkedHashSet;
6.55 +import java.util.Set;
6.56 +import java.util.Map;
6.57 +import java.util.HashMap;
6.58 +import sun.misc.Unsafe;
6.59 +import sun.reflect.ConstantPool;
6.60 +import sun.reflect.Reflection;
6.61 +import sun.reflect.ReflectionFactory;
6.62 +import sun.reflect.SignatureIterator;
6.63 +import sun.reflect.generics.factory.CoreReflectionFactory;
6.64 +import sun.reflect.generics.factory.GenericsFactory;
6.65 +import sun.reflect.generics.repository.ClassRepository;
6.66 +import sun.reflect.generics.repository.MethodRepository;
6.67 +import sun.reflect.generics.repository.ConstructorRepository;
6.68 +import sun.reflect.generics.scope.ClassScope;
6.69 +import sun.security.util.SecurityConstants;
6.70 +import java.lang.annotation.Annotation;
6.71 +import sun.reflect.annotation.*;
6.72 +
6.73 +/**
6.74 + * Instances of the class {@code Class} represent classes and
6.75 + * interfaces in a running Java application. An enum is a kind of
6.76 + * class and an annotation is a kind of interface. Every array also
6.77 + * belongs to a class that is reflected as a {@code Class} object
6.78 + * that is shared by all arrays with the same element type and number
6.79 + * of dimensions. The primitive Java types ({@code boolean},
6.80 + * {@code byte}, {@code char}, {@code short},
6.81 + * {@code int}, {@code long}, {@code float}, and
6.82 + * {@code double}), and the keyword {@code void} are also
6.83 + * represented as {@code Class} objects.
6.84 + *
6.85 + * <p> {@code Class} has no public constructor. Instead {@code Class}
6.86 + * objects are constructed automatically by the Java Virtual Machine as classes
6.87 + * are loaded and by calls to the {@code defineClass} method in the class
6.88 + * loader.
6.89 + *
6.90 + * <p> The following example uses a {@code Class} object to print the
6.91 + * class name of an object:
6.92 + *
6.93 + * <p> <blockquote><pre>
6.94 + * void printClassName(Object obj) {
6.95 + * System.out.println("The class of " + obj +
6.96 + * " is " + obj.getClass().getName());
6.97 + * }
6.98 + * </pre></blockquote>
6.99 + *
6.100 + * <p> It is also possible to get the {@code Class} object for a named
6.101 + * type (or for void) using a class literal. See Section 15.8.2 of
6.102 + * <cite>The Java™ Language Specification</cite>.
6.103 + * For example:
6.104 + *
6.105 + * <p> <blockquote>
6.106 + * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
6.107 + * </blockquote>
6.108 + *
6.109 + * @param <T> the type of the class modeled by this {@code Class}
6.110 + * object. For example, the type of {@code String.class} is {@code
6.111 + * Class<String>}. Use {@code Class<?>} if the class being modeled is
6.112 + * unknown.
6.113 + *
6.114 + * @author unascribed
6.115 + * @see java.lang.ClassLoader#defineClass(byte[], int, int)
6.116 + * @since JDK1.0
6.117 + */
6.118 +public final
6.119 + class Class<T> implements java.io.Serializable,
6.120 + java.lang.reflect.GenericDeclaration,
6.121 + java.lang.reflect.Type,
6.122 + java.lang.reflect.AnnotatedElement {
6.123 + private static final int ANNOTATION= 0x00002000;
6.124 + private static final int ENUM = 0x00004000;
6.125 + private static final int SYNTHETIC = 0x00001000;
6.126 +
6.127 + private static native void registerNatives();
6.128 + static {
6.129 + registerNatives();
6.130 + }
6.131 +
6.132 + /*
6.133 + * Constructor. Only the Java Virtual Machine creates Class
6.134 + * objects.
6.135 + */
6.136 + private Class() {}
6.137 +
6.138 +
6.139 + /**
6.140 + * Converts the object to a string. The string representation is the
6.141 + * string "class" or "interface", followed by a space, and then by the
6.142 + * fully qualified name of the class in the format returned by
6.143 + * {@code getName}. If this {@code Class} object represents a
6.144 + * primitive type, this method returns the name of the primitive type. If
6.145 + * this {@code Class} object represents void this method returns
6.146 + * "void".
6.147 + *
6.148 + * @return a string representation of this class object.
6.149 + */
6.150 + public String toString() {
6.151 + return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
6.152 + + getName();
6.153 + }
6.154 +
6.155 +
6.156 + /**
6.157 + * Returns the {@code Class} object associated with the class or
6.158 + * interface with the given string name. Invoking this method is
6.159 + * equivalent to:
6.160 + *
6.161 + * <blockquote>
6.162 + * {@code Class.forName(className, true, currentLoader)}
6.163 + * </blockquote>
6.164 + *
6.165 + * where {@code currentLoader} denotes the defining class loader of
6.166 + * the current class.
6.167 + *
6.168 + * <p> For example, the following code fragment returns the
6.169 + * runtime {@code Class} descriptor for the class named
6.170 + * {@code java.lang.Thread}:
6.171 + *
6.172 + * <blockquote>
6.173 + * {@code Class t = Class.forName("java.lang.Thread")}
6.174 + * </blockquote>
6.175 + * <p>
6.176 + * A call to {@code forName("X")} causes the class named
6.177 + * {@code X} to be initialized.
6.178 + *
6.179 + * @param className the fully qualified name of the desired class.
6.180 + * @return the {@code Class} object for the class with the
6.181 + * specified name.
6.182 + * @exception LinkageError if the linkage fails
6.183 + * @exception ExceptionInInitializerError if the initialization provoked
6.184 + * by this method fails
6.185 + * @exception ClassNotFoundException if the class cannot be located
6.186 + */
6.187 + public static Class<?> forName(String className)
6.188 + throws ClassNotFoundException {
6.189 + return forName0(className, true, ClassLoader.getCallerClassLoader());
6.190 + }
6.191 +
6.192 +
6.193 + /**
6.194 + * Returns the {@code Class} object associated with the class or
6.195 + * interface with the given string name, using the given class loader.
6.196 + * Given the fully qualified name for a class or interface (in the same
6.197 + * format returned by {@code getName}) this method attempts to
6.198 + * locate, load, and link the class or interface. The specified class
6.199 + * loader is used to load the class or interface. If the parameter
6.200 + * {@code loader} is null, the class is loaded through the bootstrap
6.201 + * class loader. The class is initialized only if the
6.202 + * {@code initialize} parameter is {@code true} and if it has
6.203 + * not been initialized earlier.
6.204 + *
6.205 + * <p> If {@code name} denotes a primitive type or void, an attempt
6.206 + * will be made to locate a user-defined class in the unnamed package whose
6.207 + * name is {@code name}. Therefore, this method cannot be used to
6.208 + * obtain any of the {@code Class} objects representing primitive
6.209 + * types or void.
6.210 + *
6.211 + * <p> If {@code name} denotes an array class, the component type of
6.212 + * the array class is loaded but not initialized.
6.213 + *
6.214 + * <p> For example, in an instance method the expression:
6.215 + *
6.216 + * <blockquote>
6.217 + * {@code Class.forName("Foo")}
6.218 + * </blockquote>
6.219 + *
6.220 + * is equivalent to:
6.221 + *
6.222 + * <blockquote>
6.223 + * {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
6.224 + * </blockquote>
6.225 + *
6.226 + * Note that this method throws errors related to loading, linking or
6.227 + * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
6.228 + * Java Language Specification</em>.
6.229 + * Note that this method does not check whether the requested class
6.230 + * is accessible to its caller.
6.231 + *
6.232 + * <p> If the {@code loader} is {@code null}, and a security
6.233 + * manager is present, and the caller's class loader is not null, then this
6.234 + * method calls the security manager's {@code checkPermission} method
6.235 + * with a {@code RuntimePermission("getClassLoader")} permission to
6.236 + * ensure it's ok to access the bootstrap class loader.
6.237 + *
6.238 + * @param name fully qualified name of the desired class
6.239 + * @param initialize whether the class must be initialized
6.240 + * @param loader class loader from which the class must be loaded
6.241 + * @return class object representing the desired class
6.242 + *
6.243 + * @exception LinkageError if the linkage fails
6.244 + * @exception ExceptionInInitializerError if the initialization provoked
6.245 + * by this method fails
6.246 + * @exception ClassNotFoundException if the class cannot be located by
6.247 + * the specified class loader
6.248 + *
6.249 + * @see java.lang.Class#forName(String)
6.250 + * @see java.lang.ClassLoader
6.251 + * @since 1.2
6.252 + */
6.253 + public static Class<?> forName(String name, boolean initialize,
6.254 + ClassLoader loader)
6.255 + throws ClassNotFoundException
6.256 + {
6.257 + if (loader == null) {
6.258 + SecurityManager sm = System.getSecurityManager();
6.259 + if (sm != null) {
6.260 + ClassLoader ccl = ClassLoader.getCallerClassLoader();
6.261 + if (ccl != null) {
6.262 + sm.checkPermission(
6.263 + SecurityConstants.GET_CLASSLOADER_PERMISSION);
6.264 + }
6.265 + }
6.266 + }
6.267 + return forName0(name, initialize, loader);
6.268 + }
6.269 +
6.270 + /** Called after security checks have been made. */
6.271 + private static native Class<?> forName0(String name, boolean initialize,
6.272 + ClassLoader loader)
6.273 + throws ClassNotFoundException;
6.274 +
6.275 + /**
6.276 + * Creates a new instance of the class represented by this {@code Class}
6.277 + * object. The class is instantiated as if by a {@code new}
6.278 + * expression with an empty argument list. The class is initialized if it
6.279 + * has not already been initialized.
6.280 + *
6.281 + * <p>Note that this method propagates any exception thrown by the
6.282 + * nullary constructor, including a checked exception. Use of
6.283 + * this method effectively bypasses the compile-time exception
6.284 + * checking that would otherwise be performed by the compiler.
6.285 + * The {@link
6.286 + * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
6.287 + * Constructor.newInstance} method avoids this problem by wrapping
6.288 + * any exception thrown by the constructor in a (checked) {@link
6.289 + * java.lang.reflect.InvocationTargetException}.
6.290 + *
6.291 + * @return a newly allocated instance of the class represented by this
6.292 + * object.
6.293 + * @exception IllegalAccessException if the class or its nullary
6.294 + * constructor is not accessible.
6.295 + * @exception InstantiationException
6.296 + * if this {@code Class} represents an abstract class,
6.297 + * an interface, an array class, a primitive type, or void;
6.298 + * or if the class has no nullary constructor;
6.299 + * or if the instantiation fails for some other reason.
6.300 + * @exception ExceptionInInitializerError if the initialization
6.301 + * provoked by this method fails.
6.302 + * @exception SecurityException
6.303 + * If a security manager, <i>s</i>, is present and any of the
6.304 + * following conditions is met:
6.305 + *
6.306 + * <ul>
6.307 + *
6.308 + * <li> invocation of
6.309 + * {@link SecurityManager#checkMemberAccess
6.310 + * s.checkMemberAccess(this, Member.PUBLIC)} denies
6.311 + * creation of new instances of this class
6.312 + *
6.313 + * <li> the caller's class loader is not the same as or an
6.314 + * ancestor of the class loader for the current class and
6.315 + * invocation of {@link SecurityManager#checkPackageAccess
6.316 + * s.checkPackageAccess()} denies access to the package
6.317 + * of this class
6.318 + *
6.319 + * </ul>
6.320 + *
6.321 + */
6.322 + public T newInstance()
6.323 + throws InstantiationException, IllegalAccessException
6.324 + {
6.325 + if (System.getSecurityManager() != null) {
6.326 + checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
6.327 + }
6.328 + return newInstance0();
6.329 + }
6.330 +
6.331 + private T newInstance0()
6.332 + throws InstantiationException, IllegalAccessException
6.333 + {
6.334 + // NOTE: the following code may not be strictly correct under
6.335 + // the current Java memory model.
6.336 +
6.337 + // Constructor lookup
6.338 + if (cachedConstructor == null) {
6.339 + if (this == Class.class) {
6.340 + throw new IllegalAccessException(
6.341 + "Can not call newInstance() on the Class for java.lang.Class"
6.342 + );
6.343 + }
6.344 + try {
6.345 + Class<?>[] empty = {};
6.346 + final Constructor<T> c = getConstructor0(empty, Member.DECLARED);
6.347 + // Disable accessibility checks on the constructor
6.348 + // since we have to do the security check here anyway
6.349 + // (the stack depth is wrong for the Constructor's
6.350 + // security check to work)
6.351 + java.security.AccessController.doPrivileged(
6.352 + new java.security.PrivilegedAction<Void>() {
6.353 + public Void run() {
6.354 + c.setAccessible(true);
6.355 + return null;
6.356 + }
6.357 + });
6.358 + cachedConstructor = c;
6.359 + } catch (NoSuchMethodException e) {
6.360 + throw new InstantiationException(getName());
6.361 + }
6.362 + }
6.363 + Constructor<T> tmpConstructor = cachedConstructor;
6.364 + // Security check (same as in java.lang.reflect.Constructor)
6.365 + int modifiers = tmpConstructor.getModifiers();
6.366 + if (!Reflection.quickCheckMemberAccess(this, modifiers)) {
6.367 + Class<?> caller = Reflection.getCallerClass(3);
6.368 + if (newInstanceCallerCache != caller) {
6.369 + Reflection.ensureMemberAccess(caller, this, null, modifiers);
6.370 + newInstanceCallerCache = caller;
6.371 + }
6.372 + }
6.373 + // Run constructor
6.374 + try {
6.375 + return tmpConstructor.newInstance((Object[])null);
6.376 + } catch (InvocationTargetException e) {
6.377 + Unsafe.getUnsafe().throwException(e.getTargetException());
6.378 + // Not reached
6.379 + return null;
6.380 + }
6.381 + }
6.382 + private volatile transient Constructor<T> cachedConstructor;
6.383 + private volatile transient Class<?> newInstanceCallerCache;
6.384 +
6.385 +
6.386 + /**
6.387 + * Determines if the specified {@code Object} is assignment-compatible
6.388 + * with the object represented by this {@code Class}. This method is
6.389 + * the dynamic equivalent of the Java language {@code instanceof}
6.390 + * operator. The method returns {@code true} if the specified
6.391 + * {@code Object} argument is non-null and can be cast to the
6.392 + * reference type represented by this {@code Class} object without
6.393 + * raising a {@code ClassCastException.} It returns {@code false}
6.394 + * otherwise.
6.395 + *
6.396 + * <p> Specifically, if this {@code Class} object represents a
6.397 + * declared class, this method returns {@code true} if the specified
6.398 + * {@code Object} argument is an instance of the represented class (or
6.399 + * of any of its subclasses); it returns {@code false} otherwise. If
6.400 + * this {@code Class} object represents an array class, this method
6.401 + * returns {@code true} if the specified {@code Object} argument
6.402 + * can be converted to an object of the array class by an identity
6.403 + * conversion or by a widening reference conversion; it returns
6.404 + * {@code false} otherwise. If this {@code Class} object
6.405 + * represents an interface, this method returns {@code true} if the
6.406 + * class or any superclass of the specified {@code Object} argument
6.407 + * implements this interface; it returns {@code false} otherwise. If
6.408 + * this {@code Class} object represents a primitive type, this method
6.409 + * returns {@code false}.
6.410 + *
6.411 + * @param obj the object to check
6.412 + * @return true if {@code obj} is an instance of this class
6.413 + *
6.414 + * @since JDK1.1
6.415 + */
6.416 + public native boolean isInstance(Object obj);
6.417 +
6.418 +
6.419 + /**
6.420 + * Determines if the class or interface represented by this
6.421 + * {@code Class} object is either the same as, or is a superclass or
6.422 + * superinterface of, the class or interface represented by the specified
6.423 + * {@code Class} parameter. It returns {@code true} if so;
6.424 + * otherwise it returns {@code false}. If this {@code Class}
6.425 + * object represents a primitive type, this method returns
6.426 + * {@code true} if the specified {@code Class} parameter is
6.427 + * exactly this {@code Class} object; otherwise it returns
6.428 + * {@code false}.
6.429 + *
6.430 + * <p> Specifically, this method tests whether the type represented by the
6.431 + * specified {@code Class} parameter can be converted to the type
6.432 + * represented by this {@code Class} object via an identity conversion
6.433 + * or via a widening reference conversion. See <em>The Java Language
6.434 + * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
6.435 + *
6.436 + * @param cls the {@code Class} object to be checked
6.437 + * @return the {@code boolean} value indicating whether objects of the
6.438 + * type {@code cls} can be assigned to objects of this class
6.439 + * @exception NullPointerException if the specified Class parameter is
6.440 + * null.
6.441 + * @since JDK1.1
6.442 + */
6.443 + public native boolean isAssignableFrom(Class<?> cls);
6.444 +
6.445 +
6.446 + /**
6.447 + * Determines if the specified {@code Class} object represents an
6.448 + * interface type.
6.449 + *
6.450 + * @return {@code true} if this object represents an interface;
6.451 + * {@code false} otherwise.
6.452 + */
6.453 + public native boolean isInterface();
6.454 +
6.455 +
6.456 + /**
6.457 + * Determines if this {@code Class} object represents an array class.
6.458 + *
6.459 + * @return {@code true} if this object represents an array class;
6.460 + * {@code false} otherwise.
6.461 + * @since JDK1.1
6.462 + */
6.463 + public native boolean isArray();
6.464 +
6.465 +
6.466 + /**
6.467 + * Determines if the specified {@code Class} object represents a
6.468 + * primitive type.
6.469 + *
6.470 + * <p> There are nine predefined {@code Class} objects to represent
6.471 + * the eight primitive types and void. These are created by the Java
6.472 + * Virtual Machine, and have the same names as the primitive types that
6.473 + * they represent, namely {@code boolean}, {@code byte},
6.474 + * {@code char}, {@code short}, {@code int},
6.475 + * {@code long}, {@code float}, and {@code double}.
6.476 + *
6.477 + * <p> These objects may only be accessed via the following public static
6.478 + * final variables, and are the only {@code Class} objects for which
6.479 + * this method returns {@code true}.
6.480 + *
6.481 + * @return true if and only if this class represents a primitive type
6.482 + *
6.483 + * @see java.lang.Boolean#TYPE
6.484 + * @see java.lang.Character#TYPE
6.485 + * @see java.lang.Byte#TYPE
6.486 + * @see java.lang.Short#TYPE
6.487 + * @see java.lang.Integer#TYPE
6.488 + * @see java.lang.Long#TYPE
6.489 + * @see java.lang.Float#TYPE
6.490 + * @see java.lang.Double#TYPE
6.491 + * @see java.lang.Void#TYPE
6.492 + * @since JDK1.1
6.493 + */
6.494 + public native boolean isPrimitive();
6.495 +
6.496 + /**
6.497 + * Returns true if this {@code Class} object represents an annotation
6.498 + * type. Note that if this method returns true, {@link #isInterface()}
6.499 + * would also return true, as all annotation types are also interfaces.
6.500 + *
6.501 + * @return {@code true} if this class object represents an annotation
6.502 + * type; {@code false} otherwise
6.503 + * @since 1.5
6.504 + */
6.505 + public boolean isAnnotation() {
6.506 + return (getModifiers() & ANNOTATION) != 0;
6.507 + }
6.508 +
6.509 + /**
6.510 + * Returns {@code true} if this class is a synthetic class;
6.511 + * returns {@code false} otherwise.
6.512 + * @return {@code true} if and only if this class is a synthetic class as
6.513 + * defined by the Java Language Specification.
6.514 + * @since 1.5
6.515 + */
6.516 + public boolean isSynthetic() {
6.517 + return (getModifiers() & SYNTHETIC) != 0;
6.518 + }
6.519 +
6.520 + /**
6.521 + * Returns the name of the entity (class, interface, array class,
6.522 + * primitive type, or void) represented by this {@code Class} object,
6.523 + * as a {@code String}.
6.524 + *
6.525 + * <p> If this class object represents a reference type that is not an
6.526 + * array type then the binary name of the class is returned, as specified
6.527 + * by
6.528 + * <cite>The Java™ Language Specification</cite>.
6.529 + *
6.530 + * <p> If this class object represents a primitive type or void, then the
6.531 + * name returned is a {@code String} equal to the Java language
6.532 + * keyword corresponding to the primitive type or void.
6.533 + *
6.534 + * <p> If this class object represents a class of arrays, then the internal
6.535 + * form of the name consists of the name of the element type preceded by
6.536 + * one or more '{@code [}' characters representing the depth of the array
6.537 + * nesting. The encoding of element type names is as follows:
6.538 + *
6.539 + * <blockquote><table summary="Element types and encodings">
6.540 + * <tr><th> Element Type <th> <th> Encoding
6.541 + * <tr><td> boolean <td> <td align=center> Z
6.542 + * <tr><td> byte <td> <td align=center> B
6.543 + * <tr><td> char <td> <td align=center> C
6.544 + * <tr><td> class or interface
6.545 + * <td> <td align=center> L<i>classname</i>;
6.546 + * <tr><td> double <td> <td align=center> D
6.547 + * <tr><td> float <td> <td align=center> F
6.548 + * <tr><td> int <td> <td align=center> I
6.549 + * <tr><td> long <td> <td align=center> J
6.550 + * <tr><td> short <td> <td align=center> S
6.551 + * </table></blockquote>
6.552 + *
6.553 + * <p> The class or interface name <i>classname</i> is the binary name of
6.554 + * the class specified above.
6.555 + *
6.556 + * <p> Examples:
6.557 + * <blockquote><pre>
6.558 + * String.class.getName()
6.559 + * returns "java.lang.String"
6.560 + * byte.class.getName()
6.561 + * returns "byte"
6.562 + * (new Object[3]).getClass().getName()
6.563 + * returns "[Ljava.lang.Object;"
6.564 + * (new int[3][4][5][6][7][8][9]).getClass().getName()
6.565 + * returns "[[[[[[[I"
6.566 + * </pre></blockquote>
6.567 + *
6.568 + * @return the name of the class or interface
6.569 + * represented by this object.
6.570 + */
6.571 + public String getName() {
6.572 + String name = this.name;
6.573 + if (name == null)
6.574 + this.name = name = getName0();
6.575 + return name;
6.576 + }
6.577 +
6.578 + // cache the name to reduce the number of calls into the VM
6.579 + private transient String name;
6.580 + private native String getName0();
6.581 +
6.582 + /**
6.583 + * Returns the class loader for the class. Some implementations may use
6.584 + * null to represent the bootstrap class loader. This method will return
6.585 + * null in such implementations if this class was loaded by the bootstrap
6.586 + * class loader.
6.587 + *
6.588 + * <p> If a security manager is present, and the caller's class loader is
6.589 + * not null and the caller's class loader is not the same as or an ancestor of
6.590 + * the class loader for the class whose class loader is requested, then
6.591 + * this method calls the security manager's {@code checkPermission}
6.592 + * method with a {@code RuntimePermission("getClassLoader")}
6.593 + * permission to ensure it's ok to access the class loader for the class.
6.594 + *
6.595 + * <p>If this object
6.596 + * represents a primitive type or void, null is returned.
6.597 + *
6.598 + * @return the class loader that loaded the class or interface
6.599 + * represented by this object.
6.600 + * @throws SecurityException
6.601 + * if a security manager exists and its
6.602 + * {@code checkPermission} method denies
6.603 + * access to the class loader for the class.
6.604 + * @see java.lang.ClassLoader
6.605 + * @see SecurityManager#checkPermission
6.606 + * @see java.lang.RuntimePermission
6.607 + */
6.608 + public ClassLoader getClassLoader() {
6.609 + ClassLoader cl = getClassLoader0();
6.610 + if (cl == null)
6.611 + return null;
6.612 + SecurityManager sm = System.getSecurityManager();
6.613 + if (sm != null) {
6.614 + ClassLoader ccl = ClassLoader.getCallerClassLoader();
6.615 + if (ccl != null && ccl != cl && !cl.isAncestor(ccl)) {
6.616 + sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
6.617 + }
6.618 + }
6.619 + return cl;
6.620 + }
6.621 +
6.622 + // Package-private to allow ClassLoader access
6.623 + native ClassLoader getClassLoader0();
6.624 +
6.625 +
6.626 + /**
6.627 + * Returns an array of {@code TypeVariable} objects that represent the
6.628 + * type variables declared by the generic declaration represented by this
6.629 + * {@code GenericDeclaration} object, in declaration order. Returns an
6.630 + * array of length 0 if the underlying generic declaration declares no type
6.631 + * variables.
6.632 + *
6.633 + * @return an array of {@code TypeVariable} objects that represent
6.634 + * the type variables declared by this generic declaration
6.635 + * @throws java.lang.reflect.GenericSignatureFormatError if the generic
6.636 + * signature of this generic declaration does not conform to
6.637 + * the format specified in
6.638 + * <cite>The Java™ Virtual Machine Specification</cite>
6.639 + * @since 1.5
6.640 + */
6.641 + public TypeVariable<Class<T>>[] getTypeParameters() {
6.642 + if (getGenericSignature() != null)
6.643 + return (TypeVariable<Class<T>>[])getGenericInfo().getTypeParameters();
6.644 + else
6.645 + return (TypeVariable<Class<T>>[])new TypeVariable<?>[0];
6.646 + }
6.647 +
6.648 +
6.649 + /**
6.650 + * Returns the {@code Class} representing the superclass of the entity
6.651 + * (class, interface, primitive type or void) represented by this
6.652 + * {@code Class}. If this {@code Class} represents either the
6.653 + * {@code Object} class, an interface, a primitive type, or void, then
6.654 + * null is returned. If this object represents an array class then the
6.655 + * {@code Class} object representing the {@code Object} class is
6.656 + * returned.
6.657 + *
6.658 + * @return the superclass of the class represented by this object.
6.659 + */
6.660 + public native Class<? super T> getSuperclass();
6.661 +
6.662 +
6.663 + /**
6.664 + * Returns the {@code Type} representing the direct superclass of
6.665 + * the entity (class, interface, primitive type or void) represented by
6.666 + * this {@code Class}.
6.667 + *
6.668 + * <p>If the superclass is a parameterized type, the {@code Type}
6.669 + * object returned must accurately reflect the actual type
6.670 + * parameters used in the source code. The parameterized type
6.671 + * representing the superclass is created if it had not been
6.672 + * created before. See the declaration of {@link
6.673 + * java.lang.reflect.ParameterizedType ParameterizedType} for the
6.674 + * semantics of the creation process for parameterized types. If
6.675 + * this {@code Class} represents either the {@code Object}
6.676 + * class, an interface, a primitive type, or void, then null is
6.677 + * returned. If this object represents an array class then the
6.678 + * {@code Class} object representing the {@code Object} class is
6.679 + * returned.
6.680 + *
6.681 + * @throws java.lang.reflect.GenericSignatureFormatError if the generic
6.682 + * class signature does not conform to the format specified in
6.683 + * <cite>The Java™ Virtual Machine Specification</cite>
6.684 + * @throws TypeNotPresentException if the generic superclass
6.685 + * refers to a non-existent type declaration
6.686 + * @throws java.lang.reflect.MalformedParameterizedTypeException if the
6.687 + * generic superclass refers to a parameterized type that cannot be
6.688 + * instantiated for any reason
6.689 + * @return the superclass of the class represented by this object
6.690 + * @since 1.5
6.691 + */
6.692 + public Type getGenericSuperclass() {
6.693 + if (getGenericSignature() != null) {
6.694 + // Historical irregularity:
6.695 + // Generic signature marks interfaces with superclass = Object
6.696 + // but this API returns null for interfaces
6.697 + if (isInterface())
6.698 + return null;
6.699 + return getGenericInfo().getSuperclass();
6.700 + } else
6.701 + return getSuperclass();
6.702 + }
6.703 +
6.704 + /**
6.705 + * Gets the package for this class. The class loader of this class is used
6.706 + * to find the package. If the class was loaded by the bootstrap class
6.707 + * loader the set of packages loaded from CLASSPATH is searched to find the
6.708 + * package of the class. Null is returned if no package object was created
6.709 + * by the class loader of this class.
6.710 + *
6.711 + * <p> Packages have attributes for versions and specifications only if the
6.712 + * information was defined in the manifests that accompany the classes, and
6.713 + * if the class loader created the package instance with the attributes
6.714 + * from the manifest.
6.715 + *
6.716 + * @return the package of the class, or null if no package
6.717 + * information is available from the archive or codebase.
6.718 + */
6.719 + public Package getPackage() {
6.720 + return Package.getPackage(this);
6.721 + }
6.722 +
6.723 +
6.724 + /**
6.725 + * Determines the interfaces implemented by the class or interface
6.726 + * represented by this object.
6.727 + *
6.728 + * <p> If this object represents a class, the return value is an array
6.729 + * containing objects representing all interfaces implemented by the
6.730 + * class. The order of the interface objects in the array corresponds to
6.731 + * the order of the interface names in the {@code implements} clause
6.732 + * of the declaration of the class represented by this object. For
6.733 + * example, given the declaration:
6.734 + * <blockquote>
6.735 + * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
6.736 + * </blockquote>
6.737 + * suppose the value of {@code s} is an instance of
6.738 + * {@code Shimmer}; the value of the expression:
6.739 + * <blockquote>
6.740 + * {@code s.getClass().getInterfaces()[0]}
6.741 + * </blockquote>
6.742 + * is the {@code Class} object that represents interface
6.743 + * {@code FloorWax}; and the value of:
6.744 + * <blockquote>
6.745 + * {@code s.getClass().getInterfaces()[1]}
6.746 + * </blockquote>
6.747 + * is the {@code Class} object that represents interface
6.748 + * {@code DessertTopping}.
6.749 + *
6.750 + * <p> If this object represents an interface, the array contains objects
6.751 + * representing all interfaces extended by the interface. The order of the
6.752 + * interface objects in the array corresponds to the order of the interface
6.753 + * names in the {@code extends} clause of the declaration of the
6.754 + * interface represented by this object.
6.755 + *
6.756 + * <p> If this object represents a class or interface that implements no
6.757 + * interfaces, the method returns an array of length 0.
6.758 + *
6.759 + * <p> If this object represents a primitive type or void, the method
6.760 + * returns an array of length 0.
6.761 + *
6.762 + * @return an array of interfaces implemented by this class.
6.763 + */
6.764 + public native Class<?>[] getInterfaces();
6.765 +
6.766 + /**
6.767 + * Returns the {@code Type}s representing the interfaces
6.768 + * directly implemented by the class or interface represented by
6.769 + * this object.
6.770 + *
6.771 + * <p>If a superinterface is a parameterized type, the
6.772 + * {@code Type} object returned for it must accurately reflect
6.773 + * the actual type parameters used in the source code. The
6.774 + * parameterized type representing each superinterface is created
6.775 + * if it had not been created before. See the declaration of
6.776 + * {@link java.lang.reflect.ParameterizedType ParameterizedType}
6.777 + * for the semantics of the creation process for parameterized
6.778 + * types.
6.779 + *
6.780 + * <p> If this object represents a class, the return value is an
6.781 + * array containing objects representing all interfaces
6.782 + * implemented by the class. The order of the interface objects in
6.783 + * the array corresponds to the order of the interface names in
6.784 + * the {@code implements} clause of the declaration of the class
6.785 + * represented by this object. In the case of an array class, the
6.786 + * interfaces {@code Cloneable} and {@code Serializable} are
6.787 + * returned in that order.
6.788 + *
6.789 + * <p>If this object represents an interface, the array contains
6.790 + * objects representing all interfaces directly extended by the
6.791 + * interface. The order of the interface objects in the array
6.792 + * corresponds to the order of the interface names in the
6.793 + * {@code extends} clause of the declaration of the interface
6.794 + * represented by this object.
6.795 + *
6.796 + * <p>If this object represents a class or interface that
6.797 + * implements no interfaces, the method returns an array of length
6.798 + * 0.
6.799 + *
6.800 + * <p>If this object represents a primitive type or void, the
6.801 + * method returns an array of length 0.
6.802 + *
6.803 + * @throws java.lang.reflect.GenericSignatureFormatError
6.804 + * if the generic class signature does not conform to the format
6.805 + * specified in
6.806 + * <cite>The Java™ Virtual Machine Specification</cite>
6.807 + * @throws TypeNotPresentException if any of the generic
6.808 + * superinterfaces refers to a non-existent type declaration
6.809 + * @throws java.lang.reflect.MalformedParameterizedTypeException
6.810 + * if any of the generic superinterfaces refer to a parameterized
6.811 + * type that cannot be instantiated for any reason
6.812 + * @return an array of interfaces implemented by this class
6.813 + * @since 1.5
6.814 + */
6.815 + public Type[] getGenericInterfaces() {
6.816 + if (getGenericSignature() != null)
6.817 + return getGenericInfo().getSuperInterfaces();
6.818 + else
6.819 + return getInterfaces();
6.820 + }
6.821 +
6.822 +
6.823 + /**
6.824 + * Returns the {@code Class} representing the component type of an
6.825 + * array. If this class does not represent an array class this method
6.826 + * returns null.
6.827 + *
6.828 + * @return the {@code Class} representing the component type of this
6.829 + * class if this class is an array
6.830 + * @see java.lang.reflect.Array
6.831 + * @since JDK1.1
6.832 + */
6.833 + public native Class<?> getComponentType();
6.834 +
6.835 +
6.836 + /**
6.837 + * Returns the Java language modifiers for this class or interface, encoded
6.838 + * in an integer. The modifiers consist of the Java Virtual Machine's
6.839 + * constants for {@code public}, {@code protected},
6.840 + * {@code private}, {@code final}, {@code static},
6.841 + * {@code abstract} and {@code interface}; they should be decoded
6.842 + * using the methods of class {@code Modifier}.
6.843 + *
6.844 + * <p> If the underlying class is an array class, then its
6.845 + * {@code public}, {@code private} and {@code protected}
6.846 + * modifiers are the same as those of its component type. If this
6.847 + * {@code Class} represents a primitive type or void, its
6.848 + * {@code public} modifier is always {@code true}, and its
6.849 + * {@code protected} and {@code private} modifiers are always
6.850 + * {@code false}. If this object represents an array class, a
6.851 + * primitive type or void, then its {@code final} modifier is always
6.852 + * {@code true} and its interface modifier is always
6.853 + * {@code false}. The values of its other modifiers are not determined
6.854 + * by this specification.
6.855 + *
6.856 + * <p> The modifier encodings are defined in <em>The Java Virtual Machine
6.857 + * Specification</em>, table 4.1.
6.858 + *
6.859 + * @return the {@code int} representing the modifiers for this class
6.860 + * @see java.lang.reflect.Modifier
6.861 + * @since JDK1.1
6.862 + */
6.863 + public native int getModifiers();
6.864 +
6.865 +
6.866 + /**
6.867 + * Gets the signers of this class.
6.868 + *
6.869 + * @return the signers of this class, or null if there are no signers. In
6.870 + * particular, this method returns null if this object represents
6.871 + * a primitive type or void.
6.872 + * @since JDK1.1
6.873 + */
6.874 + public native Object[] getSigners();
6.875 +
6.876 +
6.877 + /**
6.878 + * Set the signers of this class.
6.879 + */
6.880 + native void setSigners(Object[] signers);
6.881 +
6.882 +
6.883 + /**
6.884 + * If this {@code Class} object represents a local or anonymous
6.885 + * class within a method, returns a {@link
6.886 + * java.lang.reflect.Method Method} object representing the
6.887 + * immediately enclosing method of the underlying class. Returns
6.888 + * {@code null} otherwise.
6.889 + *
6.890 + * In particular, this method returns {@code null} if the underlying
6.891 + * class is a local or anonymous class immediately enclosed by a type
6.892 + * declaration, instance initializer or static initializer.
6.893 + *
6.894 + * @return the immediately enclosing method of the underlying class, if
6.895 + * that class is a local or anonymous class; otherwise {@code null}.
6.896 + * @since 1.5
6.897 + */
6.898 + public Method getEnclosingMethod() {
6.899 + EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
6.900 +
6.901 + if (enclosingInfo == null)
6.902 + return null;
6.903 + else {
6.904 + if (!enclosingInfo.isMethod())
6.905 + return null;
6.906 +
6.907 + MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(),
6.908 + getFactory());
6.909 + Class<?> returnType = toClass(typeInfo.getReturnType());
6.910 + Type [] parameterTypes = typeInfo.getParameterTypes();
6.911 + Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
6.912 +
6.913 + // Convert Types to Classes; returned types *should*
6.914 + // be class objects since the methodDescriptor's used
6.915 + // don't have generics information
6.916 + for(int i = 0; i < parameterClasses.length; i++)
6.917 + parameterClasses[i] = toClass(parameterTypes[i]);
6.918 +
6.919 + /*
6.920 + * Loop over all declared methods; match method name,
6.921 + * number of and type of parameters, *and* return
6.922 + * type. Matching return type is also necessary
6.923 + * because of covariant returns, etc.
6.924 + */
6.925 + for(Method m: enclosingInfo.getEnclosingClass().getDeclaredMethods()) {
6.926 + if (m.getName().equals(enclosingInfo.getName()) ) {
6.927 + Class<?>[] candidateParamClasses = m.getParameterTypes();
6.928 + if (candidateParamClasses.length == parameterClasses.length) {
6.929 + boolean matches = true;
6.930 + for(int i = 0; i < candidateParamClasses.length; i++) {
6.931 + if (!candidateParamClasses[i].equals(parameterClasses[i])) {
6.932 + matches = false;
6.933 + break;
6.934 + }
6.935 + }
6.936 +
6.937 + if (matches) { // finally, check return type
6.938 + if (m.getReturnType().equals(returnType) )
6.939 + return m;
6.940 + }
6.941 + }
6.942 + }
6.943 + }
6.944 +
6.945 + throw new InternalError("Enclosing method not found");
6.946 + }
6.947 + }
6.948 +
6.949 + private native Object[] getEnclosingMethod0();
6.950 +
6.951 + private EnclosingMethodInfo getEnclosingMethodInfo() {
6.952 + Object[] enclosingInfo = getEnclosingMethod0();
6.953 + if (enclosingInfo == null)
6.954 + return null;
6.955 + else {
6.956 + return new EnclosingMethodInfo(enclosingInfo);
6.957 + }
6.958 + }
6.959 +
6.960 + private final static class EnclosingMethodInfo {
6.961 + private Class<?> enclosingClass;
6.962 + private String name;
6.963 + private String descriptor;
6.964 +
6.965 + private EnclosingMethodInfo(Object[] enclosingInfo) {
6.966 + if (enclosingInfo.length != 3)
6.967 + throw new InternalError("Malformed enclosing method information");
6.968 + try {
6.969 + // The array is expected to have three elements:
6.970 +
6.971 + // the immediately enclosing class
6.972 + enclosingClass = (Class<?>) enclosingInfo[0];
6.973 + assert(enclosingClass != null);
6.974 +
6.975 + // the immediately enclosing method or constructor's
6.976 + // name (can be null).
6.977 + name = (String) enclosingInfo[1];
6.978 +
6.979 + // the immediately enclosing method or constructor's
6.980 + // descriptor (null iff name is).
6.981 + descriptor = (String) enclosingInfo[2];
6.982 + assert((name != null && descriptor != null) || name == descriptor);
6.983 + } catch (ClassCastException cce) {
6.984 + throw new InternalError("Invalid type in enclosing method information");
6.985 + }
6.986 + }
6.987 +
6.988 + boolean isPartial() {
6.989 + return enclosingClass == null || name == null || descriptor == null;
6.990 + }
6.991 +
6.992 + boolean isConstructor() { return !isPartial() && "<init>".equals(name); }
6.993 +
6.994 + boolean isMethod() { return !isPartial() && !isConstructor() && !"<clinit>".equals(name); }
6.995 +
6.996 + Class<?> getEnclosingClass() { return enclosingClass; }
6.997 +
6.998 + String getName() { return name; }
6.999 +
6.1000 + String getDescriptor() { return descriptor; }
6.1001 +
6.1002 + }
6.1003 +
6.1004 + private static Class<?> toClass(Type o) {
6.1005 + if (o instanceof GenericArrayType)
6.1006 + return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()),
6.1007 + 0)
6.1008 + .getClass();
6.1009 + return (Class<?>)o;
6.1010 + }
6.1011 +
6.1012 + /**
6.1013 + * If this {@code Class} object represents a local or anonymous
6.1014 + * class within a constructor, returns a {@link
6.1015 + * java.lang.reflect.Constructor Constructor} object representing
6.1016 + * the immediately enclosing constructor of the underlying
6.1017 + * class. Returns {@code null} otherwise. In particular, this
6.1018 + * method returns {@code null} if the underlying class is a local
6.1019 + * or anonymous class immediately enclosed by a type declaration,
6.1020 + * instance initializer or static initializer.
6.1021 + *
6.1022 + * @return the immediately enclosing constructor of the underlying class, if
6.1023 + * that class is a local or anonymous class; otherwise {@code null}.
6.1024 + * @since 1.5
6.1025 + */
6.1026 + public Constructor<?> getEnclosingConstructor() {
6.1027 + EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
6.1028 +
6.1029 + if (enclosingInfo == null)
6.1030 + return null;
6.1031 + else {
6.1032 + if (!enclosingInfo.isConstructor())
6.1033 + return null;
6.1034 +
6.1035 + ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(),
6.1036 + getFactory());
6.1037 + Type [] parameterTypes = typeInfo.getParameterTypes();
6.1038 + Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
6.1039 +
6.1040 + // Convert Types to Classes; returned types *should*
6.1041 + // be class objects since the methodDescriptor's used
6.1042 + // don't have generics information
6.1043 + for(int i = 0; i < parameterClasses.length; i++)
6.1044 + parameterClasses[i] = toClass(parameterTypes[i]);
6.1045 +
6.1046 + /*
6.1047 + * Loop over all declared constructors; match number
6.1048 + * of and type of parameters.
6.1049 + */
6.1050 + for(Constructor<?> c: enclosingInfo.getEnclosingClass().getDeclaredConstructors()) {
6.1051 + Class<?>[] candidateParamClasses = c.getParameterTypes();
6.1052 + if (candidateParamClasses.length == parameterClasses.length) {
6.1053 + boolean matches = true;
6.1054 + for(int i = 0; i < candidateParamClasses.length; i++) {
6.1055 + if (!candidateParamClasses[i].equals(parameterClasses[i])) {
6.1056 + matches = false;
6.1057 + break;
6.1058 + }
6.1059 + }
6.1060 +
6.1061 + if (matches)
6.1062 + return c;
6.1063 + }
6.1064 + }
6.1065 +
6.1066 + throw new InternalError("Enclosing constructor not found");
6.1067 + }
6.1068 + }
6.1069 +
6.1070 +
6.1071 + /**
6.1072 + * If the class or interface represented by this {@code Class} object
6.1073 + * is a member of another class, returns the {@code Class} object
6.1074 + * representing the class in which it was declared. This method returns
6.1075 + * null if this class or interface is not a member of any other class. If
6.1076 + * this {@code Class} object represents an array class, a primitive
6.1077 + * type, or void,then this method returns null.
6.1078 + *
6.1079 + * @return the declaring class for this class
6.1080 + * @since JDK1.1
6.1081 + */
6.1082 + public native Class<?> getDeclaringClass();
6.1083 +
6.1084 +
6.1085 + /**
6.1086 + * Returns the immediately enclosing class of the underlying
6.1087 + * class. If the underlying class is a top level class this
6.1088 + * method returns {@code null}.
6.1089 + * @return the immediately enclosing class of the underlying class
6.1090 + * @since 1.5
6.1091 + */
6.1092 + public Class<?> getEnclosingClass() {
6.1093 + // There are five kinds of classes (or interfaces):
6.1094 + // a) Top level classes
6.1095 + // b) Nested classes (static member classes)
6.1096 + // c) Inner classes (non-static member classes)
6.1097 + // d) Local classes (named classes declared within a method)
6.1098 + // e) Anonymous classes
6.1099 +
6.1100 +
6.1101 + // JVM Spec 4.8.6: A class must have an EnclosingMethod
6.1102 + // attribute if and only if it is a local class or an
6.1103 + // anonymous class.
6.1104 + EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
6.1105 +
6.1106 + if (enclosingInfo == null) {
6.1107 + // This is a top level or a nested class or an inner class (a, b, or c)
6.1108 + return getDeclaringClass();
6.1109 + } else {
6.1110 + Class<?> enclosingClass = enclosingInfo.getEnclosingClass();
6.1111 + // This is a local class or an anonymous class (d or e)
6.1112 + if (enclosingClass == this || enclosingClass == null)
6.1113 + throw new InternalError("Malformed enclosing method information");
6.1114 + else
6.1115 + return enclosingClass;
6.1116 + }
6.1117 + }
6.1118 +
6.1119 + /**
6.1120 + * Returns the simple name of the underlying class as given in the
6.1121 + * source code. Returns an empty string if the underlying class is
6.1122 + * anonymous.
6.1123 + *
6.1124 + * <p>The simple name of an array is the simple name of the
6.1125 + * component type with "[]" appended. In particular the simple
6.1126 + * name of an array whose component type is anonymous is "[]".
6.1127 + *
6.1128 + * @return the simple name of the underlying class
6.1129 + * @since 1.5
6.1130 + */
6.1131 + public String getSimpleName() {
6.1132 + if (isArray())
6.1133 + return getComponentType().getSimpleName()+"[]";
6.1134 +
6.1135 + String simpleName = getSimpleBinaryName();
6.1136 + if (simpleName == null) { // top level class
6.1137 + simpleName = getName();
6.1138 + return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
6.1139 + }
6.1140 + // According to JLS3 "Binary Compatibility" (13.1) the binary
6.1141 + // name of non-package classes (not top level) is the binary
6.1142 + // name of the immediately enclosing class followed by a '$' followed by:
6.1143 + // (for nested and inner classes): the simple name.
6.1144 + // (for local classes): 1 or more digits followed by the simple name.
6.1145 + // (for anonymous classes): 1 or more digits.
6.1146 +
6.1147 + // Since getSimpleBinaryName() will strip the binary name of
6.1148 + // the immediatly enclosing class, we are now looking at a
6.1149 + // string that matches the regular expression "\$[0-9]*"
6.1150 + // followed by a simple name (considering the simple of an
6.1151 + // anonymous class to be the empty string).
6.1152 +
6.1153 + // Remove leading "\$[0-9]*" from the name
6.1154 + int length = simpleName.length();
6.1155 + if (length < 1 || simpleName.charAt(0) != '$')
6.1156 + throw new InternalError("Malformed class name");
6.1157 + int index = 1;
6.1158 + while (index < length && isAsciiDigit(simpleName.charAt(index)))
6.1159 + index++;
6.1160 + // Eventually, this is the empty string iff this is an anonymous class
6.1161 + return simpleName.substring(index);
6.1162 + }
6.1163 +
6.1164 + /**
6.1165 + * Character.isDigit answers {@code true} to some non-ascii
6.1166 + * digits. This one does not.
6.1167 + */
6.1168 + private static boolean isAsciiDigit(char c) {
6.1169 + return '0' <= c && c <= '9';
6.1170 + }
6.1171 +
6.1172 + /**
6.1173 + * Returns the canonical name of the underlying class as
6.1174 + * defined by the Java Language Specification. Returns null if
6.1175 + * the underlying class does not have a canonical name (i.e., if
6.1176 + * it is a local or anonymous class or an array whose component
6.1177 + * type does not have a canonical name).
6.1178 + * @return the canonical name of the underlying class if it exists, and
6.1179 + * {@code null} otherwise.
6.1180 + * @since 1.5
6.1181 + */
6.1182 + public String getCanonicalName() {
6.1183 + if (isArray()) {
6.1184 + String canonicalName = getComponentType().getCanonicalName();
6.1185 + if (canonicalName != null)
6.1186 + return canonicalName + "[]";
6.1187 + else
6.1188 + return null;
6.1189 + }
6.1190 + if (isLocalOrAnonymousClass())
6.1191 + return null;
6.1192 + Class<?> enclosingClass = getEnclosingClass();
6.1193 + if (enclosingClass == null) { // top level class
6.1194 + return getName();
6.1195 + } else {
6.1196 + String enclosingName = enclosingClass.getCanonicalName();
6.1197 + if (enclosingName == null)
6.1198 + return null;
6.1199 + return enclosingName + "." + getSimpleName();
6.1200 + }
6.1201 + }
6.1202 +
6.1203 + /**
6.1204 + * Returns {@code true} if and only if the underlying class
6.1205 + * is an anonymous class.
6.1206 + *
6.1207 + * @return {@code true} if and only if this class is an anonymous class.
6.1208 + * @since 1.5
6.1209 + */
6.1210 + public boolean isAnonymousClass() {
6.1211 + return "".equals(getSimpleName());
6.1212 + }
6.1213 +
6.1214 + /**
6.1215 + * Returns {@code true} if and only if the underlying class
6.1216 + * is a local class.
6.1217 + *
6.1218 + * @return {@code true} if and only if this class is a local class.
6.1219 + * @since 1.5
6.1220 + */
6.1221 + public boolean isLocalClass() {
6.1222 + return isLocalOrAnonymousClass() && !isAnonymousClass();
6.1223 + }
6.1224 +
6.1225 + /**
6.1226 + * Returns {@code true} if and only if the underlying class
6.1227 + * is a member class.
6.1228 + *
6.1229 + * @return {@code true} if and only if this class is a member class.
6.1230 + * @since 1.5
6.1231 + */
6.1232 + public boolean isMemberClass() {
6.1233 + return getSimpleBinaryName() != null && !isLocalOrAnonymousClass();
6.1234 + }
6.1235 +
6.1236 + /**
6.1237 + * Returns the "simple binary name" of the underlying class, i.e.,
6.1238 + * the binary name without the leading enclosing class name.
6.1239 + * Returns {@code null} if the underlying class is a top level
6.1240 + * class.
6.1241 + */
6.1242 + private String getSimpleBinaryName() {
6.1243 + Class<?> enclosingClass = getEnclosingClass();
6.1244 + if (enclosingClass == null) // top level class
6.1245 + return null;
6.1246 + // Otherwise, strip the enclosing class' name
6.1247 + try {
6.1248 + return getName().substring(enclosingClass.getName().length());
6.1249 + } catch (IndexOutOfBoundsException ex) {
6.1250 + throw new InternalError("Malformed class name");
6.1251 + }
6.1252 + }
6.1253 +
6.1254 + /**
6.1255 + * Returns {@code true} if this is a local class or an anonymous
6.1256 + * class. Returns {@code false} otherwise.
6.1257 + */
6.1258 + private boolean isLocalOrAnonymousClass() {
6.1259 + // JVM Spec 4.8.6: A class must have an EnclosingMethod
6.1260 + // attribute if and only if it is a local class or an
6.1261 + // anonymous class.
6.1262 + return getEnclosingMethodInfo() != null;
6.1263 + }
6.1264 +
6.1265 + /**
6.1266 + * Returns an array containing {@code Class} objects representing all
6.1267 + * the public classes and interfaces that are members of the class
6.1268 + * represented by this {@code Class} object. This includes public
6.1269 + * class and interface members inherited from superclasses and public class
6.1270 + * and interface members declared by the class. This method returns an
6.1271 + * array of length 0 if this {@code Class} object has no public member
6.1272 + * classes or interfaces. This method also returns an array of length 0 if
6.1273 + * this {@code Class} object represents a primitive type, an array
6.1274 + * class, or void.
6.1275 + *
6.1276 + * @return the array of {@code Class} objects representing the public
6.1277 + * members of this class
6.1278 + * @exception SecurityException
6.1279 + * If a security manager, <i>s</i>, is present and any of the
6.1280 + * following conditions is met:
6.1281 + *
6.1282 + * <ul>
6.1283 + *
6.1284 + * <li> invocation of
6.1285 + * {@link SecurityManager#checkMemberAccess
6.1286 + * s.checkMemberAccess(this, Member.PUBLIC)} method
6.1287 + * denies access to the classes within this class
6.1288 + *
6.1289 + * <li> the caller's class loader is not the same as or an
6.1290 + * ancestor of the class loader for the current class and
6.1291 + * invocation of {@link SecurityManager#checkPackageAccess
6.1292 + * s.checkPackageAccess()} denies access to the package
6.1293 + * of this class
6.1294 + *
6.1295 + * </ul>
6.1296 + *
6.1297 + * @since JDK1.1
6.1298 + */
6.1299 + public Class<?>[] getClasses() {
6.1300 + // be very careful not to change the stack depth of this
6.1301 + // checkMemberAccess call for security reasons
6.1302 + // see java.lang.SecurityManager.checkMemberAccess
6.1303 + checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
6.1304 +
6.1305 + // Privileged so this implementation can look at DECLARED classes,
6.1306 + // something the caller might not have privilege to do. The code here
6.1307 + // is allowed to look at DECLARED classes because (1) it does not hand
6.1308 + // out anything other than public members and (2) public member access
6.1309 + // has already been ok'd by the SecurityManager.
6.1310 +
6.1311 + return java.security.AccessController.doPrivileged(
6.1312 + new java.security.PrivilegedAction<Class<?>[]>() {
6.1313 + public Class[] run() {
6.1314 + List<Class<?>> list = new ArrayList<>();
6.1315 + Class<?> currentClass = Class.this;
6.1316 + while (currentClass != null) {
6.1317 + Class<?>[] members = currentClass.getDeclaredClasses();
6.1318 + for (int i = 0; i < members.length; i++) {
6.1319 + if (Modifier.isPublic(members[i].getModifiers())) {
6.1320 + list.add(members[i]);
6.1321 + }
6.1322 + }
6.1323 + currentClass = currentClass.getSuperclass();
6.1324 + }
6.1325 + return list.toArray(new Class[0]);
6.1326 + }
6.1327 + });
6.1328 + }
6.1329 +
6.1330 +
6.1331 + /**
6.1332 + * Returns an array containing {@code Field} objects reflecting all
6.1333 + * the accessible public fields of the class or interface represented by
6.1334 + * this {@code Class} object. The elements in the array returned are
6.1335 + * not sorted and are not in any particular order. This method returns an
6.1336 + * array of length 0 if the class or interface has no accessible public
6.1337 + * fields, or if it represents an array class, a primitive type, or void.
6.1338 + *
6.1339 + * <p> Specifically, if this {@code Class} object represents a class,
6.1340 + * this method returns the public fields of this class and of all its
6.1341 + * superclasses. If this {@code Class} object represents an
6.1342 + * interface, this method returns the fields of this interface and of all
6.1343 + * its superinterfaces.
6.1344 + *
6.1345 + * <p> The implicit length field for array class is not reflected by this
6.1346 + * method. User code should use the methods of class {@code Array} to
6.1347 + * manipulate arrays.
6.1348 + *
6.1349 + * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
6.1350 + *
6.1351 + * @return the array of {@code Field} objects representing the
6.1352 + * public fields
6.1353 + * @exception SecurityException
6.1354 + * If a security manager, <i>s</i>, is present and any of the
6.1355 + * following conditions is met:
6.1356 + *
6.1357 + * <ul>
6.1358 + *
6.1359 + * <li> invocation of
6.1360 + * {@link SecurityManager#checkMemberAccess
6.1361 + * s.checkMemberAccess(this, Member.PUBLIC)} denies
6.1362 + * access to the fields within this class
6.1363 + *
6.1364 + * <li> the caller's class loader is not the same as or an
6.1365 + * ancestor of the class loader for the current class and
6.1366 + * invocation of {@link SecurityManager#checkPackageAccess
6.1367 + * s.checkPackageAccess()} denies access to the package
6.1368 + * of this class
6.1369 + *
6.1370 + * </ul>
6.1371 + *
6.1372 + * @since JDK1.1
6.1373 + */
6.1374 + public Field[] getFields() throws SecurityException {
6.1375 + // be very careful not to change the stack depth of this
6.1376 + // checkMemberAccess call for security reasons
6.1377 + // see java.lang.SecurityManager.checkMemberAccess
6.1378 + checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
6.1379 + return copyFields(privateGetPublicFields(null));
6.1380 + }
6.1381 +
6.1382 +
6.1383 + /**
6.1384 + * Returns an array containing {@code Method} objects reflecting all
6.1385 + * the public <em>member</em> methods of the class or interface represented
6.1386 + * by this {@code Class} object, including those declared by the class
6.1387 + * or interface and those inherited from superclasses and
6.1388 + * superinterfaces. Array classes return all the (public) member methods
6.1389 + * inherited from the {@code Object} class. The elements in the array
6.1390 + * returned are not sorted and are not in any particular order. This
6.1391 + * method returns an array of length 0 if this {@code Class} object
6.1392 + * represents a class or interface that has no public member methods, or if
6.1393 + * this {@code Class} object represents a primitive type or void.
6.1394 + *
6.1395 + * <p> The class initialization method {@code <clinit>} is not
6.1396 + * included in the returned array. If the class declares multiple public
6.1397 + * member methods with the same parameter types, they are all included in
6.1398 + * the returned array.
6.1399 + *
6.1400 + * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
6.1401 + *
6.1402 + * @return the array of {@code Method} objects representing the
6.1403 + * public methods of this class
6.1404 + * @exception SecurityException
6.1405 + * If a security manager, <i>s</i>, is present and any of the
6.1406 + * following conditions is met:
6.1407 + *
6.1408 + * <ul>
6.1409 + *
6.1410 + * <li> invocation of
6.1411 + * {@link SecurityManager#checkMemberAccess
6.1412 + * s.checkMemberAccess(this, Member.PUBLIC)} denies
6.1413 + * access to the methods within this class
6.1414 + *
6.1415 + * <li> the caller's class loader is not the same as or an
6.1416 + * ancestor of the class loader for the current class and
6.1417 + * invocation of {@link SecurityManager#checkPackageAccess
6.1418 + * s.checkPackageAccess()} denies access to the package
6.1419 + * of this class
6.1420 + *
6.1421 + * </ul>
6.1422 + *
6.1423 + * @since JDK1.1
6.1424 + */
6.1425 + public Method[] getMethods() throws SecurityException {
6.1426 + // be very careful not to change the stack depth of this
6.1427 + // checkMemberAccess call for security reasons
6.1428 + // see java.lang.SecurityManager.checkMemberAccess
6.1429 + checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
6.1430 + return copyMethods(privateGetPublicMethods());
6.1431 + }
6.1432 +
6.1433 +
6.1434 + /**
6.1435 + * Returns an array containing {@code Constructor} objects reflecting
6.1436 + * all the public constructors of the class represented by this
6.1437 + * {@code Class} object. An array of length 0 is returned if the
6.1438 + * class has no public constructors, or if the class is an array class, or
6.1439 + * if the class reflects a primitive type or void.
6.1440 + *
6.1441 + * Note that while this method returns an array of {@code
6.1442 + * Constructor<T>} objects (that is an array of constructors from
6.1443 + * this class), the return type of this method is {@code
6.1444 + * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
6.1445 + * might be expected. This less informative return type is
6.1446 + * necessary since after being returned from this method, the
6.1447 + * array could be modified to hold {@code Constructor} objects for
6.1448 + * different classes, which would violate the type guarantees of
6.1449 + * {@code Constructor<T>[]}.
6.1450 + *
6.1451 + * @return the array of {@code Constructor} objects representing the
6.1452 + * public constructors of this class
6.1453 + * @exception SecurityException
6.1454 + * If a security manager, <i>s</i>, is present and any of the
6.1455 + * following conditions is met:
6.1456 + *
6.1457 + * <ul>
6.1458 + *
6.1459 + * <li> invocation of
6.1460 + * {@link SecurityManager#checkMemberAccess
6.1461 + * s.checkMemberAccess(this, Member.PUBLIC)} denies
6.1462 + * access to the constructors within this class
6.1463 + *
6.1464 + * <li> the caller's class loader is not the same as or an
6.1465 + * ancestor of the class loader for the current class and
6.1466 + * invocation of {@link SecurityManager#checkPackageAccess
6.1467 + * s.checkPackageAccess()} denies access to the package
6.1468 + * of this class
6.1469 + *
6.1470 + * </ul>
6.1471 + *
6.1472 + * @since JDK1.1
6.1473 + */
6.1474 + public Constructor<?>[] getConstructors() throws SecurityException {
6.1475 + // be very careful not to change the stack depth of this
6.1476 + // checkMemberAccess call for security reasons
6.1477 + // see java.lang.SecurityManager.checkMemberAccess
6.1478 + checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
6.1479 + return copyConstructors(privateGetDeclaredConstructors(true));
6.1480 + }
6.1481 +
6.1482 +
6.1483 + /**
6.1484 + * Returns a {@code Field} object that reflects the specified public
6.1485 + * member field of the class or interface represented by this
6.1486 + * {@code Class} object. The {@code name} parameter is a
6.1487 + * {@code String} specifying the simple name of the desired field.
6.1488 + *
6.1489 + * <p> The field to be reflected is determined by the algorithm that
6.1490 + * follows. Let C be the class represented by this object:
6.1491 + * <OL>
6.1492 + * <LI> If C declares a public field with the name specified, that is the
6.1493 + * field to be reflected.</LI>
6.1494 + * <LI> If no field was found in step 1 above, this algorithm is applied
6.1495 + * recursively to each direct superinterface of C. The direct
6.1496 + * superinterfaces are searched in the order they were declared.</LI>
6.1497 + * <LI> If no field was found in steps 1 and 2 above, and C has a
6.1498 + * superclass S, then this algorithm is invoked recursively upon S.
6.1499 + * If C has no superclass, then a {@code NoSuchFieldException}
6.1500 + * is thrown.</LI>
6.1501 + * </OL>
6.1502 + *
6.1503 + * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
6.1504 + *
6.1505 + * @param name the field name
6.1506 + * @return the {@code Field} object of this class specified by
6.1507 + * {@code name}
6.1508 + * @exception NoSuchFieldException if a field with the specified name is
6.1509 + * not found.
6.1510 + * @exception NullPointerException if {@code name} is {@code null}
6.1511 + * @exception SecurityException
6.1512 + * If a security manager, <i>s</i>, is present and any of the
6.1513 + * following conditions is met:
6.1514 + *
6.1515 + * <ul>
6.1516 + *
6.1517 + * <li> invocation of
6.1518 + * {@link SecurityManager#checkMemberAccess
6.1519 + * s.checkMemberAccess(this, Member.PUBLIC)} denies
6.1520 + * access to the field
6.1521 + *
6.1522 + * <li> the caller's class loader is not the same as or an
6.1523 + * ancestor of the class loader for the current class and
6.1524 + * invocation of {@link SecurityManager#checkPackageAccess
6.1525 + * s.checkPackageAccess()} denies access to the package
6.1526 + * of this class
6.1527 + *
6.1528 + * </ul>
6.1529 + *
6.1530 + * @since JDK1.1
6.1531 + */
6.1532 + public Field getField(String name)
6.1533 + throws NoSuchFieldException, SecurityException {
6.1534 + // be very careful not to change the stack depth of this
6.1535 + // checkMemberAccess call for security reasons
6.1536 + // see java.lang.SecurityManager.checkMemberAccess
6.1537 + checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
6.1538 + Field field = getField0(name);
6.1539 + if (field == null) {
6.1540 + throw new NoSuchFieldException(name);
6.1541 + }
6.1542 + return field;
6.1543 + }
6.1544 +
6.1545 +
6.1546 + /**
6.1547 + * Returns a {@code Method} object that reflects the specified public
6.1548 + * member method of the class or interface represented by this
6.1549 + * {@code Class} object. The {@code name} parameter is a
6.1550 + * {@code String} specifying the simple name of the desired method. The
6.1551 + * {@code parameterTypes} parameter is an array of {@code Class}
6.1552 + * objects that identify the method's formal parameter types, in declared
6.1553 + * order. If {@code parameterTypes} is {@code null}, it is
6.1554 + * treated as if it were an empty array.
6.1555 + *
6.1556 + * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
6.1557 + * {@code NoSuchMethodException} is raised. Otherwise, the method to
6.1558 + * be reflected is determined by the algorithm that follows. Let C be the
6.1559 + * class represented by this object:
6.1560 + * <OL>
6.1561 + * <LI> C is searched for any <I>matching methods</I>. If no matching
6.1562 + * method is found, the algorithm of step 1 is invoked recursively on
6.1563 + * the superclass of C.</LI>
6.1564 + * <LI> If no method was found in step 1 above, the superinterfaces of C
6.1565 + * are searched for a matching method. If any such method is found, it
6.1566 + * is reflected.</LI>
6.1567 + * </OL>
6.1568 + *
6.1569 + * To find a matching method in a class C: If C declares exactly one
6.1570 + * public method with the specified name and exactly the same formal
6.1571 + * parameter types, that is the method reflected. If more than one such
6.1572 + * method is found in C, and one of these methods has a return type that is
6.1573 + * more specific than any of the others, that method is reflected;
6.1574 + * otherwise one of the methods is chosen arbitrarily.
6.1575 + *
6.1576 + * <p>Note that there may be more than one matching method in a
6.1577 + * class because while the Java language forbids a class to
6.1578 + * declare multiple methods with the same signature but different
6.1579 + * return types, the Java virtual machine does not. This
6.1580 + * increased flexibility in the virtual machine can be used to
6.1581 + * implement various language features. For example, covariant
6.1582 + * returns can be implemented with {@linkplain
6.1583 + * java.lang.reflect.Method#isBridge bridge methods}; the bridge
6.1584 + * method and the method being overridden would have the same
6.1585 + * signature but different return types.
6.1586 + *
6.1587 + * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
6.1588 + *
6.1589 + * @param name the name of the method
6.1590 + * @param parameterTypes the list of parameters
6.1591 + * @return the {@code Method} object that matches the specified
6.1592 + * {@code name} and {@code parameterTypes}
6.1593 + * @exception NoSuchMethodException if a matching method is not found
6.1594 + * or if the name is "<init>"or "<clinit>".
6.1595 + * @exception NullPointerException if {@code name} is {@code null}
6.1596 + * @exception SecurityException
6.1597 + * If a security manager, <i>s</i>, is present and any of the
6.1598 + * following conditions is met:
6.1599 + *
6.1600 + * <ul>
6.1601 + *
6.1602 + * <li> invocation of
6.1603 + * {@link SecurityManager#checkMemberAccess
6.1604 + * s.checkMemberAccess(this, Member.PUBLIC)} denies
6.1605 + * access to the method
6.1606 + *
6.1607 + * <li> the caller's class loader is not the same as or an
6.1608 + * ancestor of the class loader for the current class and
6.1609 + * invocation of {@link SecurityManager#checkPackageAccess
6.1610 + * s.checkPackageAccess()} denies access to the package
6.1611 + * of this class
6.1612 + *
6.1613 + * </ul>
6.1614 + *
6.1615 + * @since JDK1.1
6.1616 + */
6.1617 + public Method getMethod(String name, Class<?>... parameterTypes)
6.1618 + throws NoSuchMethodException, SecurityException {
6.1619 + // be very careful not to change the stack depth of this
6.1620 + // checkMemberAccess call for security reasons
6.1621 + // see java.lang.SecurityManager.checkMemberAccess
6.1622 + checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
6.1623 + Method method = getMethod0(name, parameterTypes);
6.1624 + if (method == null) {
6.1625 + throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
6.1626 + }
6.1627 + return method;
6.1628 + }
6.1629 +
6.1630 +
6.1631 + /**
6.1632 + * Returns a {@code Constructor} object that reflects the specified
6.1633 + * public constructor of the class represented by this {@code Class}
6.1634 + * object. The {@code parameterTypes} parameter is an array of
6.1635 + * {@code Class} objects that identify the constructor's formal
6.1636 + * parameter types, in declared order.
6.1637 + *
6.1638 + * If this {@code Class} object represents an inner class
6.1639 + * declared in a non-static context, the formal parameter types
6.1640 + * include the explicit enclosing instance as the first parameter.
6.1641 + *
6.1642 + * <p> The constructor to reflect is the public constructor of the class
6.1643 + * represented by this {@code Class} object whose formal parameter
6.1644 + * types match those specified by {@code parameterTypes}.
6.1645 + *
6.1646 + * @param parameterTypes the parameter array
6.1647 + * @return the {@code Constructor} object of the public constructor that
6.1648 + * matches the specified {@code parameterTypes}
6.1649 + * @exception NoSuchMethodException if a matching method is not found.
6.1650 + * @exception SecurityException
6.1651 + * If a security manager, <i>s</i>, is present and any of the
6.1652 + * following conditions is met:
6.1653 + *
6.1654 + * <ul>
6.1655 + *
6.1656 + * <li> invocation of
6.1657 + * {@link SecurityManager#checkMemberAccess
6.1658 + * s.checkMemberAccess(this, Member.PUBLIC)} denies
6.1659 + * access to the constructor
6.1660 + *
6.1661 + * <li> the caller's class loader is not the same as or an
6.1662 + * ancestor of the class loader for the current class and
6.1663 + * invocation of {@link SecurityManager#checkPackageAccess
6.1664 + * s.checkPackageAccess()} denies access to the package
6.1665 + * of this class
6.1666 + *
6.1667 + * </ul>
6.1668 + *
6.1669 + * @since JDK1.1
6.1670 + */
6.1671 + public Constructor<T> getConstructor(Class<?>... parameterTypes)
6.1672 + throws NoSuchMethodException, SecurityException {
6.1673 + // be very careful not to change the stack depth of this
6.1674 + // checkMemberAccess call for security reasons
6.1675 + // see java.lang.SecurityManager.checkMemberAccess
6.1676 + checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
6.1677 + return getConstructor0(parameterTypes, Member.PUBLIC);
6.1678 + }
6.1679 +
6.1680 +
6.1681 + /**
6.1682 + * Returns an array of {@code Class} objects reflecting all the
6.1683 + * classes and interfaces declared as members of the class represented by
6.1684 + * this {@code Class} object. This includes public, protected, default
6.1685 + * (package) access, and private classes and interfaces declared by the
6.1686 + * class, but excludes inherited classes and interfaces. This method
6.1687 + * returns an array of length 0 if the class declares no classes or
6.1688 + * interfaces as members, or if this {@code Class} object represents a
6.1689 + * primitive type, an array class, or void.
6.1690 + *
6.1691 + * @return the array of {@code Class} objects representing all the
6.1692 + * declared members of this class
6.1693 + * @exception SecurityException
6.1694 + * If a security manager, <i>s</i>, is present and any of the
6.1695 + * following conditions is met:
6.1696 + *
6.1697 + * <ul>
6.1698 + *
6.1699 + * <li> invocation of
6.1700 + * {@link SecurityManager#checkMemberAccess
6.1701 + * s.checkMemberAccess(this, Member.DECLARED)} denies
6.1702 + * access to the declared classes within this class
6.1703 + *
6.1704 + * <li> the caller's class loader is not the same as or an
6.1705 + * ancestor of the class loader for the current class and
6.1706 + * invocation of {@link SecurityManager#checkPackageAccess
6.1707 + * s.checkPackageAccess()} denies access to the package
6.1708 + * of this class
6.1709 + *
6.1710 + * </ul>
6.1711 + *
6.1712 + * @since JDK1.1
6.1713 + */
6.1714 + public Class<?>[] getDeclaredClasses() throws SecurityException {
6.1715 + // be very careful not to change the stack depth of this
6.1716 + // checkMemberAccess call for security reasons
6.1717 + // see java.lang.SecurityManager.checkMemberAccess
6.1718 + checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
6.1719 + return getDeclaredClasses0();
6.1720 + }
6.1721 +
6.1722 +
6.1723 + /**
6.1724 + * Returns an array of {@code Field} objects reflecting all the fields
6.1725 + * declared by the class or interface represented by this
6.1726 + * {@code Class} object. This includes public, protected, default
6.1727 + * (package) access, and private fields, but excludes inherited fields.
6.1728 + * The elements in the array returned are not sorted and are not in any
6.1729 + * particular order. This method returns an array of length 0 if the class
6.1730 + * or interface declares no fields, or if this {@code Class} object
6.1731 + * represents a primitive type, an array class, or void.
6.1732 + *
6.1733 + * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
6.1734 + *
6.1735 + * @return the array of {@code Field} objects representing all the
6.1736 + * declared fields of this class
6.1737 + * @exception SecurityException
6.1738 + * If a security manager, <i>s</i>, is present and any of the
6.1739 + * following conditions is met:
6.1740 + *
6.1741 + * <ul>
6.1742 + *
6.1743 + * <li> invocation of
6.1744 + * {@link SecurityManager#checkMemberAccess
6.1745 + * s.checkMemberAccess(this, Member.DECLARED)} denies
6.1746 + * access to the declared fields within this class
6.1747 + *
6.1748 + * <li> the caller's class loader is not the same as or an
6.1749 + * ancestor of the class loader for the current class and
6.1750 + * invocation of {@link SecurityManager#checkPackageAccess
6.1751 + * s.checkPackageAccess()} denies access to the package
6.1752 + * of this class
6.1753 + *
6.1754 + * </ul>
6.1755 + *
6.1756 + * @since JDK1.1
6.1757 + */
6.1758 + public Field[] getDeclaredFields() throws SecurityException {
6.1759 + // be very careful not to change the stack depth of this
6.1760 + // checkMemberAccess call for security reasons
6.1761 + // see java.lang.SecurityManager.checkMemberAccess
6.1762 + checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
6.1763 + return copyFields(privateGetDeclaredFields(false));
6.1764 + }
6.1765 +
6.1766 +
6.1767 + /**
6.1768 + * Returns an array of {@code Method} objects reflecting all the
6.1769 + * methods declared by the class or interface represented by this
6.1770 + * {@code Class} object. This includes public, protected, default
6.1771 + * (package) access, and private methods, but excludes inherited methods.
6.1772 + * The elements in the array returned are not sorted and are not in any
6.1773 + * particular order. This method returns an array of length 0 if the class
6.1774 + * or interface declares no methods, or if this {@code Class} object
6.1775 + * represents a primitive type, an array class, or void. The class
6.1776 + * initialization method {@code <clinit>} is not included in the
6.1777 + * returned array. If the class declares multiple public member methods
6.1778 + * with the same parameter types, they are all included in the returned
6.1779 + * array.
6.1780 + *
6.1781 + * <p> See <em>The Java Language Specification</em>, section 8.2.
6.1782 + *
6.1783 + * @return the array of {@code Method} objects representing all the
6.1784 + * declared methods of this class
6.1785 + * @exception SecurityException
6.1786 + * If a security manager, <i>s</i>, is present and any of the
6.1787 + * following conditions is met:
6.1788 + *
6.1789 + * <ul>
6.1790 + *
6.1791 + * <li> invocation of
6.1792 + * {@link SecurityManager#checkMemberAccess
6.1793 + * s.checkMemberAccess(this, Member.DECLARED)} denies
6.1794 + * access to the declared methods within this class
6.1795 + *
6.1796 + * <li> the caller's class loader is not the same as or an
6.1797 + * ancestor of the class loader for the current class and
6.1798 + * invocation of {@link SecurityManager#checkPackageAccess
6.1799 + * s.checkPackageAccess()} denies access to the package
6.1800 + * of this class
6.1801 + *
6.1802 + * </ul>
6.1803 + *
6.1804 + * @since JDK1.1
6.1805 + */
6.1806 + public Method[] getDeclaredMethods() throws SecurityException {
6.1807 + // be very careful not to change the stack depth of this
6.1808 + // checkMemberAccess call for security reasons
6.1809 + // see java.lang.SecurityManager.checkMemberAccess
6.1810 + checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
6.1811 + return copyMethods(privateGetDeclaredMethods(false));
6.1812 + }
6.1813 +
6.1814 +
6.1815 + /**
6.1816 + * Returns an array of {@code Constructor} objects reflecting all the
6.1817 + * constructors declared by the class represented by this
6.1818 + * {@code Class} object. These are public, protected, default
6.1819 + * (package) access, and private constructors. The elements in the array
6.1820 + * returned are not sorted and are not in any particular order. If the
6.1821 + * class has a default constructor, it is included in the returned array.
6.1822 + * This method returns an array of length 0 if this {@code Class}
6.1823 + * object represents an interface, a primitive type, an array class, or
6.1824 + * void.
6.1825 + *
6.1826 + * <p> See <em>The Java Language Specification</em>, section 8.2.
6.1827 + *
6.1828 + * @return the array of {@code Constructor} objects representing all the
6.1829 + * declared constructors of this class
6.1830 + * @exception SecurityException
6.1831 + * If a security manager, <i>s</i>, is present and any of the
6.1832 + * following conditions is met:
6.1833 + *
6.1834 + * <ul>
6.1835 + *
6.1836 + * <li> invocation of
6.1837 + * {@link SecurityManager#checkMemberAccess
6.1838 + * s.checkMemberAccess(this, Member.DECLARED)} denies
6.1839 + * access to the declared constructors within this class
6.1840 + *
6.1841 + * <li> the caller's class loader is not the same as or an
6.1842 + * ancestor of the class loader for the current class and
6.1843 + * invocation of {@link SecurityManager#checkPackageAccess
6.1844 + * s.checkPackageAccess()} denies access to the package
6.1845 + * of this class
6.1846 + *
6.1847 + * </ul>
6.1848 + *
6.1849 + * @since JDK1.1
6.1850 + */
6.1851 + public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
6.1852 + // be very careful not to change the stack depth of this
6.1853 + // checkMemberAccess call for security reasons
6.1854 + // see java.lang.SecurityManager.checkMemberAccess
6.1855 + checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
6.1856 + return copyConstructors(privateGetDeclaredConstructors(false));
6.1857 + }
6.1858 +
6.1859 +
6.1860 + /**
6.1861 + * Returns a {@code Field} object that reflects the specified declared
6.1862 + * field of the class or interface represented by this {@code Class}
6.1863 + * object. The {@code name} parameter is a {@code String} that
6.1864 + * specifies the simple name of the desired field. Note that this method
6.1865 + * will not reflect the {@code length} field of an array class.
6.1866 + *
6.1867 + * @param name the name of the field
6.1868 + * @return the {@code Field} object for the specified field in this
6.1869 + * class
6.1870 + * @exception NoSuchFieldException if a field with the specified name is
6.1871 + * not found.
6.1872 + * @exception NullPointerException if {@code name} is {@code null}
6.1873 + * @exception SecurityException
6.1874 + * If a security manager, <i>s</i>, is present and any of the
6.1875 + * following conditions is met:
6.1876 + *
6.1877 + * <ul>
6.1878 + *
6.1879 + * <li> invocation of
6.1880 + * {@link SecurityManager#checkMemberAccess
6.1881 + * s.checkMemberAccess(this, Member.DECLARED)} denies
6.1882 + * access to the declared field
6.1883 + *
6.1884 + * <li> the caller's class loader is not the same as or an
6.1885 + * ancestor of the class loader for the current class and
6.1886 + * invocation of {@link SecurityManager#checkPackageAccess
6.1887 + * s.checkPackageAccess()} denies access to the package
6.1888 + * of this class
6.1889 + *
6.1890 + * </ul>
6.1891 + *
6.1892 + * @since JDK1.1
6.1893 + */
6.1894 + public Field getDeclaredField(String name)
6.1895 + throws NoSuchFieldException, SecurityException {
6.1896 + // be very careful not to change the stack depth of this
6.1897 + // checkMemberAccess call for security reasons
6.1898 + // see java.lang.SecurityManager.checkMemberAccess
6.1899 + checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
6.1900 + Field field = searchFields(privateGetDeclaredFields(false), name);
6.1901 + if (field == null) {
6.1902 + throw new NoSuchFieldException(name);
6.1903 + }
6.1904 + return field;
6.1905 + }
6.1906 +
6.1907 +
6.1908 + /**
6.1909 + * Returns a {@code Method} object that reflects the specified
6.1910 + * declared method of the class or interface represented by this
6.1911 + * {@code Class} object. The {@code name} parameter is a
6.1912 + * {@code String} that specifies the simple name of the desired
6.1913 + * method, and the {@code parameterTypes} parameter is an array of
6.1914 + * {@code Class} objects that identify the method's formal parameter
6.1915 + * types, in declared order. If more than one method with the same
6.1916 + * parameter types is declared in a class, and one of these methods has a
6.1917 + * return type that is more specific than any of the others, that method is
6.1918 + * returned; otherwise one of the methods is chosen arbitrarily. If the
6.1919 + * name is "<init>"or "<clinit>" a {@code NoSuchMethodException}
6.1920 + * is raised.
6.1921 + *
6.1922 + * @param name the name of the method
6.1923 + * @param parameterTypes the parameter array
6.1924 + * @return the {@code Method} object for the method of this class
6.1925 + * matching the specified name and parameters
6.1926 + * @exception NoSuchMethodException if a matching method is not found.
6.1927 + * @exception NullPointerException if {@code name} is {@code null}
6.1928 + * @exception SecurityException
6.1929 + * If a security manager, <i>s</i>, is present and any of the
6.1930 + * following conditions is met:
6.1931 + *
6.1932 + * <ul>
6.1933 + *
6.1934 + * <li> invocation of
6.1935 + * {@link SecurityManager#checkMemberAccess
6.1936 + * s.checkMemberAccess(this, Member.DECLARED)} denies
6.1937 + * access to the declared method
6.1938 + *
6.1939 + * <li> the caller's class loader is not the same as or an
6.1940 + * ancestor of the class loader for the current class and
6.1941 + * invocation of {@link SecurityManager#checkPackageAccess
6.1942 + * s.checkPackageAccess()} denies access to the package
6.1943 + * of this class
6.1944 + *
6.1945 + * </ul>
6.1946 + *
6.1947 + * @since JDK1.1
6.1948 + */
6.1949 + public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
6.1950 + throws NoSuchMethodException, SecurityException {
6.1951 + // be very careful not to change the stack depth of this
6.1952 + // checkMemberAccess call for security reasons
6.1953 + // see java.lang.SecurityManager.checkMemberAccess
6.1954 + checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
6.1955 + Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
6.1956 + if (method == null) {
6.1957 + throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
6.1958 + }
6.1959 + return method;
6.1960 + }
6.1961 +
6.1962 +
6.1963 + /**
6.1964 + * Returns a {@code Constructor} object that reflects the specified
6.1965 + * constructor of the class or interface represented by this
6.1966 + * {@code Class} object. The {@code parameterTypes} parameter is
6.1967 + * an array of {@code Class} objects that identify the constructor's
6.1968 + * formal parameter types, in declared order.
6.1969 + *
6.1970 + * If this {@code Class} object represents an inner class
6.1971 + * declared in a non-static context, the formal parameter types
6.1972 + * include the explicit enclosing instance as the first parameter.
6.1973 + *
6.1974 + * @param parameterTypes the parameter array
6.1975 + * @return The {@code Constructor} object for the constructor with the
6.1976 + * specified parameter list
6.1977 + * @exception NoSuchMethodException if a matching method is not found.
6.1978 + * @exception SecurityException
6.1979 + * If a security manager, <i>s</i>, is present and any of the
6.1980 + * following conditions is met:
6.1981 + *
6.1982 + * <ul>
6.1983 + *
6.1984 + * <li> invocation of
6.1985 + * {@link SecurityManager#checkMemberAccess
6.1986 + * s.checkMemberAccess(this, Member.DECLARED)} denies
6.1987 + * access to the declared constructor
6.1988 + *
6.1989 + * <li> the caller's class loader is not the same as or an
6.1990 + * ancestor of the class loader for the current class and
6.1991 + * invocation of {@link SecurityManager#checkPackageAccess
6.1992 + * s.checkPackageAccess()} denies access to the package
6.1993 + * of this class
6.1994 + *
6.1995 + * </ul>
6.1996 + *
6.1997 + * @since JDK1.1
6.1998 + */
6.1999 + public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
6.2000 + throws NoSuchMethodException, SecurityException {
6.2001 + // be very careful not to change the stack depth of this
6.2002 + // checkMemberAccess call for security reasons
6.2003 + // see java.lang.SecurityManager.checkMemberAccess
6.2004 + checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
6.2005 + return getConstructor0(parameterTypes, Member.DECLARED);
6.2006 + }
6.2007 +
6.2008 + /**
6.2009 + * Finds a resource with a given name. The rules for searching resources
6.2010 + * associated with a given class are implemented by the defining
6.2011 + * {@linkplain ClassLoader class loader} of the class. This method
6.2012 + * delegates to this object's class loader. If this object was loaded by
6.2013 + * the bootstrap class loader, the method delegates to {@link
6.2014 + * ClassLoader#getSystemResourceAsStream}.
6.2015 + *
6.2016 + * <p> Before delegation, an absolute resource name is constructed from the
6.2017 + * given resource name using this algorithm:
6.2018 + *
6.2019 + * <ul>
6.2020 + *
6.2021 + * <li> If the {@code name} begins with a {@code '/'}
6.2022 + * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
6.2023 + * portion of the {@code name} following the {@code '/'}.
6.2024 + *
6.2025 + * <li> Otherwise, the absolute name is of the following form:
6.2026 + *
6.2027 + * <blockquote>
6.2028 + * {@code modified_package_name/name}
6.2029 + * </blockquote>
6.2030 + *
6.2031 + * <p> Where the {@code modified_package_name} is the package name of this
6.2032 + * object with {@code '/'} substituted for {@code '.'}
6.2033 + * (<tt>'\u002e'</tt>).
6.2034 + *
6.2035 + * </ul>
6.2036 + *
6.2037 + * @param name name of the desired resource
6.2038 + * @return A {@link java.io.InputStream} object or {@code null} if
6.2039 + * no resource with this name is found
6.2040 + * @throws NullPointerException If {@code name} is {@code null}
6.2041 + * @since JDK1.1
6.2042 + */
6.2043 + public InputStream getResourceAsStream(String name) {
6.2044 + name = resolveName(name);
6.2045 + ClassLoader cl = getClassLoader0();
6.2046 + if (cl==null) {
6.2047 + // A system class.
6.2048 + return ClassLoader.getSystemResourceAsStream(name);
6.2049 + }
6.2050 + return cl.getResourceAsStream(name);
6.2051 + }
6.2052 +
6.2053 + /**
6.2054 + * Finds a resource with a given name. The rules for searching resources
6.2055 + * associated with a given class are implemented by the defining
6.2056 + * {@linkplain ClassLoader class loader} of the class. This method
6.2057 + * delegates to this object's class loader. If this object was loaded by
6.2058 + * the bootstrap class loader, the method delegates to {@link
6.2059 + * ClassLoader#getSystemResource}.
6.2060 + *
6.2061 + * <p> Before delegation, an absolute resource name is constructed from the
6.2062 + * given resource name using this algorithm:
6.2063 + *
6.2064 + * <ul>
6.2065 + *
6.2066 + * <li> If the {@code name} begins with a {@code '/'}
6.2067 + * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
6.2068 + * portion of the {@code name} following the {@code '/'}.
6.2069 + *
6.2070 + * <li> Otherwise, the absolute name is of the following form:
6.2071 + *
6.2072 + * <blockquote>
6.2073 + * {@code modified_package_name/name}
6.2074 + * </blockquote>
6.2075 + *
6.2076 + * <p> Where the {@code modified_package_name} is the package name of this
6.2077 + * object with {@code '/'} substituted for {@code '.'}
6.2078 + * (<tt>'\u002e'</tt>).
6.2079 + *
6.2080 + * </ul>
6.2081 + *
6.2082 + * @param name name of the desired resource
6.2083 + * @return A {@link java.net.URL} object or {@code null} if no
6.2084 + * resource with this name is found
6.2085 + * @since JDK1.1
6.2086 + */
6.2087 + public java.net.URL getResource(String name) {
6.2088 + name = resolveName(name);
6.2089 + ClassLoader cl = getClassLoader0();
6.2090 + if (cl==null) {
6.2091 + // A system class.
6.2092 + return ClassLoader.getSystemResource(name);
6.2093 + }
6.2094 + return cl.getResource(name);
6.2095 + }
6.2096 +
6.2097 +
6.2098 +
6.2099 + /** protection domain returned when the internal domain is null */
6.2100 + private static java.security.ProtectionDomain allPermDomain;
6.2101 +
6.2102 +
6.2103 + /**
6.2104 + * Returns the {@code ProtectionDomain} of this class. If there is a
6.2105 + * security manager installed, this method first calls the security
6.2106 + * manager's {@code checkPermission} method with a
6.2107 + * {@code RuntimePermission("getProtectionDomain")} permission to
6.2108 + * ensure it's ok to get the
6.2109 + * {@code ProtectionDomain}.
6.2110 + *
6.2111 + * @return the ProtectionDomain of this class
6.2112 + *
6.2113 + * @throws SecurityException
6.2114 + * if a security manager exists and its
6.2115 + * {@code checkPermission} method doesn't allow
6.2116 + * getting the ProtectionDomain.
6.2117 + *
6.2118 + * @see java.security.ProtectionDomain
6.2119 + * @see SecurityManager#checkPermission
6.2120 + * @see java.lang.RuntimePermission
6.2121 + * @since 1.2
6.2122 + */
6.2123 + public java.security.ProtectionDomain getProtectionDomain() {
6.2124 + SecurityManager sm = System.getSecurityManager();
6.2125 + if (sm != null) {
6.2126 + sm.checkPermission(SecurityConstants.GET_PD_PERMISSION);
6.2127 + }
6.2128 + java.security.ProtectionDomain pd = getProtectionDomain0();
6.2129 + if (pd == null) {
6.2130 + if (allPermDomain == null) {
6.2131 + java.security.Permissions perms =
6.2132 + new java.security.Permissions();
6.2133 + perms.add(SecurityConstants.ALL_PERMISSION);
6.2134 + allPermDomain =
6.2135 + new java.security.ProtectionDomain(null, perms);
6.2136 + }
6.2137 + pd = allPermDomain;
6.2138 + }
6.2139 + return pd;
6.2140 + }
6.2141 +
6.2142 +
6.2143 + /**
6.2144 + * Returns the ProtectionDomain of this class.
6.2145 + */
6.2146 + private native java.security.ProtectionDomain getProtectionDomain0();
6.2147 +
6.2148 +
6.2149 + /**
6.2150 + * Set the ProtectionDomain for this class. Called by
6.2151 + * ClassLoader.defineClass.
6.2152 + */
6.2153 + native void setProtectionDomain0(java.security.ProtectionDomain pd);
6.2154 +
6.2155 +
6.2156 + /*
6.2157 + * Return the Virtual Machine's Class object for the named
6.2158 + * primitive type.
6.2159 + */
6.2160 + static native Class getPrimitiveClass(String name);
6.2161 +
6.2162 +
6.2163 + /*
6.2164 + * Check if client is allowed to access members. If access is denied,
6.2165 + * throw a SecurityException.
6.2166 + *
6.2167 + * Be very careful not to change the stack depth of this checkMemberAccess
6.2168 + * call for security reasons.
6.2169 + * See java.lang.SecurityManager.checkMemberAccess.
6.2170 + *
6.2171 + * <p> Default policy: allow all clients access with normal Java access
6.2172 + * control.
6.2173 + */
6.2174 + private void checkMemberAccess(int which, ClassLoader ccl) {
6.2175 + SecurityManager s = System.getSecurityManager();
6.2176 + if (s != null) {
6.2177 + s.checkMemberAccess(this, which);
6.2178 + ClassLoader cl = getClassLoader0();
6.2179 + if ((ccl != null) && (ccl != cl) &&
6.2180 + ((cl == null) || !cl.isAncestor(ccl))) {
6.2181 + String name = this.getName();
6.2182 + int i = name.lastIndexOf('.');
6.2183 + if (i != -1) {
6.2184 + s.checkPackageAccess(name.substring(0, i));
6.2185 + }
6.2186 + }
6.2187 + }
6.2188 + }
6.2189 +
6.2190 + /**
6.2191 + * Add a package name prefix if the name is not absolute Remove leading "/"
6.2192 + * if name is absolute
6.2193 + */
6.2194 + private String resolveName(String name) {
6.2195 + if (name == null) {
6.2196 + return name;
6.2197 + }
6.2198 + if (!name.startsWith("/")) {
6.2199 + Class<?> c = this;
6.2200 + while (c.isArray()) {
6.2201 + c = c.getComponentType();
6.2202 + }
6.2203 + String baseName = c.getName();
6.2204 + int index = baseName.lastIndexOf('.');
6.2205 + if (index != -1) {
6.2206 + name = baseName.substring(0, index).replace('.', '/')
6.2207 + +"/"+name;
6.2208 + }
6.2209 + } else {
6.2210 + name = name.substring(1);
6.2211 + }
6.2212 + return name;
6.2213 + }
6.2214 +
6.2215 + /**
6.2216 + * Reflection support.
6.2217 + */
6.2218 +
6.2219 + // Caches for certain reflective results
6.2220 + private static boolean useCaches = true;
6.2221 + private volatile transient SoftReference<Field[]> declaredFields;
6.2222 + private volatile transient SoftReference<Field[]> publicFields;
6.2223 + private volatile transient SoftReference<Method[]> declaredMethods;
6.2224 + private volatile transient SoftReference<Method[]> publicMethods;
6.2225 + private volatile transient SoftReference<Constructor<T>[]> declaredConstructors;
6.2226 + private volatile transient SoftReference<Constructor<T>[]> publicConstructors;
6.2227 + // Intermediate results for getFields and getMethods
6.2228 + private volatile transient SoftReference<Field[]> declaredPublicFields;
6.2229 + private volatile transient SoftReference<Method[]> declaredPublicMethods;
6.2230 +
6.2231 + // Incremented by the VM on each call to JVM TI RedefineClasses()
6.2232 + // that redefines this class or a superclass.
6.2233 + private volatile transient int classRedefinedCount = 0;
6.2234 +
6.2235 + // Value of classRedefinedCount when we last cleared the cached values
6.2236 + // that are sensitive to class redefinition.
6.2237 + private volatile transient int lastRedefinedCount = 0;
6.2238 +
6.2239 + // Clears cached values that might possibly have been obsoleted by
6.2240 + // a class redefinition.
6.2241 + private void clearCachesOnClassRedefinition() {
6.2242 + if (lastRedefinedCount != classRedefinedCount) {
6.2243 + declaredFields = publicFields = declaredPublicFields = null;
6.2244 + declaredMethods = publicMethods = declaredPublicMethods = null;
6.2245 + declaredConstructors = publicConstructors = null;
6.2246 + annotations = declaredAnnotations = null;
6.2247 +
6.2248 + // Use of "volatile" (and synchronization by caller in the case
6.2249 + // of annotations) ensures that no thread sees the update to
6.2250 + // lastRedefinedCount before seeing the caches cleared.
6.2251 + // We do not guard against brief windows during which multiple
6.2252 + // threads might redundantly work to fill an empty cache.
6.2253 + lastRedefinedCount = classRedefinedCount;
6.2254 + }
6.2255 + }
6.2256 +
6.2257 + // Generic signature handling
6.2258 + private native String getGenericSignature();
6.2259 +
6.2260 + // Generic info repository; lazily initialized
6.2261 + private transient ClassRepository genericInfo;
6.2262 +
6.2263 + // accessor for factory
6.2264 + private GenericsFactory getFactory() {
6.2265 + // create scope and factory
6.2266 + return CoreReflectionFactory.make(this, ClassScope.make(this));
6.2267 + }
6.2268 +
6.2269 + // accessor for generic info repository
6.2270 + private ClassRepository getGenericInfo() {
6.2271 + // lazily initialize repository if necessary
6.2272 + if (genericInfo == null) {
6.2273 + // create and cache generic info repository
6.2274 + genericInfo = ClassRepository.make(getGenericSignature(),
6.2275 + getFactory());
6.2276 + }
6.2277 + return genericInfo; //return cached repository
6.2278 + }
6.2279 +
6.2280 + // Annotations handling
6.2281 + private native byte[] getRawAnnotations();
6.2282 +
6.2283 + native ConstantPool getConstantPool();
6.2284 +
6.2285 + //
6.2286 + //
6.2287 + // java.lang.reflect.Field handling
6.2288 + //
6.2289 + //
6.2290 +
6.2291 + // Returns an array of "root" fields. These Field objects must NOT
6.2292 + // be propagated to the outside world, but must instead be copied
6.2293 + // via ReflectionFactory.copyField.
6.2294 + private Field[] privateGetDeclaredFields(boolean publicOnly) {
6.2295 + checkInitted();
6.2296 + Field[] res = null;
6.2297 + if (useCaches) {
6.2298 + clearCachesOnClassRedefinition();
6.2299 + if (publicOnly) {
6.2300 + if (declaredPublicFields != null) {
6.2301 + res = declaredPublicFields.get();
6.2302 + }
6.2303 + } else {
6.2304 + if (declaredFields != null) {
6.2305 + res = declaredFields.get();
6.2306 + }
6.2307 + }
6.2308 + if (res != null) return res;
6.2309 + }
6.2310 + // No cached value available; request value from VM
6.2311 + res = Reflection.filterFields(this, getDeclaredFields0(publicOnly));
6.2312 + if (useCaches) {
6.2313 + if (publicOnly) {
6.2314 + declaredPublicFields = new SoftReference<>(res);
6.2315 + } else {
6.2316 + declaredFields = new SoftReference<>(res);
6.2317 + }
6.2318 + }
6.2319 + return res;
6.2320 + }
6.2321 +
6.2322 + // Returns an array of "root" fields. These Field objects must NOT
6.2323 + // be propagated to the outside world, but must instead be copied
6.2324 + // via ReflectionFactory.copyField.
6.2325 + private Field[] privateGetPublicFields(Set<Class<?>> traversedInterfaces) {
6.2326 + checkInitted();
6.2327 + Field[] res = null;
6.2328 + if (useCaches) {
6.2329 + clearCachesOnClassRedefinition();
6.2330 + if (publicFields != null) {
6.2331 + res = publicFields.get();
6.2332 + }
6.2333 + if (res != null) return res;
6.2334 + }
6.2335 +
6.2336 + // No cached value available; compute value recursively.
6.2337 + // Traverse in correct order for getField().
6.2338 + List<Field> fields = new ArrayList<>();
6.2339 + if (traversedInterfaces == null) {
6.2340 + traversedInterfaces = new HashSet<>();
6.2341 + }
6.2342 +
6.2343 + // Local fields
6.2344 + Field[] tmp = privateGetDeclaredFields(true);
6.2345 + addAll(fields, tmp);
6.2346 +
6.2347 + // Direct superinterfaces, recursively
6.2348 + for (Class<?> c : getInterfaces()) {
6.2349 + if (!traversedInterfaces.contains(c)) {
6.2350 + traversedInterfaces.add(c);
6.2351 + addAll(fields, c.privateGetPublicFields(traversedInterfaces));
6.2352 + }
6.2353 + }
6.2354 +
6.2355 + // Direct superclass, recursively
6.2356 + if (!isInterface()) {
6.2357 + Class<?> c = getSuperclass();
6.2358 + if (c != null) {
6.2359 + addAll(fields, c.privateGetPublicFields(traversedInterfaces));
6.2360 + }
6.2361 + }
6.2362 +
6.2363 + res = new Field[fields.size()];
6.2364 + fields.toArray(res);
6.2365 + if (useCaches) {
6.2366 + publicFields = new SoftReference<>(res);
6.2367 + }
6.2368 + return res;
6.2369 + }
6.2370 +
6.2371 + private static void addAll(Collection<Field> c, Field[] o) {
6.2372 + for (int i = 0; i < o.length; i++) {
6.2373 + c.add(o[i]);
6.2374 + }
6.2375 + }
6.2376 +
6.2377 +
6.2378 + //
6.2379 + //
6.2380 + // java.lang.reflect.Constructor handling
6.2381 + //
6.2382 + //
6.2383 +
6.2384 + // Returns an array of "root" constructors. These Constructor
6.2385 + // objects must NOT be propagated to the outside world, but must
6.2386 + // instead be copied via ReflectionFactory.copyConstructor.
6.2387 + private Constructor<T>[] privateGetDeclaredConstructors(boolean publicOnly) {
6.2388 + checkInitted();
6.2389 + Constructor<T>[] res = null;
6.2390 + if (useCaches) {
6.2391 + clearCachesOnClassRedefinition();
6.2392 + if (publicOnly) {
6.2393 + if (publicConstructors != null) {
6.2394 + res = publicConstructors.get();
6.2395 + }
6.2396 + } else {
6.2397 + if (declaredConstructors != null) {
6.2398 + res = declaredConstructors.get();
6.2399 + }
6.2400 + }
6.2401 + if (res != null) return res;
6.2402 + }
6.2403 + // No cached value available; request value from VM
6.2404 + if (isInterface()) {
6.2405 + res = new Constructor[0];
6.2406 + } else {
6.2407 + res = getDeclaredConstructors0(publicOnly);
6.2408 + }
6.2409 + if (useCaches) {
6.2410 + if (publicOnly) {
6.2411 + publicConstructors = new SoftReference<>(res);
6.2412 + } else {
6.2413 + declaredConstructors = new SoftReference<>(res);
6.2414 + }
6.2415 + }
6.2416 + return res;
6.2417 + }
6.2418 +
6.2419 + //
6.2420 + //
6.2421 + // java.lang.reflect.Method handling
6.2422 + //
6.2423 + //
6.2424 +
6.2425 + // Returns an array of "root" methods. These Method objects must NOT
6.2426 + // be propagated to the outside world, but must instead be copied
6.2427 + // via ReflectionFactory.copyMethod.
6.2428 + private Method[] privateGetDeclaredMethods(boolean publicOnly) {
6.2429 + checkInitted();
6.2430 + Method[] res = null;
6.2431 + if (useCaches) {
6.2432 + clearCachesOnClassRedefinition();
6.2433 + if (publicOnly) {
6.2434 + if (declaredPublicMethods != null) {
6.2435 + res = declaredPublicMethods.get();
6.2436 + }
6.2437 + } else {
6.2438 + if (declaredMethods != null) {
6.2439 + res = declaredMethods.get();
6.2440 + }
6.2441 + }
6.2442 + if (res != null) return res;
6.2443 + }
6.2444 + // No cached value available; request value from VM
6.2445 + res = Reflection.filterMethods(this, getDeclaredMethods0(publicOnly));
6.2446 + if (useCaches) {
6.2447 + if (publicOnly) {
6.2448 + declaredPublicMethods = new SoftReference<>(res);
6.2449 + } else {
6.2450 + declaredMethods = new SoftReference<>(res);
6.2451 + }
6.2452 + }
6.2453 + return res;
6.2454 + }
6.2455 +
6.2456 + static class MethodArray {
6.2457 + private Method[] methods;
6.2458 + private int length;
6.2459 +
6.2460 + MethodArray() {
6.2461 + methods = new Method[20];
6.2462 + length = 0;
6.2463 + }
6.2464 +
6.2465 + void add(Method m) {
6.2466 + if (length == methods.length) {
6.2467 + methods = Arrays.copyOf(methods, 2 * methods.length);
6.2468 + }
6.2469 + methods[length++] = m;
6.2470 + }
6.2471 +
6.2472 + void addAll(Method[] ma) {
6.2473 + for (int i = 0; i < ma.length; i++) {
6.2474 + add(ma[i]);
6.2475 + }
6.2476 + }
6.2477 +
6.2478 + void addAll(MethodArray ma) {
6.2479 + for (int i = 0; i < ma.length(); i++) {
6.2480 + add(ma.get(i));
6.2481 + }
6.2482 + }
6.2483 +
6.2484 + void addIfNotPresent(Method newMethod) {
6.2485 + for (int i = 0; i < length; i++) {
6.2486 + Method m = methods[i];
6.2487 + if (m == newMethod || (m != null && m.equals(newMethod))) {
6.2488 + return;
6.2489 + }
6.2490 + }
6.2491 + add(newMethod);
6.2492 + }
6.2493 +
6.2494 + void addAllIfNotPresent(MethodArray newMethods) {
6.2495 + for (int i = 0; i < newMethods.length(); i++) {
6.2496 + Method m = newMethods.get(i);
6.2497 + if (m != null) {
6.2498 + addIfNotPresent(m);
6.2499 + }
6.2500 + }
6.2501 + }
6.2502 +
6.2503 + int length() {
6.2504 + return length;
6.2505 + }
6.2506 +
6.2507 + Method get(int i) {
6.2508 + return methods[i];
6.2509 + }
6.2510 +
6.2511 + void removeByNameAndSignature(Method toRemove) {
6.2512 + for (int i = 0; i < length; i++) {
6.2513 + Method m = methods[i];
6.2514 + if (m != null &&
6.2515 + m.getReturnType() == toRemove.getReturnType() &&
6.2516 + m.getName() == toRemove.getName() &&
6.2517 + arrayContentsEq(m.getParameterTypes(),
6.2518 + toRemove.getParameterTypes())) {
6.2519 + methods[i] = null;
6.2520 + }
6.2521 + }
6.2522 + }
6.2523 +
6.2524 + void compactAndTrim() {
6.2525 + int newPos = 0;
6.2526 + // Get rid of null slots
6.2527 + for (int pos = 0; pos < length; pos++) {
6.2528 + Method m = methods[pos];
6.2529 + if (m != null) {
6.2530 + if (pos != newPos) {
6.2531 + methods[newPos] = m;
6.2532 + }
6.2533 + newPos++;
6.2534 + }
6.2535 + }
6.2536 + if (newPos != methods.length) {
6.2537 + methods = Arrays.copyOf(methods, newPos);
6.2538 + }
6.2539 + }
6.2540 +
6.2541 + Method[] getArray() {
6.2542 + return methods;
6.2543 + }
6.2544 + }
6.2545 +
6.2546 +
6.2547 + // Returns an array of "root" methods. These Method objects must NOT
6.2548 + // be propagated to the outside world, but must instead be copied
6.2549 + // via ReflectionFactory.copyMethod.
6.2550 + private Method[] privateGetPublicMethods() {
6.2551 + checkInitted();
6.2552 + Method[] res = null;
6.2553 + if (useCaches) {
6.2554 + clearCachesOnClassRedefinition();
6.2555 + if (publicMethods != null) {
6.2556 + res = publicMethods.get();
6.2557 + }
6.2558 + if (res != null) return res;
6.2559 + }
6.2560 +
6.2561 + // No cached value available; compute value recursively.
6.2562 + // Start by fetching public declared methods
6.2563 + MethodArray methods = new MethodArray();
6.2564 + {
6.2565 + Method[] tmp = privateGetDeclaredMethods(true);
6.2566 + methods.addAll(tmp);
6.2567 + }
6.2568 + // Now recur over superclass and direct superinterfaces.
6.2569 + // Go over superinterfaces first so we can more easily filter
6.2570 + // out concrete implementations inherited from superclasses at
6.2571 + // the end.
6.2572 + MethodArray inheritedMethods = new MethodArray();
6.2573 + Class<?>[] interfaces = getInterfaces();
6.2574 + for (int i = 0; i < interfaces.length; i++) {
6.2575 + inheritedMethods.addAll(interfaces[i].privateGetPublicMethods());
6.2576 + }
6.2577 + if (!isInterface()) {
6.2578 + Class<?> c = getSuperclass();
6.2579 + if (c != null) {
6.2580 + MethodArray supers = new MethodArray();
6.2581 + supers.addAll(c.privateGetPublicMethods());
6.2582 + // Filter out concrete implementations of any
6.2583 + // interface methods
6.2584 + for (int i = 0; i < supers.length(); i++) {
6.2585 + Method m = supers.get(i);
6.2586 + if (m != null && !Modifier.isAbstract(m.getModifiers())) {
6.2587 + inheritedMethods.removeByNameAndSignature(m);
6.2588 + }
6.2589 + }
6.2590 + // Insert superclass's inherited methods before
6.2591 + // superinterfaces' to satisfy getMethod's search
6.2592 + // order
6.2593 + supers.addAll(inheritedMethods);
6.2594 + inheritedMethods = supers;
6.2595 + }
6.2596 + }
6.2597 + // Filter out all local methods from inherited ones
6.2598 + for (int i = 0; i < methods.length(); i++) {
6.2599 + Method m = methods.get(i);
6.2600 + inheritedMethods.removeByNameAndSignature(m);
6.2601 + }
6.2602 + methods.addAllIfNotPresent(inheritedMethods);
6.2603 + methods.compactAndTrim();
6.2604 + res = methods.getArray();
6.2605 + if (useCaches) {
6.2606 + publicMethods = new SoftReference<>(res);
6.2607 + }
6.2608 + return res;
6.2609 + }
6.2610 +
6.2611 +
6.2612 + //
6.2613 + // Helpers for fetchers of one field, method, or constructor
6.2614 + //
6.2615 +
6.2616 + private Field searchFields(Field[] fields, String name) {
6.2617 + String internedName = name.intern();
6.2618 + for (int i = 0; i < fields.length; i++) {
6.2619 + if (fields[i].getName() == internedName) {
6.2620 + return getReflectionFactory().copyField(fields[i]);
6.2621 + }
6.2622 + }
6.2623 + return null;
6.2624 + }
6.2625 +
6.2626 + private Field getField0(String name) throws NoSuchFieldException {
6.2627 + // Note: the intent is that the search algorithm this routine
6.2628 + // uses be equivalent to the ordering imposed by
6.2629 + // privateGetPublicFields(). It fetches only the declared
6.2630 + // public fields for each class, however, to reduce the number
6.2631 + // of Field objects which have to be created for the common
6.2632 + // case where the field being requested is declared in the
6.2633 + // class which is being queried.
6.2634 + Field res = null;
6.2635 + // Search declared public fields
6.2636 + if ((res = searchFields(privateGetDeclaredFields(true), name)) != null) {
6.2637 + return res;
6.2638 + }
6.2639 + // Direct superinterfaces, recursively
6.2640 + Class<?>[] interfaces = getInterfaces();
6.2641 + for (int i = 0; i < interfaces.length; i++) {
6.2642 + Class<?> c = interfaces[i];
6.2643 + if ((res = c.getField0(name)) != null) {
6.2644 + return res;
6.2645 + }
6.2646 + }
6.2647 + // Direct superclass, recursively
6.2648 + if (!isInterface()) {
6.2649 + Class<?> c = getSuperclass();
6.2650 + if (c != null) {
6.2651 + if ((res = c.getField0(name)) != null) {
6.2652 + return res;
6.2653 + }
6.2654 + }
6.2655 + }
6.2656 + return null;
6.2657 + }
6.2658 +
6.2659 + private static Method searchMethods(Method[] methods,
6.2660 + String name,
6.2661 + Class<?>[] parameterTypes)
6.2662 + {
6.2663 + Method res = null;
6.2664 + String internedName = name.intern();
6.2665 + for (int i = 0; i < methods.length; i++) {
6.2666 + Method m = methods[i];
6.2667 + if (m.getName() == internedName
6.2668 + && arrayContentsEq(parameterTypes, m.getParameterTypes())
6.2669 + && (res == null
6.2670 + || res.getReturnType().isAssignableFrom(m.getReturnType())))
6.2671 + res = m;
6.2672 + }
6.2673 +
6.2674 + return (res == null ? res : getReflectionFactory().copyMethod(res));
6.2675 + }
6.2676 +
6.2677 +
6.2678 + private Method getMethod0(String name, Class<?>[] parameterTypes) {
6.2679 + // Note: the intent is that the search algorithm this routine
6.2680 + // uses be equivalent to the ordering imposed by
6.2681 + // privateGetPublicMethods(). It fetches only the declared
6.2682 + // public methods for each class, however, to reduce the
6.2683 + // number of Method objects which have to be created for the
6.2684 + // common case where the method being requested is declared in
6.2685 + // the class which is being queried.
6.2686 + Method res = null;
6.2687 + // Search declared public methods
6.2688 + if ((res = searchMethods(privateGetDeclaredMethods(true),
6.2689 + name,
6.2690 + parameterTypes)) != null) {
6.2691 + return res;
6.2692 + }
6.2693 + // Search superclass's methods
6.2694 + if (!isInterface()) {
6.2695 + Class<? super T> c = getSuperclass();
6.2696 + if (c != null) {
6.2697 + if ((res = c.getMethod0(name, parameterTypes)) != null) {
6.2698 + return res;
6.2699 + }
6.2700 + }
6.2701 + }
6.2702 + // Search superinterfaces' methods
6.2703 + Class<?>[] interfaces = getInterfaces();
6.2704 + for (int i = 0; i < interfaces.length; i++) {
6.2705 + Class<?> c = interfaces[i];
6.2706 + if ((res = c.getMethod0(name, parameterTypes)) != null) {
6.2707 + return res;
6.2708 + }
6.2709 + }
6.2710 + // Not found
6.2711 + return null;
6.2712 + }
6.2713 +
6.2714 + private Constructor<T> getConstructor0(Class<?>[] parameterTypes,
6.2715 + int which) throws NoSuchMethodException
6.2716 + {
6.2717 + Constructor<T>[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC));
6.2718 + for (Constructor<T> constructor : constructors) {
6.2719 + if (arrayContentsEq(parameterTypes,
6.2720 + constructor.getParameterTypes())) {
6.2721 + return getReflectionFactory().copyConstructor(constructor);
6.2722 + }
6.2723 + }
6.2724 + throw new NoSuchMethodException(getName() + ".<init>" + argumentTypesToString(parameterTypes));
6.2725 + }
6.2726 +
6.2727 + //
6.2728 + // Other helpers and base implementation
6.2729 + //
6.2730 +
6.2731 + private static boolean arrayContentsEq(Object[] a1, Object[] a2) {
6.2732 + if (a1 == null) {
6.2733 + return a2 == null || a2.length == 0;
6.2734 + }
6.2735 +
6.2736 + if (a2 == null) {
6.2737 + return a1.length == 0;
6.2738 + }
6.2739 +
6.2740 + if (a1.length != a2.length) {
6.2741 + return false;
6.2742 + }
6.2743 +
6.2744 + for (int i = 0; i < a1.length; i++) {
6.2745 + if (a1[i] != a2[i]) {
6.2746 + return false;
6.2747 + }
6.2748 + }
6.2749 +
6.2750 + return true;
6.2751 + }
6.2752 +
6.2753 + private static Field[] copyFields(Field[] arg) {
6.2754 + Field[] out = new Field[arg.length];
6.2755 + ReflectionFactory fact = getReflectionFactory();
6.2756 + for (int i = 0; i < arg.length; i++) {
6.2757 + out[i] = fact.copyField(arg[i]);
6.2758 + }
6.2759 + return out;
6.2760 + }
6.2761 +
6.2762 + private static Method[] copyMethods(Method[] arg) {
6.2763 + Method[] out = new Method[arg.length];
6.2764 + ReflectionFactory fact = getReflectionFactory();
6.2765 + for (int i = 0; i < arg.length; i++) {
6.2766 + out[i] = fact.copyMethod(arg[i]);
6.2767 + }
6.2768 + return out;
6.2769 + }
6.2770 +
6.2771 + private static <U> Constructor<U>[] copyConstructors(Constructor<U>[] arg) {
6.2772 + Constructor<U>[] out = arg.clone();
6.2773 + ReflectionFactory fact = getReflectionFactory();
6.2774 + for (int i = 0; i < out.length; i++) {
6.2775 + out[i] = fact.copyConstructor(out[i]);
6.2776 + }
6.2777 + return out;
6.2778 + }
6.2779 +
6.2780 + private native Field[] getDeclaredFields0(boolean publicOnly);
6.2781 + private native Method[] getDeclaredMethods0(boolean publicOnly);
6.2782 + private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
6.2783 + private native Class<?>[] getDeclaredClasses0();
6.2784 +
6.2785 + private static String argumentTypesToString(Class<?>[] argTypes) {
6.2786 + StringBuilder buf = new StringBuilder();
6.2787 + buf.append("(");
6.2788 + if (argTypes != null) {
6.2789 + for (int i = 0; i < argTypes.length; i++) {
6.2790 + if (i > 0) {
6.2791 + buf.append(", ");
6.2792 + }
6.2793 + Class<?> c = argTypes[i];
6.2794 + buf.append((c == null) ? "null" : c.getName());
6.2795 + }
6.2796 + }
6.2797 + buf.append(")");
6.2798 + return buf.toString();
6.2799 + }
6.2800 +
6.2801 + /** use serialVersionUID from JDK 1.1 for interoperability */
6.2802 + private static final long serialVersionUID = 3206093459760846163L;
6.2803 +
6.2804 +
6.2805 + /**
6.2806 + * Class Class is special cased within the Serialization Stream Protocol.
6.2807 + *
6.2808 + * A Class instance is written initially into an ObjectOutputStream in the
6.2809 + * following format:
6.2810 + * <pre>
6.2811 + * {@code TC_CLASS} ClassDescriptor
6.2812 + * A ClassDescriptor is a special cased serialization of
6.2813 + * a {@code java.io.ObjectStreamClass} instance.
6.2814 + * </pre>
6.2815 + * A new handle is generated for the initial time the class descriptor
6.2816 + * is written into the stream. Future references to the class descriptor
6.2817 + * are written as references to the initial class descriptor instance.
6.2818 + *
6.2819 + * @see java.io.ObjectStreamClass
6.2820 + */
6.2821 + private static final ObjectStreamField[] serialPersistentFields =
6.2822 + new ObjectStreamField[0];
6.2823 +
6.2824 +
6.2825 + /**
6.2826 + * Returns the assertion status that would be assigned to this
6.2827 + * class if it were to be initialized at the time this method is invoked.
6.2828 + * If this class has had its assertion status set, the most recent
6.2829 + * setting will be returned; otherwise, if any package default assertion
6.2830 + * status pertains to this class, the most recent setting for the most
6.2831 + * specific pertinent package default assertion status is returned;
6.2832 + * otherwise, if this class is not a system class (i.e., it has a
6.2833 + * class loader) its class loader's default assertion status is returned;
6.2834 + * otherwise, the system class default assertion status is returned.
6.2835 + * <p>
6.2836 + * Few programmers will have any need for this method; it is provided
6.2837 + * for the benefit of the JRE itself. (It allows a class to determine at
6.2838 + * the time that it is initialized whether assertions should be enabled.)
6.2839 + * Note that this method is not guaranteed to return the actual
6.2840 + * assertion status that was (or will be) associated with the specified
6.2841 + * class when it was (or will be) initialized.
6.2842 + *
6.2843 + * @return the desired assertion status of the specified class.
6.2844 + * @see java.lang.ClassLoader#setClassAssertionStatus
6.2845 + * @see java.lang.ClassLoader#setPackageAssertionStatus
6.2846 + * @see java.lang.ClassLoader#setDefaultAssertionStatus
6.2847 + * @since 1.4
6.2848 + */
6.2849 + public boolean desiredAssertionStatus() {
6.2850 + ClassLoader loader = getClassLoader();
6.2851 + // If the loader is null this is a system class, so ask the VM
6.2852 + if (loader == null)
6.2853 + return desiredAssertionStatus0(this);
6.2854 +
6.2855 + // If the classloader has been initialized with the assertion
6.2856 + // directives, ask it. Otherwise, ask the VM.
6.2857 + synchronized(loader.assertionLock) {
6.2858 + if (loader.classAssertionStatus != null) {
6.2859 + return loader.desiredAssertionStatus(getName());
6.2860 + }
6.2861 + }
6.2862 + return desiredAssertionStatus0(this);
6.2863 + }
6.2864 +
6.2865 + // Retrieves the desired assertion status of this class from the VM
6.2866 + private static native boolean desiredAssertionStatus0(Class<?> clazz);
6.2867 +
6.2868 + /**
6.2869 + * Returns true if and only if this class was declared as an enum in the
6.2870 + * source code.
6.2871 + *
6.2872 + * @return true if and only if this class was declared as an enum in the
6.2873 + * source code
6.2874 + * @since 1.5
6.2875 + */
6.2876 + public boolean isEnum() {
6.2877 + // An enum must both directly extend java.lang.Enum and have
6.2878 + // the ENUM bit set; classes for specialized enum constants
6.2879 + // don't do the former.
6.2880 + return (this.getModifiers() & ENUM) != 0 &&
6.2881 + this.getSuperclass() == java.lang.Enum.class;
6.2882 + }
6.2883 +
6.2884 + // Fetches the factory for reflective objects
6.2885 + private static ReflectionFactory getReflectionFactory() {
6.2886 + if (reflectionFactory == null) {
6.2887 + reflectionFactory =
6.2888 + java.security.AccessController.doPrivileged
6.2889 + (new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
6.2890 + }
6.2891 + return reflectionFactory;
6.2892 + }
6.2893 + private static ReflectionFactory reflectionFactory;
6.2894 +
6.2895 + // To be able to query system properties as soon as they're available
6.2896 + private static boolean initted = false;
6.2897 + private static void checkInitted() {
6.2898 + if (initted) return;
6.2899 + AccessController.doPrivileged(new PrivilegedAction<Void>() {
6.2900 + public Void run() {
6.2901 + // Tests to ensure the system properties table is fully
6.2902 + // initialized. This is needed because reflection code is
6.2903 + // called very early in the initialization process (before
6.2904 + // command-line arguments have been parsed and therefore
6.2905 + // these user-settable properties installed.) We assume that
6.2906 + // if System.out is non-null then the System class has been
6.2907 + // fully initialized and that the bulk of the startup code
6.2908 + // has been run.
6.2909 +
6.2910 + if (System.out == null) {
6.2911 + // java.lang.System not yet fully initialized
6.2912 + return null;
6.2913 + }
6.2914 +
6.2915 + String val =
6.2916 + System.getProperty("sun.reflect.noCaches");
6.2917 + if (val != null && val.equals("true")) {
6.2918 + useCaches = false;
6.2919 + }
6.2920 +
6.2921 + initted = true;
6.2922 + return null;
6.2923 + }
6.2924 + });
6.2925 + }
6.2926 +
6.2927 + /**
6.2928 + * Returns the elements of this enum class or null if this
6.2929 + * Class object does not represent an enum type.
6.2930 + *
6.2931 + * @return an array containing the values comprising the enum class
6.2932 + * represented by this Class object in the order they're
6.2933 + * declared, or null if this Class object does not
6.2934 + * represent an enum type
6.2935 + * @since 1.5
6.2936 + */
6.2937 + public T[] getEnumConstants() {
6.2938 + T[] values = getEnumConstantsShared();
6.2939 + return (values != null) ? values.clone() : null;
6.2940 + }
6.2941 +
6.2942 + /**
6.2943 + * Returns the elements of this enum class or null if this
6.2944 + * Class object does not represent an enum type;
6.2945 + * identical to getEnumConstants except that the result is
6.2946 + * uncloned, cached, and shared by all callers.
6.2947 + */
6.2948 + T[] getEnumConstantsShared() {
6.2949 + if (enumConstants == null) {
6.2950 + if (!isEnum()) return null;
6.2951 + try {
6.2952 + final Method values = getMethod("values");
6.2953 + java.security.AccessController.doPrivileged(
6.2954 + new java.security.PrivilegedAction<Void>() {
6.2955 + public Void run() {
6.2956 + values.setAccessible(true);
6.2957 + return null;
6.2958 + }
6.2959 + });
6.2960 + enumConstants = (T[])values.invoke(null);
6.2961 + }
6.2962 + // These can happen when users concoct enum-like classes
6.2963 + // that don't comply with the enum spec.
6.2964 + catch (InvocationTargetException ex) { return null; }
6.2965 + catch (NoSuchMethodException ex) { return null; }
6.2966 + catch (IllegalAccessException ex) { return null; }
6.2967 + }
6.2968 + return enumConstants;
6.2969 + }
6.2970 + private volatile transient T[] enumConstants = null;
6.2971 +
6.2972 + /**
6.2973 + * Returns a map from simple name to enum constant. This package-private
6.2974 + * method is used internally by Enum to implement
6.2975 + * public static <T extends Enum<T>> T valueOf(Class<T>, String)
6.2976 + * efficiently. Note that the map is returned by this method is
6.2977 + * created lazily on first use. Typically it won't ever get created.
6.2978 + */
6.2979 + Map<String, T> enumConstantDirectory() {
6.2980 + if (enumConstantDirectory == null) {
6.2981 + T[] universe = getEnumConstantsShared();
6.2982 + if (universe == null)
6.2983 + throw new IllegalArgumentException(
6.2984 + getName() + " is not an enum type");
6.2985 + Map<String, T> m = new HashMap<>(2 * universe.length);
6.2986 + for (T constant : universe)
6.2987 + m.put(((Enum<?>)constant).name(), constant);
6.2988 + enumConstantDirectory = m;
6.2989 + }
6.2990 + return enumConstantDirectory;
6.2991 + }
6.2992 + private volatile transient Map<String, T> enumConstantDirectory = null;
6.2993 +
6.2994 + /**
6.2995 + * Casts an object to the class or interface represented
6.2996 + * by this {@code Class} object.
6.2997 + *
6.2998 + * @param obj the object to be cast
6.2999 + * @return the object after casting, or null if obj is null
6.3000 + *
6.3001 + * @throws ClassCastException if the object is not
6.3002 + * null and is not assignable to the type T.
6.3003 + *
6.3004 + * @since 1.5
6.3005 + */
6.3006 + public T cast(Object obj) {
6.3007 + if (obj != null && !isInstance(obj))
6.3008 + throw new ClassCastException(cannotCastMsg(obj));
6.3009 + return (T) obj;
6.3010 + }
6.3011 +
6.3012 + private String cannotCastMsg(Object obj) {
6.3013 + return "Cannot cast " + obj.getClass().getName() + " to " + getName();
6.3014 + }
6.3015 +
6.3016 + /**
6.3017 + * Casts this {@code Class} object to represent a subclass of the class
6.3018 + * represented by the specified class object. Checks that that the cast
6.3019 + * is valid, and throws a {@code ClassCastException} if it is not. If
6.3020 + * this method succeeds, it always returns a reference to this class object.
6.3021 + *
6.3022 + * <p>This method is useful when a client needs to "narrow" the type of
6.3023 + * a {@code Class} object to pass it to an API that restricts the
6.3024 + * {@code Class} objects that it is willing to accept. A cast would
6.3025 + * generate a compile-time warning, as the correctness of the cast
6.3026 + * could not be checked at runtime (because generic types are implemented
6.3027 + * by erasure).
6.3028 + *
6.3029 + * @return this {@code Class} object, cast to represent a subclass of
6.3030 + * the specified class object.
6.3031 + * @throws ClassCastException if this {@code Class} object does not
6.3032 + * represent a subclass of the specified class (here "subclass" includes
6.3033 + * the class itself).
6.3034 + * @since 1.5
6.3035 + */
6.3036 + public <U> Class<? extends U> asSubclass(Class<U> clazz) {
6.3037 + if (clazz.isAssignableFrom(this))
6.3038 + return (Class<? extends U>) this;
6.3039 + else
6.3040 + throw new ClassCastException(this.toString());
6.3041 + }
6.3042 +
6.3043 + /**
6.3044 + * @throws NullPointerException {@inheritDoc}
6.3045 + * @since 1.5
6.3046 + */
6.3047 + public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
6.3048 + if (annotationClass == null)
6.3049 + throw new NullPointerException();
6.3050 +
6.3051 + initAnnotationsIfNecessary();
6.3052 + return (A) annotations.get(annotationClass);
6.3053 + }
6.3054 +
6.3055 + /**
6.3056 + * @throws NullPointerException {@inheritDoc}
6.3057 + * @since 1.5
6.3058 + */
6.3059 + public boolean isAnnotationPresent(
6.3060 + Class<? extends Annotation> annotationClass) {
6.3061 + if (annotationClass == null)
6.3062 + throw new NullPointerException();
6.3063 +
6.3064 + return getAnnotation(annotationClass) != null;
6.3065 + }
6.3066 +
6.3067 +
6.3068 + /**
6.3069 + * @since 1.5
6.3070 + */
6.3071 + public Annotation[] getAnnotations() {
6.3072 + initAnnotationsIfNecessary();
6.3073 + return AnnotationParser.toArray(annotations);
6.3074 + }
6.3075 +
6.3076 + /**
6.3077 + * @since 1.5
6.3078 + */
6.3079 + public Annotation[] getDeclaredAnnotations() {
6.3080 + initAnnotationsIfNecessary();
6.3081 + return AnnotationParser.toArray(declaredAnnotations);
6.3082 + }
6.3083 +
6.3084 + // Annotations cache
6.3085 + private transient Map<Class<? extends Annotation>, Annotation> annotations;
6.3086 + private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
6.3087 +
6.3088 + private synchronized void initAnnotationsIfNecessary() {
6.3089 + clearCachesOnClassRedefinition();
6.3090 + if (annotations != null)
6.3091 + return;
6.3092 + declaredAnnotations = AnnotationParser.parseAnnotations(
6.3093 + getRawAnnotations(), getConstantPool(), this);
6.3094 + Class<?> superClass = getSuperclass();
6.3095 + if (superClass == null) {
6.3096 + annotations = declaredAnnotations;
6.3097 + } else {
6.3098 + annotations = new HashMap<>();
6.3099 + superClass.initAnnotationsIfNecessary();
6.3100 + for (Map.Entry<Class<? extends Annotation>, Annotation> e : superClass.annotations.entrySet()) {
6.3101 + Class<? extends Annotation> annotationClass = e.getKey();
6.3102 + if (AnnotationType.getInstance(annotationClass).isInherited())
6.3103 + annotations.put(annotationClass, e.getValue());
6.3104 + }
6.3105 + annotations.putAll(declaredAnnotations);
6.3106 + }
6.3107 + }
6.3108 +
6.3109 + // Annotation types cache their internal (AnnotationType) form
6.3110 +
6.3111 + private AnnotationType annotationType;
6.3112 +
6.3113 + void setAnnotationType(AnnotationType type) {
6.3114 + annotationType = type;
6.3115 + }
6.3116 +
6.3117 + AnnotationType getAnnotationType() {
6.3118 + return annotationType;
6.3119 + }
6.3120 +}