在Java中,我们通常可以在while条件内执行赋值.然而Kotlin抱怨它.所以下面的代码不能编译:
val br = BufferedReader(InputStreamReader(
conn.inputStream))
var output: String
println("Output from Server .... \n")
while ((output = br.readLine()) != null) { // <--- error here: Assignments are not expressions, and only expressions are allowed in this context
println(output)
}
Run Code Online (Sandbox Code Playgroud)
根据这个其他线程,这似乎是最好的解决方案:
val reader = BufferedReader(reader)
var line: String? = null;
while ({ line = reader.readLine(); line }() != null) { // <--- The IDE asks me to replace this line for while(true), what the...?
System.out.println(line);
}
Run Code Online (Sandbox Code Playgroud)
但是吗?
使用弹簧注释自动装配非基元
@Autowired
lateinit var metaDataService: MetaDataService
Run Code Online (Sandbox Code Playgroud)
作品.
但这不起作用:
@Value("\${cacheTimeSeconds}")
lateinit var cacheTimeSeconds: Int
Run Code Online (Sandbox Code Playgroud)
有错误:
原始类型不允许使用lateinit修饰符.
如何将原始属性自动装配到kotlin类中?
假设我有一个typealias像这样的Kotlin函数类型的Kotlin 1.1
typealias Consumer<T> = (T) -> Unit
Run Code Online (Sandbox Code Playgroud)
我可以从Java访问它
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
Function1<? super T, Unit> action = ...
Run Code Online (Sandbox Code Playgroud)
是否有可能Function1以其Kotlin类型名称(即Consumer)从Java 访问Kotlin 接口?
Exceptions.kt:
@Suppress("NOTHING_TO_INLINE")
inline fun generateStyleNotCorrectException(key: String, value: String) =
AOPException(key + " = " + value)
Run Code Online (Sandbox Code Playgroud)
在kotlin:
fun inKotlin(key: String, value: String) {
throw generateStyleNotCorrectException(key, value) }
Run Code Online (Sandbox Code Playgroud)
它在kotlin中工作,并且函数是内联的.
但是当在Java代码中使用时,它只是无法内联,并且仍然是一个正常的静态方法调用(从反编译的内容中看到).
像这样的东西:
public static final void inJava(String key, String value) throws AOPException {
throw ExceptionsKt.generateStyleNotCorrectException(key, value);
// when decompiled, it has the same contents as before , not the inlined contents.
}
Run Code Online (Sandbox Code Playgroud) 我一直试图找出如何申请@IntRange(from = 1)我的 Kotlin 财产。经过几次失败的尝试,我终于在 Java 中创建了我想要的类,并将其转换为 Android Studio 中的 Kotlin。这是我的 Java 类:
import android.support.annotation.IntRange;
public class SimpleThing {
private int val;
@IntRange(from = 1)
public int getVal() {
return val;
}
public void setVal(@IntRange(from = 1) int val) {
this.val = val;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我从 Android Studio 得到的自动转换:
import android.support.annotation.IntRange
class SimpleThing {
@get:IntRange(from = 1)
var `val`: Int = 0
}
Run Code Online (Sandbox Code Playgroud)
在@IntRange似乎得到应用吸气剂,但不二传手。是否可以将此注释也应用于 setter,以便出现适当的 lint 警告。目前我刚刚覆盖了 set 方法来抛出一个 IllegalArgumentException 像这样:
@get:IntRange(from = 1)
var rowSize: …Run Code Online (Sandbox Code Playgroud) android kotlin android-lint android-annotations kotlin-interop
阅读有关SAM转换的Java互操作文档,我期待Kotlin函数
Collections.sortWith(comparator: kotlin.Comparator<in T> /* = java.util.Comparator<in T> */)
能够在不需要显式指定参数的情况下获取lambda函数是Comparator.但是,以下代码给出type inference failed:
val someNumbers = arrayListOf(1, 5, 2)
someNumbers.sortWith({ x, y -> 1 })
Run Code Online (Sandbox Code Playgroud)
然而:
val someNumbers = arrayListOf(1, 5, 2)
someNumbers.sortWith(Comparator { x, y -> 1 })
Run Code Online (Sandbox Code Playgroud)
编译并正确运行
当使用Kotlin与Firebase数据库一起工作时,List<String>如果我使用GenericTypeIndicator如下所示,我似乎无法检索类型的值:
snap.getValue(object : GenericTypeIndicator<List<String>>() {})
Run Code Online (Sandbox Code Playgroud)
它会从Firebase SDK中生成异常,如下所示:
com.google.firebase.database.DatabaseException: Generic wildcard types are not supported
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
但是,如果我从Java调用它,如下所示,它可以工作:
snap.getValue(new GenericTypeIndicator<List<String>>() {})
Run Code Online (Sandbox Code Playgroud)
我猜它与类型reifying有关,所以我这样做:
inline fun <reified T> genericType() = object: GenericTypeIndicator<T>() {}
val stringListIndicator = genericType<List<String>>()
snap.getValue(stringListIndicator)
Run Code Online (Sandbox Code Playgroud)
但同样的例外发生了.
为什么会这样?
编辑:我尝试使用jadx-0.6.1反编译Java和Kotlin版本.
Java来源:
public class Randommmm {
private static final GenericTypeIndicator<List<String>> ti = new GenericTypeIndicator<List<String>>() {
};
public static List<String> x(DataSnapshot snap) {
return snap.getValue(ti);
}
} …Run Code Online (Sandbox Code Playgroud) 在Java中,我有可能"实现"注释.
示例Java注释:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface JavaClassAnno {
String[] value();
}
Run Code Online (Sandbox Code Playgroud)
示例Java"实现":
class MyAnnotationLiteral
extends AnnotationLiteral<JavaClassAnno>
implements JavaClassAnno { // <--- works in Java
private String value;
public MyAnnotationLiteral(String value) {
this.value = value;
}
@Override
public String[] value() {
return new String[] { value };
}
}
Run Code Online (Sandbox Code Playgroud)
试图将其移植到Kotlin不起作用,因为它说注释是最终的,因此不能继承,即以下将不起作用:
class MyAnnotationLiteral(private val internalValue: String)
: AnnotationLiteral<JavaClassAnno>(),
JavaClassAnno { // <--- doesn't work in Kotlin (annotation can not be inherited)
override fun value(): Array<String> {
return arrayOf(internalValue)
}
}
Run Code Online (Sandbox Code Playgroud)
你如何"实施/扩展"Kotlin方式的注释?无法找到任何理由为什么Kotlin在这方面与Java有所不同.任何提示如何解决这个问题的提示或任何说明原因的方法都是受欢迎的.
以下问题包含此星座的用例: …
我对这个与为从 Maven 包导入的抽象类提供实现相关的 Kotlin 错误感到困惑。
我有一个用 Kotlin 编写的 Maven 库,并公开了一个名为 APIGatewayRequestHandler 的抽象类。在导入库的应用程序中,我提供了抽象类的实现:
class GetWelcomeMessageHandler : APIGatewayRequestHandler<WelcomeMessage>()
fun handleAPIGatewayRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, context: com.amazonaws.services.lambda.runtime.Context?): WelcomeMessage {
return WelcomeMessage()
}
}
Run Code Online (Sandbox Code Playgroud)
从库中反编译的抽象类如下所示:
public abstract class APIGatewayRequestHandler<T> public constructor() : com.amazonaws.services.lambda.runtime.RequestHandler<com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, T> {
public abstract fun handleAPIGatewayRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, context: com.amazonaws.services.lambda.runtime.Context?): T
public open fun handleRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent?, context: com.amazonaws.services.lambda.runtime.Context?): T {
/* compiled code */
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Class 'GetWelcomeMessageHandler' is not abstract and does not implement abstract base class member
public abstract fun handleAPIGatewayRequest(input: APIGatewayProxyRequestEvent, …Run Code Online (Sandbox Code Playgroud) 我遇到了kotlin equals函数来比较两个相同类型的列表。它对于带有数据类的纯Kotlin很好用。
我正在Kotlin项目中使用Java库,其中的回调方法以X秒的时间间隔返回对象列表。尝试针对每个调用将旧列表与新列表进行比较,但是即使项目相同且相等,equals也会返回false。
val mOldList: MutableList<MyObject>()? = null
override fun updatedList(list: MutableList<MyObject>){
// other code
if (mOldList.equals(list)) // false everytime
}
Run Code Online (Sandbox Code Playgroud)
这是因为Java的来自库的equals方法吗?
列表比较的替代建议将是有益的。
kotlin ×10
kotlin-interop ×10
java ×3
android ×1
android-lint ×1
firebase ×1
inline ×1
spring ×1