Skip to content

Commit 40b95db

Browse files
Update behavior of signature retrieve (#544)
* #542 (comment): implemented javap output prefill with timeout * Added new algorithm
1 parent 0fa01f4 commit 40b95db

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

src/net/JNetReflector/InternalExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,13 @@ public static string RemoveThrowsAndCleanupSignature(this string methodSignature
301301
if (methodSignature.Contains(SpecialNames.JavaLangThrows))
302302
{
303303
methodSignature = methodSignature.Substring(0, methodSignature.IndexOf(SpecialNames.JavaLangThrows));
304-
return methodSignature.TrimEnd();
304+
return RemoveThrowsAndCleanupSignature(methodSignature.TrimEnd());
305305
}
306306
else if (methodSignature.EndsWith(';'))
307307
{
308-
return methodSignature.Substring(0, methodSignature.Length - 1);
308+
return RemoveThrowsAndCleanupSignature(methodSignature.Substring(0, methodSignature.Length - 1));
309309
}
310-
return methodSignature;
310+
return methodSignature.Replace(", ", ",");
311311
}
312312

313313
public static string AddClassNameToSignature(this string methodSignature, string className)

src/net/JNetReflector/JNetReflectorHelper.cs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,24 @@ public static IDictionary<Class, IReadOnlyDictionary<string, string>> ExtractJav
9595
si.Arguments = arguments;
9696

9797
process = Process.Start(si);
98-
TimeSpan initial = process.TotalProcessorTime;
98+
TimeSpan initialTotalProcessorTime = process.TotalProcessorTime;
9999
int cycles = 0;
100100
while ((exited = process.WaitForExit(100)) == false)
101101
{
102102
process.Refresh();
103-
TimeSpan current = process.TotalProcessorTime;
104-
if ((current - initial) < TimeSpan.FromMilliseconds(10)) break; // process is idle???
103+
TimeSpan currentTotalProcessorTime = process.TotalProcessorTime;
104+
if ((currentTotalProcessorTime - initialTotalProcessorTime) < TimeSpan.FromMilliseconds(10)) break; // process is idle???
105105
if (++cycles > toBeAnalyzed.Count) break; // elapsed max cycles
106106
}
107107
if (exited && process.ExitCode != 0) throw new InvalidOperationException($"javap falied with error {process.ExitCode}");
108+
108109
Dictionary<string, string> map = new Dictionary<string, string>();
109110
string line;
110111
int classCounter = -1;
111112
string methodName = string.Empty;
112113
string className = string.Empty;
113114
bool nextLineIsDescriptor = false;
115+
#if OLD_ALGORITHM
114116
while ((line = process.StandardOutput.ReadLine()) != null)
115117
{
116118
if (line.Contains("Compiled from"))
@@ -136,6 +138,59 @@ public static IDictionary<Class, IReadOnlyDictionary<string, string>> ExtractJav
136138
methodName = methodName.AddClassNameToSignature(className);
137139
}
138140
}
141+
#else
142+
int endCounter = 0;
143+
int cycleCounter = 0;
144+
do
145+
{
146+
line = process.StandardOutput.ReadLine();
147+
if (line == null)
148+
{
149+
if (endCounter != toBeAnalyzed.Count)
150+
{
151+
// wait a while
152+
cycleCounter++;
153+
System.Threading.Thread.Sleep(1);
154+
continue;
155+
}
156+
else break;
157+
}
158+
else
159+
{
160+
if (line.Contains("Compiled from"))
161+
{
162+
if (classCounter != -1) { dict.TryAdd(toBeAnalyzed[classCounter], map); }
163+
classCounter++;
164+
className = toBeAnalyzed[classCounter].Name;
165+
map = new Dictionary<string, string>();
166+
}
167+
if (nextLineIsDescriptor)
168+
{
169+
nextLineIsDescriptor = false;
170+
string signature = line.TrimStart().TrimEnd();
171+
signature = signature.Remove(0, "descriptor: ".Length);
172+
map.Add(methodName, signature);
173+
}
174+
bool isInterfaceOrClassLine = line.Contains("class") || line.Contains("interface");
175+
if (line.Contains("public") && !isInterfaceOrClassLine)
176+
{
177+
nextLineIsDescriptor = true;
178+
methodName = line.TrimStart().TrimEnd();
179+
methodName = methodName.RemoveThrowsAndCleanupSignature();
180+
methodName = methodName.AddClassNameToSignature(className);
181+
}
182+
183+
cycleCounter = 0;
184+
}
185+
186+
if (line.TrimEnd() == "}")
187+
{
188+
endCounter++;
189+
}
190+
if (endCounter == toBeAnalyzed.Count) break;
191+
}
192+
while (cycleCounter < 100);
193+
#endif
139194
dict.TryAdd(toBeAnalyzed[classCounter], map);
140195
}
141196
catch { }

0 commit comments

Comments
 (0)