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.tools.jjs; 27 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 import java.util.Collections; 31 import java.util.List; 32 import java.util.WeakHashMap; 33 import java.util.stream.Collectors; 34 import jdk.nashorn.internal.runtime.JSType; 35 import jdk.nashorn.internal.runtime.PropertyMap; 36 import jdk.nashorn.internal.runtime.ScriptObject; 37 import jdk.nashorn.internal.runtime.ScriptRuntime; 38 import jdk.nashorn.internal.objects.NativeJava; 39 40 /* 41 * A helper class to get properties of a given object for source code completion. 42 */ 43 final class PropertiesHelper { 44 private PropertiesHelper() {} 45 46 // cached properties list 47 private static final WeakHashMap<Object, List<String>> propsCache = new WeakHashMap<>(); 48 49 // returns the list of properties of the given object 50 static List<String> getProperties(final Object obj) { 51 assert obj != null && obj != ScriptRuntime.UNDEFINED; 52 53 if (JSType.isPrimitive(obj)) { 54 return getProperties(JSType.toScriptObject(obj)); 55 } 56 57 if (obj instanceof ScriptObject) { 58 final ScriptObject sobj = (ScriptObject)obj; 59 final PropertyMap pmap = sobj.getMap(); 60 if (propsCache.containsKey(pmap)) { 61 return propsCache.get(pmap); 62 } 63 final String[] keys = sobj.getAllKeys(); 64 List<String> props = Arrays.asList(keys); 65 props = props.stream() 66 .filter(s -> Character.isJavaIdentifierStart(s.charAt(0))) 67 .collect(Collectors.toList()); 68 Collections.sort(props); 69 // cache properties against the PropertyMap 70 propsCache.put(pmap, props); 71 return props; 72 } 73 74 if (NativeJava.isType(ScriptRuntime.UNDEFINED, obj)) { 75 if (propsCache.containsKey(obj)) { 76 return propsCache.get(obj); 77 } 78 final List<String> props = NativeJava.getProperties(obj); 79 Collections.sort(props); 80 // cache properties against the StaticClass representing the class 81 propsCache.put(obj, props); 82 return props; 83 } 84 85 final Class<?> clazz = obj.getClass(); 86 if (propsCache.containsKey(clazz)) { 87 return propsCache.get(clazz); 88 } 89 90 final List<String> props = NativeJava.getProperties(obj); 91 Collections.sort(props); 92 // cache properties against the Class object 93 propsCache.put(clazz, props); 94 return props; 95 } 96 97 // returns the list of properties of the given object that start with the given prefix 98 static List<String> getProperties(final Object obj, final String prefix) { 99 assert prefix != null && !prefix.isEmpty(); 100 return getProperties(obj).stream() 101 .filter(s -> s.startsWith(prefix)) 102 .collect(Collectors.toList()); 103 } 104 }