Groovy中的ObjectMapper mapper = []是什么意思? [英] What does `ObjectMapper mapper = []` mean in groovy?

查看:180
本文介绍了Groovy中的ObjectMapper mapper = []是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对groovy很陌生,并且正在阅读项目的来源 gretty

  import org.codehaus.jackson.map。 ObjectMapper 
class JacksonCategory {
static final ObjectMapper mapper = []
...
}

我不明白代码 ObjectMapper mapper = [] [] 在这里呢?我认为它是 list ,但是如何将它分配给 ObjectMapper






更新



取决于 Dunes的答案,似乎 [] 表示调用默认构造函数。所以,它意味着:

pre $ static final ObjectMapper mapper = new ObjectMapper()
<


但是: $ b println s // - >它是````不``





 整数i = [] 

抛出异常:

 抓住:org.codehaus.groovy.runtime.typehandling.GroovyCastException:无法用类转换对象'[]'
'java.util.ArrayList'到类'java.lang.Integer'
org.codehaus.groovy.runtime.typehandling.GroovyCastException:无法使用类
'java.util强制转换对象'[]'。 ArrayList'to class'java.lang.Integer'


解决方案

这是对ObjectMapper默认构造函数的调用。



http://mrhaki.blogspot.com/2009/09/groovy-goodness-using-lists-and-maps-as.html



似乎 [] 总是被创建为一个空的ArrayList,但是当被分配给一个单独的类型时,groovy会尝试执行类型强制找到一个合适的构造函数。

使用字符串时,它只是调用列表中的toString方法并将其作为字符串。对于它的对象,它会寻找具有适当数量和类型的参数的构造函数。



Groovy并不期望对扩展Number(Integer,BigDecimal)的java库类例如:

 等等)并抛出ClassCastException。 A类{
字符串值;
A(){this(value); }
A(String value){this.value = value; }
}

def A defaultCons = [];
//相当于新的A()
def A argsCons = [x];
//相当于新的A(x)
def list = [1,2];
//文字ArrayList记法
def String str = [];
//相当于str =[]

println带默认构造函数的:+ defaultCons.value;
printlnA with String arg constructo:+ argsCons.value;
printlnlist:+ list;
printlnstr:+ str;


I'm new to groovy, and I'm reading the source of a project gretty

import org.codehaus.jackson.map.ObjectMapper
class JacksonCategory {
static final ObjectMapper mapper = []
    ...
}

I don't understand the code ObjectMapper mapper = [], what does [] mean here? I thought it's a list, but how to assign it to a ObjectMapper?


UPDATE

Depends on Dunes's answer, seems [] means invocation of default constructor. So, it means:

static final ObjectMapper mapper = new ObjectMapper()

But:

String s = []
println s // -> it's `[]` not ``

And

Integer i = []

throws exception:

Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[]' 
with class 'java.util.ArrayList' to class 'java.lang.Integer' 
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[]' with class  
'java.util.ArrayList' to class 'java.lang.Integer'

解决方案

It's a call to the default constructor of ObjectMapper.

http://mrhaki.blogspot.com/2009/09/groovy-goodness-using-lists-and-maps-as.html

It seems [] is always created as an empty ArrayList, but when assigned to a separate type groovy tries to do type coercion and find an appropriate constructor.

With strings it just calls the toString method on the list and makes that the string. For objects it looks for constructors with the appropriate number and type of arguments.

Groovy does not expect to have to do this for java library classes that extend Number (Integer, BigDecimal, etc) and throws a ClassCastException instead.

Examples:

class A {
    String value;
    A() { this("value"); }
    A(String value) { this.value = value; }
}

def A defaultCons = [];
// equivalent to new A()
def A argsCons = ["x"];
// equivalent to new A("x")
def list = [1,2];
// literal ArrayList notation
def String str = [];
// equivalent to str = "[]"

println "A with default constructor: " + defaultCons.value;
println "A with String arg constructo: " + argsCons.value;
println "list: " + list;
println "str: " + str;

这篇关于Groovy中的ObjectMapper mapper = []是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