Skip to content

Commit 56e978f

Browse files
atsushienojonpryor
authored andcommitted
[generator] Constructors support [Obsolete] (#273)
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.
1 parent 6807817 commit 56e978f

File tree

7 files changed

+96
-2
lines changed

7 files changed

+96
-2
lines changed

tools/generator/Ctor.cs

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ public void Generate (StreamWriter sw, string indent, CodeGenerationOptions opt,
162162
opt.CodeGenerator.WriteConstructorIdField (this, sw, indent, opt);
163163
sw.WriteLine ("{0}// Metadata.xml XPath constructor reference: path=\"{1}/constructor[@name='{2}'{3}]\"", indent, type.MetadataXPathReference, type.JavaSimpleName, Parameters.GetMethodXPathPredicate ());
164164
sw.WriteLine ("{0}[Register (\"{1}\", \"{2}\", \"{3}\"{4})]", indent, ".ctor", jni_sig, String.Empty, this.AdditionalAttributeString ());
165+
if (Deprecated != null)
166+
sw.WriteLine ("{0}[Obsolete (@\"{1}\")]", indent, Deprecated.Replace ("\"", "\"\""));
165167
GenerateCustomAttributes (sw, indent);
166168
sw.WriteLine ("{0}{1} unsafe {2} ({3})\n{0}\t: {4} (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)",
167169
indent, Visibility, Name, GenBase.GetSignature (this, opt), use_base ? "base" : "this");

tools/generator/Tests/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
These tests are not intuitive at all. So we need some documentation here.
3+
4+
These tests compare outcomes from generator to the "expected" outcomes.
5+
6+
It does not work together with class-parse, so you cannot pass any jars
7+
nor java sources, which is a pain point (but those who created these tests
8+
didn't care).
9+
10+
There are two "expected" set of files. One is "expected" and the other is
11+
"expected.ji". They are different per Java.Interop output methods.
12+
13+
The differences between "expected" and "expected.ji" are almost only
14+
annoying, but this test blindly compares those differences. So if you are
15+
going to add tests you will have to duplicate your work twice...
16+
17+
Tests that use `BaseGeneratorTest` are organized as:
18+
19+
./BaseGeneratorTest.cs - sets up generation and compilation options.
20+
./Compiler.cs - implements C# compilation with `CodeDomProvider`.
21+
./(others).cs - the actual `TestFixture`s.
22+
23+
What those tests do are:
24+
25+
- invoke class-parse (and perhaps jar2xml), to generate XML API inputs to generator.
26+
- invoke generator, to generate comparable sources.
27+
- optionally invoke csc to see if it builds.
28+
29+
`BaseGeneratorTest` takes the arguments below,
30+
31+
- outputRelativePath - path to generator output subdir
32+
- apiDescriptionFile - path to the input API XML output. Sadly existing tests
33+
are organized horribly and they reside in the "expected" directory.
34+
- expectedRelativePath - path to the "expected" file generation.
35+
- additionalSupportPaths - path to additional compilation items.
36+
37+
The test outputs are generated to `out` and `out.ji` directories, per
38+
the generator's output method.
39+
40+
When you are creating a new test, it is easier to once generate results in
41+
those `out` and `out.ji` directories, and copy them as "expected" and
42+
"expected.ji" with required changes (so that they become really expected
43+
contents).

tools/generator/Tests/expected.ji/Constructors/Xamarin.Test.SomeObject.cs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ protected SomeObject (IntPtr javaReference, JniHandleOwnership transfer) : base
3232

3333
// Metadata.xml XPath constructor reference: path="/api/package[@name='xamarin.test']/class[@name='SomeObject']/constructor[@name='SomeObject' and count(parameter)=0]"
3434
[Register (".ctor", "()V", "")]
35+
[Obsolete (@"deprecated")]
3536
public unsafe SomeObject ()
3637
: base (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
3738
{

tools/generator/Tests/expected/Constructors/Constructors.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<package name="xamarin.test">
88
<class abstract="false" deprecated="not deprecated" extends="java.lang.Object" extends-generic-aware="java.lang.Object"
99
final="false" name="SomeObject" static="false" visibility="public">
10-
<constructor deprecated="not deprecated" final="false" name="SomeObject" static="false" type="xamarin.test.SomeObject" visibility="public">
10+
<constructor deprecated="deprecated" final="false" name="SomeObject" static="false" type="xamarin.test.SomeObject" visibility="public">
1111
</constructor>
1212
<constructor deprecated="not deprecated" final="false" name="SomeObject" static="false" type="xamarin.test.SomeObject" visibility="public">
1313
<parameter name="aint" type="int">

tools/generator/Tests/expected/Constructors/Xamarin.Test.SomeObject.cs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ protected SomeObject (IntPtr javaReference, JniHandleOwnership transfer) : base
2828
static IntPtr id_ctor;
2929
// Metadata.xml XPath constructor reference: path="/api/package[@name='xamarin.test']/class[@name='SomeObject']/constructor[@name='SomeObject' and count(parameter)=0]"
3030
[Register (".ctor", "()V", "")]
31+
[Obsolete (@"deprecated")]
3132
public unsafe SomeObject ()
3233
: base (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
3334
{

tools/generator/Tests/generator-Tests.csproj

+25-1
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,32 @@
154154
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
155155
</Content>
156156
</ItemGroup>
157+
<ItemGroup>
158+
<None Include="expected\**\*.cs" />
159+
<None Include="expected\**\*.xml" />
160+
<None Include="expected.ji\**\*.cs" />
161+
<None Include="expected.ji\**\*.xml" />
162+
<None Include="..\Tests-Core\api-cp.xml">
163+
<Link>Tests-Core\api-cp.xml</Link>
164+
</None>
165+
<None Include="..\Tests-Core\api.xml">
166+
<Link>Tests-Core\api.xml</Link>
167+
</None>
168+
<None Include="..\Tests-Core\fields-cp.xml">
169+
<Link>Tests-Core\fields-cp.xml</Link>
170+
</None>
171+
<None Include="..\Tests-Core\fields.xml">
172+
<Link>Tests-Core\fields.xml</Link>
173+
</None>
174+
<None Include="..\Tests-Core\methods-cp.xml">
175+
<Link>Tests-Core\methods-cp.xml</Link>
176+
</None>
177+
<None Include="..\Tests-Core\methods.xml">
178+
<Link>Tests-Core\methods.xml</Link>
179+
</None>
180+
</ItemGroup>
157181
<ItemGroup>
158182
<None Include="packages.config" />
159183
</ItemGroup>
160184
<Import Project="expected.targets" />
161-
</Project>
185+
</Project>

tools/generator/generator.sln

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2012
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "generator", "generator.csproj", "{D14A1B5C-2060-4930-92BE-F7190256C735}"
5+
EndProject
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "generator-Tests", "Tests\generator-Tests.csproj", "{4EEAB1A7-99C1-4302-9C18-01A7B481409B}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{D14A1B5C-2060-4930-92BE-F7190256C735}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D14A1B5C-2060-4930-92BE-F7190256C735}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D14A1B5C-2060-4930-92BE-F7190256C735}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D14A1B5C-2060-4930-92BE-F7190256C735}.Release|Any CPU.Build.0 = Release|Any CPU
18+
{4EEAB1A7-99C1-4302-9C18-01A7B481409B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{4EEAB1A7-99C1-4302-9C18-01A7B481409B}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{4EEAB1A7-99C1-4302-9C18-01A7B481409B}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{4EEAB1A7-99C1-4302-9C18-01A7B481409B}.Release|Any CPU.Build.0 = Release|Any CPU
22+
EndGlobalSection
23+
EndGlobal

0 commit comments

Comments
 (0)