mardi 8 mars 2022

How to cast an object to another type using reflection at runtime

I have an object abstractSwift of type AbstractMT. I want to cast this object to another object at runtime. Below is my code snippet to get some insight.

Class<?> swiftClass = Class.forName("com.prowidesoftware.swift.model.mt.mt7xx.MT760");
AbstractMT abstractSwift = AbstractMT.fromJson(jsonSwiftMessageStr);

I want something like below at Runtime

AbstractMT abstractSwift = AbstractMT.fromJson(jsonSwiftMessageStr);
MT760 mt760 = (MT760) abstractSwift

Please suggest a suitable way to do the same.





Aucun commentaire:

Enregistrer un commentaire