In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to solve the problem of type erasure in Java generics", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to solve the problem of type erasure in Java generics" article.
Suppose there are two bean classes
/ * Test. * / @ Data@NoArgsConstructor@AllArgsConstructorpublic static class Foo {public String name;} / * * Test. * / @ Data@NoArgsConstructor@AllArgsConstructorpublic static class Dummy {public String name;}
And another object.
@ NoArgsConstructor@AllArgsConstructor@Datapublic static class Spec {public String spec; public T deserializeTo () throws JsonProcessingException {var mapper = new ObjectMapper (); return (T) mapper.readValue (spec, Foo.class);}}
You can see that the above two types of json serialized strings are saved in the Spec object, and a method is provided to deserialize string spec into the corresponding type. The ideal way is to get the actual type of parameter type T in the deserialization method. Theoretically, the runtime Spec type is determined, so T should also be determined, but because of type erasure, you can't actually get his type.
Try to get the generic type through ((ParameterizedType) getClass (). GetGenericSuperclass ()). GetActualTypeArguments (), which is not available after testing
@ Test public void test () throws JsonProcessingException {var foo = new Foo ("foo"); var spec = new Spec (mapper.writeValueAsString (foo)); var deserialized = spec.deserializeTo (); Assertions.assertTrue (deserialized instanceof Foo);} @ NoArgsConstructor @ AllArgsConstructor @ Data public static class Spec {public String spec Private Class getSpecClass () {return (Class) ((ParameterizedType) getClass (). GetGenericSuperclass ()) .getActualTypeArguments () [0];} public T deserializeTo () throws JsonProcessingException {var mapper = new ObjectMapper (); System.out.println (spec); return (T) mapper.readValue (spec, getSpecClass ()) }}
There will be the following mistakes
Java.lang.ClassCastException: class java.lang.Class cannot be cast to class java.lang.reflect.ParameterizedType (java.lang.Class and java.lang.reflect.ParameterizedType are in module java.base of loader 'bootstrap')
There are two ways to get around this problem
The first is relatively simple, which is to pass in the class of the type directly when creating the spec object, so that you can use it directly.
@ Datapublic abstract static class AbstractSpec {public String spec; public AbstractSpec (String spec) {this.spec = spec;} private Class getSpecClass () {return (Class) ((ParameterizedType) getClass () .getGenericSuperclass ()) .getActualTypeArguments () [0];} public T deserializeTo () throws JsonProcessingException {var mapper = new ObjectMapper (); System.out.println (spec) Return (T) mapper.readValue (spec, getSpecClass ());}} public static class Spec extends AbstractSpec {public Spec (String spec) {super (spec);}} @ Testpublic void test () throws JsonProcessingException {var foo = new Foo ("foo"); var spec = new Spec (mapper.writeValueAsString (foo)); var deserialized = spec.deserializeTo (); Assertions.assertTrue (deserialized instanceof Foo);}
Here the spec class can be deserialized smoothly.
The difference between this and the initial failure of case is the addition of a subclass, the main difference is that the return value of getGenericSuperclass is different, in the case of non-subclass, the Object is obtained.
Therefore, in theory, the type information of the subclass Spec actually stores the type parameter information in the parent class, that is, Foo in the example. In the https://stackoverflow.com/questions/42874197/getgenericsuperclass-in-java-how-does-it-work way, you can see the corresponding type information in the bytecode of the Spec class.
$javap-verbose. / org/apache/flink/kubernetes/operator/controller/GenericTest\ $Spec.class | grep Signature # 15 = Utf8 Signature Start Length Slot Name SignatureSignature: # 19 / / Lorg/apache/flink/kubernetes/operator/controller/GenericTest$AbstractSpec The above is about the content of this article on "how to solve the problem of type erasure in Java generics". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.