8287834: Add SymbolLookup::or method

Reviewed-by: psandoz
This commit is contained in:
Maurizio Cimadamore 2023-05-22 14:51:09 +00:00
parent 4f88437b7f
commit 91aeb5de58
2 changed files with 167 additions and 0 deletions

View file

@ -129,6 +129,28 @@ public interface SymbolLookup {
*/
Optional<MemorySegment> find(String name);
/**
* {@return a composed symbol lookup that returns result of finding the symbol with this lookup if found,
* otherwise returns the result of finding the symbol with the other lookup}
*
* @apiNote This method could be used to chain multiple symbol lookups together, e.g. so that symbols could
* be retrieved, in order, from multiple libraries:
* {@snippet lang = java:
* var lookup = SymbolLookup.libraryLookup("foo", arena)
* .or(SymbolLookup.libraryLookup("bar", arena))
* .or(SymbolLookup.loaderLookup());
*}
* The above code creates a symbol lookup that first searches for symbols in the "foo" library. If no symbol is found
* in "foo" then "bar" is searched. Finally, if a symbol is not found in neither "foo" nor "bar", the {@linkplain
* SymbolLookup#loaderLookup() loader lookup} is used.
*
* @param other the symbol lookup that should be used to look for symbols not found in this lookup.
*/
default SymbolLookup or(SymbolLookup other) {
Objects.requireNonNull(other);
return name -> find(name).or(() -> other.find(name));
}
/**
* Returns a symbol lookup for symbols in the libraries associated with the caller's class loader.
* <p>