< prev index next >

src/java.base/share/classes/java/io/ObjectStreamClass.java

Print this page
rev 53109 : [mq]: 6996807-FieldReflectorKey-hash-code-compuation-can-be-improved
   1 /*
   2  * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


2258 
2259         if (entry instanceof FieldReflector) {
2260             return (FieldReflector) entry;
2261         } else if (entry instanceof InvalidClassException) {
2262             throw (InvalidClassException) entry;
2263         } else if (entry instanceof RuntimeException) {
2264             throw (RuntimeException) entry;
2265         } else if (entry instanceof Error) {
2266             throw (Error) entry;
2267         } else {
2268             throw new InternalError("unexpected entry: " + entry);
2269         }
2270     }
2271 
2272     /**
2273      * FieldReflector cache lookup key.  Keys are considered equal if they
2274      * refer to the same class and equivalent field formats.
2275      */
2276     private static class FieldReflectorKey extends WeakReference<Class<?>> {
2277 
2278         private final String sigs;
2279         private final int hash;
2280         private final boolean nullClass;
2281 
2282         FieldReflectorKey(Class<?> cl, ObjectStreamField[] fields,
2283                           ReferenceQueue<Class<?>> queue)
2284         {
2285             super(cl, queue);
2286             nullClass = (cl == null);
2287             StringBuilder sbuf = new StringBuilder();
2288             for (int i = 0; i < fields.length; i++) {
2289                 ObjectStreamField f = fields[i];
2290                 sbuf.append(f.getName()).append(f.getSignature());

2291             }
2292             sigs = sbuf.toString();
2293             hash = System.identityHashCode(cl) + sigs.hashCode();
2294         }
2295 
2296         public int hashCode() {
2297             return hash;
2298         }
2299 
2300         public boolean equals(Object obj) {
2301             if (obj == this) {
2302                 return true;
2303             }
2304 
2305             if (obj instanceof FieldReflectorKey) {
2306                 FieldReflectorKey other = (FieldReflectorKey) obj;
2307                 Class<?> referent;
2308                 return (nullClass ? other.nullClass
2309                                   : ((referent = get()) != null) &&
2310                                     (referent == other.get())) &&
2311                     sigs.equals(other.sigs);
2312             } else {
2313                 return false;
2314             }
2315         }
2316     }
2317 
2318     /**
2319      * Matches given set of serializable fields with serializable fields
2320      * obtained from the given local class descriptor (which contain bindings
2321      * to reflective Field objects).  Returns list of ObjectStreamFields in
2322      * which each ObjectStreamField whose signature matches that of a local
2323      * field contains a Field object for that field; unmatched
2324      * ObjectStreamFields contain null Field objects.  Shared/unshared settings
2325      * of the returned ObjectStreamFields also reflect those of matched local
2326      * ObjectStreamFields.  Throws InvalidClassException if unresolvable type
2327      * conflicts exist between the two sets of fields.
2328      */
2329     private static ObjectStreamField[] matchFields(ObjectStreamField[] fields,
2330                                                    ObjectStreamClass localDesc)
2331         throws InvalidClassException


   1 /*
   2  * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


2258 
2259         if (entry instanceof FieldReflector) {
2260             return (FieldReflector) entry;
2261         } else if (entry instanceof InvalidClassException) {
2262             throw (InvalidClassException) entry;
2263         } else if (entry instanceof RuntimeException) {
2264             throw (RuntimeException) entry;
2265         } else if (entry instanceof Error) {
2266             throw (Error) entry;
2267         } else {
2268             throw new InternalError("unexpected entry: " + entry);
2269         }
2270     }
2271 
2272     /**
2273      * FieldReflector cache lookup key.  Keys are considered equal if they
2274      * refer to the same class and equivalent field formats.
2275      */
2276     private static class FieldReflectorKey extends WeakReference<Class<?>> {
2277 
2278         private final String[] sigs;
2279         private final int hash;
2280         private final boolean nullClass;
2281 
2282         FieldReflectorKey(Class<?> cl, ObjectStreamField[] fields,
2283                           ReferenceQueue<Class<?>> queue)
2284         {
2285             super(cl, queue);
2286             nullClass = (cl == null);
2287             sigs = new String[2 * fields.length];
2288             for (int i = 0; i < fields.length; i++) {
2289                 ObjectStreamField f = fields[i];
2290                 sigs[2 * i] = f.getName();
2291                 sigs[2 * i + 1] = f.getSignature();
2292             }
2293             hash = System.identityHashCode(cl) + Arrays.hashCode(sigs);

2294         }
2295 
2296         public int hashCode() {
2297             return hash;
2298         }
2299 
2300         public boolean equals(Object obj) {
2301             if (obj == this) {
2302                 return true;
2303             }
2304 
2305             if (obj instanceof FieldReflectorKey) {
2306                 FieldReflectorKey other = (FieldReflectorKey) obj;
2307                 Class<?> referent;
2308                 return (nullClass ? other.nullClass
2309                                   : ((referent = get()) != null) &&
2310                                     (referent == other.get())) &&
2311                     Arrays.equals(sigs, other.sigs);
2312             } else {
2313                 return false;
2314             }
2315         }
2316     }
2317 
2318     /**
2319      * Matches given set of serializable fields with serializable fields
2320      * obtained from the given local class descriptor (which contain bindings
2321      * to reflective Field objects).  Returns list of ObjectStreamFields in
2322      * which each ObjectStreamField whose signature matches that of a local
2323      * field contains a Field object for that field; unmatched
2324      * ObjectStreamFields contain null Field objects.  Shared/unshared settings
2325      * of the returned ObjectStreamFields also reflect those of matched local
2326      * ObjectStreamFields.  Throws InvalidClassException if unresolvable type
2327      * conflicts exist between the two sets of fields.
2328      */
2329     private static ObjectStreamField[] matchFields(ObjectStreamField[] fields,
2330                                                    ObjectStreamClass localDesc)
2331         throws InvalidClassException


< prev index next >