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 package jdk.internal.jshell.jdi; 26 27 import java.util.HashMap; 28 import java.util.Objects; 29 import com.sun.jdi.ReferenceType; 30 import java.util.List; 31 import com.sun.jdi.VirtualMachine; 32 33 /** 34 * Tracks the state of a class. 35 */ 36 class ClassTracker { 37 38 private final VirtualMachine vm; 39 private final HashMap<String, ClassInfo> map; 40 41 ClassTracker(VirtualMachine vm) { 42 this.vm = vm; 43 this.map = new HashMap<>(); 44 } 45 46 /** 47 * Associates a class name, class bytes, and ReferenceType. 48 */ 49 class ClassInfo { 50 51 // The name of the class -- always set 52 private final String className; 53 54 // The corresponding compiled class bytes when a load or redefine 55 // is started. May not be the loaded bytes. May be null. 56 private byte[] bytes; 57 58 // The class bytes successfully loaded/redefined into the remote VM. 59 private byte[] loadedBytes; 60 61 // The corresponding JDI ReferenceType. Used by redefineClasses and 62 // acts as indicator of successful load (null if not loaded). 63 private ReferenceType rt; 64 65 private ClassInfo(String className) { 66 this.className = className; 67 } 68 69 String getClassName() { 70 return className; 71 } 72 73 byte[] getLoadedBytes() { 74 return loadedBytes; 75 } 76 77 byte[] getBytes() { 78 return bytes; 79 } 80 81 private void setBytes(byte[] potentialBytes) { 82 this.bytes = potentialBytes; 83 } 84 85 // The class has been successful loaded redefined. The class bytes 86 // sent are now actually loaded. 87 void markLoaded() { 88 loadedBytes = bytes; 89 } 90 91 // Ask JDI for the ReferenceType, null if not loaded. 92 ReferenceType getReferenceTypeOrNull() { 93 if (rt == null) { 94 rt = nameToRef(className); 95 } 96 return rt; 97 } 98 99 private ReferenceType nameToRef(String name) { 100 List<ReferenceType> rtl = vm.classesByName(name); 101 if (rtl.size() != 1) { 102 return null; 103 } 104 return rtl.get(0); 105 } 106 107 @Override 108 public boolean equals(Object o) { 109 return o instanceof ClassInfo 110 && ((ClassInfo) o).className.equals(className); 111 } 112 113 @Override 114 public int hashCode() { 115 return Objects.hashCode(this.className); 116 } 117 } 118 119 // Map a class name to the current compiled class bytes. 120 ClassInfo classInfo(String className, byte[] bytes) { 121 ClassInfo ci = get(className); 122 ci.setBytes(bytes); 123 return ci; 124 } 125 126 // Lookup the ClassInfo by class name, create if it does not exist. 127 ClassInfo get(String className) { 128 return map.computeIfAbsent(className, k -> new ClassInfo(k)); 129 } 130 }