简体   繁体   English

使用 lombok.Getter(lazy = true) 和 java 10 的不兼容类型错误

[英]Incompatible types error using lombok.Getter(lazy = true) with java 10

I'm trying to do some caching using reactor , reactor.ipc.netty.http.client.HttpClient and initialize it as lazy getter field with lombok's @Getter(lazy = true) .我试图做一些使用缓存reactorreactor.ipc.netty.http.client.HttpClient并初始化它与龙目岛的懒惰吸气场@Getter(lazy = true)

All works fine with Java 8 but fails to compile with error: incompatible types: Duration cannot be converted to String with Java 10 on this snippet在 Java 8 中一切正常,但无法编译并error: incompatible types: Duration cannot be converted to String此代码段上的 Java 10 error: incompatible types: Duration cannot be converted to String

@Value
public static class Translations {

    Map<String, Translation> translations;

    @Value
    public static class Translation {

        Map<String, String> content;

    }
}

@Getter(lazy = true)
Mono<Map<String, Translations.Translation>> translations = httpClient
        .get(String.format("%s/translations/%s", endpoint, translationGroup), Function.identity())
        .flatMap(it -> it.receive().aggregate().asByteArray())
        .map(byteArray -> {
            try {
                return objectMapper.readValue(byteArray, Translations.class);
            } catch (IOException e) {
                throw new UncheckedIOException("Failed to get translation for " + translationGroup, e);
            }
        })
        .map(Translations::getTranslations)
        .retryWhen(it -> it.delayElements(Duration.ofMillis(200)))
        .cache(Duration.ofMinutes(5))
        .timeout(Duration.ofSeconds(10));

but it compiles perfectly fine with但它编译得很好

@Getter(lazy = true)
Mono<Map<String, Translations.Translation>> translations = Mono.just(new byte[]{})
        .map(byteArray -> {
            try {
                return objectMapper.readValue(byteArray, Translations.class);
            } catch (IOException e) {
                throw new UncheckedIOException("Failed to get translation for " + translationGroup, e);
            }
        })
        .map(Translations::getTranslations)
        .retryWhen(it -> it.delayElements(Duration.ofMillis(200)))
        .cache(Duration.ofMinutes(5))
        .timeout(Duration.ofSeconds(10));

How to know what's wrong and how its possible to workaround?如何知道出了什么问题以及如何解决?

I suggest to move the initialization code to a separate method.我建议将初始化代码移到单独的方法中。

@Getter(lazy=true)
SomeType t = <complicatedInitializationCode>;

becomes变成

@Getter(lazy=true)
SomeType t = initializeT();

private SomeType initializeT() {
    return <complicatedInitializationCode>;
}

Disclosure: I am a lombok developer.披露:我是龙目岛的开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM