Skip to content

Commit 09f394d

Browse files
C#: Add extension method flag to type model (#7075)
Set bit 20 in CSharpTypeMapping.MapFlags when IMethodSymbol.IsExtensionMethod is true. This bit is unused by both the JVM spec and Java's Flag enum. On the Java side, widen the Method constructor's flag mask to preserve bit 20 and add JavaType.Method.isExtensionMethod() so recipes can detect extension methods without knowing the raw bit position.
1 parent 62afd92 commit 09f394d

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

rewrite-csharp/csharp/OpenRewrite/CSharp/CSharpTypeMapping.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ private static long MapFlags(ISymbol symbol)
351351
if (symbol.IsStatic) flags |= 8; // Flag.Static
352352
if (symbol.IsAbstract) flags |= 1024; // Flag.Abstract
353353
if (symbol.IsSealed) flags |= 16; // Flag.Final (sealed ~ final)
354+
// C#-specific: mark extension methods (bit 20, not used by Java Flag enum)
355+
if (symbol is IMethodSymbol { IsExtensionMethod: true }) flags |= 1L << 20;
354356
return flags;
355357
}
356358

rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,10 @@ public boolean isNumeric() {
12961296
@Getter
12971297
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
12981298
class Method implements JavaType {
1299+
// C#-specific: marks extension methods (bit 20, not used by Java Flag enum)
1300+
private static final long EXTENSION_METHOD_FLAG = 1L << 20;
1301+
private static final long VALID_METHOD_FLAGS = Flag.VALID_FLAGS | EXTENSION_METHOD_FLAG;
1302+
12991303
@With
13001304
@Nullable
13011305
@NonFinal
@@ -1387,7 +1391,7 @@ public Method(@Nullable Integer managedReference, long flagsBitMap, @Nullable Fu
13871391
FullyQualified @Nullable [] annotations, @Nullable List<String> defaultValue,
13881392
String @Nullable [] declaredFormalTypeNames) {
13891393
this.managedReference = managedReference;
1390-
this.flagsBitMap = flagsBitMap & Flag.VALID_FLAGS;
1394+
this.flagsBitMap = flagsBitMap & VALID_METHOD_FLAGS;
13911395
this.declaringType = unknownIfNull(declaringType);
13921396
this.name = name;
13931397
this.returnType = unknownIfNull(returnType);
@@ -1636,6 +1640,13 @@ public Method withFlags(Set<Flag> flags) {
16361640
return withFlagsBitMap(Flag.flagsToBitMap(flags));
16371641
}
16381642

1643+
/**
1644+
* @return {@code true} if this method is a C# extension method.
1645+
*/
1646+
public boolean isExtensionMethod() {
1647+
return (flagsBitMap & EXTENSION_METHOD_FLAG) != 0;
1648+
}
1649+
16391650
@Override
16401651
public boolean equals(Object o) {
16411652
if (this == o) return true;

0 commit comments

Comments
 (0)