Skip to content

Commit f291722

Browse files
atsushienojonpryor
authored andcommitted
[class-parse] really support Java8 and other various doclet types. (#125)
ClassPath class had a red-herring property named "DocletType", which was NEVER assigned and therefore never worked. Its existence is optimistic so that it premised that any consuming code knows which doclet type its argument javadoc is generated with. So, just kill it and implement working code here. Now that we get a working doclet parser, a handful of Java8 scraper issues were discovered, so fixed them all. Namely: - Arrays are replaced as :A, not mere []. - the new DroidDoc actually uses [] as is, and I believe it used to be so too in Javadocs. On the other hand, [] may show up only within droiddocs, so skip that string.Replace() for Java8. - Java8 doc parsers simply passed '-' which actually needed to be escaped. It caused errors when argument names were simple like 'x-y' (which was treated as character range specification in regex). - regex matching for "anything except for '('" should actually be "... except for '(' and ')'" and then they should have been escaped too.
1 parent 45aff90 commit f291722

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

src/Xamarin.Android.Tools.Bytecode/ClassPath.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class ClassPath {
2626

2727
public string ApiSource { get; set; }
2828

29-
public JavaDocletType DocletType { get; set; }
29+
public JavaDocletType? DocletType { get; set; }
3030

3131
public IEnumerable<string> DocumentationPaths { get; set; }
3232

@@ -224,9 +224,28 @@ void FixupParametersFromDocs (XElement api)
224224
}
225225
}
226226

227+
JavaDocletType GetDocletType (string path)
228+
{
229+
var kind = JavaDocletType.DroidDoc;
230+
char [] buf = new char [500];
231+
string packagesHtml = Path.Combine (path, "packages.html");
232+
if (File.Exists (packagesHtml) && File.ReadAllText (packagesHtml).Contains ("<body class=\"gc-documentation develop reference api "))
233+
kind = JavaDocletType.DroidDoc2;
234+
using (var reader = File.OpenText (Path.Combine (path, "index.html")))
235+
reader.ReadBlock (buf, 0, buf.Length);
236+
string rawHTML = new string (buf);
237+
if (rawHTML.Contains ("Generated by javadoc (build 1.6"))
238+
kind = JavaDocletType.Java6;
239+
else if (rawHTML.Contains ("Generated by javadoc (version 1.7"))
240+
kind = JavaDocletType.Java7;
241+
else if (rawHTML.Contains ("Generated by javadoc (1.8"))
242+
kind = JavaDocletType.Java8;
243+
return kind;
244+
}
245+
227246
IAndroidDocScraper CreateDocScraper (string src)
228247
{
229-
switch (DocletType) {
248+
switch (DocletType ?? GetDocletType (src)) {
230249
default: return new DroidDoc2Scraper (src);
231250
case JavaDocletType.DroidDoc: return new DroidDocScraper (src);
232251
case JavaDocletType.Java6: return new JavaDocScraper (src);

src/Xamarin.Android.Tools.Bytecode/JavaDocumentScraper.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,14 @@
3333

3434
namespace Xamarin.Android.Tools.Bytecode
3535
{
36-
enum JavaDocKind {
37-
DroidDoc,
38-
DroidDoc2,
39-
Java6,
40-
Java7,
41-
Java8
42-
}
43-
4436
class DroidDocScraper : AndroidDocScraper
4537
{
4638
const String pattern_head_droiddoc = "<span class=\"sympad\"><a href=\".*";
4739

4840
public DroidDocScraper (string dir)
4941
: base (dir, pattern_head_droiddoc, null, " ", false)
5042
{
43+
ShouldEscapeBrackets = true;
5144
}
5245
}
5346

@@ -59,6 +52,7 @@ class DroidDoc2Scraper : AndroidDocScraper
5952
public DroidDoc2Scraper (string dir)
6053
: base (dir, pattern_head_droiddoc, reset_pattern_head, " ", true, "\\(", ", ", "\\)", "\\s*</code>")
6154
{
55+
ShouldEscapeBrackets = true;
6256
}
6357

6458
string prev_path;
@@ -115,13 +109,15 @@ public Java7DocScraper (string dir)
115109

116110
class Java8DocScraper : AndroidDocScraper
117111
{
118-
const String pattern_head_javadoc = "<td class=\"col.+\"><code><strong><a href=\"[./]*"; // I'm not sure how path could be specified... (./ , ../ , or even /)
112+
const String pattern_head_javadoc = "<td class=\"col.+\"><code><span class=\"memberNameLink\"><a href=\"[./]*"; // I'm not sure how path could be specified... (./ , ../ , or even /)
119113
const String reset_pattern_head_javadoc = "<td><code>";
120114
const String parameter_pair_splitter_javadoc = "&nbsp;";
121115

122116
public Java8DocScraper (string dir)
123-
: base (dir, pattern_head_javadoc, reset_pattern_head_javadoc, parameter_pair_splitter_javadoc, true, "-", "-", "-", null)
117+
: base (dir, pattern_head_javadoc, reset_pattern_head_javadoc, parameter_pair_splitter_javadoc, true, "\\-", "\\-", "\\-", null)
124118
{
119+
ShouldAlterArraySpec = true;
120+
ShouldEliminateGenericArguments = true;
125121
}
126122
}
127123

@@ -167,6 +163,10 @@ protected AndroidDocScraper (string dir, String patternHead, String resetPattern
167163
// LoadDocument (f.Substring (dir.Length + 1), f);
168164
}
169165

166+
protected bool ShouldEscapeBrackets { get; set; }
167+
protected bool ShouldAlterArraySpec { get; set; }
168+
protected bool ShouldEliminateGenericArguments { get; set; }
169+
170170
protected virtual IEnumerable<string> GetContentLines (string path)
171171
{
172172
return File.ReadAllText (path).Split ('\n');
@@ -197,11 +197,18 @@ public virtual String[] GetParameterNames (string package, string type, string m
197197
for (int i = 0; i < ptypes.Length; i++) {
198198
if (i != 0)
199199
buffer.Append (param_sep);
200-
buffer.Append (ptypes [i].Replace ('$', '.'));
200+
var ptype = ptypes [i];
201+
if (ShouldEliminateGenericArguments)
202+
while (ptype.IndexOf ('<') > 0 && ptype.IndexOf ('>') > ptype.IndexOf ('<'))
203+
ptype = ptype.Substring (0, ptype.IndexOf ('<')) + ptype.Substring (ptype.IndexOf ('>') + 1);
204+
buffer.Append (ptype.Replace ('$', '.'));
201205
}
202-
buffer.Replace ("[", "\\[").Replace ("]", "\\]");
206+
if (ShouldEscapeBrackets)
207+
buffer.Replace ("[", "\\[").Replace ("]", "\\]");
208+
if (ShouldAlterArraySpec)
209+
buffer.Replace ("[]", ":A");
203210
buffer.Append (close_method);
204-
buffer.Append ("\".*\\(([^(]*)\\)");
211+
buffer.Append ("\".*\\(([^\\(\\)]*)\\)");
205212
buffer.Append (post_close_method_parens);
206213
buffer.Replace ("?", "\\?");
207214
Regex pattern = new Regex (buffer.ToString (), RegexOptions.Multiline);

0 commit comments

Comments
 (0)