Skip to content

Commit 1614864

Browse files
committed
[java-source-utils] Add Java source code utilities
There are two parts of the current `.jar` binding toolchain which are painful and could be improved: 1. Parameter names 2. Documentation extraction Parameter names (1) are important because they become the names of event members as part of ["event-ification"][0]. As such they are semantically important, and the default behavior of "p0" makes for a terrible user experience. *If* the `.class` files in the `.jar` file are built with `javac -parameters` (4273e5c), then the `.class` file will contain parameter names and we're good. However, this may not be the case. If the `.class` files are built with `javac -g`, then we'll try to deduce parameter names from debug info, but that's also problematic. What's left? It is not unusual for Java libraries to provide "source `.jar`" files, e.g. Android provides `android-stubs-src.jar` files, and other libraries may provide a `*-sources.jar` file. The contents of these files are *Java source code*. These files are used by Android IDEs to provide documentation for the Java library. They contain classes, methods, parameter names, and associated Javadoc documentation. What they are *not* guaranteed to do is *compile*. As such, we can't compile them ourselves with `javac -parameters` and then process the `.class` files, as they may refer to unresolvable types. "Interestingly", we *already* have some tooling to deal with this: `tools/param-name-importer` uses a custom Irony grammar to parse the Android SDK `*-stubs-src.jar` files to grab parameter names. However, this tooling is *too strict*; try to pass arbitrary Java source code at it, and it quickly fails. Which brings us to documentation (2): we have a [javadoc2mdoc][1] tool which will parse Javadoc HTML documentation and convert it into [**mdoc**(5)][2] documentation, which can be later turned into [XML documentation comments][3] files by way of [**mdoc export-msxdoc**(1)][4], but this tool is (1) painful to maintain, because it processes Javadoc *HTML*, and (2) *requires Javadoc HTML*. Google hasn't updated their downloadable Javadoc `.zip` file since API-24 (2016-October). API-29 is currently stable. If we want newer docs, we either need to scrape the developer.android.com/reference website to use with the existing tooling, or... we need to be able to read the Javadoc comments within the `*-stubs-src.jar` files provided with the Android SDK. (Note: Android SDK docs are Apache 2; file format conversion is fine.) We thus have two use-cases for which parsing Java source code would provide a solution. As luck would have it, there's a decent Apache 2-licensed Java project which supports parsing Java source code: [JavaParser][5]. Add a new `tools/java-source-utils` program which will parse Java source code to produce two separate artifacts: parameter names and consolidated Javadoc documentation: $ java -jar java-source-utils.jar --help java-source-utils [<-a|--aar> AAR]* [<-j|--jar> JAR]* [--bootclasspath CLASSPATH] [<-P|--output-params> OUT.params.txt] [<-D|--output-javadocs> OUT.xml] FILES Provide `--output-params FILE`, and the specified file will be created which follows the file format laid out in [`JavaParameterNamesLoader.cs`][6]: package java.lang ;--------------------------------------- class Object wait(long timeout) Provide `--output-javadocs FILE`, and the resulting file will be a `class-parse`-like XML file which uses `//@jni-signature` as the "key" and a child `<javadoc/>` element to contain documentation, e.g.: <api api-source="java-source-utils"> <package name="java.lang"> <class name="Object" jni-signature="Ljava/lang/Object;"> <javadoc>…</javadoc> <constructor jni-signature="()V"> <javadoc>…</javadoc> </constructor> <method name="wait" jni-signature="(J)V"> <javadoc>…</javadoc> </method> </class> </package </api> This should make it possible to update the Xamarin.Android API documentation without resorting to web scraping (and updating the code to deal with whatever new HTML dialects are now used). TODO: In some scenarios, types won't be resolvable. What should output be? We don't want to *require* that everything be resolvable -- it's painful, and possibly impossible, e.g. w/ internal types -- so instead we should "demark" the unresolvable types. `.params.txt` output will use `.*` as a type prefix, e.g. method(.*UnresolvableType foo, int bar); `docs.xml` will output `L.*UnresolvableType;`. fix JavaParameterNamesLoader.cs to support the above. [0]: https://docs.microsoft.com/en-us/xamarin/android/internals/api-design#events-and-listeners [1]: https://github.com/xamarin/xamarin-android/tree/d48cf04f9749664bf48fc16bcb920d5d941cccab/tools/javadoc2mdoc [2]: http://docs.go-mono.com/?link=man%3amdoc(5) [3]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/xmldoc/ [4]: http://docs.go-mono.com/?link=man%3amdoc-export-msxdoc(1) [5]: https://javaparser.org [6]: https://github.com/xamarin/java.interop/blob/93df5a200e7b6f1b5add451aff66bbcb24293720/src/Xamarin.Android.Tools.Bytecode/JavaParameterNamesLoader.cs#L45-L68
1 parent 80b4667 commit 1614864

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2187
-1
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ indent_style = space
3030
indent_size = 4
3131

