Thu, 11 Jul 2013 16:34:55 +0530
8020325: static property does not work on accessible, public classes
Reviewed-by: attila, hannesw, lagergren
jlaskey@3 | 1 | /* |
jlaskey@7 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
jlaskey@3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jlaskey@3 | 4 | * |
jlaskey@3 | 5 | * This code is free software; you can redistribute it and/or modify it |
jlaskey@3 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jlaskey@3 | 7 | * published by the Free Software Foundation. Oracle designates this |
jlaskey@3 | 8 | * particular file as subject to the "Classpath" exception as provided |
jlaskey@3 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jlaskey@3 | 10 | * |
jlaskey@3 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jlaskey@3 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jlaskey@3 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jlaskey@3 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jlaskey@3 | 15 | * accompanied this code). |
jlaskey@3 | 16 | * |
jlaskey@3 | 17 | * You should have received a copy of the GNU General Public License version |
jlaskey@3 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jlaskey@3 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jlaskey@3 | 20 | * |
jlaskey@3 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jlaskey@3 | 22 | * or visit www.oracle.com if you need additional information or have any |
jlaskey@3 | 23 | * questions. |
jlaskey@3 | 24 | */ |
jlaskey@3 | 25 | |
jlaskey@3 | 26 | package jdk.nashorn.internal.objects; |
jlaskey@3 | 27 | |
jlaskey@3 | 28 | import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
jlaskey@3 | 29 | |
lagergren@11 | 30 | import java.io.PrintWriter; |
jlaskey@3 | 31 | import java.util.Objects; |
jlaskey@3 | 32 | import jdk.nashorn.internal.objects.annotations.Attribute; |
jlaskey@3 | 33 | import jdk.nashorn.internal.objects.annotations.Function; |
jlaskey@3 | 34 | import jdk.nashorn.internal.objects.annotations.ScriptClass; |
jlaskey@3 | 35 | import jdk.nashorn.internal.objects.annotations.Where; |
lagergren@11 | 36 | import jdk.nashorn.internal.runtime.Context; |
jlaskey@3 | 37 | import jdk.nashorn.internal.runtime.PropertyListenerManager; |
jlaskey@3 | 38 | import jdk.nashorn.internal.runtime.PropertyMap; |
jlaskey@3 | 39 | import jdk.nashorn.internal.runtime.ScriptFunction; |
jlaskey@3 | 40 | import jdk.nashorn.internal.runtime.ScriptObject; |
jlaskey@3 | 41 | import jdk.nashorn.internal.runtime.linker.LinkerCallSite; |
jlaskey@3 | 42 | |
jlaskey@3 | 43 | /** |
jlaskey@3 | 44 | * Nashorn specific debug utils. This is meant for Nashorn developers. |
jlaskey@3 | 45 | * The interface is subject to change without notice!! |
jlaskey@3 | 46 | * |
jlaskey@3 | 47 | */ |
jlaskey@3 | 48 | @ScriptClass("Debug") |
sundar@82 | 49 | public final class NativeDebug extends ScriptObject { |
hannesw@380 | 50 | |
hannesw@380 | 51 | // initialized by nasgen |
sundar@418 | 52 | @SuppressWarnings("unused") |
hannesw@380 | 53 | private static PropertyMap $nasgenmap$; |
hannesw@380 | 54 | |
sundar@414 | 55 | private NativeDebug() { |
sundar@414 | 56 | // don't create me! |
sundar@414 | 57 | throw new UnsupportedOperationException(); |
jlaskey@3 | 58 | } |
jlaskey@3 | 59 | |
jlaskey@3 | 60 | @Override |
jlaskey@3 | 61 | public String getClassName() { |
jlaskey@3 | 62 | return "Debug"; |
jlaskey@3 | 63 | } |
jlaskey@3 | 64 | |
jlaskey@3 | 65 | /** |
jlaskey@3 | 66 | * Nashorn extension: get context, context utility |
jlaskey@3 | 67 | * |
jlaskey@3 | 68 | * @param self self reference |
jlaskey@3 | 69 | * @return context |
jlaskey@3 | 70 | */ |
jlaskey@3 | 71 | @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
jlaskey@3 | 72 | public static Object getContext(final Object self) { |
sundar@82 | 73 | final SecurityManager sm = System.getSecurityManager(); |
sundar@82 | 74 | if (sm != null) { |
sundar@136 | 75 | sm.checkPermission(new RuntimePermission("nashorn.getContext")); |
sundar@82 | 76 | } |
jlaskey@3 | 77 | return Global.getThisContext(); |
jlaskey@3 | 78 | } |
jlaskey@3 | 79 | |
jlaskey@3 | 80 | /** |
jlaskey@3 | 81 | * Nashorn extension: get map from {@link ScriptObject} |
jlaskey@3 | 82 | * |
jlaskey@3 | 83 | * @param self self reference |
jlaskey@3 | 84 | * @param obj script object |
jlaskey@3 | 85 | * @return the map for the current ScriptObject |
jlaskey@3 | 86 | */ |
jlaskey@3 | 87 | @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
jlaskey@3 | 88 | public static Object map(final Object self, final Object obj) { |
jlaskey@3 | 89 | if (obj instanceof ScriptObject) { |
jlaskey@3 | 90 | return ((ScriptObject)obj).getMap(); |
jlaskey@3 | 91 | } |
jlaskey@3 | 92 | return UNDEFINED; |
jlaskey@3 | 93 | } |
jlaskey@3 | 94 | |
jlaskey@3 | 95 | /** |
jlaskey@3 | 96 | * Nashorn extension: get spill vector from {@link ScriptObject} |
jlaskey@3 | 97 | * |
jlaskey@3 | 98 | * @param self self reference |
jlaskey@3 | 99 | * @param obj script object |
jlaskey@3 | 100 | * @return the spill vector for the given ScriptObject |
jlaskey@3 | 101 | */ |
jlaskey@3 | 102 | @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
jlaskey@3 | 103 | public static Object spill(final Object self, final Object obj) { |
jlaskey@3 | 104 | if (obj instanceof ScriptObject) { |
jlaskey@3 | 105 | return ((ScriptObject)obj).spill; |
jlaskey@3 | 106 | } |
jlaskey@3 | 107 | return UNDEFINED; |
jlaskey@3 | 108 | } |
jlaskey@3 | 109 | |
jlaskey@3 | 110 | /** |
jlaskey@3 | 111 | * Check object identity comparison regardless of type |
jlaskey@3 | 112 | * |
jlaskey@3 | 113 | * @param self self reference |
jlaskey@3 | 114 | * @param obj1 first object in comparison |
jlaskey@3 | 115 | * @param obj2 second object in comparison |
jlaskey@3 | 116 | * @return true if reference identity |
jlaskey@3 | 117 | */ |
jlaskey@3 | 118 | @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
jlaskey@3 | 119 | public static Object identical(final Object self, final Object obj1, final Object obj2) { |
jlaskey@3 | 120 | return obj1 == obj2; |
jlaskey@3 | 121 | } |
jlaskey@3 | 122 | |
jlaskey@3 | 123 | /** |
jlaskey@3 | 124 | * Object util - getClass |
jlaskey@3 | 125 | * |
jlaskey@3 | 126 | * @param self self reference |
jlaskey@3 | 127 | * @param obj object |
jlaskey@3 | 128 | * @return class of {@code obj} |
jlaskey@3 | 129 | */ |
jlaskey@3 | 130 | @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
jlaskey@3 | 131 | public static Object getClass(final Object self, final Object obj) { |
jlaskey@3 | 132 | if (obj != null) { |
jlaskey@3 | 133 | return obj.getClass(); |
jlaskey@3 | 134 | } |
jlaskey@3 | 135 | return UNDEFINED; |
jlaskey@3 | 136 | } |
jlaskey@3 | 137 | |
jlaskey@3 | 138 | /** |
jlaskey@3 | 139 | * Object util - equals |
jlaskey@3 | 140 | * |
jlaskey@3 | 141 | * @param self self reference |
jlaskey@3 | 142 | * @param obj1 first object in comparison |
jlaskey@3 | 143 | * @param obj2 second object in comparison |
jlaskey@3 | 144 | * @return return {@link Object#equals(Object)} for objects. |
jlaskey@3 | 145 | */ |
jlaskey@3 | 146 | @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
jlaskey@3 | 147 | public static Object equals(final Object self, final Object obj1, final Object obj2) { |
sundar@418 | 148 | return Objects.equals(obj1, obj2); |
jlaskey@3 | 149 | } |
jlaskey@3 | 150 | |
jlaskey@3 | 151 | /** |
jlaskey@3 | 152 | * Object util - toJavaString |
jlaskey@3 | 153 | * |
jlaskey@3 | 154 | * @param self self reference |
jlaskey@3 | 155 | * @param obj object to represent as a string |
jlaskey@3 | 156 | * @return Java string representation of {@code obj} |
jlaskey@3 | 157 | */ |
jlaskey@3 | 158 | @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
jlaskey@3 | 159 | public static Object toJavaString(final Object self, final Object obj) { |
jlaskey@3 | 160 | return Objects.toString(obj); |
jlaskey@3 | 161 | } |
jlaskey@3 | 162 | |
jlaskey@3 | 163 | /** |
jlaskey@3 | 164 | * Do not call overridden toString -- use default toString impl |
jlaskey@3 | 165 | * |
jlaskey@3 | 166 | * @param self self reference |
jlaskey@3 | 167 | * @param obj object to represent as a string |
jlaskey@3 | 168 | * @return string representation |
jlaskey@3 | 169 | */ |
jlaskey@3 | 170 | @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
jlaskey@3 | 171 | public static Object toIdentString(final Object self, final Object obj) { |
jlaskey@3 | 172 | if (obj == null) { |
jlaskey@3 | 173 | return "null"; |
jlaskey@3 | 174 | } |
jlaskey@3 | 175 | |
jlaskey@3 | 176 | final int hash = System.identityHashCode(obj); |
jlaskey@3 | 177 | return obj.getClass() + "@" + Integer.toHexString(hash); |
jlaskey@3 | 178 | } |
jlaskey@3 | 179 | |
jlaskey@3 | 180 | /** |
sundar@418 | 181 | * Returns the property listener count for a script object |
sundar@428 | 182 | * |
sundar@428 | 183 | * @param self self reference |
sundar@428 | 184 | * @param obj script object whose listener count is returned |
sundar@418 | 185 | * @return listener count |
sundar@418 | 186 | */ |
sundar@418 | 187 | @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
sundar@418 | 188 | public static Object getListenerCount(final Object self, final Object obj) { |
sundar@418 | 189 | return (obj instanceof ScriptObject)? ((ScriptObject)obj).getListenerCount() : 0; |
sundar@418 | 190 | } |
sundar@418 | 191 | |
sundar@418 | 192 | /** |
jlaskey@3 | 193 | * Dump all Nashorn debug mode counters. Calling this may be better if |
jlaskey@3 | 194 | * you want to print all counters. This way you can avoid too many callsites |
jlaskey@3 | 195 | * due to counter access itself!! |
jlaskey@3 | 196 | * @param self self reference |
jlaskey@3 | 197 | * @return undefined |
jlaskey@3 | 198 | */ |
lagergren@253 | 199 | @SuppressWarnings("resource") |
jlaskey@3 | 200 | @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) |
jlaskey@3 | 201 | public static Object dumpCounters(final Object self) { |
sundar@41 | 202 | final PrintWriter out = Context.getCurrentErr(); |
lagergren@11 | 203 | |
jlaskey@3 | 204 | out.println("ScriptObject count " + ScriptObject.getCount()); |
jlaskey@3 | 205 | out.println("Scope count " + ScriptObject.getScopeCount()); |
jlaskey@3 | 206 | out.println("ScriptObject listeners added " + PropertyListenerManager.getListenersAdded()); |
jlaskey@3 | 207 | out.println("ScriptObject listeners removed " + PropertyListenerManager.getListenersRemoved()); |
hannesw@380 | 208 | out.println("ScriptFunction constructor calls " + ScriptFunction.getConstructorCount()); |
jlaskey@3 | 209 | out.println("ScriptFunction invokes " + ScriptFunction.getInvokes()); |
jlaskey@3 | 210 | out.println("ScriptFunction allocations " + ScriptFunction.getAllocations()); |
jlaskey@3 | 211 | out.println("PropertyMap count " + PropertyMap.getCount()); |
jlaskey@3 | 212 | out.println("PropertyMap cloned " + PropertyMap.getClonedCount()); |
sundar@418 | 213 | out.println("PropertyMap shared " + PropertyMap.getSharedCount()); |
sundar@418 | 214 | out.println("PropertyMap duplicated " + PropertyMap.getDuplicatedCount()); |
jlaskey@3 | 215 | out.println("PropertyMap history hit " + PropertyMap.getHistoryHit()); |
jlaskey@3 | 216 | out.println("PropertyMap proto invalidations " + PropertyMap.getProtoInvalidations()); |
jlaskey@3 | 217 | out.println("PropertyMap proto history hit " + PropertyMap.getProtoHistoryHit()); |
jlaskey@3 | 218 | out.println("PropertyMap setProtoNewMapCount " + PropertyMap.getSetProtoNewMapCount()); |
jlaskey@3 | 219 | out.println("Callsite count " + LinkerCallSite.getCount()); |
jlaskey@3 | 220 | out.println("Callsite misses " + LinkerCallSite.getMissCount()); |
jlaskey@3 | 221 | out.println("Callsite misses by site at " + LinkerCallSite.getMissSamplingPercentage() + "%"); |
lagergren@11 | 222 | |
jlaskey@3 | 223 | LinkerCallSite.getMissCounts(out); |
lagergren@11 | 224 | |
jlaskey@3 | 225 | return UNDEFINED; |
jlaskey@3 | 226 | } |
jlaskey@3 | 227 | } |