标签: java-10

Java 10 中的 java.io.Reader TransferTo(java.io.Writer) 方法是什么?InputStream有类似的方法吗?

我阅读了 Java 10 文档java.io.Reader.transferTo(...),它说:

从此读取器读取所有字符,并按照读取顺序将字符写入给定写入器

transferTo中的 方法Reader将非常有用,因为目前将数据从读取器复制到写入器非常冗长。由于我们在现实生活中经常使用InputStreamOutputStream使用它们,是否有类似的方法?

java inputstream reader java-10

5
推荐指数
1
解决办法
1354
查看次数

Java 10 上具有 JPA 静态元模型的 JHipster5 项目

我一直在尝试升级我的 JHipster 5 应用程序以使用 Java 10,但我无法让它使用 Maven 编译和处理 JPA 静态元模型。

\n\n

显然maven-compiler-plugin不是hibernate-jpamodelgen为了生成 JPA 静态元模型而触发。

\n\n

为了升级项目,我有:

\n\n
    \n
  • 安装Oracle\xc2\xb4s JDK 10.0.1
  • \n
  • 将我的 pom.xml 切换为<java.version>10</java.version>
  • \n
  • 升级了 maven-compiler-plugin 以添加java.xml.bind模块(因为从 Java 10 开始默认不包含该模块),如下所示:

    \n\n
        <plugin>\n        <groupId>org.apache.maven.plugins</groupId>\n        <artifactId>maven-compiler-plugin</artifactId>\n        <version>${maven-compiler-plugin.version}</version>\n        <configuration>\n            <!-- fork is needed so compiler args can be used -->\n            <fork>true</fork>\n            <compilerArgs>\n                <arg>-J--add-modules</arg>\n                <arg>-Jjava.xml.bind</arg>\n            </compilerArgs>\n            <annotationProcessorPaths>\n                <path>\n                    <groupId>org.mapstruct</groupId>\n                    <artifactId>mapstruct-processor</artifactId>\n                    <version>${mapstruct.version}</version>\n                </path>\n                <!-- For JPA static metamodel generation -->\n                <path>\n                    <groupId>org.hibernate</groupId>\n                    <artifactId>hibernate-jpamodelgen</artifactId>\n                    <version>${hibernate.version}</version>\n                </path>\n\n            </annotationProcessorPaths>\n …
    Run Code Online (Sandbox Code Playgroud)

jpa metamodel maven-compiler-plugin jhipster java-10

5
推荐指数
1
解决办法
1628
查看次数

Java 10 ifPresentOrElse 返回布尔值

我对“如何正确执行此操作”有点困惑:

 // return true: if present and number of lines != 0
  boolean isValid(Optional<File> optFile) {
    return optFile.ifPresentOrElse(f -> return !isZeroLine(f), return false);
 }

 private boolean isZeroLine(File f)  {
    return MyFileUtils.getNbLinesByFile(f) == 0;
 }
Run Code Online (Sandbox Code Playgroud)

我知道语法不正确并且无法编译,但这只是为了让您了解这个想法。

我怎样才能把它变成“干净的代码”?即避免这样做:

