-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathJavaCompiler.cs
More file actions
158 lines (134 loc) · 6.22 KB
/
JavaCompiler.cs
File metadata and controls
158 lines (134 loc) · 6.22 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Fiddle.Compilers.Implementation.Java {
public class JavaCompiler : ICompiler {
public JavaCompiler(string code, string jdkPath = null) : this(code, new ExecutionProperties(),
new CompilerProperties(), jdkPath) { }
public JavaCompiler(string code, IExecutionProperties execProps, ICompilerProperties compProps,
string jdkPath = null) {
SourceCode = code;
ExecuteProperties = execProps;
CompilerProperties = compProps;
if (string.IsNullOrWhiteSpace(jdkPath) || !Directory.Exists(jdkPath))
//Search for JDK if jdkPath is invalid directory
FindJdk();
else
//Use JDK Path parameter
JdkPath = jdkPath;
}
private string JdkPath { get; set; }
private string JavacPath => Path.Combine(JdkPath, "bin", "javac.exe");
private string JavaPath => Path.Combine(JdkPath, "bin", "java.exe");
private string ClassName { get; set; }
public IExecutionProperties ExecuteProperties { get; set; }
public ICompilerProperties CompilerProperties { get; set; }
public string SourceCode { get; set; }
public ICompileResult CompileResult { get; set; }
public IExecuteResult ExecuteResult { get; set; }
public Language Language { get; set; } = Language.Java;
public async Task<ICompileResult> Compile() {
ToValidCode();
string tmp = Path.Combine(Path.GetTempPath(), $"{ClassName}.java");
File.WriteAllText(tmp, SourceCode);
var runResult = await ExecuteThreaded<string>.Execute(
() => JdkHelper.CompileJava(JavacPath, tmp, CompilerProperties), (int) CompilerProperties.Timeout
);
string output = runResult.ReturnValue;
var error = runResult.Exception;
int time = runResult.ElapsedMilliseconds;
IEnumerable<IDiagnostic> diagnostics = null;
IEnumerable<Exception> errors = null;
if (!string.IsNullOrWhiteSpace(output))
diagnostics = new List<IDiagnostic> {
new JavaDiagnostic(output, -1, -1, -1, -1, Severity.Info)
};
if (error != null)
errors = new List<Exception> {error};
var result =
new JavaCompileResult(time, SourceCode, diagnostics, null, errors);
CompileResult = result;
return result;
}
public async Task<IExecuteResult> Execute() {
if (CompileResult == null || CompileResult.SourceCode != SourceCode) await Compile();
if (!CompileResult.Success) {
var result = new JavaExecuteResult(0, "", null, CompileResult,
new CompileException("Could not compile, javac responded with some errors!"));
ExecuteResult = result;
return result;
} else {
var runResult = await ExecuteThreaded<string>.Execute(
() => JreHelper.ExecuteJava(JavaPath, ClassName, ExecuteProperties), (int) ExecuteProperties.Timeout
);
string output = runResult.ReturnValue;
int time = runResult.ElapsedMilliseconds;
var error = runResult.Exception;
var result =
new JavaExecuteResult(time, output, null, CompileResult, error);
ExecuteResult = result;
return result;
}
}
public void Dispose() { }
private void FindJdk() {
string programFiles;
string programFilesX86;
if (Environment.Is64BitProcess) {
programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
} else {
programFiles = Environment.ExpandEnvironmentVariables("%ProgramW6432%");
programFilesX86 = Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%");
}
string environmentVariable = Environment.GetEnvironmentVariable("path");
if (!string.IsNullOrWhiteSpace(environmentVariable)) {
string[] path = environmentVariable.Split(';');
string jdk = path.FirstOrDefault(p => p.Contains("jdk"));
if (!string.IsNullOrWhiteSpace(jdk)) {
JdkPath = jdk;
return;
}
}
string javaPath86 = Path.Combine(programFilesX86, "Java");
string java = JdkHelper.SearchJavaPath(javaPath86);
if (java != null) {
JdkPath = java;
return;
}
string javaPath = Path.Combine(programFiles, "Java");
java = JdkHelper.SearchJavaPath(javaPath);
if (java != null) {
JdkPath = java;
return;
}
throw new CompileException("Java Development Kit (JDK) could not be found on this System!");
}
private void ToValidCode() {
ToValidMain();
ToValidClass();
}
private void ToValidClass() {
var findClass = new Regex("class ([A-Za-z]+)");
var match = findClass.Match(SourceCode);
if (match.Success) {
string matchString = SourceCode.Substring(match.Index, match.Length);
ClassName = matchString.Split(' ')[1]; //split "class Test" -> ["class", "Test"] and pick [1]: "Test"
} else {
ClassName = "FiddleClass";
SourceCode = $"public class {ClassName} {{\n" +
$"{SourceCode}\n" +
"}";
}
}
private void ToValidMain() {
if (!SourceCode.Contains("static void main"))
SourceCode = "public static void main(String[] args) {\n" +
$"{SourceCode}\n" +
"}";
}
}
}