Tag: Java

Kotlin反射问题

我有这些方法在Java库中声明: Engine.java: public <T extends EntitySystem> T getSystem(Class<T> systemType) Entity.java: public <T extends Component> T getComponent(Class<T> componentClass) 现在,我使用这些方法很多,我真的很喜欢到处使用MyComponent::class (即kotlin反射)而不是更详细的javaClass<MyComponent>() 。 我的EntitySystem和Component实现是用Kotlin编写的。 所以我想我会创建带KClasses扩展函数,但我不太清楚如何使它们工作。 东西沿线… public fun <C : Component> Entity.getComponent(type: KClass<out Component>): C { return getComponent(type.javaClass) } 但是这不起作用有几个原因:编译器说类型推断失败,因为javaClass返回Class<KClass<C>> 。 而我需要Class<C> 。 我也不知道如何使方法正确通用。 任何人都可以帮我创建这些方法吗?

如何使用Maven编译Java + Kotlin项目?

我试图编译有Kotlin类引用Java类的Maven项目。 这是我父母POM的一部分: … <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> … <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${compiler-plugin-version}</version> <configuration> <source>${java-version}</source> <target>${java-version}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.plugin.version}</version> <executions> <execution> <id>compile</id> <phase>process-sources</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>process-test-sources</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> <configuration> <scanForAnnotations>false</scanForAnnotations> </configuration> </plugin> 和POM的相关部分: <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> </dependency> … <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <configuration> <sourceDirs> <source>${project.basedir}/src/main/kotlin</source> […]

在Android中将Java与Kotlin结合

我在Android应用程序开发中从Java稍微转移到Kotlin,但是有些情况下我不想在Kotlin中编写代码,并希望用Java编写这些特定的案例: Kotlin删除了findViewByIds额外用法,这是省力的 我知道它现在是null-safety ,相反的Java总是触发与NullPointerException 还提供了Lambda表达式以及许多其他功能。 但是,仍然有一些我的代码不能写在Kotlin中,例如static成员non-primitive字段。 Kotlin注释实际上可以替代那些static members 。 但是喜欢Java的一些编码特性,不想离开它。 我的问题是可以将Java和Kotlin结合在一起吗?

Java与Kotlin接口声明

说我有这个Java和Kotlin接口: public interface JavaInterface { void onTest(); } interface KotlinInterface { fun onTest() } 为什么不能在没有构造函数的情况下创建Kotlin接口的实例? // this is okay val javaInterface: JavaInterface = JavaInterface { } // compile-time exception: interface does not have constructor val kotlinInterface1: KotlinInterface = KotlinInterface { } // this is okay val kotlinInterface2: KotlinInterface = object : KotlinInterface { override fun […]

Kotlin工具栏菜单图标问题

我将以下Java代码转换为Kotlin代码。 Android Studio不显示任何错误消息。 但结果是不同的。 我添加了两个模拟器结果的图片。 正如你所看到的Java工作正常,并显示在工具栏中的3点菜单图标。 但是Kotlin代码不起作用。 是否有人知道错误是什么? 希望得到一些帮助。 谢谢。 Java的: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_home, container, false); //… setHasOptionsMenu(true); return view; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { menuInflater.inflate(R.menu.menu_main, menu); } @Override public void onPrepareOptionsMenu(Menu menu) { MenuItem settings = menu.findItem(R.id.settings); MenuItem singOut […]

将java转换为kotlin

我正在用orbit软件包开发一个Kotlin应用程序,但是我有一些问题来定义演员Unresolved reference: T class HelloActor():AbstractActor<T>,Hello 应该是什么T参数? 在docs中: * @param <T> a class that represents the state of this actor.

在Kotlin的匿名类与lambdas

