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

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

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

问题

Specifically this line:

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

特别是这一行:

"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

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

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

Same in other Java code that has String concatenation

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

throw new IllegalArgumentException(new StringBuilder()
    .append(getClass())
    .append(" cannot deserialize to ")
    .append(typeOfT)
    .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:

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

Specifically this line:

"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.

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_17 // <-- was 1.8
}
kotlinOptions {
    jvmTarget = "17" // <-- was 1.8
    languageVersion = '1.9'
}

Now I can obviously replace the code with

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

Same in other Java code that has String concatenation

        throw new IllegalArgumentException(new StringBuilder()
            .append(getClass())
            .append(" cannot deserialize to ")
            .append(typeOfT)
            .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?

error: cannot find symbol, 

symbol: method makeConcatWithConstants(Lookup,String,MethodType,String), 

location: interface StringConcatFactory

答案1

得分: 1

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

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

结果如下:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_9
    targetCompatibility JavaVersion.VERSION_17

    coreLibraryDesugaringEnabled true
}
kotlinOptions {
    jvmTarget = "17"
    languageVersion = '1.9' 
}

dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'
}
英文:

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:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_9
    targetCompatibility JavaVersion.VERSION_17

    coreLibraryDesugaringEnabled true
}
kotlinOptions {
    jvmTarget = "17"
    languageVersion = '1.9' 
}

dependencies {
    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:

确定