-
Notifications
You must be signed in to change notification settings - Fork 2
/
Javap.py
84 lines (68 loc) · 2.42 KB
/
Javap.py
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
import sublime, sublime_plugin, subprocess, platform, urllib, os
class JavapCommand(sublime_plugin.TextCommand):
def run(self, edit):
filename = self.view.file_name()
print(self)
print(filename)
decompiled, errormessages = self.decompile(filename)
print(errormessages)
print('Editing window')
self.edit_window(edit, decompiled, filename)
def decompile(self, filename):
executable = self.get_javap_exec()
filepath, extension = os.path.splitext(filename)
print('Detected extension:')
print(extension)
basename = os.path.basename(filepath)
dirname = os.path.dirname(filepath)
command = [executable, '-c', '-l', '-private', '-verbose', '-classpath', dirname, basename]
print('Executing:')
print(command)
return self.exec_command(command)
def exec_command(self, command):
os_alias = platform.system().lower()
if 'windows' in os_alias:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startupinfo)
out, err = p.communicate()
print('Command executed')
fixed = out.decode("utf-8").replace('\r\n', '\n')
return (fixed, err)
else:
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print('Command executed')
fixed = out.decode("utf-8")
return (fixed, err)
def edit_window(self, edit, contents, filename):
view = self.view
window = sublime.active_window()
if view != window.transient_view_in_group(window.active_group()):
view.set_scratch(True)
view.set_syntax_file('Packages/Java/Java.tmLanguage')
view.erase(edit, sublime.Region(0, view.size()))
view.insert(edit, 0, contents)
view.sel().clear()
view.sel().add(sublime.Region(0))
def get_new_filename(self, filename):
return filename.replace("class", "java")
def get_javap_exec(self):
os_alias = platform.system().lower()
print(os_alias)
if 'windows' in os_alias:
return 'javap.exe'
elif 'linux' in os_alias:
return 'javap'
elif 'darwin' in os_alias:
return 'javap'
class OpenClassFileCommand(sublime_plugin.EventListener):
def on_load(self, view):
filename = view.file_name()
extension = os.path.splitext(filename)[1]
if extension == ".class":
view.run_command("javap")
def on_activated(self, view):
header = view.substr(sublime.Region(0, 9))
if header == "cafe babe":
self.on_load(view)