suppose you have moduleA and moduleB. ModuleA defines an interface (for instance for a Service) and ModuleB has a concrete class that implements the interface (provides the service).
Now if the interface has a default method and you invoke it on the class in moduleB (from another module) is this invocation supposed to be performed inside moduleA or moduleB? Apparently it is from moduleA ... what's the rationale?
Example: suppose you have a code that does this:
InputStream is = this.getClass().getResourceAsStream(fullPath); if this code lies in the implementation of the service in moduleB the stream will be opened. But if the code lies in the default method in moduleA then when the service is invoked on moduleB you will need to have an "open" resource in moduleB (so it seems that the invocation thinks it is from "outside" moduleB).
would like to read about the reason for that.
thanks
editing my question with an example. Suppose you have in moduleA this code:
public interface PropertiesProvider { public default Properties get(String domain) { Class clazz =this.getClass(); System.out.println(" CLASS " +clazz); InputStream is = clazz.getResourceAsStream(domain) ; if (is != null) { Properties props = new Properties(); try { props.load(is); return props; } catch (IOException e) { //log } } return null; } }
and in moduleB
public class PropertiesProviderImpl implements PropertiesProvider {} if you invoke the service from ModuleA the call is traced to come from class PropertiesProviderImpl finds the resource but does not load it if it is not "opened"
if you copy the code into PropertiesProviderImpl the calls is traced to that same class finds the ressource and loads it even when it is not "opened"
So my question is: why the difference since the call comes from the same class? (the difference being that in one case the method is kind-of inherited from the default method in the interface)
getResourceAsStreamstart from the classpath related at the classloader of the class ofthis(in your case, who loaded that class), search in it, if it doesn't find the resource it fallback on the parent classloader and its classpath