erasure

Gson desearilize list and class, how does erasure work here?

大兔子大兔子 提交于 2021-02-11 00:22:14
问题 I intend to write a generic method to convert a json list into it's specific list with class. This is the generic json parser: public class JsonParserUtils { private static Gson gson = new Gson(); public static <T> String toJson(T object) { if (object == null) { return null; } return gson.toJson(object); } public static <T> T fromJson(String json, Class<T> className) { if (StringUtils.isEmpty(json)) { return null; } T object = gson.fromJson(json, className); return object; } public static <T>

Gson desearilize list and class, how does erasure work here?

筅森魡賤 提交于 2021-02-11 00:19:50
问题 I intend to write a generic method to convert a json list into it's specific list with class. This is the generic json parser: public class JsonParserUtils { private static Gson gson = new Gson(); public static <T> String toJson(T object) { if (object == null) { return null; } return gson.toJson(object); } public static <T> T fromJson(String json, Class<T> className) { if (StringUtils.isEmpty(json)) { return null; } T object = gson.fromJson(json, className); return object; } public static <T>

How does Gson TypeToken work?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 10:37:39
问题 I understand that in Java contrary to, for example, C# generics are compile-time feature and is removed via type erasure. So, how does Gson's TypeToken really work? How does it get the generic type of an object? 回答1: From §4.6 of the JLS (emphasis mine): Type erasure is a mapping from types (possibly including parameterized types and type variables) to types (that are never parameterized types or type variables). We write |T| for the erasure of type T. The erasure mapping is defined as

Java Type Erasure: Rules of cast insertion?

三世轮回 提交于 2019-12-12 02:30:43
问题 The Java tutorial on type erasure doesn't seem to detail the specific rules of cast insertion by the compiler. Can someone please explain the specific rules that cause the transformation detailed by the tutorial (reproduced below): public class Node<T> { public T data; public Node(T data) { this.data = data; } public void setData(T data) { System.out.println("Node.setData"); this.data = data; } } public class MyNode extends Node<Integer> { public MyNode(Integer data) { super(data); } public

Erasure type and Bridging Method clarification

蹲街弑〆低调 提交于 2019-12-11 07:26:49
问题 The code following is taken from Oracle documentation of generics - class Node<T> { public T data; public Node(T data) { this.data = data; } public void setData(T data) { System.out.println("Node.setData"); this.data = data; } } class MyNode extends Node<Integer> { public MyNode(Integer data) { super(data); } public void setData(Integer data) { System.out.println("MyNode.setData"); super.setData(data); } public static void main(String[] args) { MyNode mn = new MyNode(5); Node n = mn; // A raw

Pattern matching on List[T] and Set[T] in Scala vs. Haskell: effects of type erasure

試著忘記壹切 提交于 2019-12-10 03:44:16
问题 Would the Haskell equivalent of the code below produce correct answers? Can this Scala code be fixed to produce correct answers ? If yes, how ? object TypeErasurePatternMatchQuestion extends App { val li=List(1,2,3) val ls=List("1","2","3") val si=Set(1,2,3) val ss=Set("1","2","3") def whatIsIt(o:Any)=o match{ case o:List[Int] => "List[Int]" case o:List[String] => "List[String]" case o:Set[Int] => "Set[Int]" case o:Set[String] => "Set[String]" } println(whatIsIt(li)) println(whatIsIt(ls))

Duplicate Method while Method-Overloading

社会主义新天地 提交于 2019-12-07 05:27:13
问题 Following code gives compilation error with error "Duplicate Method" static int test(int i){ return 1; } static String test(int i){ return "abc"; } This is expected as both the overloaded method have same signature and differ only in return type. But the following code compiles fine with a warning: static int test1(List<Integer> l){ return 1; } static String test1(List<String> l){ return "abc"; } As, we know the Java Generics works on Erasure, which mean in byte-code, both these method have

Two methods with same erasure aren't necessary override-equivalent (or they signatures aren't subsignatures between them)?

和自甴很熟 提交于 2019-12-06 22:57:07
问题 Im reading the incredible book “a programmer's guide to java scjp certification” for jdk6 and theres a section about generic overriding. On it is described subsignature and override-equivalent and describes some examples of override-equivalent that I quote: Given the following three generic method declarations in a class: static <T> void merge (MyStack<T> s1, MyStack<T> s2) { /*...*/ } static <T> void merge (MyStack<T> s1, MyStack<? extends T> s2) { /*...*/ } static <T> void merge (MyStack<T>

Pattern matching on List[T] and Set[T] in Scala vs. Haskell: effects of type erasure

拥有回忆 提交于 2019-12-05 06:23:53
Would the Haskell equivalent of the code below produce correct answers? Can this Scala code be fixed to produce correct answers ? If yes, how ? object TypeErasurePatternMatchQuestion extends App { val li=List(1,2,3) val ls=List("1","2","3") val si=Set(1,2,3) val ss=Set("1","2","3") def whatIsIt(o:Any)=o match{ case o:List[Int] => "List[Int]" case o:List[String] => "List[String]" case o:Set[Int] => "Set[Int]" case o:Set[String] => "Set[String]" } println(whatIsIt(li)) println(whatIsIt(ls)) println(whatIsIt(si)) println(whatIsIt(ss)) } prints: List[Int] List[Int] Set[Int] Set[Int] but I would

Scala ambiguous reference to overloaded definition with two implicit parameters

北城以北 提交于 2019-12-04 19:14:41
lazy val productService = BeanLookup[ProductDataService] object BeanLookup { def apply[T](implicit manifest: Manifest[T], context: ActorContext) = { val beanLookup = context.actorFor("/user/spring/beanLookup") Await.result( (beanLookup.ask(LookupBean(manifest.erasure))(timeout)).mapTo[T](manifest), timeout.duration) } def apply[T](implicit manifest: Manifest[T], system: ActorSystem) = { val beanLookup = system.actorFor("/user/spring/beanLookup") Await.result( (beanLookup.ask(LookupBean(manifest.erasure))(timeout)).mapTo[T](manifest), timeout.duration) } } scalac complains : scala: ambiguous