src/share/classes/com/sun/tools/javadoc/SerializedForm.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javadoc;
aoqi@0 27
aoqi@0 28 import com.sun.javadoc.*;
aoqi@0 29 import com.sun.tools.javac.code.Flags;
aoqi@0 30 import com.sun.tools.javac.code.Kinds;
aoqi@0 31 import com.sun.tools.javac.code.Scope;
aoqi@0 32 import com.sun.tools.javac.code.Symbol.ClassSymbol;
aoqi@0 33 import com.sun.tools.javac.code.Symbol.MethodSymbol;
aoqi@0 34 import com.sun.tools.javac.code.Symbol.VarSymbol;
aoqi@0 35 import com.sun.tools.javac.util.ListBuffer;
aoqi@0 36 import com.sun.tools.javac.util.Name;
aoqi@0 37 import com.sun.tools.javac.util.Names;
aoqi@0 38
aoqi@0 39 /**
aoqi@0 40 * The serialized form is the specification of a class' serialization
aoqi@0 41 * state. <p>
aoqi@0 42 *
aoqi@0 43 * It consists of the following information:<p>
aoqi@0 44 *
aoqi@0 45 * <pre>
aoqi@0 46 * 1. Whether class is Serializable or Externalizable.
aoqi@0 47 * 2. Javadoc for serialization methods.
aoqi@0 48 * a. For Serializable, the optional readObject, writeObject,
aoqi@0 49 * readResolve and writeReplace.
aoqi@0 50 * serialData tag describes, in prose, the sequence and type
aoqi@0 51 * of optional data written by writeObject.
aoqi@0 52 * b. For Externalizable, writeExternal and readExternal.
aoqi@0 53 * serialData tag describes, in prose, the sequence and type
aoqi@0 54 * of optional data written by writeExternal.
aoqi@0 55 * 3. Javadoc for serialization data layout.
aoqi@0 56 * a. For Serializable, the name,type and description
aoqi@0 57 * of each Serializable fields.
aoqi@0 58 * b. For Externalizable, data layout is described by 2(b).
aoqi@0 59 * </pre>
aoqi@0 60 *
aoqi@0 61 * <p><b>This is NOT part of any supported API.
aoqi@0 62 * If you write code that depends on this, you do so at your own risk.
aoqi@0 63 * This code and its internal interfaces are subject to change or
aoqi@0 64 * deletion without notice.</b>
aoqi@0 65 *
aoqi@0 66 * @since 1.2
aoqi@0 67 * @author Joe Fialli
aoqi@0 68 * @author Neal Gafter (rewrite but not too proud)
aoqi@0 69 */
aoqi@0 70 class SerializedForm {
aoqi@0 71 ListBuffer<MethodDoc> methods = new ListBuffer<MethodDoc>();
aoqi@0 72
aoqi@0 73 /* List of FieldDocImpl - Serializable fields.
aoqi@0 74 * Singleton list if class defines Serializable fields explicitly.
aoqi@0 75 * Otherwise, list of default serializable fields.
aoqi@0 76 * 0 length list for Externalizable.
aoqi@0 77 */
aoqi@0 78 private final ListBuffer<FieldDocImpl> fields = new ListBuffer<FieldDocImpl>();
aoqi@0 79
aoqi@0 80 /* True if class specifies serializable fields explicitly.
aoqi@0 81 * using special static member, serialPersistentFields.
aoqi@0 82 */
aoqi@0 83 private boolean definesSerializableFields = false;
aoqi@0 84
aoqi@0 85 // Specially treated field/method names defined by Serialization.
aoqi@0 86 private static final String SERIALIZABLE_FIELDS = "serialPersistentFields";
aoqi@0 87 private static final String READOBJECT = "readObject";
aoqi@0 88 private static final String WRITEOBJECT = "writeObject";
aoqi@0 89 private static final String READRESOLVE = "readResolve";
aoqi@0 90 private static final String WRITEREPLACE = "writeReplace";
aoqi@0 91 private static final String READOBJECTNODATA = "readObjectNoData";
aoqi@0 92
aoqi@0 93 /**
aoqi@0 94 * Constructor.
aoqi@0 95 *
aoqi@0 96 * Catalog Serializable fields for Serializable class.
aoqi@0 97 * Catalog serialization methods for Serializable and
aoqi@0 98 * Externalizable classes.
aoqi@0 99 */
aoqi@0 100 SerializedForm(DocEnv env, ClassSymbol def, ClassDocImpl cd) {
aoqi@0 101 if (cd.isExternalizable()) {
aoqi@0 102 /* look up required public accessible methods,
aoqi@0 103 * writeExternal and readExternal.
aoqi@0 104 */
aoqi@0 105 String[] readExternalParamArr = { "java.io.ObjectInput" };
aoqi@0 106 String[] writeExternalParamArr = { "java.io.ObjectOutput" };
aoqi@0 107 MethodDoc md = cd.findMethod("readExternal", readExternalParamArr);
aoqi@0 108 if (md != null) {
aoqi@0 109 methods.append(md);
aoqi@0 110 }
aoqi@0 111 md = cd.findMethod("writeExternal", writeExternalParamArr);
aoqi@0 112 if (md != null) {
aoqi@0 113 methods.append(md);
aoqi@0 114 Tag tag[] = md.tags("serialData");
aoqi@0 115 }
aoqi@0 116 // } else { // isSerializable() //### ???
aoqi@0 117 } else if (cd.isSerializable()) {
aoqi@0 118
aoqi@0 119 VarSymbol dsf = getDefinedSerializableFields(def);
aoqi@0 120 if (dsf != null) {
aoqi@0 121
aoqi@0 122 /* Define serializable fields with array of ObjectStreamField.
aoqi@0 123 * Each ObjectStreamField should be documented by a
aoqi@0 124 * serialField tag.
aoqi@0 125 */
aoqi@0 126 definesSerializableFields = true;
aoqi@0 127 //### No modifier filtering applied here.
aoqi@0 128 FieldDocImpl dsfDoc = env.getFieldDoc(dsf);
aoqi@0 129 fields.append(dsfDoc);
aoqi@0 130 mapSerialFieldTagImplsToFieldDocImpls(dsfDoc, env, def);
aoqi@0 131 } else {
aoqi@0 132
aoqi@0 133 /* Calculate default Serializable fields as all
aoqi@0 134 * non-transient, non-static fields.
aoqi@0 135 * Fields should be documented by serial tag.
aoqi@0 136 */
aoqi@0 137 computeDefaultSerializableFields(env, def, cd);
aoqi@0 138 }
aoqi@0 139
aoqi@0 140 /* Check for optional customized readObject, writeObject,
aoqi@0 141 * readResolve and writeReplace, which can all contain
aoqi@0 142 * the serialData tag. */
aoqi@0 143 addMethodIfExist(env, def, READOBJECT);
aoqi@0 144 addMethodIfExist(env, def, WRITEOBJECT);
aoqi@0 145 addMethodIfExist(env, def, READRESOLVE);
aoqi@0 146 addMethodIfExist(env, def, WRITEREPLACE);
aoqi@0 147 addMethodIfExist(env, def, READOBJECTNODATA);
aoqi@0 148 }
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 /*
aoqi@0 152 * Check for explicit Serializable fields.
aoqi@0 153 * Check for a private static array of ObjectStreamField with
aoqi@0 154 * name SERIALIZABLE_FIELDS.
aoqi@0 155 */
aoqi@0 156 private VarSymbol getDefinedSerializableFields(ClassSymbol def) {
aoqi@0 157 Names names = def.name.table.names;
aoqi@0 158
aoqi@0 159 /* SERIALIZABLE_FIELDS can be private,
aoqi@0 160 * so must lookup by ClassSymbol, not by ClassDocImpl.
aoqi@0 161 */
aoqi@0 162 for (Scope.Entry e = def.members().lookup(names.fromString(SERIALIZABLE_FIELDS)); e.scope != null; e = e.next()) {
aoqi@0 163 if (e.sym.kind == Kinds.VAR) {
aoqi@0 164 VarSymbol f = (VarSymbol)e.sym;
aoqi@0 165 if ((f.flags() & Flags.STATIC) != 0 &&
aoqi@0 166 (f.flags() & Flags.PRIVATE) != 0) {
aoqi@0 167 return f;
aoqi@0 168 }
aoqi@0 169 }
aoqi@0 170 }
aoqi@0 171 return null;
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 /*
aoqi@0 175 * Compute default Serializable fields from all members of ClassSymbol.
aoqi@0 176 *
aoqi@0 177 * Since the fields of ClassDocImpl might not contain private or
aoqi@0 178 * package accessible fields, must walk over all members of ClassSymbol.
aoqi@0 179 */
aoqi@0 180 private void computeDefaultSerializableFields(DocEnv env,
aoqi@0 181 ClassSymbol def,
aoqi@0 182 ClassDocImpl cd) {
aoqi@0 183 for (Scope.Entry e = def.members().elems; e != null; e = e.sibling) {
aoqi@0 184 if (e.sym != null && e.sym.kind == Kinds.VAR) {
aoqi@0 185 VarSymbol f = (VarSymbol)e.sym;
aoqi@0 186 if ((f.flags() & Flags.STATIC) == 0 &&
aoqi@0 187 (f.flags() & Flags.TRANSIENT) == 0) {
aoqi@0 188 //### No modifier filtering applied here.
aoqi@0 189 FieldDocImpl fd = env.getFieldDoc(f);
aoqi@0 190 //### Add to beginning.
aoqi@0 191 //### Preserve order used by old 'javadoc'.
aoqi@0 192 fields.prepend(fd);
aoqi@0 193 }
aoqi@0 194 }
aoqi@0 195 }
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 /*
aoqi@0 199 * Catalog Serializable method if it exists in current ClassSymbol.
aoqi@0 200 * Do not look for method in superclasses.
aoqi@0 201 *
aoqi@0 202 * Serialization requires these methods to be non-static.
aoqi@0 203 *
aoqi@0 204 * @param method should be an unqualified Serializable method
aoqi@0 205 * name either READOBJECT, WRITEOBJECT, READRESOLVE
aoqi@0 206 * or WRITEREPLACE.
aoqi@0 207 * @param visibility the visibility flag for the given method.
aoqi@0 208 */
aoqi@0 209 private void addMethodIfExist(DocEnv env, ClassSymbol def, String methodName) {
aoqi@0 210 Names names = def.name.table.names;
aoqi@0 211
aoqi@0 212 for (Scope.Entry e = def.members().lookup(names.fromString(methodName)); e.scope != null; e = e.next()) {
aoqi@0 213 if (e.sym.kind == Kinds.MTH) {
aoqi@0 214 MethodSymbol md = (MethodSymbol)e.sym;
aoqi@0 215 if ((md.flags() & Flags.STATIC) == 0) {
aoqi@0 216 /*
aoqi@0 217 * WARNING: not robust if unqualifiedMethodName is overloaded
aoqi@0 218 * method. Signature checking could make more robust.
aoqi@0 219 * READOBJECT takes a single parameter, java.io.ObjectInputStream.
aoqi@0 220 * WRITEOBJECT takes a single parameter, java.io.ObjectOutputStream.
aoqi@0 221 */
aoqi@0 222 methods.append(env.getMethodDoc(md));
aoqi@0 223 }
aoqi@0 224 }
aoqi@0 225 }
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 /*
aoqi@0 229 * Associate serialField tag fieldName with FieldDocImpl member.
aoqi@0 230 * Note: A serialField tag does not have to map an existing field
aoqi@0 231 * of a class.
aoqi@0 232 */
aoqi@0 233 private void mapSerialFieldTagImplsToFieldDocImpls(FieldDocImpl spfDoc,
aoqi@0 234 DocEnv env,
aoqi@0 235 ClassSymbol def) {
aoqi@0 236 Names names = def.name.table.names;
aoqi@0 237
aoqi@0 238 SerialFieldTag[] sfTag = spfDoc.serialFieldTags();
aoqi@0 239 for (int i = 0; i < sfTag.length; i++) {
aoqi@0 240 if (sfTag[i].fieldName() == null || sfTag[i].fieldType() == null) // ignore malformed @serialField tags
aoqi@0 241 continue;
aoqi@0 242
aoqi@0 243 Name fieldName = names.fromString(sfTag[i].fieldName());
aoqi@0 244
aoqi@0 245 // Look for a FieldDocImpl that is documented by serialFieldTagImpl.
aoqi@0 246 for (Scope.Entry e = def.members().lookup(fieldName); e.scope != null; e = e.next()) {
aoqi@0 247 if (e.sym.kind == Kinds.VAR) {
aoqi@0 248 VarSymbol f = (VarSymbol)e.sym;
aoqi@0 249 FieldDocImpl fdi = env.getFieldDoc(f);
aoqi@0 250 ((SerialFieldTagImpl)(sfTag[i])).mapToFieldDocImpl(fdi);
aoqi@0 251 break;
aoqi@0 252 }
aoqi@0 253 }
aoqi@0 254 }
aoqi@0 255 }
aoqi@0 256
aoqi@0 257 /**
aoqi@0 258 * Return serializable fields in class. <p>
aoqi@0 259 *
aoqi@0 260 * Returns either a list of default fields documented by serial tag comment or
aoqi@0 261 * javadoc comment<p>
aoqi@0 262 * Or Returns a single FieldDocImpl for serialPersistentField. There is a
aoqi@0 263 * serialField tag for each serializable field.<p>
aoqi@0 264 *
aoqi@0 265 * @return an array of FieldDocImpl for representing the visible
aoqi@0 266 * fields in this class.
aoqi@0 267 */
aoqi@0 268 FieldDoc[] fields() {
aoqi@0 269 return (FieldDoc[])fields.toArray(new FieldDocImpl[fields.length()]);
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 /**
aoqi@0 273 * Return serialization methods in class.
aoqi@0 274 *
aoqi@0 275 * @return an array of MethodDocImpl for serialization methods in this class.
aoqi@0 276 */
aoqi@0 277 MethodDoc[] methods() {
aoqi@0 278 return methods.toArray(new MethodDoc[methods.length()]);
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 /**
aoqi@0 282 * Returns true if Serializable fields are defined explicitly using
aoqi@0 283 * member, serialPersistentFields.
aoqi@0 284 *
aoqi@0 285 * @see #fields()
aoqi@0 286 */
aoqi@0 287 boolean definesSerializableFields() {
aoqi@0 288 return definesSerializableFields;
aoqi@0 289 }
aoqi@0 290 }

mercurial