1 /* 2 * Copyright (c) 2015, 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 23 * questions. 24 */ 25 26 package jdk.nashorn.internal.runtime; 27 28 import java.io.Serializable; 29 import jdk.nashorn.internal.objects.NativeSymbol; 30 31 /** 32 * This class represents a unique, non-String Object property key as defined in ECMAScript 6. 33 */ 34 public final class Symbol implements Serializable { 35 36 private final String name; 37 38 private static final long serialVersionUID = -2988436597549486913L; 39 40 /** 41 * Symbol constructor 42 * @param name symbol name 43 */ 44 public Symbol(final String name) { 45 this.name = name; 46 } 47 48 @Override 49 public String toString() { 50 return "Symbol(" + name + ")"; 51 } 52 53 /** 54 * Return the symbol's name 55 * @return the name 56 */ 57 public final String getName() { 58 return name; 59 } 60 61 private Object writeReplace() { 62 // If this symbol is a globally registered one, replace it with a 63 // GlobalSymbol in serialized stream. 64 return NativeSymbol.keyFor(null, this) == name ? new GlobalSymbol(name) : this; 65 } 66 67 /** 68 * Represents a globally registered (with NativeSymbol._for) symbol in the 69 * serialized stream. Upon deserialization, it resolves to the globally 70 * registered symbol. 71 */ 72 private static class GlobalSymbol implements Serializable { 73 private static final long serialVersionUID = 1L; 74 75 private final String name; 76 77 GlobalSymbol(final String name) { 78 this.name = name; 79 } 80 81 private Object readResolve() { 82 return NativeSymbol._for(null, name); 83 } 84 } 85 }