Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a method to get the Macro references from a Processor #6360

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions biz.aQute.bndlib.tests/test/test/ProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -23,6 +24,7 @@
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.OSInformation;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.Processor.MacroReference;
import aQute.bnd.osgi.Processor.PropertyKey;
import aQute.bnd.osgi.resource.RequirementBuilder;
import aQute.bnd.osgi.resource.ResourceBuilder;
Expand All @@ -36,6 +38,86 @@

public class ProcessorTest {

@Test
void testMacroReferences() throws IOException {
testMacroReference("""
c
""", """
a ${b} ${c} ${now}
""", MacroReference.ALL, "b", "c", "now");
testMacroReference("""
c
""", """
a ${b} ${c} ${now}
""", MacroReference.UNKNOWN, "b");
testMacroReference("""
c
""", """
a ${b} ${c} ${now}
""", MacroReference.COMMAND, "now");
testMacroReference("""
c
""", """
a ${b} ${c} ${now}
""", MacroReference.EXISTS, "c");
}

@Test
void testMacroReferencesWithArgs() throws IOException {
testMacroReference("""
c
""", """
a ${foo;${bar;${baz}}}
""", MacroReference.UNKNOWN, "foo", "bar", "baz");
}

@Test
void testMacroReferencesCommandWithArgs() throws IOException {
testMacroReference("""
c
""", """
a ${uniq;${bar};${baz};x}
""", MacroReference.UNKNOWN, "bar", "baz");
testMacroReference("""
c
""", """
a ${uniq;${bar};${baz};x,${c}}
""", MacroReference.EXISTS, "c");
testMacroReference("""
c
""", """
a ${uniq;${bar};${baz};x,${c}}
""", MacroReference.COMMAND, "uniq");
}

@Test
void testMacroReferencesDef() throws IOException {
testMacroReference("""
c
""", """
a ${def;foo;bar}
""", MacroReference.UNKNOWN, "foo");
testMacroReference("""
c
""", """
a ${def;c;bar}
""", MacroReference.UNKNOWN);
testMacroReference("""
c
""", """
a ${template;foo;bar}
""", MacroReference.UNKNOWN, "foo");
}

private void testMacroReference(String parentSource, String childSource, Processor.MacroReference macro,
String... references) throws IOException {
try (Processor parent = new Processor(); Processor child = new Processor(parent)) {
parent.setProperties(new StringReader(parentSource));
child.setProperties(new StringReader(childSource));
assertThat(child.getMacroReferences(macro)).containsExactly(references);
}
}

@Test
public void testFixup() throws IOException {
try (Processor p = new Processor()) {
Expand Down
104 changes: 64 additions & 40 deletions biz.aQute.bndlib/src/aQute/bnd/osgi/Macro.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
*/
public class Macro {
private final static String NULLVALUE = "c29e43048791e250dfd5723e7b8aa048df802c9262cfa8fbc4475b2e392a8ad2";
private final static String LITERALVALUE = "017a3ddbfc0fcd27bcdb2590cdb713a379ae59ef";
protected final static String LITERALVALUE = "017a3ddbfc0fcd27bcdb2590cdb713a379ae59ef";
private final static Pattern NUMERIC_P = Pattern
.compile("[-+]?(\\d*\\.?\\d+|\\d+\\.)(e[-+]?[0-9]+)?");

Expand Down Expand Up @@ -342,7 +342,7 @@ public String replace(String key, Link link) {
return replace(key, null, link, '{', '}');
}

private String replace(String key, List<String> args, Link link, char begin, char end) {
protected String replace(String key, List<String> args, Link link, char begin, char end) {
String value = getMacro(key, args, link, begin, end);
if (value != LITERALVALUE) {
if (value != null)
Expand Down Expand Up @@ -405,6 +405,26 @@ private String doCommands(String[] args, Link source) {
return doCommand(this, args[0], args);
}

protected BiFunction<Object, String[], Object> getFunction(String method) {
Processor rover = domain;
while (rover != null) {
BiFunction<Object, String[], Object> function = getFunction(rover, method);
if (function != null)
return function;

rover = rover.getParent();
}

for (int i = 0; targets != null && i < targets.length; i++) {
BiFunction<Object, String[], Object> function = getFunction(targets[i], method);
if (function != null)
return function;
}

BiFunction<Object, String[], Object> function = getFunction(this, method);
return function;
}

private String doCommand(Object target, String method, String[] args) {
if (target == null)
; // System.err.println("Huh? Target should never be null " +
Expand All @@ -422,43 +442,7 @@ private String doCommand(Object target, String method, String[] args) {
}
}

Map<String, BiFunction<Object, String[], Object>> macros = macrosByClass.computeIfAbsent(target.getClass(),
c -> Arrays.stream(c.getMethods())
.filter(m -> (m.getName()
.charAt(0) == '_') && (m.getParameterCount() == 1)
&& (m.getParameterTypes()[0] == String[].class))
.collect(toMap(m -> m.getName()
.substring(1), m -> {
Memoize<MethodHandle> mh = Memoize.supplier(() -> {
try {
return publicLookup().unreflect(m);
} catch (Exception e) {
throw Exceptions.duck(e);
}
});
if (Modifier.isStatic(m.getModifiers())) {
return (Object t, String[] a) -> {
try {
return mh.get()
.invoke(a);
} catch (Throwable e) {
throw Exceptions.duck(e);
}
};
} else {
return (Object t, String[] a) -> {
try {
return mh.get()
.invoke(t, a);
} catch (Throwable e) {
throw Exceptions.duck(e);
}
};
}
})));

String macro = method.replace('-', '_');
BiFunction<Object, String[], Object> invoker = macros.get(macro);
BiFunction<Object, String[], Object> invoker = getFunction(target, method);
if (invoker == null) {
return null;
}
Expand All @@ -481,6 +465,46 @@ private String doCommand(Object target, String method, String[] args) {
return null;
}

BiFunction<Object, String[], Object> getFunction(Object target, String method) {
Map<String, BiFunction<Object, String[], Object>> macros = macrosByClass.computeIfAbsent(target.getClass(),
c -> Arrays.stream(c.getMethods())
.filter(m -> (m.getName()
.charAt(0) == '_') && (m.getParameterCount() == 1) && (m.getParameterTypes()[0] == String[].class))
.collect(toMap(m -> m.getName()
.substring(1), m -> {
Memoize<MethodHandle> mh = Memoize.supplier(() -> {
try {
return publicLookup().unreflect(m);
} catch (Exception e) {
throw Exceptions.duck(e);
}
});
if (Modifier.isStatic(m.getModifiers())) {
return (Object t, String[] a) -> {
try {
return mh.get()
.invoke(a);
} catch (Throwable e) {
throw Exceptions.duck(e);
}
};
} else {
return (Object t, String[] a) -> {
try {
return mh.get()
.invoke(t, a);
} catch (Throwable e) {
throw Exceptions.duck(e);
}
};
}
})));

String macro = method.replace('-', '_');
BiFunction<Object, String[], Object> invoker = macros.get(macro);
return invoker;
}

/**
* Return a unique list where the duplicates are removed.
*/
Expand Down Expand Up @@ -1458,7 +1482,7 @@ public Properties getFlattenedProperties(boolean ignoreInstructions) {
}
}

static final String _osfileHelp = "${osfile;<base>;<path>}, create correct OS dependent path";
static final String _osfileHelp = "${osfile;<base>;<path>}, create correct OS dependent path";

public String _osfile(String[] args) {
verifyCommand(args, _osfileHelp, null, 3, 3);
Expand Down
Loading