1 /*
   2  * Copyright (c) 2014, 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 
  26 package jdk.jshell.execution;
  27 
  28 import java.io.ObjectInput;
  29 import java.io.ObjectOutput;
  30 import java.util.HashMap;
  31 import java.util.List;
  32 import java.util.Map;
  33 import java.util.stream.Stream;
  34 import com.sun.jdi.ReferenceType;
  35 import com.sun.jdi.VirtualMachine;
  36 import jdk.jshell.spi.ExecutionControl;
  37 import static java.util.stream.Collectors.toMap;
  38 
  39 /**
  40  * Abstract JDI implementation of {@link jdk.jshell.spi.ExecutionControl}
  41  */
  42 public abstract class JDIExecutionControl extends StreamingExecutionControl implements ExecutionControl {
  43 
  44     /**
  45      * Mapping from class names to JDI {@link com.sun.jdi.ReferenceType}.
  46      */
  47     protected Map<String, ReferenceType> toReferenceType = new HashMap<>();
  48 
  49     /**
  50      * Create an instance.
  51      * @param out the output from the remote agent
  52      * @param in the input to the remote agent
  53      * @param log the log
  54      */
  55     protected JDIExecutionControl(ObjectOutput out, ObjectInput in, ECLogger log) {
  56         super(out, in, log);
  57     }
  58 
  59     /**
  60      * Returns the JDI VirtualMachine instance.
  61      *
  62      * @return the virtual machine
  63      * @throws EngineTerminationException if the VM is dead/disconnected
  64      */
  65     protected abstract VirtualMachine vm() throws EngineTerminationException;
  66     
  67     /**
  68      * Redefine the specified classes. Where 'redefine' is, as in JDI and JVMTI,
  69      * an in-place replacement of the classes (preserving class identity) --
  70      * that is, existing references to the class do not need to be recompiled.
  71      * This implementation uses JDI
  72      * {@link com.sun.jdi.VirtualMachine#redefineClasses(java.util.Map) }.
  73      * It will be unsuccessful if
  74      * the signature of the class has changed (see the JDI spec). The
  75      * JShell-core is designed to adapt to unsuccessful redefine.
  76      */
  77     @Override
  78     public void redefine(ClassBytecodes[] cbcs)
  79             throws ClassInstallException, EngineTerminationException {
  80         try {
  81             // Convert to the JDI ReferenceType to class bytes map form needed
  82             // by JDI.
  83             VirtualMachine vm = vm();
  84             Map<ReferenceType, byte[]> rmp = Stream.of(cbcs)
  85                     .collect(toMap(
  86                             cbc -> toReferenceType.computeIfAbsent(cbc.name, name -> nameToRef(vm, name)),
  87                             cbc -> cbc.bytecodes));
  88             // Attempt redefine.  Throws exceptions on failure.
  89             vm().redefineClasses(rmp);
  90         } catch (EngineTerminationException ex) {
  91             throw ex;
  92         } catch (Exception ex) {
  93             throw new ClassInstallException("redefine: " + ex.getMessage(), new boolean[cbcs.length]);
  94         }
  95     }
  96 
  97     private static ReferenceType nameToRef(VirtualMachine vm, String name) {
  98         List<ReferenceType> rtl = vm.classesByName(name);
  99         if (rtl.size() != 1) {
 100             return null;
 101         }
 102         return rtl.get(0);
 103     }
 104 
 105 }