code_junkie

Casting to Unknown Type When Class Name as a String

Nov 14th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. public class ExampleClass {
  2. public static void main(String[] args) {
  3. // TODO Auto-generated method stub
  4. Horse hr1 = new Horse();
  5. Horse hr2 = new Horse();
  6. Horse hr3 = new Horse();
  7. Horse hr4 = new Horse();
  8. Set hrSet = new HashSet();
  9. hrSet.add(hr1);
  10. hrSet.add(hr2);
  11. hrSet.add(hr3);
  12. hrSet.add(hr4);
  13. Horse hr;
  14. String hor = "sher_pkg.Horse";
  15. callHorse(hrSet,hor);
  16. }
  17. public static void callHorse(Set xSet,String clsName){
  18. try {
  19. Class hrt = Class.forName(clsName);
  20.  
  21. Iterator hritr = xSet.iterator();
  22. while(hritr.hasNext()){
  23. exam(hrt.cast(hritr.next()));
  24. }
  25. } catch (ClassNotFoundException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. public static void exam(Object obj){ //I want to use exam(Horse hrr)
  30. System.out.println(obj);
  31. }
  32. }
  33.  
  34. public class Test {
  35. public void test(Object obj) {
  36. if (obj instanceof Horse) {
  37. Horse c = (Horse) obj;
  38. noise(c);
  39. }
  40. if (obj instanceof Cow) {
  41. Cow c = (Cow) obj;
  42. noise(c);
  43. }
  44. }
  45.  
  46. public void noise(Horse h) {
  47. System.out.println("Neigh");
  48. }
  49.  
  50. public void noise(Cow c) {
  51. System.out.println("Moo");
  52. }
  53.  
  54. public static void main(String[] args) {
  55. Object o1 = new Horse();
  56. Object o2 = new Cow();
  57. Test tester = new Test();
  58. tester.test(o1);
  59. tester.test(o2);
  60. }
  61. }
  62.  
  63. class Horse {}
  64.  
  65. class Cow {}
  66.  
  67. if (obj instanceof Horse) {
  68. Horse c = (Horse) obj;
  69. noise(c);
  70. }
  71.  
  72. if (obj instanceof Horse) {
  73. handleNoise(obj, Horse.class);
  74. }
  75.  
  76. void handleNoise(Object obj, Class clazz) {
  77. noise(clazz.cast(obj));
  78. }
  79.  
  80. public class Test {
  81. public void test(Animal obj) {
  82. obj.noise();
  83. }
  84.  
  85. public static void main(String[] args) {
  86. Animal o1 = new Horse();
  87. Animal o2 = new Cow();
  88. Test tester = new Test();
  89. tester.test(o1);
  90. tester.test(o2);
  91. }
  92. }
  93.  
  94. interface Animal {
  95. void noise();
  96. }
  97.  
  98. class Horse implements Animal {
  99. public void noise() {
  100. System.out.println("Neigh");
  101. }
  102. }
  103.  
  104. class Cow implements Animal {
  105. public void noise() {
  106. System.out.println("Moo");
  107. }
  108. }
  109.  
  110. if (obj instanceof Cust) {
  111. loopOverSet(c.getCustPhonSet());
  112. } else if (obj instanceof Name) {
  113. loopOverSet(c.getCustNameSet());
  114. }
  115. // and so on for the rest...
  116.  
  117. void loopOVerSet(Set cxSet) {
  118. if (cxSet != null && cxSet.size() > 0) {
  119. Iterator cxSetIterator = cxSet.iterator();
  120. while (cxSetIterator.hasNext())
  121. {
  122. ((StringProp)cxSetIterator.next()).stringProp();
  123. }
  124. }
  125. }
  126.  
  127. public static void callHorse(Set<Horse> xSet) {
  128. Iterator<Horse> hritr = xSet.iterator();
  129. while (hritr.hasNext()) {
  130. exam(hritr.next());
  131. }
  132. }
  133. public static void exam(Horse obj) { //I want to use exam(Horse hrr)
  134. System.out.println(obj);
  135. }
  136.  
  137. exam(Horse hrr)
  138.  
  139. try {
  140. Class hrt = Class.forName(clsName);
  141.  
  142. Iterator hritr = xSet.iterator();
  143. while(hritr.hasNext()){
  144. exam((Horse)hrt.cast(hritr.next()));
  145. }
  146. }
  147.  
  148. (final Set xSet<Horse>, final String clsName){
  149. ...}
  150.  
  151. public static void callHorse(Set<?> xSet, String clsName) {
  152. try {
  153. Class<?> hrt = Class.forName(clsName);
  154. for (Object x : xSet) {
  155. exam(hrt.cast(x));
  156. }
  157. } catch (ClassNotFoundException e) {
  158. e.printStackTrace();
  159. }
  160. }
  161.  
  162. public static void exam(Object obj) {
  163. System.out.println("Object " + obj);
  164. }
  165.  
  166. public static void exam(Horse obj) {
  167. System.out.println("Horse " + obj);
  168. }
  169.  
  170. public static void main(String[] args) {
  171. Set<Horse> horses = new HashSet<Horse>();
  172. horses.add(new Horse());
  173. horses.add(new Horse());
  174. horses.add(new Horse());
  175. horses.add(new Horse());
  176.  
  177. callHorse(horses);
  178. }
  179.  
  180. public static void callHorse(Set<Horse> horses) {
  181. for (Horse horse : horses) {
  182. exam(horse);
  183. }
  184. }
  185.  
  186. public static void exam(Horse horse) {
  187. System.out.println(horse);
  188. }
  189.  
  190. public static void main(String[] args) {
  191. Set<Horse> horses = new HashSet<Horse>();
  192. horses.add(new Horse());
  193. horses.add(new Horse());
  194. horses.add(new Horse());
  195. horses.add(new Horse());
  196.  
  197. examineHorses(horses);
  198. }
  199.  
  200. public static void examineHorses(Set<Horse> horses) {
  201. for (Horse horse : horses) {
  202. horse.examine();
  203. }
  204. }
  205.  
  206. // in Horse.java
  207. public class Horse {
  208. public void examine() {
  209. System.out.println(this);
  210. }
  211. ...
  212. }
  213.  
  214. import java.lang.reflect.*;
  215.  
  216. public class Test {
  217.  
  218. public static void exam( Object o ) {
  219. System.out.println( "Object version called" );
  220. }
  221.  
  222.  
  223. public static void exam( Test t ) {
  224. System.out.println( "Test version called" );
  225. }
  226.  
  227. public static void main (String[] args) {
  228.  
  229. try {
  230.  
  231. // Create an instance of Test but reference it as an Object
  232.  
  233. Object untypedTest = new Test();
  234.  
  235. // Calling exam directly will invoke the Object version
  236.  
  237. exam( untypedTest );
  238.  
  239. // But if we use reflection to select the version of exam
  240. // that takes the desired class name, we can invoke it without
  241. // even explicitly casting
  242.  
  243. String className = "Test";
  244.  
  245. Class[] examMethodParams = { Class.forName( className ) };
  246.  
  247. Method examMethod = Test.class.getMethod( "exam", examMethodParams );
  248.  
  249. Object[] actualParams = { untypedTest };
  250.  
  251. examMethod.invoke( null, actualParams );
  252.  
  253. } catch (Exception e) {
  254. e.printStackTrace();
  255. }
  256.  
  257. }
  258.  
  259. }
  260.  
  261. if (obj instanceof Cust)
  262. {
  263. Cust c = (Cust) obj;
  264. Set cxSet = c.getCustPhonSet();
  265. CustPhon cx;
  266. if (cxSet != null && cxSet.size() > 0)
  267. {
  268. Iterator cxSetIterator = cxSet.iterator();
  269. while (cxSetIterator.hasNext())
  270. {
  271. cx = (CustPhon) cxSetIterator.next();
  272. this.stringProp(cx);
  273. }
  274. }
  275. //....pattern continues here... CustPhon is replaced by various classes like CustNam etc... Also getCustPhonSet by getCustNamSet etc...
  276. }
  277.  
  278. public void dynamicIteration(Set xlSet, String clsName)
  279. {
  280. if (xSet != null && xSet.size() > 0)
  281. {
  282. try{
  283. Class clsinstance = Class.forName(clsName);
  284. Iterator itr = generalSet.iterator();
  285. while(itr.hasNext())
  286. {
  287. this.stringProp(clsinstance.cast(itr.next()));// See this is wrong.. thats y i posted here by using a simple Horse example
  288. }
  289. }catch(ClassNotFoundException e)
  290. {
  291. e.printStackTrace();
  292. }
  293. }
  294. }
  295.  
  296. //process customer email address
  297. Set cxSet = c.getCustPhonSet();
  298. className = "pkg.CustPhon";
  299. dynamicIteration(cxSet,className);
  300. // Similarly for other patterns
Add Comment
Please, Sign In to add comment