0N/A/*
2362N/A * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/Aimport java.lang.reflect.Field;
6338N/Aimport sun.reflect.CallerSensitive;
6306N/Aimport sun.reflect.Reflection;
6306N/Aimport sun.reflect.misc.ReflectUtil;
0N/A
0N/A/**
0N/A * A description of a Serializable field from a Serializable class. An array
0N/A * of ObjectStreamFields is used to declare the Serializable fields of a class.
0N/A *
0N/A * @author Mike Warres
0N/A * @author Roger Riggs
0N/A * @see ObjectStreamClass
0N/A * @since 1.2
0N/A */
0N/Apublic class ObjectStreamField
0N/A implements Comparable<Object>
0N/A{
0N/A
0N/A /** field name */
0N/A private final String name;
0N/A /** canonical JVM signature of field type */
0N/A private final String signature;
0N/A /** field type (Object.class if unknown non-primitive type) */
1805N/A private final Class<?> type;
0N/A /** whether or not to (de)serialize field values as unshared */
0N/A private final boolean unshared;
0N/A /** corresponding reflective field object, if any */
0N/A private final Field field;
0N/A /** offset of field value in enclosing field group */
0N/A private int offset = 0;
0N/A
0N/A /**
0N/A * Create a Serializable field with the specified type. This field should
0N/A * be documented with a <code>serialField</code> tag.
0N/A *
0N/A * @param name the name of the serializable field
0N/A * @param type the <code>Class</code> object of the serializable field
0N/A */
0N/A public ObjectStreamField(String name, Class<?> type) {
0N/A this(name, type, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates an ObjectStreamField representing a serializable field with the
0N/A * given name and type. If unshared is false, values of the represented
0N/A * field are serialized and deserialized in the default manner--if the
0N/A * field is non-primitive, object values are serialized and deserialized as
0N/A * if they had been written and read by calls to writeObject and
0N/A * readObject. If unshared is true, values of the represented field are
0N/A * serialized and deserialized as if they had been written and read by
0N/A * calls to writeUnshared and readUnshared.
0N/A *
0N/A * @param name field name
0N/A * @param type field type
0N/A * @param unshared if false, write/read field values in the same manner
0N/A * as writeObject/readObject; if true, write/read in the same
0N/A * manner as writeUnshared/readUnshared
0N/A * @since 1.4
0N/A */
0N/A public ObjectStreamField(String name, Class<?> type, boolean unshared) {
0N/A if (name == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A this.name = name;
0N/A this.type = type;
0N/A this.unshared = unshared;
1805N/A signature = getClassSignature(type).intern();
0N/A field = null;
0N/A }
0N/A
0N/A /**
0N/A * Creates an ObjectStreamField representing a field with the given name,
0N/A * signature and unshared setting.
0N/A */
0N/A ObjectStreamField(String name, String signature, boolean unshared) {
0N/A if (name == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A this.name = name;
0N/A this.signature = signature.intern();
0N/A this.unshared = unshared;
0N/A field = null;
0N/A
0N/A switch (signature.charAt(0)) {
0N/A case 'Z': type = Boolean.TYPE; break;
0N/A case 'B': type = Byte.TYPE; break;
0N/A case 'C': type = Character.TYPE; break;
0N/A case 'S': type = Short.TYPE; break;
0N/A case 'I': type = Integer.TYPE; break;
0N/A case 'J': type = Long.TYPE; break;
0N/A case 'F': type = Float.TYPE; break;
0N/A case 'D': type = Double.TYPE; break;
0N/A case 'L':
0N/A case '[': type = Object.class; break;
0N/A default: throw new IllegalArgumentException("illegal signature");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates an ObjectStreamField representing the given field with the
0N/A * specified unshared setting. For compatibility with the behavior of
0N/A * earlier serialization implementations, a "showType" parameter is
0N/A * necessary to govern whether or not a getType() call on this
0N/A * ObjectStreamField (if non-primitive) will return Object.class (as
0N/A * opposed to a more specific reference type).
0N/A */
0N/A ObjectStreamField(Field field, boolean unshared, boolean showType) {
0N/A this.field = field;
0N/A this.unshared = unshared;
0N/A name = field.getName();
1805N/A Class<?> ftype = field.getType();
0N/A type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
1805N/A signature = getClassSignature(ftype).intern();
0N/A }
0N/A
0N/A /**
0N/A * Get the name of this field.
0N/A *
0N/A * @return a <code>String</code> representing the name of the serializable
0N/A * field
0N/A */
0N/A public String getName() {
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Get the type of the field. If the type is non-primitive and this
0N/A * <code>ObjectStreamField</code> was obtained from a deserialized {@link
0N/A * ObjectStreamClass} instance, then <code>Object.class</code> is returned.
0N/A * Otherwise, the <code>Class</code> object for the type of the field is
0N/A * returned.
0N/A *
0N/A * @return a <code>Class</code> object representing the type of the
0N/A * serializable field
0N/A */
6338N/A @CallerSensitive
0N/A public Class<?> getType() {
6380N/A if (System.getSecurityManager() != null) {
6380N/A Class<?> caller = Reflection.getCallerClass();
6380N/A if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(), type.getClassLoader())) {
6380N/A ReflectUtil.checkPackageAccess(type);
6380N/A }
6306N/A }
0N/A return type;
0N/A }
0N/A
0N/A /**
0N/A * Returns character encoding of field type. The encoding is as follows:
0N/A * <blockquote><pre>
0N/A * B byte
0N/A * C char
0N/A * D double
0N/A * F float
0N/A * I int
0N/A * J long
0N/A * L class or interface
0N/A * S short
0N/A * Z boolean
0N/A * [ array
0N/A * </pre></blockquote>
0N/A *
0N/A * @return the typecode of the serializable field
0N/A */
0N/A // REMIND: deprecate?
0N/A public char getTypeCode() {
0N/A return signature.charAt(0);
0N/A }
0N/A
0N/A /**
0N/A * Return the JVM type signature.
0N/A *
0N/A * @return null if this field has a primitive type.
0N/A */
0N/A // REMIND: deprecate?
0N/A public String getTypeString() {
0N/A return isPrimitive() ? null : signature;
0N/A }
0N/A
0N/A /**
0N/A * Offset of field within instance data.
0N/A *
0N/A * @return the offset of this field
0N/A * @see #setOffset
0N/A */
0N/A // REMIND: deprecate?
0N/A public int getOffset() {
0N/A return offset;
0N/A }
0N/A
0N/A /**
0N/A * Offset within instance data.
0N/A *
0N/A * @param offset the offset of the field
0N/A * @see #getOffset
0N/A */
0N/A // REMIND: deprecate?
0N/A protected void setOffset(int offset) {
0N/A this.offset = offset;
0N/A }
0N/A
0N/A /**
0N/A * Return true if this field has a primitive type.
0N/A *
0N/A * @return true if and only if this field corresponds to a primitive type
0N/A */
0N/A // REMIND: deprecate?
0N/A public boolean isPrimitive() {
0N/A char tcode = signature.charAt(0);
0N/A return ((tcode != 'L') && (tcode != '['));
0N/A }
0N/A
0N/A /**
0N/A * Returns boolean value indicating whether or not the serializable field
0N/A * represented by this ObjectStreamField instance is unshared.
0N/A *
0N/A * @since 1.4
0N/A */
0N/A public boolean isUnshared() {
0N/A return unshared;
0N/A }
0N/A
0N/A /**
0N/A * Compare this field with another <code>ObjectStreamField</code>. Return
0N/A * -1 if this is smaller, 0 if equal, 1 if greater. Types that are
0N/A * primitives are "smaller" than object types. If equal, the field names
0N/A * are compared.
0N/A */
0N/A // REMIND: deprecate?
0N/A public int compareTo(Object obj) {
0N/A ObjectStreamField other = (ObjectStreamField) obj;
0N/A boolean isPrim = isPrimitive();
0N/A if (isPrim != other.isPrimitive()) {
0N/A return isPrim ? -1 : 1;
0N/A }
0N/A return name.compareTo(other.name);
0N/A }
0N/A
0N/A /**
0N/A * Return a string that describes this field.
0N/A */
0N/A public String toString() {
0N/A return signature + ' ' + name;
0N/A }
0N/A
0N/A /**
0N/A * Returns field represented by this ObjectStreamField, or null if
0N/A * ObjectStreamField is not associated with an actual field.
0N/A */
0N/A Field getField() {
0N/A return field;
0N/A }
0N/A
0N/A /**
0N/A * Returns JVM type signature of field (similar to getTypeString, except
0N/A * that signature strings are returned for primitive fields as well).
0N/A */
0N/A String getSignature() {
0N/A return signature;
0N/A }
1805N/A
1805N/A /**
1805N/A * Returns JVM type signature for given class.
1805N/A */
1805N/A private static String getClassSignature(Class<?> cl) {
1805N/A StringBuilder sbuf = new StringBuilder();
1805N/A while (cl.isArray()) {
1805N/A sbuf.append('[');
1805N/A cl = cl.getComponentType();
1805N/A }
1805N/A if (cl.isPrimitive()) {
1805N/A if (cl == Integer.TYPE) {
1805N/A sbuf.append('I');
1805N/A } else if (cl == Byte.TYPE) {
1805N/A sbuf.append('B');
1805N/A } else if (cl == Long.TYPE) {
1805N/A sbuf.append('J');
1805N/A } else if (cl == Float.TYPE) {
1805N/A sbuf.append('F');
1805N/A } else if (cl == Double.TYPE) {
1805N/A sbuf.append('D');
1805N/A } else if (cl == Short.TYPE) {
1805N/A sbuf.append('S');
1805N/A } else if (cl == Character.TYPE) {
1805N/A sbuf.append('C');
1805N/A } else if (cl == Boolean.TYPE) {
1805N/A sbuf.append('Z');
1805N/A } else if (cl == Void.TYPE) {
1805N/A sbuf.append('V');
1805N/A } else {
1805N/A throw new InternalError();
1805N/A }
1805N/A } else {
1805N/A sbuf.append('L' + cl.getName().replace('.', '/') + ';');
1805N/A }
1805N/A return sbuf.toString();
1805N/A }
0N/A}