我可以实例化一个在Kotlin中使用递归泛型的具体Java类,如果是,那么如何?
细节
我试图实例化一个使用递归泛型的Java类,类似于下面的例子.我找到了一个解决方法,将Java类包装在一个新类中,但感觉就像我正在回避一个我应该能够直接处理的问题.
具有递归泛型的Java类
public class MyLegacyClass<T extends MyLegacyClass<T>> {
// implementation ...
}
Run Code Online (Sandbox Code Playgroud)
它是如何在Java中实例化的
// In Java we just ignore the generic type...
MyLegacyClass myLegacyClass = new MyLegacyClass();
Run Code Online (Sandbox Code Playgroud)
尝试在Kotlin中实例化失败
class myClass {
// Error: One type argument expected for class...
val x: MyLegacyClass = MyLegacyClass()
// Still 'Error: One type argument expected for class..' You start to see the problem here.
val y: MyLegacyClass<MyLegacyClass<MyLegacyClass<MyLegacyClass>>> = MyLegacyClass()
}
Run Code Online (Sandbox Code Playgroud)
Kotlin解决方法
class MyLegacyClassWrapper : MyLegacyClass<MyLegacyClassWrapper>()
class myClass {
val x: MyLegacyClass<MyLegacyClassWrapper> = …Run Code Online (Sandbox Code Playgroud) 我想验证集合是否包含至少一个非null元素.我试过is(not(empty())),但是这通过了下面的测试.
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.not;
public class SandBoxTest {
@Test
public void shouldTestThis() {
Collection<Integer> collection = new ArrayList<Integer>();
collection.add(null);
assertThat(collection, is(not(empty())));
}
}
Run Code Online (Sandbox Code Playgroud)
有一种优雅/简单的方法吗?
不起作用的事情
@Test
public void should(){
Collection<String> collection = new ArrayList();
collection.add("gfas");
collection.add("asda");
assertThat(collection, contains(notNullValue()));
}
java.lang.AssertionError:
Expected: iterable containing [not null]
but: Not matched: "asda"
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
Run Code Online (Sandbox Code Playgroud) 我有几个应该使用HSQLDB的单元测试,但我知道其中一些实际上正在使用物理数据库.我想在测试中添加一个检查,以确保正在使用的DataSource用于HSQLDB,而不是实时DB.
从hibernate session对象(org.hibernate.classic.Session),我如何检查DataSource
更新:
我也可以访问会话工厂(org.hibernate.impl.SessionFactory).
详细信息: Hibernate 3.2
我有一个在Jenkins中运行的Gradle构建,并使用Jenkins的Artifactory插件部署到Artifactory.我正在将tar.gz文件上传到Artifactory.该插件tar.gz正确上传,但它也上传了一个jar.
如何排除它jar以使其未部署到Artifactory?
build.gradle (artifacts部分)
artifacts {
archives tar
}
Run Code Online (Sandbox Code Playgroud) 我注意到当我将鼠标悬停在局部变量上时,当我的调试器在lambda内停止时,Cannot find local variable 'variable_name'即使它在lambda中可见并且已经使用,它也会报告.
示例代码
public class Main {
public static void main(String[] args) {
String a = "hello_world";
m1(a);
}
private static void m1(String a) {
AccessController.doPrivileged((PrivilegedAction<String>) () -> {
System.out.println("blala " + a);
return "abc";
});
}
}
Run Code Online (Sandbox Code Playgroud)
尝试使用System.out.println("blala " + a);和之后的断点,return "abc"它总是报告相同的错误.

我使用的AccessController.doPrivileged是因为它是我在原始代码中使用的,当然我使用的是Java 8.
它在Watchers和中说同样的事情Evaluate Expression.
我尝试使用"匿名类"版本,调试器a正确看到了值

private static void m1(String a) {
AccessController.doPrivileged(new PrivilegedAction<String>() {
@Override
public String run() {
System.out.println("blala …Run Code Online (Sandbox Code Playgroud) 我有一个方法返回从自定义spliterator生成的流; 分裂器不是安全的.由于分裂器不是踏板安全的,并且它保持状态,我想防止它并行运行.有没有办法阻止返回的流并行运行?
我无法找到任何执行此操作的文档或示例.我确实sequential()在BaseStream类上找到了一个方法,但这似乎并没有阻止用户调用parallel()以获得并行流.
如何覆盖由“allOf”关键字继承的 json 模式中定义的验证规则?
例子:
{
"$schema": "http://json-schema.org/draft-06/schema",
"title": "My JSON Schema",
"description": "",
"definitions": {
"a": {
"type": "object",
"properties": {
"b": {
"type": "object",
"properties": {
"c": {
"type": "string",
"minLength": 1,
"maxLength": 100
}
},
"required": [
"c"
]
}
},
"required": [
"b"
]
}
},
"properties": {
"main": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/a"
}
]
},
"sub": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/a"
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
json 模式定义了两个对象:
我有一个现有的scala项目,SBT其中有几个模块。\n我想开始添加新模块kotlin- 我不需要添加到kotlin现有模块的能力(但如果可能的话会很好)\n我可以创建新的专用模块新kotlin代码的模块,如果这是必要的,只要现有scala代码可以调用新添加的kotlin模块(反之亦然,有它就好,但如果不可能的话,没有“kotlin 调用 scala”也可以生存) )
这件事可行、可行吗?如果可以的话,这将如何完成?
\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 build.sbt\n............\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Module1ScalaWithJava (EXISTING)\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 resources\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 scala\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Module2ScalaOnly (EXISTING)\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 scala\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 scala\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 NewModuleKotlinOnly (I WANT THIS)\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ???KOTLIN????\nRun Code Online (Sandbox Code Playgroud)\n 我已经在IntelliJ 13中成功创建了一个Android项目,我想设置Android测试框架.我使用新项目向导使用Gradle创建android项目.当我添加新模块时,我只有"Gradle:Android Module"和"Gradle:Java Library"的选项,缺少"测试模块"选项.
如何生成Android测试模块?我已经阅读http://www.jetbrains.com/idea/webhelp/testing-android-applications.html但我找不到任何"测试"选项.
如果无法自动生成Android测试模块,那么如何使用Gradle Android项目配置和使用Android测试框架?非常感谢您对示例或文档的链接.
详细信息: IntelliJ 13.1.3
在 Scala 中,有没有办法指定函数应该声明默认参数值?
例如,在下面的代码中,有没有办法在签名中指定indirectHelloName所提供的函数必须为第二个参数提供默认值?
def helloName(name: String, greating: String = "hello"): Unit = {
println(s"$greating $name")
}
def indirectHelloName(name: String, function: (String,String) => Unit): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
function(name) //This would use the default value for the second argument.
}
}
Run Code Online (Sandbox Code Playgroud) java ×5
gradle ×3
junit ×2
kotlin ×2
scala ×2
android ×1
artifactory ×1
assertions ×1
generics ×1
hamcrest ×1
hibernate ×1
hsqldb ×1
intellij-14 ×1
interop ×1
java-stream ×1
jenkins ×1
jsonschema ×1
sbt ×1
spliterator ×1
unit-testing ×1