//Reflective Visitor // Ako zelimo da dodamo novi Visit objekat,moramo da izmenimo // Visitor interface,a potom i implementiramo sam metod u svakom od // Visitora. // // Resenje je u Reflective Visitoru.Sa njim nam je potreban samo jedan metod //u Visitor interfejsu - visit(Object). Svi ostali metodi visit() // mogu se dodati kasnije import java.lang.reflect.Method; //hijerarhija elementa interface Element { public void accept( ReflectiveVisitor v ); } class This implements Element { public void accept( ReflectiveVisitor v ) { v.visit( this ); } public String thiss() { return "Ovaj el"; } } class That implements Element { public void accept( ReflectiveVisitor v ) { v.visit( this ); } public String that() { return "Onaj el"; } } class TheOther implements Element { public void accept( ReflectiveVisitor v ) { v.visit( this ); } public String theOther() { return "Inace el"; } } //hijerarhija operacije abstract class ReflectiveVisitor { abstract public void visit( Object o ); public void visitTheOther( TheOther e ) { System.out.println( "ReflectiveVisitor: Operacija nad " + e.theOther() ); } // 1. Potraga za visitElementClassName() u tekucoj klasi // 2. Potraga za visitElementClassName() u nadklasama // 3. Potraga za visitElementClassName() u interfejsima // 4. Potraga za visitObject() u tekucoj klasi protected Method getMethod( Class c ) { Class newc = c; Method m = null; while (m == null && newc != Object.class) { String method = newc.getName(); method = "visit" + method.substring( method.lastIndexOf('.') + 1 ); try { m = getClass().getMethod( method, new Class[] { newc } ); } catch (NoSuchMethodException ex) { newc = newc.getSuperclass(); } } if (newc == Object.class) { // System.out.println( "Pretraga interfejsa" ); Class[] interfaces = c.getInterfaces(); for (int i=0; i < interfaces.length; i++) { String method = interfaces[i].getName(); method = "visit" + method.substring( method.lastIndexOf('.') + 1 ); try { m = getClass().getMethod( method, new Class[] { interfaces[i] } ); } catch (NoSuchMethodException ex) { } } } if (m == null) try { m = getClass().getMethod( "visitObject", new Class[] { Object.class } ); } catch (Exception ex) { } return m; } } class UpVisitor extends ReflectiveVisitor { public void visit( Object o ) { try { getMethod( o.getClass() ).invoke( this, new Object[] { o } ); } catch (Exception ex) { System.out.println( "UpVisitor -nema odgovarajuceg visit() metoda" ); } } public void visitThis( This e ) { System.out.println( "UpVisitor: Izvrsiti Up nad " + e.thiss() ); } public void visitObject( Object e ) { System.out.println( "UpVisitor- generisanje visitObject() metoda" ); } } class DownVisitor extends ReflectiveVisitor { public void visit( Object o ) { try { getMethod( o.getClass() ).invoke( this, new Object[] { o } ); } catch (Exception ex) { System.out.println( "DownVisitor: bez odgovarajuceg visit() metoda" ); } } public void visitThat( That e ) { System.out.println( "DownVisitor: Down izvsen nad " + e.that() ); } } class Visitor3 { public static void main( String[] args ) { Element[] list = { new This(), new That(), new TheOther() }; UpVisitor up = new UpVisitor(); DownVisitor down = new DownVisitor(); for (int i=0; i < list.length; i++) list[i].accept( up ); for (int i=0; i < list.length; i++) list[i].accept( down ); } }