X

Geertjan's Blog

  • November 4, 2008

@org.openide.util.lookup.ServiceProvider

Geertjan Wielenga
Product Manager
Read issue 150447 and then look at the code below:
public interface DemoInterface {
public String getName();
}
@org.openide.util.lookup.ServiceProvider(service=DemoInterface.class)
public class DemoServiceProvider1 implements DemoInterface {
public String getName() {
return "John";
}
}
public class Installer extends ModuleInstall {
@Override
public void restored() {
for (DemoInterface di : Lookup.getDefault().lookupAll(DemoInterface.class)) {
System.out.println("Hello " + di.getName());
}
}
}

The above works using a build I picked up today. No more META-INF/services (unless you want to continue using that approach). I especially like Jarda's comment in that issue: "This is how our future should look like". Would be nice to work with Actions and so on via annotations and to increasingly forget the layer.xml file. Seems extremely likely that will be increasingly possible. That would be the biggest revolution for the NetBeans Platform since its creation.

Join the discussion

Comments ( 7 )
  • Tom Wheeler Tuesday, November 4, 2008

    You might find Jesse's original proposal interesting:

    http://wiki.netbeans.org/DeclarativeRegistrationUsingAnnotations

    In particular, see the part about actions towards the bottom.


  • Aljoscha Rittner Wednesday, November 5, 2008

    But it could be a performance problem. The manifest-files an the layer.xml files are well known files in the JARs.

    For annotations you need to scan all the classes to detect the annotation @org.openide.util.lookup.ServiceProvider

    The boot performance of modules is a very critical section. With annotations we need a additional register cache or we can wait a long time for booting netbeans.

    best regards,

    josh.


  • Thomas Wuerthinger Wednesday, November 5, 2008

    Is this for Nb 7.0 or will it find it's way into the 6.5 release? I think this feature is pretty cool, especially if all of Jesse's proposal is implemented...


  • Geertjan Wednesday, November 5, 2008

    Definitely not in 6.5, Thomas.


  • Casper Wednesday, November 5, 2008

    Not usually a fan of having even more ways to do the same thing, but if this will be the de-facto then good.

    What happens if no service is assigned explicitly in the annotation, will it be able to just pick up on the interface by reflective means?

    Also, it starts to look a lot like next hurdle will be versioning of services. Unless we just use naive embedding in the interface name like DemoInterface14.class etc.


  • Sven Reimers Wednesday, November 5, 2008

    @Aljoscha: If I got it right all is done during module build making use of the annotation processor - generating the necessary META-INF/service files and a layer file.


  • guest Wednesday, November 5, 2008

    Yes, I've got some information from Bruce Chapman. I've made a deeper look into the diff patch :-)

    The @Service-annotation is a "syntactic sugar" to simplify the service registration at build time.

    josh.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha