langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/jdi/ClassTracker.java
author rfield
Sat, 21 May 2016 22:32:08 -0700
changeset 38535 4a25025e0b0d
child 38836 b09d1cfbf28c
permissions -rw-r--r--
8156101: JShell SPI: Provide a pluggable execution control SPI Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     1
/*
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     4
 *
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    10
 *
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    15
 * accompanied this code).
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    16
 *
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    20
 *
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    23
 * questions.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    24
 */
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    25
package jdk.internal.jshell.jdi;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    26
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    27
import java.util.HashMap;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    28
import java.util.Objects;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    29
import com.sun.jdi.ReferenceType;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    30
import java.util.List;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    31
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    32
/**
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    33
 * Tracks the state of a class.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    34
 */
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    35
class ClassTracker {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    36
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    37
    private final JDIEnv jdiEnv;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    38
    private final HashMap<String, ClassInfo> map;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    39
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    40
    ClassTracker(JDIEnv jdiEnv) {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    41
        this.jdiEnv = jdiEnv;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    42
        this.map = new HashMap<>();
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    43
    }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    44
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    45
    /**
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    46
     * Associates a class name, class bytes, and ReferenceType.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    47
     */
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    48
    class ClassInfo {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    49
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    50
        // The name of the class -- always set
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    51
        private final String className;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    52
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    53
        // The corresponding compiled class bytes when a load or redefine
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    54
        // is started.  May not be the loaded bytes.  May be null.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    55
        private byte[] bytes;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    56
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    57
        // The class bytes successfully loaded/redefined into the remote VM.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    58
        private byte[] loadedBytes;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    59
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    60
        // The corresponding JDI ReferenceType.  Used by redefineClasses and
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    61
        // acts as indicator of successful load (null if not loaded).
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    62
        private ReferenceType rt;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    63
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    64
        private ClassInfo(String className) {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    65
            this.className = className;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    66
        }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    67
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    68
        String getClassName() {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    69
            return className;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    70
        }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    71
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    72
        byte[] getLoadedBytes() {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    73
            return loadedBytes;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    74
        }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    75
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    76
        byte[] getBytes() {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    77
            return bytes;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    78
        }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    79
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    80
        private void setBytes(byte[] potentialBytes) {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    81
            this.bytes = potentialBytes;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    82
        }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    83
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    84
        // The class has been successful loaded redefined.  The class bytes
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    85
        // sent are now actually loaded.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    86
        void markLoaded() {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    87
            loadedBytes = bytes;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    88
        }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    89
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    90
        // Ask JDI for the ReferenceType, null if not loaded.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    91
        ReferenceType getReferenceTypeOrNull() {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    92
            if (rt == null) {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    93
                rt = nameToRef(className);
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    94
            }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    95
            return rt;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    96
        }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    97
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    98
        private ReferenceType nameToRef(String name) {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
    99
            List<ReferenceType> rtl = jdiEnv.vm().classesByName(name);
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   100
            if (rtl.size() != 1) {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   101
                return null;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   102
            }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   103
            return rtl.get(0);
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   104
        }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   105
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   106
        @Override
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   107
        public boolean equals(Object o) {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   108
            return o instanceof ClassInfo
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   109
                    && ((ClassInfo) o).className.equals(className);
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   110
        }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   111
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   112
        @Override
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   113
        public int hashCode() {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   114
            return Objects.hashCode(this.className);
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   115
        }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   116
    }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   117
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   118
    // Map a class name to the current compiled class bytes.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   119
    ClassInfo classInfo(String className, byte[] bytes) {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   120
        ClassInfo ci = get(className);
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   121
        ci.setBytes(bytes);
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   122
        return ci;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   123
    }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   124
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   125
    // Lookup the ClassInfo by class name, create if it does not exist.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   126
    ClassInfo get(String className) {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   127
        return map.computeIfAbsent(className, k -> new ClassInfo(k));
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   128
    }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
diff changeset
   129
}