bind to @Value instead of @Named("value")

64 views
Skip to first unread message

Michael Heuer

unread,
Jun 19, 2012, 11:27:36 AM6/19/12
to roc...@googlegroups.com
Hello,

I'm interested in extending Rocoto to bind to annotation instances (@Value) instead of @Named("value") annotations.  A quick look through the source shows that I might be able to replace

ConfigurationModule.java, line 112

                bindConstant().annotatedWith( named( name ) ).to( value );

with

                bindConstant().annotatedWith( annotation( name ) ).to( value );

where annotation(String) would return an instance of an annotation via Class.forName of name.

Does that sound reasonable, or should I just use @Named like everyone else does?  :)

   michael

Simone Tripodi

unread,
Jun 19, 2012, 3:51:28 PM6/19/12
to roc...@googlegroups.com
Hi Michael,

as you already noticed, Rocoto ATM just supports @Named - if you would
like to hack it anyway, I suggest you to have a look at Guice's
Names#named() implementation[1]

So, what you have to define is:

- @Value annotation;
- ValueImpl implements Value
- the optional annotation( name ) sttaic method wich creates the
ValueImpl implementation

Doesit help on replying yout question?

Just let me know if you need more hints, I am happy to discuss about it!!!

All the best, have a nice day,
-Simo

[1] http://code.google.com/p/google-guice/source/browse/core/src/com/google/inject/name/NamedImpl.java

http://people.apache.org/~simonetripodi/
http://simonetripodi.livejournal.com/
http://twitter.com/simonetripodi
http://www.99soft.org/

Simone Tripodi

unread,
Jun 19, 2012, 4:05:57 PM6/19/12
to roc...@googlegroups.com
Hi again Michael,

> where annotation(String) would return an instance of an annotation via
> Class.forName of name.
>
> Does that sound reasonable, or should I just use @Named like everyone else
> does?  :)

I think I caught late what you meant, apologize :)

just define a (static) method:

public (static) <A extends Annotation> Class<A> annotation( String name );

or

public (static) <A extends Annotation> A annotation( String name );

to obtain the Annotation by the name - the annotatedWith( ) method
requires an annotation type or instance as argument.

Your approach is more than reasonable, @Named is IMHO just a quick
ready-to-use annotation.

Hope that helps!
All the best,
-Simo

Michael Heuer

unread,
Jul 3, 2012, 12:46:08 PM7/3/12
to roc...@googlegroups.com
Hello Simone,

We're just using something like

public abstract class MyConfigurationModule
extends org.nnsoft.guice.rocoto.configuration.ConfigurationModule {

protected final PropertyValueBindingBuilder bindProperty(final
String name) {
checkNotNull(name, "property name must not be null");
return new PropertyValueBindingBuilder() {
@Override
public void toValue(final String value) {
checkNotNull(value, "value for property '%s' must not
be null", name);

if (looksLikeAnnotationName(name)) {
bindConstant().annotatedWith(annotation(name)).to(value);
}
else {
bindConstant().annotatedWith(named(name)).to(value);
}
}
};
}

protected static final boolean looksLikeAnnotationName(final String name) {
return name.startsWith("com.mypackage") &&
Character.isUpperCase(name.charAt(name.lastIndexOf(".") + 1));
}

protected static final <A extends Annotation> Class<A>
annotation(final String annotationClassName) {
try {
return (Class<A>) Class.forName(annotationClassName);
}
catch (ClassNotFoundException e) {
throw new ProvisionException(format("could not create
annotation class '%s': %s", annotationClassName, e.getMessage()));
}
}
}


which is kind of hacky but works for our purposes. If such might be
included in Rocoto, it would be a good idea to make
looksLikeAnnotationName more robust, or alternatively try
annotation(name) and use named(name) on ClassNotFoundException.

I could send a pull request if you're interested.

michael

Simone Tripodi

unread,
Jul 3, 2012, 3:44:02 PM7/3/12
to roc...@googlegroups.com
Hi Michael!

it is really interesting, I would be much more than glad to include
such feature in Rocoto.

Many thanks in advance, all the best!!!
Reply all
Reply to author
Forward
0 new messages