public static void main(String[] args) throws Exception { String s = "Test String"; PReJava5(s); postJava5(s); }
private static void preJava5(String s) throws Exception { // First, do a substring Class c = s.getClass(); Method m = c.getMethod("substring", new Class[] { int.class, int.class }); Object obj = m.invoke(s, new Object[] { new Integer(0), new Integer(4) }); System.out.println(obj);
// Next, do a 'length' Method m2 = c.getMethod("length", null); Integer lengthObj = (Integer)m2.invoke(s, null); int length = lengthObj.intValue(); System.out.println(length); }
private static void postJava5(String s) throws Exception { // First, do a substring Class<?> c = s.getClass(); Method m = c.getMethod("substring", int.class, int.class); Object obj = m.invoke(s, 0, 4); System.out.println(obj);// Next, do a 'length' Method m2 = c.getMethod("length"); int length = (Integer)m2.invoke(s); System.out.println(length);