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
|
import sys
import os
import re
import subprocess
import platform
ERROR_PRONE_FLAGS = [
'-Xep:FunctionalInterfaceMethodChanged:WARN',
'-Xep:ReturnValueIgnored:WARN',
]
JAVA10_EXPORTS = [
'--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED',
]
def get_java_version(exe):
p = subprocess.Popen([exe, '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
for line in (out or '').strip().split("\n") + (err or '').strip().split("\n"):
m = re.match(r'java version "(.+)"', line)
if m:
parts = m.groups()[0].split(".")
return parts[1] if parts[0] == "1" else parts[0]
m = re.match(r'openjdk version "(\d+).*"', line)
if m:
parts = m.groups()[0].split(".")
return parts[0]
return None
def get_classpath(cmd):
for i, part in enumerate(cmd):
if part == '-classpath':
i += 1
if i < len(cmd):
return cmd[i]
else:
return None
return None
def parse_args(argv):
parsed = []
for i in range(len(argv)):
if not argv[i].startswith('-'):
parsed.append(argv[i])
if len(parsed) >= 3:
break
return parsed + [argv[i + 1 :]]
def just_do_it(argv):
java, javac, error_prone_tool, javac_cmd = parse_args(argv)
ver = get_java_version(java)
if not ver:
raise Exception("Can't determine java version")
if int(ver) >= 10:
for f in javac_cmd:
if f.startswith('-Xep'):
ERROR_PRONE_FLAGS.append(f)
for f in ERROR_PRONE_FLAGS:
if f in javac_cmd:
javac_cmd.remove(f)
if '-processor' in javac_cmd:
classpath = get_classpath(javac_cmd)
if classpath:
error_prone_tool = error_prone_tool + os.pathsep + classpath
cmd = (
[javac]
+ JAVA10_EXPORTS
+ ['-processorpath', error_prone_tool, '-XDcompilePolicy=byfile']
+ [(' '.join(['-Xplugin:ErrorProne'] + ERROR_PRONE_FLAGS))]
+ javac_cmd
)
else:
cmd = (
[java, '-Xbootclasspath/p:' + error_prone_tool, 'com.google.errorprone.ErrorProneCompiler']
+ ERROR_PRONE_FLAGS
+ javac_cmd
)
if platform.system() == 'Windows':
sys.exit(subprocess.Popen(cmd).wait())
else:
os.execv(cmd[0], cmd)
if __name__ == '__main__':
just_do_it(sys.argv[1:])
|