我有一个带有可变参数的函数。这个 vararg 参数需要作为列表传递给另一个函数。
如何将 vararg 参数转换为列表?listOf()给了我一个错误。
fun insertMissingEntities(vararg entities: Entity) {
val list = listOf(entities)
passMissingEntities(list) // Type mismatch.
// Required: List<Entity>
// Found: List<Array<out Entity>>
}
Run Code Online (Sandbox Code Playgroud) 我正在将SQ 7.3-alpha1与sonar-kotlin-plugin-1.0.1.965.jar一起使用。但是,我无法在Spring数据存储库的Kotlin代码内停用特殊警告,该方法需要在方法名称中添加“ _”。我尝试了//NOSONAR和@Suppress("kotlin:S100")和@SuppressWarnings("kotlin:S100")。任何提示表示赞赏。
I have some code that I need help with ... I'm trying to build a map using two maps as a source and at the same time using java lambdas
Map<String, List<String>> motorsMapping = new HashMap<>();
motorsMapping.put("CAR", Collections.singletonList("AUDI"));
motorsMapping.put("CAR_AND_BIKE", Arrays.asList("AUDI", "BMW"));
motorsMapping.put("BIKE", Collections.singletonList("BMW"));
Map<String, List<String>> models = new HashMap<>();
models.put("AUDI", Arrays.asList("A1", "Q2"));
models.put("BMW", Arrays.asList("X1", "X5"));
motorsMapping.keySet().forEach(key -> {
Map<String, List<String>> result = new HashMap<>();
result.put(key, new ArrayList<>());
motorsMapping.get(key).forEach(value -> models.getOrDefault(value, Collections.emptyList()).forEach(property -> result.get(key).add(property)));
});
Run Code Online (Sandbox Code Playgroud)
I can do that with the …
考虑以下最小的Kotlin示例:
fun <U> someWrapper(supplier: () -> U): () -> (U) {
return { supplier() }
}
fun foo(taskExecutor: TaskExecutor): Int {
val future = CompletableFuture.supplyAsync(someWrapper {
42
}, taskExecutor::execute)
return future.join()
}
@Test
public void shouldFoo() {
assertThat(foo(), is(42));
}
Run Code Online (Sandbox Code Playgroud)
我在Jacoco中有分支覆盖规则,该规则对上面的代码失败,说someWrapper呼叫行未覆盖2个分支中的1个。不幸的是,对于我来说,排除所有someWrapper被调用的类不是一个选择。
查看反编译的Java代码:
public final int foo(TaskExecutor taskExecutor) {
Object var10000 = WrappersKt.someWrapper((Function0)null.INSTANCE);
if (var10000 != null) {
Object var2 = var10000;
var10000 = new Foo$sam$java_util_function_Supplier$0((Function0)var2);
}
Supplier var3 = (Supplier)var10000;
Function1 var4 = (Function1)(new Function1(this.taskExecutor) { …Run Code Online (Sandbox Code Playgroud) 我一直面临一个奇怪的问题。基本上,当我正常运行 Mockito 测试时,即“作为 Junit 测试运行”时,它给了我以下错误。有人可以帮助我请问我的错误是什么?
java.lang.NoSuchMethodError: org.mockito.Mockito.framework()Lorg/mockito/MockitoFramework;
at org.powermock.api.mockito.mockmaker.MockMakerLoader.doLoad(MockMakerLoader.java:45)
at org.powermock.api.mockito.mockmaker.MockMakerLoader.load(MockMakerLoader.java:36)
at org.powermock.api.mockito.mockmaker.PowerMockMaker.<init>(PowerMockMaker.java:36)
... shortened stacktrace....
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Run Code Online (Sandbox Code Playgroud)
public class ListTest {
@Test
public void letsMockListSize() {
List<?> list= mock(List.class);
when(list.size()).thenReturn(2);
assertEquals(2, list.size());
}
}
Run Code Online (Sandbox Code Playgroud)
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud) 在这个问题中,描述了Eclipse中Kotlin + Java + Gradle的基本配置。它允许我创建Kotlin代码。Kotlin和Java性质正确存在。不幸的是,该代码无法运行。不作为Kotlin应用程序也不作为JUnit测试。
从Java代码调用我的Kotlin类(即由我创建)时,它会产生NoClassDefFoundError。当从Gradle或IntelliJ运行相同的测试/应用程序时,代码将正确运行。
到目前为止,我的调查发现,执行时找不到bin文件夹中的类文件。
使用buildship插件
下图显示该src树存在。所有Java编译类都在src树中。该kotlin_bin文件夹具有正确的文件夹,但没有文件。
使用gradlew eclipse并导入.project文件
这导致相同的行为。我更喜欢用飞船来管理gradle。
工具版本
我正在使用:
有关如何进行的任何提示?我希望能够在Eclipse中运行。我们将在项目中添加一个小的Kotlin零件,我不想强迫我的团队改用IntelliJ。
是否可以获取具有默认参数的函数的函数引用,该默认参数指定为无参数调用?
InputStream.buffered()是一种扩展方法,可将a InputStream转换BufferedInputStream为8192字节的缓冲区大小。
public inline fun InputStream.buffered(bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedInputStream =
if (this is BufferedInputStream) this else BufferedInputStream(this, bufferSize)
Run Code Online (Sandbox Code Playgroud)
我想有效地使用默认参数引用扩展方法,并将其传递给另一个函数。
fun mvce() {
val working: (InputStream) -> InputStream = { it.buffered() }
val doesNotCompile: (InputStream) -> BufferedInputStream = InputStream::buffered
val alsoDoesNotCompile: (InputStream) -> InputStream = InputStream::buffered
}
Run Code Online (Sandbox Code Playgroud)
doesNotCompile并alsoDoesNotCompile产生以下错误
类型不匹配:推断的类型为KFunction2,但应为(InputStream)-> BufferedInputStream
类型不匹配:推断的类型为KFunction2,但应为(InputStream)-> InputStream
我知道错误是因为InputStream.buffered()实际上(InputStream) -> BufferedInputStream不是,而是的快捷方式(InputStream, Int) -> BufferedInputStream,将缓冲区大小作为参数传递给BufferedInputStream构造函数。
动机主要是样式方面的原因,我宁愿使用已经存在的引用,也不愿在最后一刻创建引用。
val ideal: (InputStream) -> BufferedInputStream = InputStream::buffered// …Run Code Online (Sandbox Code Playgroud) 无法弄清楚如何读取特定建筑物列表中动物的平均体重。
我编写了另一种获取每种动物的动物名称的方法,所以我知道我的持久性正在起作用。
动物园:
public class Zoo {
private List<Animal> animals;
private List<Building> buildings;
public Zoo() {
this.animals = new PersistencyController().giveAnimals();
this.gebouwen = new PersistencyController().giveBuildings();
}
public List<Animal> giveAnimalsByKind(String kindName) {
return animals.stream().filter(animal -> animal.getKind().getName() == kindName).sorted(Comparator.comparing(Animal::getWeight)).collect(Collectors.toList());
}
public double giveAvgWeightForAnimalsInBuilding(String buildingName) {
return animals.stream().collect(Collectors.averagingDouble(Animal::getWeight));
}
}
Run Code Online (Sandbox Code Playgroud)
动物:
public class Animal implements Serializable {
private int nr;
private String name;
private double weight;
private Kind kind;
public Animal(int nr, String name, double weight, Kind kind) {
this.nr = nr;
this.name = …Run Code Online (Sandbox Code Playgroud) public abstract class CommonClass {
abstract void send(<what should i put here???>) {}
}
public class ClassA extends CommonClass {
void send(List<Comments> commentsList) {
// do stuff
}
}
public class ClassB extends CommonClass {
void send(List<Post> postList) {
// do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
我是OODP的新手,我正在尝试一种能够接收任何种类的List数据的方法,以便我可以抽象出一些东西。我怎样才能做到这一点?
如果序列化源(例如 json 对象)不包含该字段,我如何指示 Jackson 不要自动反序列化kotlin.Boolean参数false?
请参阅当前示例:
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
data class Test(
var param1: String,
var param2: Boolean
)
fun main() {
val mapper = jacksonObjectMapper()
// passes, param2 is set to false, why isnt exception thrown?
val test1 = mapper.readValue("{\"param1\": \"bah\"}", Test::class.java)
// throws com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
val test2 = mapper.readValue("{\"param2\": true}", Test::class.java)
}
Run Code Online (Sandbox Code Playgroud)
在我没有给布尔参数赋值的第一种情况下,如何让 jackson 抱怨异常?
我对此很感兴趣,因为如果客户端没有为布尔参数赋值,我希望我的 API 停止并抱怨。我不希望它在发生这种情况时简单地设置为 false。
谢谢!