|
1 /* |
|
2 * Copyright (c) 2014, 2017, 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 * @since 9 |
|
43 */ |
|
44 public abstract class JdiExecutionControl extends StreamingExecutionControl implements ExecutionControl { |
|
45 |
|
46 /** |
|
47 * Mapping from class names to JDI {@link ReferenceType}. |
|
48 */ |
|
49 private final Map<String, ReferenceType> toReferenceType = new HashMap<>(); |
|
50 |
|
51 /** |
|
52 * Create an instance. |
|
53 * @param out the output from the remote agent |
|
54 * @param in the input to the remote agent |
|
55 */ |
|
56 protected JdiExecutionControl(ObjectOutput out, ObjectInput in) { |
|
57 super(out, in); |
|
58 } |
|
59 |
|
60 /** |
|
61 * Returns the JDI {@link VirtualMachine} instance. |
|
62 * |
|
63 * @return the virtual machine |
|
64 * @throws EngineTerminationException if the VM is dead/disconnected |
|
65 */ |
|
66 protected abstract VirtualMachine vm() throws EngineTerminationException; |
|
67 |
|
68 /** |
|
69 * Redefine the specified classes. Where 'redefine' is, as in JDI and JVMTI, |
|
70 * an in-place replacement of the classes (preserving class identity) -- |
|
71 * that is, existing references to the class do not need to be recompiled. |
|
72 * This implementation uses JDI |
|
73 * {@link com.sun.jdi.VirtualMachine#redefineClasses(java.util.Map) }. |
|
74 * It will be unsuccessful if |
|
75 * the signature of the class has changed (see the JDI spec). The |
|
76 * JShell-core is designed to adapt to unsuccessful redefine. |
|
77 */ |
|
78 @Override |
|
79 public void redefine(ClassBytecodes[] cbcs) |
|
80 throws ClassInstallException, EngineTerminationException { |
|
81 try { |
|
82 // Convert to the JDI ReferenceType to class bytes map form needed |
|
83 // by JDI. |
|
84 VirtualMachine vm = vm(); |
|
85 Map<ReferenceType, byte[]> rmp = Stream.of(cbcs) |
|
86 .collect(toMap( |
|
87 cbc -> referenceType(vm, cbc.name()), |
|
88 cbc -> cbc.bytecodes())); |
|
89 // Attempt redefine. Throws exceptions on failure. |
|
90 vm().redefineClasses(rmp); |
|
91 } catch (EngineTerminationException ex) { |
|
92 throw ex; |
|
93 } catch (Exception ex) { |
|
94 throw new ClassInstallException("redefine: " + ex.getMessage(), new boolean[cbcs.length]); |
|
95 } |
|
96 // forward the redefine to remote-end to register the redefined bytecode |
|
97 try { |
|
98 super.redefine(cbcs); |
|
99 } catch (NotImplementedException ex) { |
|
100 // this remote doesn't care about registering bytecode, so we don't either |
|
101 } |
|
102 } |
|
103 |
|
104 /** |
|
105 * Returns the JDI {@link ReferenceType} corresponding to the specified |
|
106 * class name. |
|
107 * |
|
108 * @param vm the current JDI {@link VirtualMachine} as returned by |
|
109 * {@code vm()} |
|
110 * @param name the class name to look-up |
|
111 * @return the corresponding {@link ReferenceType} |
|
112 */ |
|
113 protected ReferenceType referenceType(VirtualMachine vm, String name) { |
|
114 return toReferenceType.computeIfAbsent(name, n -> nameToRef(vm, n)); |
|
115 } |
|
116 |
|
117 private static ReferenceType nameToRef(VirtualMachine vm, String name) { |
|
118 List<ReferenceType> rtl = vm.classesByName(name); |
|
119 if (rtl.size() != 1) { |
|
120 return null; |
|
121 } |
|
122 return rtl.get(0); |
|
123 } |
|
124 |
|
125 } |