Tag: 属性

从`.properties`文件中检索值 lateinit属性尚未初始化

我试图创建一个弹簧启动应用程序,我的班级将从文件src/main/resources/application.properties读取。 但由于某种原因,我不能让我的Kotlin使用这些值(返回一个lateinit property url has not been initialized 。 src / main / resources / application.properties (注意,不是在任何地方显式调用的?) spring.datasource.url=someUrl spring.datasource.username=root spring.datasource.password=root spring.datasource.driverClassName=org.postgresql.Driver 科特林 @Component open class BaseDAO() { @Autowired lateinit var datasource: DataSource; } 新的错误 kotlin.UninitializedPropertyAccessException: lateinit property datasource has not been initialized at quintor.rest.persistence.BaseDAO.getDatasource(BaseDAO.kt:18) ~[classes/:na] at quintor.rest.persistence.EventDAO.getMultipleEvents(EventDAO.kt:45) ~[classes/:na] at quintor.rest.persistence.EventDAO.getComingOpenEvents(EventDAO.kt:98) ~[classes/:na] at quintor.rest.persistence.EventService.getComingEvents(EventService.kt:23) ~[classes/:na] at […]

Kotlin:如何访问构造函数中的属性

考虑以下课程: class Test() { init { log(foo) } val foo = "Bar" } 当我instatiate测试它会产生一个NullPointerException,因为foo似乎没有被初始化。 这种行为实际上是打算? 有没有解决方法?

Kotlin Android扩展视图背后的编辑魔法是什么?

将kotlinx.android.synthetic.main.<layout-name>.view.*格式的输入添加到Kotlin源代码时,它会改变Android Studio编辑器的行为。 具体来说,它现在认为任何类型的属性View有一个属性对应的布局文件中的每个视图分配一个ID。 我假设我正在做一个不是 Activity或Fragment 。 例如,假设我在源文件中声明了一个ViewHolder ,并向其中添加了一个名为x的View属性。 进一步说,我有一个名为item_test的布局,其中有三个视图声明已经分配了id, a , b和c 。 当我添加一个布局名称 item_test上面的表单的综合导入突然x有三个新的属性, a , b和c 。 或者,更正确地说,编辑器使得它看起来好像x具有这些属性。 经过一番研究,我得出以下结论: 盲目地添加布局视图ID作为属性。 任何由这样一个综合导入暗示的视图id被添加到View类型的任何属性(或View的子类)中。 这包括由类继承的这种类型的属性。 由于属性是盲目添加的,因此开发者有义务确保在合成属性被访问之前分配对应于合成导入的运行时视图,否则将引发异常。 如果在一个文件中指定了两个或多个这样的导入,那么他们的view ID的联合会被盲目地添加到View类型的所有类属性中。 允许多次导入的目的是解释一个布局文件包含另一个布局文件的情况。 这些结论是正确的吗? 围绕这个特性的实现还有其他有趣的细节吗? 我正在使用kotlin-gradle-plugin的版本1.1.2-5。

Kotlin:一个默认的ish setter,返回“this”

我知道这不是“严格的设计模式”等等等等,但… 在Kotlin中,有没有办法创建一个返回“ this ”的“default-ish”setter,就像 var foo:Bar = Something() set(f:Bar) { foo = f return this // Alas, that would not compile because Setter returns Unit } 当setter返回this时很方便,因为不需要声明一个Builder就可以创建一个Builder模式。 这只是短暂的做法: BlahBlah().setFoo(x).setFoo2(y)… 比 BlahBlah.Builder().setFoo(x)…. 要么 var b = BlahBlah() b.setFoo(x) b.setFoo2(y) … 管他呢 而且,如果一个二传手返回Unit ,为什么不this呢?

Kotlin懒惰属性和值重置:一个可重置的懒惰委托

所以我使用android的kotlin ,当充气的意见,我倾向于做到以下几点: private val recyclerView by lazy { find<RecyclerView>(R.id.recyclerView) } 这种方法将起作用。 但是,有一种情况会对应用程序造成影响。 如果这是一个片段,并且片段进入了后台, onCreateView将被再次调用,并且片段的视图层次将被重新创建。 这意味着懒惰启动的recyclerView将指向不再存在的旧视图。 解决方案是这样的: private lateinit var recyclerView: RecyclerView 并初始化onCreateView内的所有属性。 我的问题是,是否有任何方法来重置懒惰属性,以便他们可以再次初始化? 我喜欢初始化都是在一个类的顶部完成,有助于保持代码的组织。 具体的问题是在这个问题中发现: 回收后kotlin android碎片空回收站视图

kotlin别名属性代表抛出异常

我想将别名属性添加到kotlin中的一些类,这些类只是将自己委托给一些已有的属性 那么我发明了一个名为别名的方法来做到这一点,不幸的是会导致一个异常 val <T> Array<T>.length by alias(Array<T>::size) val <T> Collection<T>.length by alias(Collection<T>::size) fun <R, T> alias(alias: KProperty1<R, T>) = object : ReadOnlyProperty<R, T> { override operator fun getValue(thisRef: R, property: KProperty<*>): T { return alias.get(thisRef) } } 在异常堆栈之后 java.lang.NoSuchMethodError: [Ljava.lang.Object;.getSize()I at kt.ruby.ArrayKt$length$2.get(Array.kt:34) at kt.ruby.ArrayKt$alias$1.getValue(Array.kt:40) at kt.ruby.ArrayKt.getLength(Array.kt)

Kotlin spring-boot @ConfigurationProperties

我正在尝试创建以下bean AmazonDynamoDBAsyncClientProvider 。 我有application.properties定义endpoint和tablePrefix ,我试图注入使用@ConfigurationProperties 以下是相同的代码片段。 当我运行我的春季启动应用程序不起作用。 我已经尝试使用一个普通的java类来做相同的ConfigurationProperties类,它不会设置这些属性,但是当涉及到AmazonDynamoDBAsyncClientProvider ,属性是空的。 我在这里错过了什么? @Component open class AmazonDynamoDBAsyncClientProvider @Autowired constructor(val dynamoDBConfiguration: DynamoDBConfig){ @Bean open fun getAmazonDBAsync() = AmazonDynamoDBAsyncClientBuilder.standard() .withEndpointConfiguration( AwsClientBuilder.EndpointConfiguration(dynamoDBConfiguration.endpoint, dynamoDBConfiguration.prefix)) .build() } 这里是我试图用配置自动装配的kotlin bean @Component @ConfigurationProperties(value = "dynamo") open class DynamoDBConfig(var endpoint: String="", var prefix: String="") 最后继承了常规的java bean,它使用ConfigurationProperties填充,但是当它获得Autowired时,我看到这些属性为空/ null @Component @ConfigurationProperties("dynamo") public class DynamoDBConfiguration { private String endpoint; […]

访问没有实例的Kotlin委托类型

我已经阅读Kotlin中访问属性委托,这是关于从实例访问委托。 可以从Kotlin 1.1开始使用KProperty::getDelegate ,但是这将返回委托的实例,因此需要先创建一个类的实例。 现在我想获得委托的类型,而没有类的实例。 考虑一个具有自定义委托类型CustomDelegate的库,它需要获取委托给CustomDelegate实例的类的所有属性: class Example { var nonDelegatedProperty = "I don't care about this property" var delegatedProperty1 by lazy { "I don't care about this too" } var delegatedProperty2 by CustomDelegate("I care about this one") } 我怎么能,因为我有KClass<Example> ,但不是一个例子的Example ,获取委托给CustomDelegate所有属性?

将静态变量从Java转换为Kotlin

我试图将下面的代码转换为Kotlin,并仍然有一个由Java使用的类(Foo)。 什么是做这种转换的正确方法? 原始Java: public class Foo { public static final String C_ID = "ID"; public static final String C_NAME = "NAME"; public static final String[] VALUES = {"X", "Y", "Z"}; public static String[] getAll() { return new String[] {C_ID, C_NAME}; } } public class Bar { public void doStuff() { String var1 = Foo.C_ID; String[] […]

访问Kotlin的财产代表

Kotlin已经委派了属性,这是一个非常好的功能。 但有时get()和set()方法是不够的。 假设我想要Closeable地创建一个Closeable对象并稍后关闭它。 下面是一个如何实现这样的委托属性的例子: fun <T : Closeable> closeableLazy(initializer: () -> T) = CloseableLazyVal(initializer) class CloseableLazyVal<T : Closeable>( private val initializer: () -> T ) : ReadOnlyProperty<Any?, T> { private var value: T? = null override fun get(thisRef: Any?, desc: PropertyMetadata): T { if (value == null) { value = initializer() } return value } […]