90
90
*/
91
91
public class Macro {
92
92
private final static String NULLVALUE = "c29e43048791e250dfd5723e7b8aa048df802c9262cfa8fbc4475b2e392a8ad2" ;
93
- private final static String LITERALVALUE = "017a3ddbfc0fcd27bcdb2590cdb713a379ae59ef" ;
93
+ protected final static String LITERALVALUE = "017a3ddbfc0fcd27bcdb2590cdb713a379ae59ef" ;
94
94
private final static Pattern NUMERIC_P = Pattern
95
95
.compile ("[-+]?(\\ d*\\ .?\\ d+|\\ d+\\ .)(e[-+]?[0-9]+)?" );
96
96
@@ -342,7 +342,7 @@ public String replace(String key, Link link) {
342
342
return replace (key , null , link , '{' , '}' );
343
343
}
344
344
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 ) {
346
346
String value = getMacro (key , args , link , begin , end );
347
347
if (value != LITERALVALUE ) {
348
348
if (value != null )
@@ -405,6 +405,26 @@ private String doCommands(String[] args, Link source) {
405
405
return doCommand (this , args [0 ], args );
406
406
}
407
407
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
+
408
428
private String doCommand (Object target , String method , String [] args ) {
409
429
if (target == null )
410
430
; // System.err.println("Huh? Target should never be null " +
@@ -422,43 +442,7 @@ private String doCommand(Object target, String method, String[] args) {
422
442
}
423
443
}
424
444
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 );
462
446
if (invoker == null ) {
463
447
return null ;
464
448
}
@@ -481,6 +465,46 @@ private String doCommand(Object target, String method, String[] args) {
481
465
return null ;
482
466
}
483
467
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
+
484
508
/**
485
509
* Return a unique list where the duplicates are removed.
486
510
*/
@@ -1458,7 +1482,7 @@ public Properties getFlattenedProperties(boolean ignoreInstructions) {
1458
1482
}
1459
1483
}
1460
1484
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" ;
1462
1486
1463
1487
public String _osfile (String [] args ) {
1464
1488
verifyCommand (args , _osfileHelp , null , 3 , 3 );
0 commit comments