java map 泛型 反射_Java通过反射读取泛型

packagecom.waibizi;importjava.lang.reflect.Method;importjava.lang.reflect.ParameterizedType;importjava.lang.reflect.Type;importjava.util.List;importjava.util.Map;importbean.User;/*** 通过反射读取泛型信息

*@author吴典秋

**/

public classGet_generic_info {public void test01(Map map,Listlist){

System.out.println("Demo04.test01()");

}public Maptest02(){

System.out.println("Demo04.test02()");return null;

}

@SuppressWarnings("all") //压制所有的警告

public static voidmain(String[] args) {//TODO Auto-generated method stub

try{//获得指定方法参数泛型信息

Method m = Get_generic_info.class.getMethod("test01", Map.class,List.class);

Type[] t=m.getGenericParameterTypes();for(Type paramType : t) {

System.out.println("#"+paramType);if(paramType instanceofParameterizedType){

Type[] genericTypes=((ParameterizedType) paramType).getActualTypeArguments();for(Type genericType : genericTypes) {

System.out.println("泛型类型:"+genericType);

}

}

}//获得指定方法返回值泛型信息

Method m2 = Get_generic_info.class.getMethod("test02", null);

Type returnType=m2.getGenericReturnType();if(returnType instanceofParameterizedType){

Type[] genericTypes=((ParameterizedType) returnType).getActualTypeArguments();for(Type genericType : genericTypes) {

System.out.println("返回值,泛型类型:"+genericType);

}

}

}catch(NoSuchMethodException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(SecurityException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中的 `Map` 属性和泛型 `T` 都可以通过序列化转换为字节序列,以便在网络上传输或将其存储在磁盘上。 对于 `Map` 属性的序列化,我们可以在包含该属性的类中实现 `Serializable` 接口,并将 `Map` 属性标记为 `transient`,以便在序列化对象时跳过该属性。然后,我们可以在 `writeObject()` 和 `readObject()` 方法中手动序列化和反序列化该属性。 以下是一个示例代码,展示了如何序列化一个包含 `Map` 属性的类: ```java import java.io.*; import java.util.*; public class MyClass implements Serializable { private transient Map<String, Integer> map; private String name; public MyClass() { map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); name = "MyClass"; } private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(map.size()); for (Map.Entry<String, Integer> entry: map.entrySet()) { out.writeObject(entry.getKey()); out.writeObject(entry.getValue()); } } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); int size = in.readInt(); map = new HashMap<>(size); for (int i = 0; i < size; i++) { String key = (String) in.readObject(); Integer value = (Integer) in.readObject(); map.put(key, value); } } @Override public String toString() { return "MyClass{name='" + name + "', map=" + map + "}"; } public static void main(String[] args) { MyClass myClass = new MyClass(); System.out.println("Original object: " + myClass); try { // 序列化 FileOutputStream fos = new FileOutputStream("myclass.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(myClass); oos.close(); // 反序列化 FileInputStream fis = new FileInputStream("myclass.ser"); ObjectInputStream ois = new ObjectInputStream(fis); MyClass newMyClass = (MyClass) ois.readObject(); ois.close(); // 输出反序列化后的对象 System.out.println("Deserialized object: " + newMyClass); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } ``` 在上述代码中,`Map` 属性被标记为 `transient`,并在 `writeObject()` 和 `readObject()` 方法中手动序列化和反序列化。序列化时,我们首先调用 `defaultWriteObject()` 方法将对象的默认字段序列化,然后手动序列化 `Map` 属性。反序列化时,我们首先调用 `defaultReadObject()` 方法读取对象的默认字段,然后手动反序列化 `Map` 属性。 对于泛型 `T` 的序列化,我们可以在序列化和反序列化时使用 Java 的类型擦除机制。具体来说,在序列化时,我们可以将 `T` 视为 `Object`,并将其强制转换为 `Object` 类型;在反序列化时,我们可以将反序列化后的 `Object` 对象强制转换为 `T` 类型。 以下是一个示例代码,展示了如何序列化一个包含泛型 `T` 属性的类: ```java import java.io.*; public class MyClass<T> implements Serializable { private T data; public MyClass(T data) { this.data = data; } public T getData() { return data; } @Override public String toString() { return "MyClass{data=" + data + "}"; } public static void main(String[] args) { MyClass<String> myClass = new MyClass<>("Hello, world!"); System.out.println("Original object: " + myClass); try { // 序列化 FileOutputStream fos = new FileOutputStream("myclass.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(myClass); oos.close(); // 反序列化 FileInputStream fis = new FileInputStream("myclass.ser"); ObjectInputStream ois = new ObjectInputStream(fis); MyClass<?> newMyClass = (MyClass<?>) ois.readObject(); ois.close(); // 输出反序列化后的对象 System.out.println("Deserialized object: " + newMyClass); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } ``` 在上述代码中,我们创建了一个泛型 `T` 类型为 `String` 的 `MyClass` 对象,并将其序列化和反序列化。在序列化时,我们将泛型 `T` 视为 `Object`,并将其强制转换为 `Object` 类型;在反序列化时,我们将反序列化后的 `Object` 对象强制转换为 `T` 类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值