ClassTracker.java revision 3062:15bdc18525ff
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
26package jdk.jshell;
27
28import java.util.Arrays;
29import java.util.HashMap;
30import com.sun.jdi.ReferenceType;
31
32/**
33 * Tracks the state of a class through compilation and loading in the remote
34 * environment.
35 *
36 * @author Robert Field
37 */
38class ClassTracker {
39
40    private final JShell state;
41    private final HashMap<String, ClassInfo> map;
42
43    ClassTracker(JShell state) {
44        this.state = state;
45        this.map = new HashMap<>();
46    }
47
48    class ClassInfo {
49
50        private final String className;
51        private byte[] bytes;
52        private byte[] loadedBytes;
53        private ReferenceType rt;
54
55        private ClassInfo(String className) {
56            this.className = className;
57        }
58
59        String getClassName() {
60            return className;
61        }
62
63        byte[] getBytes() {
64            return bytes;
65        }
66
67        void setBytes(byte[] bytes) {
68            this.bytes = bytes;
69        }
70
71        void setLoaded() {
72            loadedBytes = bytes;
73        }
74
75        boolean isLoaded() {
76            return Arrays.equals(loadedBytes, bytes);
77        }
78
79        ReferenceType getReferenceTypeOrNull() {
80            if (rt == null) {
81                rt = state.executionControl().nameToRef(className);
82            }
83            return rt;
84        }
85    }
86
87    ClassInfo classInfo(String className, byte[] bytes) {
88        ClassInfo ci = map.computeIfAbsent(className, k -> new ClassInfo(k));
89        ci.setBytes(bytes);
90        return ci;
91    }
92
93    ClassInfo get(String className) {
94        return map.get(className);
95    }
96
97}
98