-
Notifications
You must be signed in to change notification settings - Fork 533
/
JavaToolTask.cs
198 lines (176 loc) · 7.05 KB
/
JavaToolTask.cs
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Xamarin.Android.Tools;
using Microsoft.Android.Build.Tasks;
namespace Xamarin.Android.Tasks
{
public abstract class JavaToolTask : AndroidToolTask
{
/*
Example Javac output for errors. Regex Matches on the first line, we then need to
process the second line to get the column number so the IDE can correctly
mark where the error is.
TestMe.java:1: error: class, interface, or enum expected
public classo TestMe { }
^
TestMe2.java:1: error: ';' expected
public class TestMe2 {public vod Test ()}
^
2 errors
*/
const string CodeErrorRegExString = @"(?<file>.+\.java):(?<line>\d+):(?<error>.+)";
/*
Sample OutOfMemoryError raised by java. RegEx matches the java.lang.* line of the error
and splits it into an exception and an error
UNEXPECTED TOP-LEVEL ERROR:
java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.util.Arrays.copyOf(Arrays.java:2219)
at java.util.ArrayList.grow(ArrayList.java:242)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:216)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:208)
at java.util.ArrayList.add(ArrayList.java:440)
at com.android.dx.ssa.Dominators$DfsWalker.visitBlock(Dominators.java:263)
at com.android.dx.ssa.SsaMethod.forEachBlockDepthFirst(SsaMethod.java:783)
at com.android.dx.ssa.Dominators.run(Dominators.java:185)
at com.android.dx.ssa.Dominators.make(Dominators.java:90)
at com.android.dx.ssa.DomFront.run(DomFront.java:86)
at com.android.dx.ssa.SsaConverter.placePhiFunctions(SsaConverter.java:297)
at com.android.dx.ssa.SsaConverter.convertToSsaMethod(SsaConverter.java:51)
at com.android.dx.ssa.Optimizer.optimize(Optimizer.java:98)
at com.android.dx.ssa.Optimizer.optimize(Optimizer.java:72)
at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:297)
at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:137)
at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:93)
at com.android.dx.command.dexer.Main.processClass(Main.java:729)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673)
at com.android.dx.command.dexer.Main.access$300(Main.java:83)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:602)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
at com.android.dx.command.dexer.Main.processOne(Main.java:632)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:505)
at com.android.dx.command.dexer.Main.runMultiDex(Main.java:334)
at com.android.dx.command.dexer.Main.run(Main.java:244)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
*/
const string ExceptionRegExString = @"(?<exception>java.lang.+):(?<error>.+)";
static readonly Regex codeErrorRegEx = new Regex (CodeErrorRegExString, RegexOptions.Compiled);
static readonly Regex exceptionRegEx = new Regex (ExceptionRegExString, RegexOptions.Compiled);
bool foundError = false;
List<string> errorLines = new List<string> ();
StringBuilder errorText = new StringBuilder ();
string file;
int line, column;
public string JavaOptions { get; set; }
public string JavaMaximumHeapSize { get; set; }
public virtual string DefaultErrorCode => null;
public string WorkingDirectory { get; set; }
protected override string ToolName {
get { return OS.IsWindows ? "java.exe" : "java"; }
}
protected virtual Regex CodeErrorRegEx => codeErrorRegEx;
protected virtual Regex ExceptionRegEx => exceptionRegEx;
protected override bool HandleTaskExecutionErrors ()
{
if (foundError) {
errorText.Clear ();
foreach (var line in errorLines) {
if (!ProcessOutput (line))
break;
}
if (foundError && errorText.Length > 0) {
Log.LogError (ToolName, DefaultErrorCode, null, file, line - 1, column + 1, 0, 0, errorText.ToString ());
}
return !Log.HasLoggedErrors;
}
return base.HandleTaskExecutionErrors ();
}
protected override string GetWorkingDirectory ()
{
if (!string.IsNullOrEmpty (WorkingDirectory))
return WorkingDirectory;
return base.GetWorkingDirectory ();
}
protected override string GenerateFullPathToTool ()
{
return Path.Combine (ToolPath, ToolExe);
}
void LogFromException (string exception, string error) {
switch (exception) {
case "java.lang.OutOfMemoryError":
Log.LogCodedError ("XA5213", Properties.Resources.XA5213, ToolName, GenerateCommandLineCommands ());
break;
default:
Log.LogCodedError (DefaultErrorCode, "{0} : {1}", exception, error);
break;
}
}
bool ProcessOutput (string singleLine)
{
var match = CodeErrorRegEx.Match (singleLine);
var exceptionMatch = ExceptionRegEx.Match (singleLine);
if (match.Success) {
if (!string.IsNullOrEmpty (file)) {
Log.LogError (ToolName, DefaultErrorCode, null, file, line - 1, column + 1, 0, 0, errorText.ToString ());
errorText.Clear ();
}
file = match.Groups ["file"].Value;
var error = match.Groups ["error"].Value;
GetLineNumber (match.Groups ["line"].Value, out line, out column);
errorText.AppendLine (error);
return true;
} else if (exceptionMatch.Success) {
var error = exceptionMatch.Groups ["error"].Value;
var exception = exceptionMatch.Groups ["exception"].Value;
line = 1;
file = "";
column = 0;
LogFromException (exception, error);
return false;
} else if (foundError) {
if (singleLine.Trim () == "^") {
column = singleLine.IndexOf ("^", StringComparison.Ordinal);
return true;
}
if (singleLine.StartsWith ("Note:", StringComparison.Ordinal) || singleLine.Trim ().EndsWith ("errors", StringComparison.Ordinal)) {
// See if we have one last error to print out
Log.LogError (ToolName, DefaultErrorCode, null, file, line - 1, column + 1, 0, 0, errorText.ToString ());
errorText.Clear ();
foundError = false;
return true;
}
errorText.AppendLine (singleLine);
}
return true;
}
protected virtual void GetLineNumber (string match, out int line, out int column)
{
line = int.Parse (match) + 1;
column = 0;
}
protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
{
var match = CodeErrorRegEx.Match (singleLine);
var exceptionMatch = ExceptionRegEx.Match (singleLine);
if (match.Success || exceptionMatch.Success) {
Log.LogMessage (MessageImportance.High, singleLine);
foundError = true;
errorLines.Add (singleLine);
return;
} else if (foundError) {
Log.LogMessage (MessageImportance.High, singleLine);
errorLines.Add (singleLine);
return;
}
base.LogEventsFromTextOutput (singleLine, messageImportance);
}
}
}