/* MyPrintable interface is defined by the following method signatures: -- object.print(String); -- object.println(String); Given an Object or Class, check whether the Class (of the Object) implements MyPrintable interface. If so, then maybe either method can be invoked on such an Object. This is somewhat depricated as of 2005.May.15 when I've re-done the same idea in a more flexible and proper way in CompareIntCls. But I haven't yet refactored this older version to use the newer version for all the class-implements-interface checking, leaving in this file only the omsInvoke stuff which hasn't yet been depricated, so this is still in use for my homework assignment. In any case: Copyright 2005 by Robert Elton Maas, all rights reserved. Commercial or other for-profit use prohibited without prior written arrangements for payment of license fee or other consideration. Non-profit personal use allowed providing you notify me of your use within one week (7 days) of start of use. Non-profit organizational use allowed providing you ask me for permission prior to switching from personal testing-use to use as operational tool for organization. I (Robert Maas) developed this as a tool to aid my work on homework assignments for distributed-Java class at De Anza college, specifically to validate an interface that both System.out and the 'out' page-variable of JSPs satisfy, so that I can write software that can run under either environment transparently. Specifically Lab3Props.html provides output services using this interface. */ import java.lang.reflect.Method; public class MyPrintableI { public static void main(String args[]) { test(); } public static void test() { verboseOSP(java.lang.String.class); verboseOSP("Hello world!"); verboseOSP(System.out); Object o1 = System.out; Method[] ms1 = objectSupportsPrinting(o1); omsInvoke(o1, ms1[1], "(First text)"); omsInvoke(o1, ms1[0], "(Second text)"); Class c = java.lang.Object.class; Method[] ms = c.getMethods(); Method m = ms[5]; omsInvoke("Object o", m, "String s"); // Throws unchecked exception } /* Verbose form of objectSupportsPrinting for demo purposes, plus actually trying to print some text to the print-capable object. */ private static void verboseOSP(Object o) { System.out.println("o = " + o); Method[] subres = objectSupportsPrinting(o); if (!(subres==null)) { System.out.println("** Yes, the object " + o + " supports printing **"); String[] args = new String[1]; System.out.print("-- Going to test printing now, first print ["); args[0] = "Some text to print, no newline."; try { subres[0].invoke(o,args); } catch (java.lang.Exception ex) { throw new java.util.MissingResourceException ("Bug: method print doesn't work: " + ex, "",""); } System.out.print("] Now println ["); args[0] = "Some more text to print, but with newline."; try { subres[1].invoke(o,args); } catch (java.lang.Exception ex) { throw new java.util.MissingResourceException ("Bug: method println doesn't work: " + ex, "",""); } System.out.println("] done."); } else System.out.println("XX No, the object " + o + " does not support printing XX"); } /* Given an Object whose class has already been found to have a suitable print or println method which takes one argument, a string, and such a Method (actually this would work for any such method, not just a print or println method discovered elsewhere in this module), and any String, invoke that method on that object with that String parameter. For example, if m is the print method, then these are equivalent: o.print(s); omsInvoke(o,m,s); except the former requires the type of o be known at compile time. */ public static void omsInvoke(Object o, Method m, String s) { String[] args = new String[1]; args[0] = s; try { m.invoke(o,args); } catch (java.lang.Exception ex) { throw new java.util.MissingResourceException ("Attempt to call Method=" + m + " on single-arg of type " + "String, where this=" + o + " gives: " + ex, "",""); } } /* Given an Object, return true if the Class of that object has methods sufficient to implement MyPrintable interface, as specified at the top of this file. */ public static Method[] objectSupportsPrinting(Object o) { Class c = o.getClass(); //System.out.println("c = " + c); Method[] res = classSupportsPrinting(c); /* if (!(res==null)) System.out.println("** Yes, "+c+" supports printing. **"); */ return res; } /* Given a Class, if it has methods sufficient to implement MyPrintable interface, as specified at the top of this file, then return array containing those methods in the sequence specified, else return null pointer. */ public static Method[] classSupportsPrinting(Class c) { Method[] ms = c.getMethods(); //System.out.println("ms = " + ms + " (length="+ ms.length +")"); Method gotprint=null; Method gotprintln=null; for (int i=0; i