我正在尝试将我的闲暇时间项目从Java重新编写到Kotlin(了解它),并遇到了一些问题。 研究把我带到{ function() }但它没有帮助我 在Java中我有这个接口: public interface Shuffling<T> { List<T> shuffle(List<T> list, ShuffleCallback callback); interface ShuffleCallback { void onShuffle(int addedTo, int randomIndex); } } 我试图在Kotlin中添加测试对象到洗牌算法列表中: val algoList = ArrayList<Shuffling<Int>>() algoList.add(Shuffling { list, callback -> { Timber.d("Test!") ArrayList<Int>() // how to return this value? }}) 如何添加多行到lambda函数? 另外我还有一个麻烦的例子: Kotlin界面: interface Drawable { fun draw() } 和Kotlin实现: private […]

Kotlin弹簧安全配置

从1.0.0-beta-242升级到Kotlin 1.0.0-beta-3595后,以下代码不能编译: @Throws(Exception::class) override fun configure(http: HttpSecurity) { http.addFilterBefore(AuthenticationFilter(authenticationManager()), BasicAuthenticationFilter::class.java) http.csrf().disable() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and().authorizeRequests() .antMatchers("/authorization/**", "/public/**").permitAll() .antMatchers("/**").authenticated() } 返回的错误是: SecurityAssembly.kt: (48, 65): Unresolved reference: permitAll 编辑: 来自流行的Spring Security框架的permitAll方法的签名是: public ExpressionInterceptUrlRegistry permitAll() { return access(permitAll); } 我错过了什么或者这是一个错误?

混合Kotlin + Java与Maven,未解决的参考

我有一个Maven项目与Kotlin代码hello.kt调用Java代码JavaFoo.java调用Kotlin代码KotlinFoo.kt 。 hello.kt也直接调用KotlinFoo.kt 。 我试图用mvn clean install使用kotlinlang的Maven文档中描述的Maven设置来构建它。 如果hello.kt不调用JavaFoo (但是我把JavaFoo留在了项目中),那么这样做就好了。 文档中说,应该在Java编译器之前调用Kotlin编译器,这意味着所有的Kotlin代码都需要在任何Java代码之前编译,即使用这种设置,您可以从Java调用Kotlin,反之则不然。 但是,文档将这种设置描述为“混合代码应用程序”,而不是“从Java调用Kotlin”。 换句话说,这种失败似乎与文件似乎暗示的一致,但与他们直接说的不一致 – 或者我只是误解了一些东西。 我想从另一个语言中调用每种语言。 有没有一个Maven的配置,将这样做,请? (我查看了混合代码设置的各种StackExchange问​​题,而且这些解决方案都不适用于我。) 按要求添加代码:pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.kotlindemo</groupId> <artifactId>kotlin-demo</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>kotlin-demo</name> <properties> <kotlin.version>1.1.2-2</kotlin.version> <kotlin.compiler.incremental>true</kotlin.compiler.incremental> <main.class>com.example.kotlindemo.HelloKt</main.class> </properties> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <executions> <execution> <id>compile</id> <phase>process-sources</phase> <goals> <goal>compile</goal> </goals> <configuration> […]

为什么intelliJ IDEA依赖范围是“提供”而不是“编译”?

我希望IntelliJ IDEA将我的库作为“编译”范围而不是“提供”范围。 这是我的gradle文件的一部分: apply plugin: 'java' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { // Logging compile 'ch.qos.logback:logback-classic:1.2.1' compile 'com.getsentry.raven:raven-logback:7.8.2' // BigQuery compile 'com.google.api-client:google-api-client:1.20.0' compile 'com.google.apis:google-api-services-bigquery:v2-rev227-1.20.0' // Configuration management compile 'commons-configuration:commons-configuration:1.10' //Json compile 'org.json:json:20160810' //Kafka compile "org.apache.kafka:kafka-clients:0.10.1.1" testCompile group: 'junit', name: 'junit', version: '4.12' testCompile 'org.assertj:assertj-core:3.0.0' testCompile 'org.mockito:mockito-all:1.10.19' } task wrapper(type: Wrapper) { gradleVersion […]