--- /dev/null 2016-07-12 14:35:29.888302783 -0700 +++ new/src/jdk.jshell/share/classes/jdk/jshell/execution/JDIExecutionControl.java 2016-07-12 22:46:56.433978399 -0700 @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.jshell.execution; + +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; +import com.sun.jdi.ReferenceType; +import com.sun.jdi.VirtualMachine; +import jdk.jshell.spi.ExecutionControl; +import static java.util.stream.Collectors.toMap; + +/** + * Abstract JDI implementation of {@link jdk.jshell.spi.ExecutionControl} + */ +public abstract class JDIExecutionControl extends StreamingExecutionControl implements ExecutionControl { + + /** + * Mapping from class names to JDI {@link com.sun.jdi.ReferenceType}. + */ + protected Map toReferenceType = new HashMap<>(); + + /** + * Create an instance. + * @param out the output from the remote agent + * @param in the input to the remote agent + * @param log the log + */ + protected JDIExecutionControl(ObjectOutput out, ObjectInput in, ECLogger log) { + super(out, in, log); + } + + /** + * Returns the JDI VirtualMachine instance. + * + * @return the virtual machine + * @throws EngineTerminationException if the VM is dead/disconnected + */ + protected abstract VirtualMachine vm() throws EngineTerminationException; + + /** + * Redefine the specified classes. Where 'redefine' is, as in JDI and JVMTI, + * an in-place replacement of the classes (preserving class identity) -- + * that is, existing references to the class do not need to be recompiled. + * This implementation uses JDI + * {@link com.sun.jdi.VirtualMachine#redefineClasses(java.util.Map) }. + * It will be unsuccessful if + * the signature of the class has changed (see the JDI spec). The + * JShell-core is designed to adapt to unsuccessful redefine. + */ + @Override + public void redefine(ClassBytecodes[] cbcs) + throws ClassInstallException, EngineTerminationException { + try { + // Convert to the JDI ReferenceType to class bytes map form needed + // by JDI. + VirtualMachine vm = vm(); + Map rmp = Stream.of(cbcs) + .collect(toMap( + cbc -> toReferenceType.computeIfAbsent(cbc.name, name -> nameToRef(vm, name)), + cbc -> cbc.bytecodes)); + // Attempt redefine. Throws exceptions on failure. + vm().redefineClasses(rmp); + } catch (EngineTerminationException ex) { + throw ex; + } catch (Exception ex) { + throw new ClassInstallException("redefine: " + ex.getMessage(), new boolean[cbcs.length]); + } + } + + private static ReferenceType nameToRef(VirtualMachine vm, String name) { + List rtl = vm.classesByName(name); + if (rtl.size() != 1) { + return null; + } + return rtl.get(0); + } + +}