Java string concatenation operation fails with "symbol not found: StringConcatFactory.makeConcatWithConstants"

huangapple go评论136阅读模式
英文:

Java string concatenation operation fails with "symbol not found: StringConcatFactory.makeConcatWithConstants"

问题

Specifically this line:

  1. "Could not deserialize radio option from list" + Arrays.toString(items.toArray())

特别是这一行:

  1. "Could not deserialize radio option from list" + Arrays.toString(items.toArray())

I'm in the process of upgrading Gradle to 8.2, Compose to 1.5.0, Kotlin to 1.9.0 (this feels like a possible culprit), Android Gradle Plugin to 8.1, and had to make updates to the Java/Kotlin JVM target.

我正在升级Gradle到8.2,Compose到1.5.0,Kotlin到1.9.0(这可能是问题的原因),Android Gradle插件到8.1,并且必须更新Java/Kotlin JVM目标。

Now I can obviously replace the code with

现在,我显然可以将代码替换为:

  1. throw new IllegalArgumentException(new StringBuilder()
  2. .append("Could not deserialize radio option from list")
  3. .append(Arrays.toString(items.toArray()))
  4. .toString());

Same in other Java code that has String concatenation

同样适用于其他具有字符串连接的Java代码:

  1. throw new IllegalArgumentException(new StringBuilder()
  2. .append(getClass())
  3. .append(" cannot deserialize to ")
  4. .append(typeOfT)
  5. .toString());

But it seems somewhat eerie that ALL string concatenation operations done in Java with the + operator would suddenly just "start failing".

但似乎有点不寻常的是,在Java中使用+运算符执行的所有字符串连接操作突然会开始失败。

Any clues what needs to be configured in order to prevent this error?

是否有任何线索可以配置以防止此错误?

error: cannot find symbol, symbol: method makeConcatWithConstants(Lookup,String,MethodType,String), location: interface StringConcatFactory

错误:找不到符号,符号:方法makeConcatWithConstants(Lookup,String,MethodType,String),位置:接口StringConcatFactory

英文:

I have the following Java snippet:

  1. public static DetailChecklistFormDescriptor.ChecklistItemRadioField.RadioOption getRadioOptionFromList(List items, int position) {
  2. Object item = items.get(position);
  3. if(item instanceof DetailChecklistFormDescriptor.ChecklistItemRadioField.RadioOption) {
  4. return (DetailChecklistFormDescriptor.ChecklistItemRadioField.RadioOption) item;
  5. } else if(item instanceof com.google.gson.internal.LinkedTreeMap) {
  6. return new DetailChecklistFormDescriptor.ChecklistItemRadioField.RadioOption(
  7. (String) ((com.google.gson.internal.LinkedTreeMap) item).get("radioKey"),
  8. (String) ((com.google.gson.internal.LinkedTreeMap) item).get("radioValue")
  9. );
  10. }
  11. throw new IllegalArgumentException("Could not deserialize radio option from list" + Arrays.toString(items.toArray())); // <-- !!!
  12. }

Specifically this line:

  1. "Could not deserialize radio option from list" + Arrays.toString(items.toArray())

I'm in the process of upgrading Gradle to 8.2, Compose to 1.5.0, Kotlin to 1.9.0 (this feels like a possible culprit), Android Gradle Plugin to 8.1, and had to make updates to the Java/Kotlin JVM target.

  1. compileOptions {
  2. sourceCompatibility JavaVersion.VERSION_1_8
  3. targetCompatibility JavaVersion.VERSION_17 // <-- was 1.8
  4. }
  5. kotlinOptions {
  6. jvmTarget = "17" // <-- was 1.8
  7. languageVersion = '1.9'
  8. }

Now I can obviously replace the code with

  1. throw new IllegalArgumentException(new StringBuilder()
  2. .append("Could not deserialize radio option from list")
  3. .append(Arrays.toString(items.toArray()))
  4. .toString());

Same in other Java code that has String concatenation

  1. throw new IllegalArgumentException(new StringBuilder()
  2. .append(getClass())
  3. .append(" cannot deserialize to ")
  4. .append(typeOfT)
  5. .toString());

But it seems somewhat eerie that ALL string concatenation operations done in Java with the + operator would suddenly just "start failing".

Any clues what needs to be configured in order to prevent this error?

  1. error: cannot find symbol,
  2. symbol: method makeConcatWithConstants(Lookup,String,MethodType,String),
  3. location: interface StringConcatFactory

答案1

得分: 1

设置更高的源兼容性(JavaVersion.VERSION_1_9)允许代码编译。

之后,启用核心库 desugaring 是重要的,因为 Android 支持从 SDK 26 开始的 Java 8,而 Java 9+ 仅在较高的 API 级别上支持,因此必须启用核心库 desugaring。

结果如下:

  1. compileOptions {
  2. sourceCompatibility JavaVersion.VERSION_1_9
  3. targetCompatibility JavaVersion.VERSION_17
  4. coreLibraryDesugaringEnabled true
  5. }
  6. kotlinOptions {
  7. jvmTarget = "17"
  8. languageVersion = '1.9'
  9. }
  10. dependencies {
  11. coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'
  12. }
英文:

Setting a higher source compatibility (JavaVersion.VERSION_1_9) allows for the code to compile.

Afterwards, it is important to enable core library desugaring, as Android supports Java 8 from SDK 26, and Java 9+ only at higher API levels, so core library desugaring must be enabled.

Result is:

  1. compileOptions {
  2. sourceCompatibility JavaVersion.VERSION_1_9
  3. targetCompatibility JavaVersion.VERSION_17
  4. coreLibraryDesugaringEnabled true
  5. }
  6. kotlinOptions {
  7. jvmTarget = "17"
  8. languageVersion = '1.9'
  9. }
  10. dependencies {
  11. coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'

huangapple
  • 本文由 发表于 2023年8月10日 19:40:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定