Reflection in .NET vs JAVAs ServiceLoader (code snippet) Friday, January 9, 2009


That's an opinion, not a complete article discussing reflection




I'm diving into JAVA lately ,well it's tasty so far !

If you are a .NETer you can perform reflection simply using the following code:


Assembly asm = Assembly.LoadFrom(@"c:\myapp\plugins\myAssembly.dll");
MyInterface IMy = null;
foreach (Type t in asm.GetTypes()) {
if (t.GetInterface("MyInterface") != null) {
IMy = (MyInterface)Activator.CreateInstance(t);
break;
}
}





JAVA's approach :


ServiceLoader<MyInterface> myImps= ServiceLoader.load(MyInterface.class);
for (Myinterface myImp: myImps) {
System.out.println(" * " + myImp.getName());
}

where you interface found in META-INF


JAVA's reflection looks straight-forward. If you're recently code using C# i guarantee you a quick start on JAVA and if you are always confused with dramastic changes every C# release have please start learning JAVA !.