PropertiesHelper.java revision 1567:2beaef2b6a88
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 26package jdk.nashorn.tools.jjs; 27 28import java.io.IOException; 29import java.util.ArrayList; 30import java.util.Arrays; 31import java.util.Collections; 32import java.util.List; 33import java.util.WeakHashMap; 34import java.util.stream.Collectors; 35import jdk.nashorn.internal.runtime.JSType; 36import jdk.nashorn.internal.runtime.NativeJavaPackage; 37import jdk.nashorn.internal.runtime.PropertyMap; 38import jdk.nashorn.internal.runtime.ScriptObject; 39import jdk.nashorn.internal.runtime.ScriptRuntime; 40import jdk.nashorn.internal.objects.NativeJava; 41 42/* 43 * A helper class to get properties of a given object for source code completion. 44 */ 45final class PropertiesHelper { 46 // Java package properties helper, may be null 47 private PackagesHelper pkgsHelper; 48 // cached properties list 49 private final WeakHashMap<Object, List<String>> propsCache = new WeakHashMap<>(); 50 51 /** 52 * Construct a new PropertiesHelper. 53 * 54 * @param classPath Class path to compute properties of java package objects 55 */ 56 PropertiesHelper(final String classPath) { 57 try { 58 this.pkgsHelper = new PackagesHelper(classPath); 59 } catch (final IOException exp) { 60 if (Main.DEBUG) { 61 exp.printStackTrace(); 62 } 63 this.pkgsHelper = null; 64 } 65 } 66 67 void close() throws Exception { 68 propsCache.clear(); 69 pkgsHelper.close(); 70 } 71 72 /** 73 * returns the list of properties of the given object. 74 * 75 * @param obj object whose property list is returned 76 * @return the list of properties of the given object 77 */ 78 List<String> getProperties(final Object obj) { 79 assert obj != null && obj != ScriptRuntime.UNDEFINED; 80 81 // wrap JS primitives as objects before gettting properties 82 if (JSType.isPrimitive(obj)) { 83 return getProperties(JSType.toScriptObject(obj)); 84 } 85 86 // Handle Java package prefix case first. Should do it before checking 87 // for its super class ScriptObject! 88 if (obj instanceof NativeJavaPackage) { 89 if (pkgsHelper != null) { 90 return pkgsHelper.getPackageProperties(((NativeJavaPackage)obj).getName()); 91 } else { 92 return Collections.<String>emptyList(); 93 } 94 } 95 96 // script object - all inherited and non-enumerable, non-index properties 97 if (obj instanceof ScriptObject) { 98 final ScriptObject sobj = (ScriptObject)obj; 99 final PropertyMap pmap = sobj.getMap(); 100 if (propsCache.containsKey(pmap)) { 101 return propsCache.get(pmap); 102 } 103 final String[] keys = sobj.getAllKeys(); 104 List<String> props = Arrays.asList(keys); 105 props = props.stream() 106 .filter(s -> Character.isJavaIdentifierStart(s.charAt(0))) 107 .collect(Collectors.toList()); 108 Collections.sort(props); 109 // cache properties against the PropertyMap 110 propsCache.put(pmap, props); 111 return props; 112 } 113 114 // java class case - don't refer to StaticClass directly 115 if (NativeJava.isType(ScriptRuntime.UNDEFINED, obj)) { 116 if (propsCache.containsKey(obj)) { 117 return propsCache.get(obj); 118 } 119 final List<String> props = NativeJava.getProperties(obj); 120 Collections.sort(props); 121 // cache properties against the StaticClass representing the class 122 propsCache.put(obj, props); 123 return props; 124 } 125 126 // any other Java object 127 final Class<?> clazz = obj.getClass(); 128 if (propsCache.containsKey(clazz)) { 129 return propsCache.get(clazz); 130 } 131 132 final List<String> props = NativeJava.getProperties(obj); 133 Collections.sort(props); 134 // cache properties against the Class object 135 propsCache.put(clazz, props); 136 return props; 137 } 138 139 /** 140 * Returns the list of properties of the given object that start with the given prefix. 141 * 142 * @param obj object whose property list is returned 143 * @param prefix property prefix to be matched 144 * @return the list of properties of the given object 145 */ 146 List<String> getProperties(final Object obj, final String prefix) { 147 assert prefix != null && !prefix.isEmpty(); 148 return getProperties(obj).stream() 149 .filter(s -> s.startsWith(prefix)) 150 .collect(Collectors.toList()); 151 } 152} 153