3232
# Code files
33-
[*.{cs,csx,vb,vbx}]
33+
[*.{cs,csx,java,vb,vbx}]
3434
insert_final_newline = true
3535
indent_style = tab
3636
tab_width = 8

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"java.configuration.updateBuildConfiguration": "automatic",
23
"nxunitExplorer.nunit": "packages/NUnit.ConsoleRunner.3.9.0/tools/nunit3-console.exe",
34
"nxunitExplorer.modules": [
45
"bin/TestDebug/generator-Tests.dll",

Directory.Build.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
<XamarinAndroidToolsDirectory Condition=" '$(XamarinAndroidToolsDirectory)' == '' ">$(MSBuildThisFileDirectory)external\xamarin-android-tools</XamarinAndroidToolsDirectory>
4444
</PropertyGroup>
4545
<PropertyGroup>
46+
<GradleHome Condition=" '$(GradleHome)' == '' ">$(MSBuildThisFileDirectory)build-tools\gradle</GradleHome>
47+
<GradleWPath Condition=" '$(GradleWPath)' == '' ">$(GradleHome)\gradlew</GradleWPath>
48+
<GradleArgs Condition=" '$(GradleArgs)' == '' ">--stacktrace --no-daemon</GradleArgs>
4649
<JavacSourceVersion Condition=" '$(JavacSourceVersion)' == '' ">1.6</JavacSourceVersion>
4750
<JavacTargetVersion Condition=" '$(JavacTargetVersion)' == '' ">1.6</JavacTargetVersion>
4851
<JreRtJarPath Condition=" '$(JreRtJarPath)' == '' And '$(JavaCPath)' != '' ">$([System.IO.Path]::GetDirectoryName('$(JavaCPath)'))\..\jre\lib\rt.jar</JreRtJarPath>

Java.Interop.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Java.Interop.Tools.JavaSour
8787
EndProject
8888
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "param-name-importer", "tools\param-name-importer\param-name-importer.csproj", "{0E3AF6C1-7638-464D-9174-485D494499DC}"
8989
EndProject
90+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "java-source-utils", "tools\java-source-utils\java-source-utils.csproj", "{F46EDFA5-C52A-4F0C-B5A2-5BB67E0D8C74}"
91+
EndProject
9092
Global
9193
GlobalSection(SharedMSBuildProjectFiles) = preSolution
9294
src\Java.Interop.NamingCustomAttributes\Java.Interop.NamingCustomAttributes.projitems*{58b564a1-570d-4da2-b02d-25bddb1a9f4f}*SharedItemsImports = 4
@@ -238,6 +240,10 @@ Global
238240
{0E3AF6C1-7638-464D-9174-485D494499DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
239241
{0E3AF6C1-7638-464D-9174-485D494499DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
240242
{0E3AF6C1-7638-464D-9174-485D494499DC}.Release|Any CPU.Build.0 = Release|Any CPU
243+
{F46EDFA5-C52A-4F0C-B5A2-5BB67E0D8C74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
244+
{F46EDFA5-C52A-4F0C-B5A2-5BB67E0D8C74}.Debug|Any CPU.Build.0 = Debug|Any CPU
245+
{F46EDFA5-C52A-4F0C-B5A2-5BB67E0D8C74}.Release|Any CPU.ActiveCfg = Release|Any CPU
246+
{F46EDFA5-C52A-4F0C-B5A2-5BB67E0D8C74}.Release|Any CPU.Build.0 = Release|Any CPU
241247
EndGlobalSection
242248
GlobalSection(SolutionProperties) = preSolution
243249
HideSolutionNode = FALSE
@@ -279,6 +285,7 @@ Global
279285
{E34BCFA0-CAA4-412C-AA1C-75DB8D67D157} = {172B608B-E6F3-41CC-9949-203A76BA247C}
280286
{093B5E94-7FB7-499F-9C11-30944BAFEE25} = {271C9F30-F679-4793-942B-0D9527CB3E2F}
281287
{0E3AF6C1-7638-464D-9174-485D494499DC} = {C8F58966-94BF-407F-914A-8654F8B8AE3B}
288+
{F46EDFA5-C52A-4F0C-B5A2-5BB67E0D8C74} = {C8F58966-94BF-407F-914A-8654F8B8AE3B}
282289
EndGlobalSection
283290
GlobalSection(ExtensibilityGlobals) = postSolution
284291
SolutionGuid = {29204E0C-382A-49A0-A814-AD7FBF9774A5}

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ run-all-tests:
4545
$(MAKE) run-test-jnimarshal || r=1 ; \
4646
$(MAKE) run-test-generator-core || r=1 ; \
4747
$(MAKE) run-ptests || r=1 ; \
48+
$(MAKE) run-java-source-utils-tests || r=1 ; \
4849
exit $$r;
4950

5051
include build-tools/scripts/msbuild.mk
@@ -142,6 +143,9 @@ run-ptests: $(PTESTS) bin/Test$(CONFIGURATION)/$(JAVA_INTEROP_LIB)
142143
$(foreach t,$(PTESTS), $(call RUN_TEST,$(t))) \
143144
exit $$r;
144145

146+
run-java-source-utils-tests:
147+
$(MSBUILD) $(MSBUILD_FLAGS) tools/java-source-utils/java-source-utils.csproj /t:RunTests
148+
145149
bin/Test$(CONFIGURATION)/$(JAVA_INTEROP_LIB): bin/$(CONFIGURATION)/$(JAVA_INTEROP_LIB)
146150
cp $< $@
147151

Binary file not shown.
Binary file not shown.
1 Byte
Binary file not shown.
18.3 KB
Binary file not shown.
17 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)