if (optFile.isPresent()) {//} else {//}
Run Code Online (Sandbox Code Playgroud)

java lambda java-10 option-type

5
推荐指数
1
解决办法
6350
查看次数

运行 Jenkins 构建会抛出:java.lang.reflect.InaccessibleObjectException

嘿,我正在尝试通过 Jenkins 构建运行 Maven Java 项目,它可以通过命令行完美运行,但是当我通过 Jenkins 运行它时。我收到此错误:

Unable to make field private final java.util.Comparator java.util.TreeMap.comparator accessible: module java.base does not "opens java.util" to unnamed module @425ea9cb
Run Code Online (Sandbox Code Playgroud)

这是完整的堆栈跟踪

Building remotely on Windows10 in workspace c:\jenkins\workspace\SELENIUM Single-Browser Automated Test
hudson.remoting.ProxyException: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Comparator java.util.TreeMap.comparator accessible: module java.base does not "opens java.util" to unnamed module @425ea9cb
    at java.lang.reflect.AccessibleObject.checkCanSetAccessible(Unknown Source)
    at java.lang.reflect.AccessibleObject.checkCanSetAccessible(Unknown Source)
    at java.lang.reflect.Field.checkCanSetAccessible(Unknown Source)
    at java.lang.reflect.Field.setAccessible(Unknown Source)
    at com.thoughtworks.xstream.core.util.Fields.locate(Fields.java:39)
    at com.thoughtworks.xstream.converters.collections.TreeMapConverter.<clinit>(TreeMapConverter.java:50)
Also:   hudson.remoting.Channel$CallSiteStackTrace: Remote call to …
Run Code Online (Sandbox Code Playgroud)

java maven jenkins java-10

5
推荐指数
1
解决办法
2万
查看次数

如何在 thenReturn 函数中返回传递给 Mockito 模拟方法的参数?

这就是我想要实现的目标。

MyClass doSomething =  mock(MyClass.class);
when(doSomething.perform(firstParameter, any(Integer.class), any(File.class)))
    .thenReturn(firstParameter);
Run Code Online (Sandbox Code Playgroud)

基本上我希望模拟类的方法始终返回传递到该方法中的第一个参数。

我尝试过使用ArgumentCaptor,就像这样

ArgumentCaptor<File> inFileCaptor = ArgumentCaptor.forClass(File.class);
MyClass doSomething = mock(MyClass.class);
when(doSomething.perform(inFileCaptor.capture(), any(Integer.class), any(File.class)))
    .thenReturn(firstParameter);
Run Code Online (Sandbox Code Playgroud)

mockito刚刚失败并出现此错误消息:

No argument value was captured!
You might have forgotten to use argument.capture() in verify()...
...or you used capture() in stubbing but stubbed method was not called.
Be aware that it is recommended to use capture() only with verify()

Examples of correct argument capturing:
    ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
    verify(mock).doSomething(argument.capture());
    assertEquals("John", argument.getValue().getName());
Run Code Online (Sandbox Code Playgroud)

我认为该ArgumentCaptor …

java mockito java-10

5
推荐指数
2
解决办法
5187
查看次数

在 Intellij Idea 中找不到符号“var”

每当我尝试从 Intellij Idea 运行我的应用程序时,我都会收到以下错误..

如果我通过 Maven 构建应用程序,它工作正常。

看看我的模块设置,JDK 是 v10,项目和模块的语言也是 v10。

知道为什么会发生这种情况吗?

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

intellij-idea java-10

5
推荐指数
1
解决办法
4621
查看次数

Java 保留类型名称“var”最佳实践?

不幸的是,Effective Java 中没有任何文章讨论保留类型名称“var”(因为它是在 java 10 中引入的)。

有一种说法是,尽可能使用接口作为类型(条款 64:通过接口引用对象)。但是使用时var

var list = new ArrayList<String>(); // infers ArrayList<String>

list属于类型ArrayList<String>

《Effective Java》指出,通过变量的实现来引用变量是一种不好的做法,因为它会使代码依赖于它。这只是我在代码中开始使用 var 时发现的一个烦恼。

问题:

使用保留类型名称“var”时是否有任何最佳实践?我什么时候应该使用它?我什么时候应该避免它?

java var effective-java java-10

5
推荐指数
0
解决办法
1303
查看次数

为什么不允许使用'var'的复合定义?

好吧,我真的认为这会起作用(在方法内):

var x, y = 1;

var x = 1, y = 2;
Run Code Online (Sandbox Code Playgroud)

但它没有,它不会编译 - "化合物定义中不允许使用var".

我想这是一个平常的权衡.这不是一个非常常用的功能,因此没有实现,但我们可以肯定,可能会在将来的版本中...

java java-10

4
推荐指数
1
解决办法
249
查看次数

CompletableFuture立即失败

我想创建一个CompletableFuture已经完成的异常.

Scala通过工厂方法提供我正在寻找的东西:

Future.failed(new RuntimeException("No Future!"))
Run Code Online (Sandbox Code Playgroud)

在Java 10或更高版本中是否有类似的东西?

java completable-future java-10

4
推荐指数
2
解决办法
2434
查看次数

Java 8 currying函数,无法确定int []返回类型

假设我们有一个像这样的lambda函数:

Function<ArrayList<Integer>, int[]> func1 = a->new int[2];
Run Code Online (Sandbox Code Playgroud)

它的作用并不重要.重要的是:输入是一个ArrayList<Integer>,输出是一个int[].

使用一些基本的测试编译并运行没有问题:

int[] func1Result1 = func1.apply(new ArrayList<Integer>()); // Non-currying works with <Integer>
System.out.println(func1Result1);
System.out.println(func1Result1.getClass());
System.out.println(Arrays.toString(func1Result1));
System.out.println();

int[] func1Result2 = func1.apply(new ArrayList<>());        // Non-currying works with <>
System.out.println(func1Result2);
System.out.println(func1Result2.getClass());
System.out.println(Arrays.toString(func1Result2));
System.out.println();

int[] func1Result3 = func1.apply(new ArrayList());          // Non-currying works without <>
System.out.println(func1Result3);
System.out.println(func1Result3.getClass());
System.out.println(Arrays.toString(func1Result3));
System.out.println();
Run Code Online (Sandbox Code Playgroud)

在线尝试.


现在让我们假设我们有一个像这样的currying lambda函数:

Function<Object, Function<Object, int[]>> func2 = a->b->new int[2];
Run Code Online (Sandbox Code Playgroud)

同样,它的作用并不重要.这次currying函数有两个Object参数,仍然输出一个int[].

使用相同的基本测试再次编译和运行没有问题:

int[] func2Result1 = func2.apply(new ArrayList<Integer>()).apply(null); // Currying works with …
Run Code Online (Sandbox Code Playgroud)

lambda types currying java-8 java-10

4
推荐指数
1
解决办法
184
查看次数