langtools/src/jdk.jshell/share/classes/jdk/jshell/ClassTracker.java
changeset 38535 4a25025e0b0d
parent 38534 425b30506f80
child 38536 42569f7fe4e6
equal deleted inserted replaced
38534:425b30506f80 38535:4a25025e0b0d
     1 /*
       
     2  * Copyright (c) 2015, 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;
       
    27 
       
    28 import java.util.Arrays;
       
    29 import java.util.HashMap;
       
    30 import java.util.Objects;
       
    31 import com.sun.jdi.ReferenceType;
       
    32 
       
    33 /**
       
    34  * Tracks the state of a class through compilation and loading in the remote
       
    35  * environment.
       
    36  *
       
    37  * @author Robert Field
       
    38  */
       
    39 class ClassTracker {
       
    40 
       
    41     private final JShell state;
       
    42     private final HashMap<String, ClassInfo> map;
       
    43 
       
    44     ClassTracker(JShell state) {
       
    45         this.state = state;
       
    46         this.map = new HashMap<>();
       
    47     }
       
    48 
       
    49     class ClassInfo {
       
    50 
       
    51         private final String className;
       
    52         private byte[] bytes;
       
    53         private byte[] loadedBytes;
       
    54         private ReferenceType rt;
       
    55 
       
    56         private ClassInfo(String className) {
       
    57             this.className = className;
       
    58         }
       
    59 
       
    60         String getClassName() {
       
    61             return className;
       
    62         }
       
    63 
       
    64         byte[] getBytes() {
       
    65             return bytes;
       
    66         }
       
    67 
       
    68         void setBytes(byte[] bytes) {
       
    69             this.bytes = bytes;
       
    70         }
       
    71 
       
    72         void setLoaded() {
       
    73             loadedBytes = bytes;
       
    74         }
       
    75 
       
    76         boolean isLoaded() {
       
    77             return Arrays.equals(loadedBytes, bytes);
       
    78         }
       
    79 
       
    80         ReferenceType getReferenceTypeOrNull() {
       
    81             if (rt == null) {
       
    82                 rt = state.executionControl().nameToRef(className);
       
    83             }
       
    84             return rt;
       
    85         }
       
    86 
       
    87         @Override
       
    88         public boolean equals(Object o) {
       
    89             return o instanceof ClassInfo &&
       
    90                     ((ClassInfo) o).className.equals(className);
       
    91         }
       
    92 
       
    93         @Override
       
    94         public int hashCode() {
       
    95             return Objects.hashCode(this.className);
       
    96         }
       
    97     }
       
    98 
       
    99     ClassInfo classInfo(String className, byte[] bytes) {
       
   100         ClassInfo ci = map.computeIfAbsent(className, k -> new ClassInfo(k));
       
   101         ci.setBytes(bytes);
       
   102         return ci;
       
   103     }
       
   104 
       
   105     ClassInfo get(String className) {
       
   106         return map.get(className);
       
   107     }
       
   108 }