swiftlang/swift-java

JavaFieldMacro generated extension failed to compile

Closed this issue · 1 comments

I was writing test examples for configuration using a Java Json library. When I only imported the Json class, it worked fine. However, when I imported the JsonFactory methods, it failed to compile. The reason I see is that JavaFieldMacro was referencing the same FieldType inside JavaClass. If one Java library has a static type that references a different class, the generated code won’t work.

https://github.com/ajermakovics/json/blob/13e49ceeaba997ca2c4f78569e75fb2e12248ce3/src/main/java/org/andrejs/json/Json.java#L16C16-L16C27

Generated extension for JavaClass

extension JavaClass<Json> {
  @JavaStaticField(isFinal: true)
  public var EMPTY: Json!

  @JavaStaticField(isFinal: false) // failed to compile
  public var of: JsonFactory!
}

Both files were generated, but the compiler wasn't able to find JsonFactory

/swift-java/Samples/JavaDependencySampleApp/.build/plugins/outputs/javadependencysampleapp/JavaJson/destination/SwiftJavaPlugin/generated/Json.swift:70:18: error: cannot find type 'JsonFactory' in scope
68 | 
69 |   @JavaStaticField(isFinal: false)
70 |   public var of: JsonFactory!
   |                  `- error: cannot find type 'JsonFactory' in scope
71 | }
72 | 
/swift-java/Samples/JavaDependencySampleApp/.build/plugins/outputs/javadependencysampleapp/JavaJson/destination/SwiftJavaPlugin/generated/Json.swift:70:18: error: cannot find type 'JsonFactory' in scope
68 | 
69 |   @JavaStaticField(isFinal: false)
70 |   public var of: JsonFactory!
   |                  `- error: cannot find type 'JsonFactory' in scope
71 | }
72 | 

macro expansion @JavaStaticField:7:46: error: cannot find 'JsonFactory' in scope
`- /swift-java/Samples/JavaDependencySampleApp/.build/plugins/outputs/javadependencysampleapp/JavaJson/destination/SwiftJavaPlugin/generated/Json.swift:70:30: note: expanded code originates here
67 |   public var EMPTY: Json!
68 | 
69 |   @JavaStaticField(isFinal: false)
   |   `- note: in expansion of macro 'JavaStaticField' on property 'of' here
70 |   public var of: JsonFactory!
   +--- macro expansion @JavaStaticField -------------------------------
   |5 |     // FieldType.self
   |6 |     set {
   |7 |         self[javaFieldName: "of", fieldType: JsonFactory?.self] = newValue
   |  |                                              `- error: cannot find 'JsonFactory' in scope
   |8 |     }
   |9 | }
   +--------------------------------------------------------------------
71 | }
72 | 

macro expansion @JavaStaticField:7:57: error: cannot convert value of type 'Optional<_>' to expected argument type 'FieldType.Type'
`- /swift-java/Samples/JavaDependencySampleApp/.build/plugins/outputs/javadependencysampleapp/JavaJson/destination/SwiftJavaPlugin/generated/Json.swift:70:30: note: expanded code originates here
67 |   public var EMPTY: Json!
68 | 
69 |   @JavaStaticField(isFinal: false)
   |   `- note: in expansion of macro 'JavaStaticField' on property 'of' here
70 |   public var of: JsonFactory!
   +--- macro expansion @JavaStaticField -------------------------------
   |5 |     // FieldType.self
   |6 |     set {
   |7 |         self[javaFieldName: "of", fieldType: JsonFactory?.self] = newValue
   |  |                                                         `- error: cannot convert value of type 'Optional<_>' to expected argument type 'FieldType.Type'
   |8 |     }
   |9 | }
   +--------------------------------------------------------------------
71 | }
72 | 
[3/5] Compiling JavaJson Json.swift

Ahh, cleaning up .build folder fixed it.