@@ -44,35 +44,39 @@ private static void AnalyzeClass (SyntaxNodeAnalysisContext context)
44
44
if ( ! Utilities . IsDerivedFrom ( classSymbol , AndroidApplication ) )
45
45
return ;
46
46
47
- var constructors = classDeclarationSyntax . Members
48
- . OfType < ConstructorDeclarationSyntax > ( ) ;
49
-
50
47
bool foundActivationConstructor = false ;
51
- foreach ( var constructor in constructors ) {
52
- var parameters = constructor . ParameterList . Parameters ;
53
- if ( parameters . Count != 2 )
54
- continue ;
55
- var type = parameters [ 0 ] . Type switch {
56
- IdentifierNameSyntax identifierNameSyntax => identifierNameSyntax . Identifier . Text ,
57
- QualifiedNameSyntax qualifiedNameSyntax => qualifiedNameSyntax . Right . Identifier . Text ,
58
- _ => parameters [ 0 ] . Type . ToString ( )
59
- } ;
60
- if ( type != "IntPtr" && type != "nint" )
61
- continue ;
62
- var ns = Utilities . GetNamespaceForParameterType ( parameters [ 1 ] , context . SemanticModel ) ;
63
- type = parameters [ 1 ] . Type switch {
64
- IdentifierNameSyntax identifierNameSyntax => identifierNameSyntax . Identifier . Text ,
65
- QualifiedNameSyntax qualifiedNameSyntax => qualifiedNameSyntax . Right . Identifier . Text ,
66
- _ => parameters [ 1 ] . Type . ToString ( )
67
- } ;
68
- var isJniHandle = ( ns == "Android.Runtime" ) && ( type == "JniHandleOwnership" ) ;
69
- if ( ! isJniHandle )
70
- continue ;
71
- foundActivationConstructor = true ;
48
+
49
+ // Check all constructors (including primary constructors) using symbol information
50
+ foreach ( var constructor in classSymbol . Constructors ) {
51
+ if ( HasActivationConstructorSignature ( constructor ) ) {
52
+ foundActivationConstructor = true ;
53
+ break ;
54
+ }
72
55
}
56
+
73
57
if ( ! foundActivationConstructor ) {
74
58
var diagnostic = Diagnostic . Create ( Rule , classDeclarationSyntax . Identifier . GetLocation ( ) , classDeclarationSyntax . Identifier . Text ) ;
75
59
context . ReportDiagnostic ( diagnostic ) ;
76
60
}
77
61
}
62
+
63
+ private static bool HasActivationConstructorSignature ( IMethodSymbol constructor )
64
+ {
65
+ if ( constructor . Parameters . Length != 2 )
66
+ return false ;
67
+
68
+ var firstParam = constructor . Parameters [ 0 ] ;
69
+ var secondParam = constructor . Parameters [ 1 ] ;
70
+
71
+ // Check first parameter: IntPtr or nint
72
+ var firstParamType = firstParam . Type . ToDisplayString ( ) ;
73
+ bool isValidFirstParam = firstParamType == "System.IntPtr" || firstParamType == "nint" ;
74
+
75
+ // Check second parameter: Android.Runtime.JniHandleOwnership
76
+ var secondParamType = secondParam . Type ;
77
+ bool isValidSecondParam = secondParamType . ContainingNamespace ? . ToDisplayString ( ) == "Android.Runtime" &&
78
+ secondParamType . Name == "JniHandleOwnership" ;
79
+
80
+ return isValidFirstParam && isValidSecondParam ;
81
+ }
78
82
}
0 commit comments