Skip to content

Commit e3c95f8

Browse files
authored
Merge pull request #6360 from pkriens/issue/6346-gradle-reference-to-project-get-macro-references
Added a method to get the Macro references from a Processor
2 parents dc30ab6 + aac9271 commit e3c95f8

File tree

3 files changed

+254
-44
lines changed

3 files changed

+254
-44
lines changed

biz.aQute.bndlib.tests/test/test/ProcessorTest.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.io.File;
1111
import java.io.IOException;
12+
import java.io.StringReader;
1213
import java.net.URI;
1314
import java.util.ArrayList;
1415
import java.util.Collection;
@@ -23,6 +24,7 @@
2324
import aQute.bnd.osgi.Constants;
2425
import aQute.bnd.osgi.OSInformation;
2526
import aQute.bnd.osgi.Processor;
27+
import aQute.bnd.osgi.Processor.MacroReference;
2628
import aQute.bnd.osgi.Processor.PropertyKey;
2729
import aQute.bnd.osgi.resource.RequirementBuilder;
2830
import aQute.bnd.osgi.resource.ResourceBuilder;
@@ -36,6 +38,86 @@
3638

3739
public class ProcessorTest {
3840

41+
@Test
42+
void testMacroReferences() throws IOException {
43+
testMacroReference("""
44+
c
45+
""", """
46+
a ${b} ${c} ${now}
47+
""", MacroReference.ALL, "b", "c", "now");
48+
testMacroReference("""
49+
c
50+
""", """
51+
a ${b} ${c} ${now}
52+
""", MacroReference.UNKNOWN, "b");
53+
testMacroReference("""
54+
c
55+
""", """
56+
a ${b} ${c} ${now}
57+
""", MacroReference.COMMAND, "now");
58+
testMacroReference("""
59+
c
60+
""", """
61+
a ${b} ${c} ${now}
62+
""", MacroReference.EXISTS, "c");
63+
}
64+
65+
@Test
66+
void testMacroReferencesWithArgs() throws IOException {
67+
testMacroReference("""
68+
c
69+
""", """
70+
a ${foo;${bar;${baz}}}
71+
""", MacroReference.UNKNOWN, "foo", "bar", "baz");
72+
}
73+
74+
@Test
75+
void testMacroReferencesCommandWithArgs() throws IOException {
76+
testMacroReference("""
77+
c
78+
""", """
79+
a ${uniq;${bar};${baz};x}
80+
""", MacroReference.UNKNOWN, "bar", "baz");
81+
testMacroReference("""
82+
c
83+
""", """
84+
a ${uniq;${bar};${baz};x,${c}}
85+
""", MacroReference.EXISTS, "c");
86+
testMacroReference("""
87+
c
88+
""", """
89+
a ${uniq;${bar};${baz};x,${c}}
90+
""", MacroReference.COMMAND, "uniq");
91+
}
92+
93+
@Test
94+
void testMacroReferencesDef() throws IOException {
95+
testMacroReference("""
96+
c
97+
""", """
98+
a ${def;foo;bar}
99+
""", MacroReference.UNKNOWN, "foo");
100+
testMacroReference("""
101+
c
102+
""", """
103+
a ${def;c;bar}
104+
""", MacroReference.UNKNOWN);
105+
testMacroReference("""
106+
c
107+
""", """
108+
a ${template;foo;bar}
109+
""", MacroReference.UNKNOWN, "foo");
110+
}
111+
112+
private void testMacroReference(String parentSource, String childSource, Processor.MacroReference macro,
113+
String... references) throws IOException {
114+
try (Processor parent = new Processor(); Processor child = new Processor(parent)) {
115+
parent.setProperties(new StringReader(parentSource));
116+
child.setProperties(new StringReader(childSource));
117+
assertThat(child.getMacroReferences(macro)).containsExactly(references);
118+
}
119+
}
120+
39121
@Test
40122
public void testFixup() throws IOException {
41123
try (Processor p = new Processor()) {

biz.aQute.bndlib/src/aQute/bnd/osgi/Macro.java

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
*/
9191
public 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

Comments
 (0)