-
-
Notifications
You must be signed in to change notification settings - Fork 432
/
JVMSUtil.java
179 lines (144 loc) · 3.92 KB
/
JVMSUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (c) 2013-2017 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
package org.adoptopenjdk.jitwatch.util;
import org.adoptopenjdk.jitwatch.model.bytecode.Opcode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.C_OPEN_ANGLE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_DOUBLE_QUOTE;
public final class JVMSUtil
{
private static final Logger logger = LoggerFactory.getLogger(JVMSUtil.class);
private static Map<String, String> bcDescriptionMap = new HashMap<>();
private static final String JVMS_HTML_FILENAME = "JVMS.html";
private static final String JVMS_CSS_FILENAME = "JVMS.css";
private JVMSUtil()
{
}
public static boolean hasLocalJVMS()
{
File file = new File(JVMS_HTML_FILENAME);
return file.exists();
}
public static boolean isJVMSLoaded()
{
return bcDescriptionMap.size() > 0;
}
public static String getJVMSCSSURL()
{
File cssFile = new File(JVMS_CSS_FILENAME);
if (cssFile.exists())
{
return cssFile.toURI().toString();
}
else
{
return null;
}
}
public static boolean fetchJVMS()
{
String html = NetUtil.fetchURL("https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html");
String css = NetUtil.fetchURL("https://docs.oracle.com/javase/specs/javaspec.css");
boolean result = false;
if (html.length() > 0 && css.length() > 0)
{
Path pathHTML = Paths.get(new File(JVMS_HTML_FILENAME).toURI());
Path pathCSS = Paths.get(new File(JVMS_CSS_FILENAME).toURI());
try
{
Files.write(pathHTML, html.getBytes(StandardCharsets.UTF_8));
Files.write(pathCSS, css.getBytes(StandardCharsets.UTF_8));
result = true;
}
catch (IOException ioe)
{
logger.error("Could not save JVMS to disk", ioe);
}
}
return result;
}
public static void loadJVMS()
{
try
{
Path path = Paths.get(new File(JVMS_HTML_FILENAME).toURI());
String html = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
int htmlLength = html.length();
String descStart = "<div class=\"section-execution\"";
int startPos = html.indexOf(descStart);
while (startPos != -1 && startPos < htmlLength)
{
int endPos = html.indexOf(descStart, startPos + descStart.length());
if (endPos != -1)
{
String desc = html.substring(startPos, endPos);
storeBytecodeDescription(desc);
startPos = endPos;
}
else if (startPos != -1)
{
String desc = html.substring(startPos);
storeBytecodeDescription(desc);
break;
}
else
{
break;
}
}
}
catch (IOException ioe)
{
logger.error("", ioe);
}
}
private static void storeBytecodeDescription(String description)
{
String title = StringUtil.getSubstringBetween(description, "<div class=\"section-execution\" title=\"", S_DOUBLE_QUOTE);
if (title != null)
{
bcDescriptionMap.put(title, description);
}
}
public static String getBytecodeDescriptions(Opcode opcode)
{
String opcodeText = opcode.getMnemonic();
String desc = bcDescriptionMap.get(opcodeText);
if (desc == null)
{
for (Map.Entry<String, String> entry : bcDescriptionMap.entrySet())
{
String key = entry.getKey();
int ltPos = key.indexOf(C_OPEN_ANGLE);
// ifge => if<cond>
// lconst_1 => lconst_<n>
if (ltPos != -1)
{
if (ltPos < opcodeText.length())
{
String subOpcodeText = opcodeText.substring(0, ltPos);
String subKey = key.substring(0, ltPos);
if (subOpcodeText.equals(subKey))
{
desc = entry.getValue();
break;
}
}
}
}
}
return desc;
}
}