Skip to content

Commit

Permalink
[generator] Constructors support [Obsolete] (#273)
Browse files Browse the repository at this point in the history
Fixes: dotnet/android#1396

If `//constructor/@deprecated` has a value of `deprecated`, then the
constructor should be `[Obsolete]`.

This is what is already done with methods.
  • Loading branch information
atsushieno authored and jonpryor committed Mar 20, 2018
1 parent 6807817 commit 56e978f
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
2 changes: 2 additions & 0 deletions tools/generator/Ctor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ public void Generate (StreamWriter sw, string indent, CodeGenerationOptions opt,
opt.CodeGenerator.WriteConstructorIdField (this, sw, indent, opt);
sw.WriteLine ("{0}// Metadata.xml XPath constructor reference: path=\"{1}/constructor[@name='{2}'{3}]\"", indent, type.MetadataXPathReference, type.JavaSimpleName, Parameters.GetMethodXPathPredicate ());
sw.WriteLine ("{0}[Register (\"{1}\", \"{2}\", \"{3}\"{4})]", indent, ".ctor", jni_sig, String.Empty, this.AdditionalAttributeString ());
if (Deprecated != null)
sw.WriteLine ("{0}[Obsolete (@\"{1}\")]", indent, Deprecated.Replace ("\"", "\"\""));
GenerateCustomAttributes (sw, indent);
sw.WriteLine ("{0}{1} unsafe {2} ({3})\n{0}\t: {4} (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)",
indent, Visibility, Name, GenBase.GetSignature (this, opt), use_base ? "base" : "this");
Expand Down
43 changes: 43 additions & 0 deletions tools/generator/Tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

These tests are not intuitive at all. So we need some documentation here.

These tests compare outcomes from generator to the "expected" outcomes.

It does not work together with class-parse, so you cannot pass any jars
nor java sources, which is a pain point (but those who created these tests
didn't care).

There are two "expected" set of files. One is "expected" and the other is
"expected.ji". They are different per Java.Interop output methods.

The differences between "expected" and "expected.ji" are almost only
annoying, but this test blindly compares those differences. So if you are
going to add tests you will have to duplicate your work twice...

Tests that use `BaseGeneratorTest` are organized as:

./BaseGeneratorTest.cs - sets up generation and compilation options.
./Compiler.cs - implements C# compilation with `CodeDomProvider`.
./(others).cs - the actual `TestFixture`s.

What those tests do are:

- invoke class-parse (and perhaps jar2xml), to generate XML API inputs to generator.
- invoke generator, to generate comparable sources.
- optionally invoke csc to see if it builds.

`BaseGeneratorTest` takes the arguments below,

- outputRelativePath - path to generator output subdir
- apiDescriptionFile - path to the input API XML output. Sadly existing tests
are organized horribly and they reside in the "expected" directory.
- expectedRelativePath - path to the "expected" file generation.
- additionalSupportPaths - path to additional compilation items.

The test outputs are generated to `out` and `out.ji` directories, per
the generator's output method.

When you are creating a new test, it is easier to once generate results in
those `out` and `out.ji` directories, and copy them as "expected" and
"expected.ji" with required changes (so that they become really expected
contents).
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected SomeObject (IntPtr javaReference, JniHandleOwnership transfer) : base

// Metadata.xml XPath constructor reference: path="/api/package[@name='xamarin.test']/class[@name='SomeObject']/constructor[@name='SomeObject' and count(parameter)=0]"
[Register (".ctor", "()V", "")]
[Obsolete (@"deprecated")]
public unsafe SomeObject ()
: base (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<package name="xamarin.test">
<class abstract="false" deprecated="not deprecated" extends="java.lang.Object" extends-generic-aware="java.lang.Object"
final="false" name="SomeObject" static="false" visibility="public">
<constructor deprecated="not deprecated" final="false" name="SomeObject" static="false" type="xamarin.test.SomeObject" visibility="public">
<constructor deprecated="deprecated" final="false" name="SomeObject" static="false" type="xamarin.test.SomeObject" visibility="public">
</constructor>
<constructor deprecated="not deprecated" final="false" name="SomeObject" static="false" type="xamarin.test.SomeObject" visibility="public">
<parameter name="aint" type="int">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected SomeObject (IntPtr javaReference, JniHandleOwnership transfer) : base
static IntPtr id_ctor;
// Metadata.xml XPath constructor reference: path="/api/package[@name='xamarin.test']/class[@name='SomeObject']/constructor[@name='SomeObject' and count(parameter)=0]"
[Register (".ctor", "()V", "")]
[Obsolete (@"deprecated")]
public unsafe SomeObject ()
: base (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
{
Expand Down
26 changes: 25 additions & 1 deletion tools/generator/Tests/generator-Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,32 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Include="expected\**\*.cs" />
<None Include="expected\**\*.xml" />
<None Include="expected.ji\**\*.cs" />
<None Include="expected.ji\**\*.xml" />
<None Include="..\Tests-Core\api-cp.xml">
<Link>Tests-Core\api-cp.xml</Link>
</None>
<None Include="..\Tests-Core\api.xml">
<Link>Tests-Core\api.xml</Link>
</None>
<None Include="..\Tests-Core\fields-cp.xml">
<Link>Tests-Core\fields-cp.xml</Link>
</None>
<None Include="..\Tests-Core\fields.xml">
<Link>Tests-Core\fields.xml</Link>
</None>
<None Include="..\Tests-Core\methods-cp.xml">
<Link>Tests-Core\methods-cp.xml</Link>
</None>
<None Include="..\Tests-Core\methods.xml">
<Link>Tests-Core\methods.xml</Link>
</None>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="expected.targets" />
</Project>
</Project>
23 changes: 23 additions & 0 deletions tools/generator/generator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "generator", "generator.csproj", "{D14A1B5C-2060-4930-92BE-F7190256C735}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "generator-Tests", "Tests\generator-Tests.csproj", "{4EEAB1A7-99C1-4302-9C18-01A7B481409B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D14A1B5C-2060-4930-92BE-F7190256C735}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D14A1B5C-2060-4930-92BE-F7190256C735}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D14A1B5C-2060-4930-92BE-F7190256C735}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D14A1B5C-2060-4930-92BE-F7190256C735}.Release|Any CPU.Build.0 = Release|Any CPU
{4EEAB1A7-99C1-4302-9C18-01A7B481409B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4EEAB1A7-99C1-4302-9C18-01A7B481409B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4EEAB1A7-99C1-4302-9C18-01A7B481409B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4EEAB1A7-99C1-4302-9C18-01A7B481409B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 56e978f

Please sign in to comment.