使用 lombok.Getter(lazy = true) 和 java 10 时出现不兼容类型错误
Incompatible types error using lombok.Getter(lazy = true) with java 10
我正在尝试使用 reactor
、reactor.ipc.netty.http.client.HttpClient
进行一些缓存,并使用 lombok 的 @Getter(lazy = true)
将其初始化为惰性 getter 字段。
在 Java 8 下一切正常,但无法在 error: incompatible types: Duration cannot be converted to String
和 Java 10 上编译此代码段
@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));
但它与
编译得很好
@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));
如何知道哪里出了问题以及如何解决?
我建议将初始化代码移到一个单独的方法中。
@Getter(lazy=true)
SomeType t = <complicatedInitializationCode>;
变成
@Getter(lazy=true)
SomeType t = initializeT();
private SomeType initializeT() {
return <complicatedInitializationCode>;
}
披露:我是 lombok 开发人员。
我正在尝试使用 reactor
、reactor.ipc.netty.http.client.HttpClient
进行一些缓存,并使用 lombok 的 @Getter(lazy = true)
将其初始化为惰性 getter 字段。
在 Java 8 下一切正常,但无法在 error: incompatible types: Duration cannot be converted to String
和 Java 10 上编译此代码段
@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));
但它与
编译得很好@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));
如何知道哪里出了问题以及如何解决?
我建议将初始化代码移到一个单独的方法中。
@Getter(lazy=true)
SomeType t = <complicatedInitializationCode>;
变成
@Getter(lazy=true)
SomeType t = initializeT();
private SomeType initializeT() {
return <complicatedInitializationCode>;
}
披露:我是 lombok 开发人员。