加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在Grails中应用Groovy扩展会为String#toBoolean()生成MissingMet

发布时间:2020-12-14 16:30:08 所属栏目:大数据 来源:网络整理
导读:背景 Groovy具有adding methods to the existing classes的功能,我找到了some interesting. 然后我discovered我需要自定义我的Grails引导程序来加载它们,所以我添加: def init = { servletContext - addExtensionModules() } def addExtensionModules() { M
背景

Groovy具有adding methods to the existing classes的功能,我找到了some interesting.

然后我discovered我需要自定义我的Grails引导程序来加载它们,所以我添加:

def init = { servletContext -> addExtensionModules() }

  def addExtensionModules() {

    Map<CachedClass,List<MetaMethod>> map = [:]
    ClassLoader classLoader = Thread.currentThread().contextClassLoader
    try {
      Enumeration<URL> resources = classLoader.getResources(MetaClassRegistryImpl.MODULE_META_INF_FILE)
      for (URL url in resources) {
        if (url.path.contains('groovy-all')) {
          // already registered
          continue
        }
        Properties properties = new Properties()
        InputStream inStream
        try {
          inStream = url.openStream()
          properties.load(inStream)
          GroovySystem.metaClassRegistry.registerExtensionModuleFromProperties(properties,classLoader,map)
        }
        catch (IOException e) {
          throw new GroovyRuntimeException("Unable to load module META-INF descriptor",e)
        } finally {
          inStream?.close()
        }
      }
    }  catch (IOException ignored) {}
    map.each { CachedClass cls,List<MetaMethod> methods ->
    cls.setNewMopMethods(methods)
  }
}

我添加了BuildConfig.groovy

compile ('ca.redtoad:groovy-crypto-extensions:0.2') {
  excludes 'groovy-all'
}

问题

问题是现在我不能使用Groovy String的toBoolean()方法:

groovy.lang.MissingMethodException: No signature of method:
java.lang.String.toBoolean() is applicable for argument types: ()
values: [] Possible solutions: asBoolean(),asBoolean(),toFloat(),
toDouble()

由于groovy已经注册,为什么缺少这种方法?我正在使用Grails 2.2.4.

编辑

在一个groovy 2.0.8控制台中测试,代码可以工作,所以可能与Grails有关.

@Grab('ca.redtoad:groovy-crypto-extensions:0.2')
@GrabExclude('org.codehaus.groovy:groovy-all')

addExtensionModules() //same method of BootStrap,ommited to make shorter.

def key = "password".toKey()
def ciphertext = "some plaintext".bytes.encrypt(key: key)
def x = new String(ciphertext.decrypt(key: key)).toBoolean()
println "S".toBoolean()

解决方法

更换

map.each { CachedClass cls,List<MetaMethod> methods ->
    cls.setNewMopMethods(methods)
}

map.each { CachedClass cls,List<MetaMethod> methods ->
    //Add new MOP methods instead of set them as new
    cls.addNewMopMethods(methods) 
}

当在CachedClass中设置新的元方法时,现有的扩展/元方法将被扩展模块中唯一提供的扩展覆盖.在这种情况下,groovy-crypto-extension在String类上使用下面的扩展方法

class java.lang.String=
[public static javax.crypto.spec.SecretKeySpec ca.redtoad.groovy.extensions.crypto.CryptoExtensionMethods.toKey(java.lang.String),public static javax.crypto.spec.SecretKeySpec ca.redtoad.groovy.extensions.crypto.CryptoExtensionMethods.toKey(java.lang.String,java.util.Map)
]

如果将这些方法设置为CachedClass,则会清除现有方法.因此必须将它们添加到CachedClass中进行替换.因此,在String类上使toBoolean可用.

挑战被接受并执行.你欠我一个款待. (礼品卡也可以接受).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读