Runtime retention helps dependency-injection

53 views
Skip to first unread message

limpb...@gmail.com

unread,
Aug 27, 2007, 1:43:53 PM8/27/07
to JSR-305: Annotations for Software Defect Detection

Guice is a dependency injection framework that uses
annotations to mark injection points. For example:
public class Foo {
@Inject
public Foo(Bar bar, Baz baz) { ... }
}

Currently Guice doesn't allow null to be injected.
We like this. It helps to prevent bugs.

But there are cases where injecting null is convenient.
We'd like to reuse the JSR-305 @Nullable annotation to
indicate that null is okay:
public class Foo {
@Inject
public Foo(Bar bar, @Nullable Baz baz) { ... }
}

For this to work, we need runtime retention for @Nullable.
Otherwise we'd have to scrape the annotation from the
classfile, or introduce another annotation that like
'@InjectNullOk'. It would be unfortunate to have to annotate
one variable with both "@Nullable" and "@InjectNullOk".

Although this example is Guice-specific it applies to other
dependency-injection frameworks and containers.

Thanks,
Jesse

Previous discussions on runtime retention:
http://groups.google.com/group/jsr-305/browse_thread/thread/499a5a31727f14dc
http://groups.google.com/group/jsr-305/browse_thread/thread/99b833628adb25e1

Guice:
http://code.google.com/p/google-guice/

Reply all
Reply to author
Forward
0 new messages