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

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 554
9d9f26857129
child 1359
25e14ad23cef
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

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

mercurial