Skip to content

Commit cb3f2d4

Browse files
committed
[Java.Interop] Rename ManagedPeer.runConstructor() to construct()
Commit 8c83f64 decided to drop the Xamarin.Android pattern: // Xamarin.Android constructor style public JavaPeerType(Args... args) { super (args...); if (getClass() == JavaPeerType.class) TypeManager.Activate ( AssemblyQualifiedTypeName, ConstructorSignature, this, new java.lang.Object[]{ args } ); } And "move" the getClass() check into a helper method: // Java.Interop constructor style: public JavaPeerType(Args... args) { super (args...); ManagedPeer.runConstructor ( JavaPeerType.class, this, AssemblyQualifiedTypeName, ConstructorSignature, args... ); } Unspecified was *why* this would be a good thing; the primary thought was that this might reduce .class size (unverified!), as there wouldn't need to be the `if` check. Upon further reflection, while the 8c83f64 approach looks cleaner, it is *less* flexible. One of the future features to work toward is FullAOT support (176240d, 06cfd83), and one of the problems there is...activation constructors. :-) What I'd like to explore doing is updating jnimarshalmethod-gen.exe to emit constructors. This mixes up the ManagedPeer.runConstructor() paradigm because in such an environment we'd instead want `native` methods in the Java Callable Warpper which the constructor would invoke: // Java class MyCallableWrapper { public MyCallableWrapper () { // What should go here? } native void n_ctor (); } The obvious thing to do is...a Xamarin.Android-style type check! class MyCallableWrapper { public MyCallableWrapper () { if (MyCallableWrapper.class == getClass() { n_ctor (); } } native void n_ctor (); } The 8c83f64-style ManagedPeer.runConstructor() idiom can't support that, not when the "constructor" native methods are per-class. Thus, there's no actual need for doing the getClass() check within ManagedPeer.runConstructor(). Remove this overload: ManagedPeer.runConstructor(Class, Object, String, String, Object[]) Rename ManagedPeer.runConstructor(Object, String, String, Object[]) to ManagedPeer.construct(), and change the existing Java types to use a Xamarin.Android-style type check.
1 parent 1c99956 commit cb3f2d4

File tree

10 files changed

+71
-73
lines changed

10 files changed

+71
-73
lines changed

