1/* 2 * Copyright (c) 2016, 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.jshell; 27 28import java.util.ArrayList; 29import java.util.Collection; 30import java.util.Collections; 31import java.util.HashMap; 32import java.util.List; 33import java.util.Map; 34import java.util.Set; 35import java.util.regex.Matcher; 36import java.util.stream.Collectors; 37import jdk.jshell.Wrap.CompoundWrap; 38import static jdk.jshell.Util.PREFIX_PATTERN; 39import static jdk.jshell.Util.REPL_CLASS_PREFIX; 40import static jdk.jshell.Util.REPL_DOESNOTMATTER_CLASS_NAME; 41import static jdk.jshell.Util.asLetters; 42 43/** 44 * 45 * @author Robert Field 46 */ 47class OuterWrapMap { 48 49 private final JShell state; 50 private final Map<String,OuterSnippetsClassWrap> classOuters = new HashMap<>(); 51 52 OuterWrapMap(JShell state) { 53 this.state = state; 54 } 55 56 OuterSnippetsClassWrap getOuter(String className) { 57 Matcher matcher = PREFIX_PATTERN.matcher(className); 58 String cn; 59 if (matcher.find() && (cn = matcher.group("class")) != null) { 60 return classOuters.get(cn); 61 } 62 return null; 63 } 64 65 private CompoundWrap wrappedInClass(String className, String imports, List<Wrap> wraps) { 66 List<Object> elems = new ArrayList<>(wraps.size() + 2); 67 elems.add(imports + 68 "class " + className + " {\n"); 69 elems.addAll(wraps); 70 elems.add("}\n"); 71 return new CompoundWrap(elems.toArray()); 72 } 73 74 OuterWrap wrapInClass(Set<Key> except, Collection<Snippet> plus, 75 List<Snippet> snippets, List<Wrap> wraps) { 76 String imports = state.maps.packageAndImportsExcept(except, plus); 77 // className is unique to the set of snippets and their version (seq) 78 String className = REPL_CLASS_PREFIX + snippets.stream() 79 .sorted((sn1, sn2) -> sn1.key().index() - sn2.key().index()) 80 .map(sn -> "" + sn.key().index() + asLetters(sn.sequenceNumber())) 81 .collect(Collectors.joining("_")); 82 CompoundWrap w = wrappedInClass(className, imports, wraps); 83 OuterSnippetsClassWrap now = new OuterSnippetsClassWrap(className, w, snippets, wraps); 84 classOuters.put(className, now); 85 return now; 86 } 87 88 OuterWrap wrapInTrialClass(Wrap wrap) { 89 String imports = state.maps.packageAndImportsExcept(null, null); 90 CompoundWrap w = wrappedInClass(REPL_DOESNOTMATTER_CLASS_NAME, imports, 91 Collections.singletonList(wrap)); 92 return new OuterWrap(w); 93 } 94 95 OuterWrap wrapImport(Wrap guts, Snippet sn) { 96 return new OuterImportSnippetWrap(guts, sn); 97 } 98} 99