我找不到任何列出支持 JDK 14 EA 构建的插件的资源。是否有更新版本的 Eclipse Java IDE 支持 JDK 14 或至少有一个插件来支持它从市场上?
我正在使用随附的 beta jpackage 工具(JEP 343)JDK14早期访问版本。
我在 Windows 中运行了以下 .bat 脚本
"%JAVA_HOME%/bin/jpackage" ^
--name NAME ^
--dest store/ ^
--input store/package/ ^
--main-jar MAIN_JAR.jar ^
--main-class library.MAIN_CLASS^
Run Code Online (Sandbox Code Playgroud)
我在运行脚本时收到以下错误。
警告:使用孵化器模块:jdk.incubator.jpackage
找不到 WiX 工具(light.exe、candle.exe) 从https://wixtoolset.org下载 WiX 3.0 或更高版本并将其添加到 PATH。错误:无效或不受支持的类型:[null]
我已经下载了 WiX311.exe 并尝试将它放在与 jpackage.exe 相同的文件夹中以及与 MAIN_JAR.jar 相同的文件夹中,但都没有解决这个问题。
所以我的问题是我需要将它添加到的“路径”是什么,以便将其打包?
我正在使用新的 Java 14 和 Spring Boot。对于数据持有者,我使用了新的很酷的记录,而不是常规的 Java 类。
public record City(Long id, String name, Integer population) {}
Run Code Online (Sandbox Code Playgroud)
稍后在我的服务类中,我使用 SpringBeanPropertyRowMapper来获取数据。
@Override
public City findById(Long id) {
String sql = "SELECT * FROM cities WHERE id = ?";
return jtm.queryForObject(sql, new Object[]{id},
new BeanPropertyRowMapper<>(City.class));
}
Run Code Online (Sandbox Code Playgroud)
我最终出现以下错误:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zetcode.model.City]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.zetcode.model.City.<init>()
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:145) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
Run Code Online (Sandbox Code Playgroud)
如何为记录添加默认构造函数或有其他方法可以解决此问题?
例如,如何将可选列表对象从一种类型转换为另一种类型
Optional<List<ProductMultipleOptionViewModel>> productOptionType1 // One type
Optional<List<ProductMultipleOption>> productOptionType2 // Other type
Run Code Online (Sandbox Code Playgroud)
ProductMultipleOptionViewModel
类型 1
@Introspected
public record ProductMultipleOptionViewModel(
ProductOptionViewModel productOption,
String optionName) {
}
Run Code Online (Sandbox Code Playgroud)
类型 2
@Introspected
public record ProductMultipleOption(
ProductOptionViewModel productOption,
String optionName) {
}
Run Code Online (Sandbox Code Playgroud)
我想从 转换Optional<List<ProductMultipleOption>>为其他Optional<List<ProductMultipleOptionViewModel>>. 我试过下面的代码
Optional<List<ProductMultipleOptionViewModel>> conveertItem = Optional.ofNullable(product.getProductMultipleOption())
.orElseGet(null)
.stream()
.map(option -> {
return new ProductMultipleOptionViewModel(
ProductOptionViewModel.valueOf(//Access the option value//), //access the option value//
);
})
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我无法访问 map 方法中的选项值
如果product.getProductMultipleOption()为 null,则返回 null 或空列表。
instanceof 甚至不适用于 List。例如
package org.practice;
import java.util.List;
public class InstanceOfDemo {
public static void main(String[] args) {
Employee emp1 = new Employee(1, "Ram", 10000);
Employee emp2 = new Employee(2, "Shyam", 20000);
Employee emp3 = new Employee(3, "Radha", 30000);
Object obj = List.of(emp1, emp2, emp3);
if (obj instanceof List empList) {
for (Employee emp : empList) { //Type mismatch: cannot convert from element type Object to Employee
System.out.println(emp.getId());
System.out.println(emp.getName());
System.out.println(emp.getSalary());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,instanceof 得到了 true,甚至声明了 empList 被创建了,但这并没有给开发人员带来任何好处,因为在 for 循环中,如果没有类型转换 …
我收到这段代码的“javax.net.ssl.SSLException:Connection reset”
ReadableByteChannel rbc = Channels.newChannel(url.getInputStream());
Run Code Online (Sandbox Code Playgroud)
但仅当在使用来自 Open JDK 14 的 jlink 构建的 Java 14 JRE 下运行时。如果我指向完整的 JDK,代码执行得很好。我怀疑 JRE 中缺少某些东西,但它是什么,为什么 jlink 没有复制丢失的文件?我的基于jdeps的jlink命令如下:
"C:\Program Files\Java\jdk-14\bin\jlink.exe" --no-header-files --no-man-pages --compress=2 --strip-debug --add-modules java.base,java.datatransfer,java.desktop,java.logging,java.xml --output %AG14InstallDir%\jre_windows2
Run Code Online (Sandbox Code Playgroud)
我在 jlink 中缺少一个模块吗?有关如何跟踪此问题的任何想法或建议?
按照建议添加调试后,我得到了这个输出。我需要复制什么来修复它?
javax.net.ssl|WARNING|2E|Thread-13|2020-06-07 10:22:24.517 UTC|null:-1|Signature algorithm, ed25519, is not supported by the underlying providers
javax.net.ssl|WARNING|2E|Thread-13|2020-06-07 10:22:24.518 UTC|null:-1|Signature algorithm, ed448, is not supported by the underlying providers
javax.net.ssl|WARNING|2E|Thread-13|2020-06-07 10:22:24.519 UTC|null:-1|No AlgorithmParameters for x25519 (
"throwable" : {
java.security.NoSuchAlgorithmException: Algorithm x25519 not available
at java.base/javax.crypto.KeyAgreement.getInstance(Unknown Source)
at java.base/sun.security.ssl.NamedGroup.<init>(Unknown …Run Code Online (Sandbox Code Playgroud) 我正在重构我的代码。我想在我的 DTO 中使用 java 记录而不是 java 类。要将 DTO 转换为实体,我使用的是 ModelMapper(2.3.5 版)。当我尝试获取有关用户的信息(调用方法将实体转换为 DTO)时,出现此错误。
Failed to instantiate instance of destination xxx.UserDto. Ensure that xxx.UserDto has a non-private no-argument constructor.
这是我的代码。
public record UserDto(String firstName,
String lastName,
String email,
String imageUrl) {}
Run Code Online (Sandbox Code Playgroud)
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@Autowired
private ModelMapper modelMapper;
@GetMapping("/user/me")
@PreAuthorize("hasRole('USER')")
public UserDto getCurrentUser(@CurrentUser UserPrincipal userPrincipal) {
return convertToDto(userRepository.findById(userPrincipal.getId())
.orElseThrow(() -> new ResourceNotFoundException("User", "id", userPrincipal.getId())));
}
private UserDto convertToDto(User user) {
UserDto userDto = modelMapper.map(user, UserDto.class);
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 java 14 的新记录功能,目前处于预览阶段。
我知道在 Java 中,每个文件必须有一个公共类,但是新的记录语法非常好且简短,让一堆文件中的每个文件都包含一行代码似乎很浪费。
特别是我想尝试为这样的简单 AST 建模,我认为将所有内容放在一个文件中确实提高了可读性和理解性。
package com.company;
public interface Expression {
}
public record IntExp(int value) implements Expression {
}
public record AddExp(Expression left, Expression right) implements Expression {
}
public record SubtractExp(Expression left, Expression right) implements Expression {
}
// Etc..
Run Code Online (Sandbox Code Playgroud)
但不幸的是,这不会编译。
所以我的问题是:
有没有办法绕过这个限制,或者有什么方法可以将这样的代码全部保存在一个地方?
虽然我知道这似乎很明显,但我会解释我的困惑。我一直认为快速排序最坏情况的时间复杂度为 O(n^2)。从 Java 7 到 Java 13 的Arrays.sort(int[])文档表示: 该算法在许多数据集上提供了 O(n log(n)) 性能,这会导致其他快速排序性能降级为二次级,并且通常比传统(单枢轴)快速排序实现。
这里的关键字是“many”,所以我假设这里的 O(n log(n)) 指的是平均情况,并且仍然存在导致 O(n^2) 最坏情况的数据集。
但在 Java 14 及更高版本中, Arrays.sort(int[])的文档表示: This Algorithm Offers O(n log(n)) Performance on all data set。
那么,现在改进的快速排序实现的最坏情况是 O(n log(n)) 吗?有人请澄清一下。
public class Product{
private String id;
private String name;
private String description;
// Getters and setters..
}
Run Code Online (Sandbox Code Playgroud)
从上面的类中,我想在运行时获取描述的字段名称:
Query query = new Query();
if (productSearchCriteria.getDescription().isPresent()) {
query.addCriteria(Criteria.where("description").is(productSearchCriteria.getDescription().get()));
}
Run Code Online (Sandbox Code Playgroud)
目前,我已经对名称进行了硬编码,但它应该是动态的,因为如果有人重命名或更改属性名称,这将不起作用。
环顾四周,我发现了一个reflection-util库。
String numberProperty = PropertyUtils.getPropertyName(Product.class, Product::getDescription);
Run Code Online (Sandbox Code Playgroud)
这给了我一个例外:
Caused by: java.lang.ClassCastException: class domain.Product$ByteBuddy$QIcFk1aK cannot be cast to class domain.Product (domain.Product$ByteBuddy$QIcFk1aK is in unnamed module of loader net.bytebuddy.dynamic.loading.ByteArrayClassLoader @33943b71; domain.Product is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @5be4b0e6)
at de.cronn.reflection.util.PropertyUtils.findMethodByGetter(PropertyUtils.java:322) ~[reflection-util-2.6.0.jar:na]
at de.cronn.reflection.util.PropertyDescriptorCache.lambda$getMethod$4(PropertyDescriptorCache.java:178) ~[reflection-util-2.6.0.jar:na]
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) ~[na:na]
at de.cronn.reflection.util.PropertyDescriptorCache.getMethod(PropertyDescriptorCache.java:178) ~[reflection-util-2.6.0.jar:na]
at …Run Code Online (Sandbox Code Playgroud) java ×10
java-14 ×10
java-record ×3
algorithm ×1
eclipse ×1
java-module ×1
java-stream ×1
jlink ×1
modelmapper ×1
packaging ×1
quicksort ×1
spring ×1
spring-boot ×1