src/Java.Interop/Java.Interop/ManagedPeer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace Java.Interop {
2020
static ManagedPeer ()
2121
{
2222
_members.JniPeerType.RegisterNativeMethods (
23-
new JniNativeMethodRegistration ("runConstructor", RunConstructorSignature, (Action<IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr>) RunConstructor)
23+
new JniNativeMethodRegistration ("construct", ConstructSignature, (Action<IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr>) Construct)
2424
);
2525
}
2626

@@ -38,10 +38,10 @@ public override JniPeerMembers JniPeerMembers {
3838
get {return _members;}
3939
}
4040

41-
const string RunConstructorSignature = "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V";
41+
const string ConstructSignature = "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V";
4242

4343
// TODO: Keep in sync with the code generated by ExportedMemberBuilder
44-
static void RunConstructor (
44+
static void Construct (
4545
IntPtr jnienv,
4646
IntPtr klass,
4747
IntPtr n_self,

src/Java.Interop/Tests/java/com/xamarin/interop/CallNonvirtualBase.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ public class CallNonvirtualBase implements GCUserPeerable {
99
ArrayList<Object> managedReferences = new ArrayList<Object>();
1010

1111
public CallNonvirtualBase () {
12-
com.xamarin.java_interop.ManagedPeer.runConstructor (
13-
CallNonvirtualBase.class,
14-
this,
15-
"Java.InteropTests.CallNonvirtualBase, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
16-
""
17-
);
12+
if (CallNonvirtualBase.class == getClass ()) {
13+
com.xamarin.java_interop.ManagedPeer.construct (
14+
this,
15+
"Java.InteropTests.CallNonvirtualBase, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
16+
""
17+
);
18+
}
1819
}
1920

2021
boolean methodInvoked;

src/Java.Interop/Tests/java/com/xamarin/interop/CallNonvirtualDerived.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ public class CallNonvirtualDerived
1111
ArrayList<Object> managedReferences = new ArrayList<Object>();
1212

1313
public CallNonvirtualDerived () {
14-
com.xamarin.java_interop.ManagedPeer.runConstructor (
15-
CallNonvirtualDerived.class,
16-
this,
17-
"Java.InteropTests.CallNonvirtualDerived, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
18-
""
19-
);
14+
if (CallNonvirtualDerived.class == getClass ()) {
15+
com.xamarin.java_interop.ManagedPeer.construct (
16+
this,
17+
"Java.InteropTests.CallNonvirtualDerived, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
18+
""
19+
);
20+
}
2021
}
2122

2223
boolean methodInvoked;

src/Java.Interop/Tests/java/com/xamarin/interop/CallNonvirtualDerived2.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ public class CallNonvirtualDerived2
1111
ArrayList<Object> managedReferences = new ArrayList<Object>();
1212

1313
public CallNonvirtualDerived2 () {
14-
com.xamarin.java_interop.ManagedPeer.runConstructor (
15-
CallNonvirtualDerived2.class,
16-
this,
17-
"Java.InteropTests.CallNonvirtualDerived2, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
18-
""
19-
);
14+
if (CallNonvirtualDerived2.class == getClass ()) {
15+
com.xamarin.java_interop.ManagedPeer.construct (
16+
this,
17+
"Java.InteropTests.CallNonvirtualDerived2, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
18+
""
19+
);
20+
}
2021
}
2122

2223
public void jiAddManagedReference (java.lang.Object obj)

src/Java.Interop/Tests/java/com/xamarin/interop/CallVirtualFromConstructorBase.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ public class CallVirtualFromConstructorBase implements GCUserPeerable {
99
ArrayList<Object> managedReferences = new ArrayList<Object>();
1010

1111
public CallVirtualFromConstructorBase (int value) {
12-
com.xamarin.java_interop.ManagedPeer.runConstructor (
13-
CallVirtualFromConstructorBase.class,
14-
this,
15-
"Java.InteropTests.CallVirtualFromConstructorBase, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
16-
"System.Int32",
17-
value
18-
);
12+
if (CallVirtualFromConstructorBase.class == getClass ()) {
13+
com.xamarin.java_interop.ManagedPeer.construct (
14+
this,
15+
"Java.InteropTests.CallVirtualFromConstructorBase, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
16+
"System.Int32",
17+
value
18+
);
19+
}
1920
calledFromConstructor (value);
2021
}
2122

src/Java.Interop/Tests/java/com/xamarin/interop/CallVirtualFromConstructorDerived.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
åpackage com.xamarin.interop;
1+
package com.xamarin.interop;
22

33
import java.util.ArrayList;
44

@@ -12,13 +12,14 @@ public class CallVirtualFromConstructorDerived
1212

1313
public CallVirtualFromConstructorDerived (int value) {
1414
super (value);
15-
com.xamarin.java_interop.ManagedPeer.runConstructor (
16-
CallVirtualFromConstructorDerived.class,
17-
this,
18-
"Java.InteropTests.CallVirtualFromConstructorDerived, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
19-
"System.Int32",
20-
value
21-
);
15+
if (CallVirtualFromConstructorDerived.class == getClass ()) {
16+
com.xamarin.java_interop.ManagedPeer.construct (
17+
this,
18+
"Java.InteropTests.CallVirtualFromConstructorDerived, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
19+
"System.Int32",
20+
value
21+
);
22+
}
2223
}
2324

2425
public static CallVirtualFromConstructorDerived newInstance (int value)

src/Java.Interop/Tests/java/com/xamarin/interop/CrossReferenceBridge.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ public class CrossReferenceBridge implements GCUserPeerable {
99
ArrayList<Object> managedReferences = new ArrayList<Object>();
1010

1111
public CrossReferenceBridge () {
12-
com.xamarin.java_interop.ManagedPeer.runConstructor (
13-
CrossReferenceBridge.class,
14-
this,
15-
"Java.InteropTests.CrossReferenceBridge, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
16-
""
17-
);
12+
if (CrossReferenceBridge.class == getClass ()) {
13+
com.xamarin.java_interop.ManagedPeer.construct (
14+
this,
15+
"Java.InteropTests.CrossReferenceBridge, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
16+
""
17+
);
18+
}
1819
}
1920

2021
public void jiAddManagedReference (java.lang.Object obj)

src/Java.Interop/Tests/java/com/xamarin/interop/SelfRegistration.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ public class SelfRegistration implements GCUserPeerable {
99
ArrayList<Object> managedReferences = new ArrayList<Object>();
1010

1111
public SelfRegistration () {
12-
com.xamarin.java_interop.ManagedPeer.runConstructor (
13-
SelfRegistration.class,
14-
this,
15-
"Java.InteropTests.SelfRegistration, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
16-
""
17-
);
12+
if (SelfRegistration.class == getClass ()) {
13+
com.xamarin.java_interop.ManagedPeer.construct (
14+
this,
15+
"Java.InteropTests.SelfRegistration, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
16+
""
17+
);
18+
}
1819
}
1920

2021
public void jiAddManagedReference (java.lang.Object obj)

src/Java.Interop/Tests/java/com/xamarin/interop/TestType.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,25 @@ public class TestType implements GCUserPeerable {
99
ArrayList<Object> managedReferences = new ArrayList<Object>();
1010

1111
public TestType () {
12-
com.xamarin.java_interop.ManagedPeer.runConstructor (
13-
TestType.class,
14-
this,
15-
"Java.InteropTests.TestType, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
16-
""
17-
);
12+
if (TestType.class == getClass ()) {
13+
com.xamarin.java_interop.ManagedPeer.construct (
14+
this,
15+
"Java.InteropTests.TestType, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
16+
""
17+
);
18+
}
1819
}
1920

2021
// For test purposes; DO NOT provide this constructor in the TestType.cs!
2122
public TestType (TestType a, int b) {
22-
com.xamarin.java_interop.ManagedPeer.runConstructor (
23-
TestType.class,
24-
this,
25-
"Java.InteropTests.TestType, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
26-
"Java.InteropTests.TestType, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null:System.Int32",
27-
a, b
28-
);
23+
if (TestType.class == getClass ()) {
24+
com.xamarin.java_interop.ManagedPeer.construct (
25+
this,
26+
"Java.InteropTests.TestType, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
27+
"Java.InteropTests.TestType, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null:System.Int32",
28+
a, b
29+
);
30+
}
2931
}
3032

3133
public void runTests () {

src/Java.Interop/java/com/xamarin/java_interop/ManagedPeer.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,7 @@ private ManagedPeer () {
66

77
// public static native void registerNativeMethods (java.lang.Class<?> nativeClass, String managedType, String methods);
88

9-
public static void runConstructor (
10-
Class<?> declaringClass,
11-
Object self,
12-
String assemblyQualifiedName,
13-
String constructorSignature,
14-
Object... arguments) {
15-
if (self.getClass() != declaringClass)
16-
return;
17-
runConstructor (self, assemblyQualifiedName, constructorSignature, arguments);
18-
}
19-
20-
static native void runConstructor (
9+
public static native void construct (
2110
Object self,
2211
String assemblyQualifiedName,
2312
String constructorSignature,

0 commit comments

Comments
 (0)