gradle没有适用的java方法(hashmap.getOrDefault)的签名

时间:2017-07-04 16:50:23

标签: java intellij-idea gradle

这是在我尝试使用Gradle构建时发生的。由于字符串是对象,似乎没有理由发生此错误:

No signature of method: java.util.HashMap.getOrDefault() is applicable for argument types: (java.lang.String, java.lang.String) values: [profile, development]
Possible solutions: getOrDefault(java.lang.Object, java.lang.Object)

代码:

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'jacoco'

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

def DEFAULT_PROFILE = 'development'
def profile = project.getProperties().getOrDefault("profile", DEFAULT_PROFILE)
def config = new ConfigSlurper(profile).parse(file("profiles.gradle").toURL())

注意我使用Intellij Idea而不是Eclipse作为我的IDE,用于Eclipse中的项目。这也是我第一次使用Gradle,所以可能是一些初学者的错误。

1 个答案:

答案 0 :(得分:1)

我不知道为什么会发生这种情况,除了ObjectString类扩展来自不同的类加载器而不是Object类,HashMap是在参数中使用。有了这样的东西,你也可以生成像could not cast java.lang.String to java.lang.String这样的消息,因为从不同的类加载器加载的同一个类是同一个类,但是关于instanceof检查的不同类。

也许如果您提供MCVE或整个实际项目,可以弥补正在进行的事情。

实际上你可以改用Groovy风格:

project.findProperty("profile") ?: DEFAULT_PROFILE