langtools/src/jdk.jshell/share/classes/jdk/jshell/execution/JDIExecutionControl.java
changeset 41941 a935ac3f5274
parent 41940 048d559e9da7
child 41942 260c724e5614
equal deleted inserted replaced
41940:048d559e9da7 41941:a935ac3f5274
     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 ReferenceType}.
       
    46      */
       
    47     private final 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      */
       
    54     protected JDIExecutionControl(ObjectOutput out, ObjectInput in) {
       
    55         super(out, in);
       
    56     }
       
    57 
       
    58     /**
       
    59      * Returns the JDI {@link VirtualMachine} instance.
       
    60      *
       
    61      * @return the virtual machine
       
    62      * @throws EngineTerminationException if the VM is dead/disconnected
       
    63      */
       
    64     protected abstract VirtualMachine vm() throws EngineTerminationException;
       
    65 
       
    66     /**
       
    67      * Redefine the specified classes. Where 'redefine' is, as in JDI and JVMTI,
       
    68      * an in-place replacement of the classes (preserving class identity) --
       
    69      * that is, existing references to the class do not need to be recompiled.
       
    70      * This implementation uses JDI
       
    71      * {@link com.sun.jdi.VirtualMachine#redefineClasses(java.util.Map) }.
       
    72      * It will be unsuccessful if
       
    73      * the signature of the class has changed (see the JDI spec). The
       
    74      * JShell-core is designed to adapt to unsuccessful redefine.
       
    75      */
       
    76     @Override
       
    77     public void redefine(ClassBytecodes[] cbcs)
       
    78             throws ClassInstallException, EngineTerminationException {
       
    79         try {
       
    80             // Convert to the JDI ReferenceType to class bytes map form needed
       
    81             // by JDI.
       
    82             VirtualMachine vm = vm();
       
    83             Map<ReferenceType, byte[]> rmp = Stream.of(cbcs)
       
    84                     .collect(toMap(
       
    85                             cbc -> referenceType(vm, cbc.name()),
       
    86                             cbc -> cbc.bytecodes()));
       
    87             // Attempt redefine.  Throws exceptions on failure.
       
    88             vm().redefineClasses(rmp);
       
    89         } catch (EngineTerminationException ex) {
       
    90             throw ex;
       
    91         } catch (Exception ex) {
       
    92             throw new ClassInstallException("redefine: " + ex.getMessage(), new boolean[cbcs.length]);
       
    93         }
       
    94     }
       
    95 
       
    96     /**
       
    97      * Returns the JDI {@link ReferenceType} corresponding to the specified
       
    98      * class name.
       
    99      *
       
   100      * @param vm the current JDI {@link VirtualMachine} as returned by
       
   101      * {@code vm()}
       
   102      * @param name the class name to look-up
       
   103      * @return the corresponding {@link ReferenceType}
       
   104      */
       
   105     protected ReferenceType referenceType(VirtualMachine vm, String name) {
       
   106         return toReferenceType.computeIfAbsent(name, n -> nameToRef(vm, n));
       
   107     }
       
   108 
       
   109     private static ReferenceType nameToRef(VirtualMachine vm, String name) {
       
   110         List<ReferenceType> rtl = vm.classesByName(name);
       
   111         if (rtl.size() != 1) {
       
   112             return null;
       
   113         }
       
   114         return rtl.get(0);
       
   115     }
       
   116 
       
   117 }