9090 */
9191public class Macro {
9292 private final static String NULLVALUE = "c29e43048791e250dfd5723e7b8aa048df802c9262cfa8fbc4475b2e392a8ad2" ;
93- private final static String LITERALVALUE = "017a3ddbfc0fcd27bcdb2590cdb713a379ae59ef" ;
93+ protected final static String LITERALVALUE = "017a3ddbfc0fcd27bcdb2590cdb713a379ae59ef" ;
9494 private final static Pattern NUMERIC_P = Pattern
9595 .compile ("[-+]?(\\ d*\\ .?\\ d+|\\ d+\\ .)(e[-+]?[0-9]+)?" );
9696
@@ -342,7 +342,7 @@ public String replace(String key, Link link) {
342342 return replace (key , null , link , '{' , '}' );
343343 }
344344
345- private String replace (String key , List <String > args , Link link , char begin , char end ) {
345+ protected String replace (String key , List <String > args , Link link , char begin , char end ) {
346346 String value = getMacro (key , args , link , begin , end );
347347 if (value != LITERALVALUE ) {
348348 if (value != null )
@@ -405,6 +405,26 @@ private String doCommands(String[] args, Link source) {
405405 return doCommand (this , args [0 ], args );
406406 }
407407
408+ protected BiFunction <Object , String [], Object > getFunction (String method ) {
409+ Processor rover = domain ;
410+ while (rover != null ) {
411+ BiFunction <Object , String [], Object > function = getFunction (rover , method );
412+ if (function != null )
413+ return function ;
414+
415+ rover = rover .getParent ();
416+ }
417+
418+ for (int i = 0 ; targets != null && i < targets .length ; i ++) {
419+ BiFunction <Object , String [], Object > function = getFunction (targets [i ], method );
420+ if (function != null )
421+ return function ;
422+ }
423+
424+ BiFunction <Object , String [], Object > function = getFunction (this , method );
425+ return function ;
426+ }
427+
408428 private String doCommand (Object target , String method , String [] args ) {
409429 if (target == null )
410430 ; // System.err.println("Huh? Target should never be null " +
@@ -422,43 +442,7 @@ private String doCommand(Object target, String method, String[] args) {
422442 }
423443 }
424444
425- Map <String , BiFunction <Object , String [], Object >> macros = macrosByClass .computeIfAbsent (target .getClass (),
426- c -> Arrays .stream (c .getMethods ())
427- .filter (m -> (m .getName ()
428- .charAt (0 ) == '_' ) && (m .getParameterCount () == 1 )
429- && (m .getParameterTypes ()[0 ] == String [].class ))
430- .collect (toMap (m -> m .getName ()
431- .substring (1 ), m -> {
432- Memoize <MethodHandle > mh = Memoize .supplier (() -> {
433- try {
434- return publicLookup ().unreflect (m );
435- } catch (Exception e ) {
436- throw Exceptions .duck (e );
437- }
438- });
439- if (Modifier .isStatic (m .getModifiers ())) {
440- return (Object t , String [] a ) -> {
441- try {
442- return mh .get ()
443- .invoke (a );
444- } catch (Throwable e ) {
445- throw Exceptions .duck (e );
446- }
447- };
448- } else {
449- return (Object t , String [] a ) -> {
450- try {
451- return mh .get ()
452- .invoke (t , a );
453- } catch (Throwable e ) {
454- throw Exceptions .duck (e );
455- }
456- };
457- }
458- })));
459-
460- String macro = method .replace ('-' , '_' );
461- BiFunction <Object , String [], Object > invoker = macros .get (macro );
445+ BiFunction <Object , String [], Object > invoker = getFunction (target , method );
462446 if (invoker == null ) {
463447 return null ;
464448 }
@@ -481,6 +465,46 @@ private String doCommand(Object target, String method, String[] args) {
481465 return null ;
482466 }
483467
468+ BiFunction <Object , String [], Object > getFunction (Object target , String method ) {
469+ Map <String , BiFunction <Object , String [], Object >> macros = macrosByClass .computeIfAbsent (target .getClass (),
470+ c -> Arrays .stream (c .getMethods ())
471+ .filter (m -> (m .getName ()
472+ .charAt (0 ) == '_' ) && (m .getParameterCount () == 1 ) && (m .getParameterTypes ()[0 ] == String [].class ))
473+ .collect (toMap (m -> m .getName ()
474+ .substring (1 ), m -> {
475+ Memoize <MethodHandle > mh = Memoize .supplier (() -> {
476+ try {
477+ return publicLookup ().unreflect (m );
478+ } catch (Exception e ) {
479+ throw Exceptions .duck (e );
480+ }
481+ });
482+ if (Modifier .isStatic (m .getModifiers ())) {
483+ return (Object t , String [] a ) -> {
484+ try {
485+ return mh .get ()
486+ .invoke (a );
487+ } catch (Throwable e ) {
488+ throw Exceptions .duck (e );
489+ }
490+ };
491+ } else {
492+ return (Object t , String [] a ) -> {
493+ try {
494+ return mh .get ()
495+ .invoke (t , a );
496+ } catch (Throwable e ) {
497+ throw Exceptions .duck (e );
498+ }
499+ };
500+ }
501+ })));
502+
503+ String macro = method .replace ('-' , '_' );
504+ BiFunction <Object , String [], Object > invoker = macros .get (macro );
505+ return invoker ;
506+ }
507+
484508 /**
485509 * Return a unique list where the duplicates are removed.
486510 */
@@ -1458,7 +1482,7 @@ public Properties getFlattenedProperties(boolean ignoreInstructions) {
14581482 }
14591483 }
14601484
1461- static final String _osfileHelp = "${osfile;<base>;<path>}, create correct OS dependent path" ;
1485+ static final String _osfileHelp = "${osfile;<base>;<path>}, create correct OS dependent path" ;
14621486
14631487 public String _osfile (String [] args ) {
14641488 verifyCommand (args , _osfileHelp , null , 3 , 3 );
0 commit comments