Archive

Posts Tagged ‘Reflection’

Hack of the day: Get a method-annotation’s default value

June 27, 2016 Leave a comment

So you have an annotation like this:

@Inherited
@Documented
@InterceptorBinding
@Retention(RUNTIME)
@Target({ METHOD, TYPE, FIELD })
public @interface Logged {

	@Nonbinding
	String value() default "debug";

	@Nonbinding
	String detailValue() default "trace";

}

But the source code is not under your control, so you can’t refactor it to use a public constant. How will you get any annotation method’s default value?
You could do it like this:

private String badWay() {
	final Method meth = Logged.class.getMethod("detailValue");
	final Logged annotation = meth.getAnnotation(Logged.class);
	return Level.valueOf(annotation.value());
}

But then, if someone from the outside refactored it and renamed the method, your compiler won’t notice and it will break at runtime, which is bad.
Here’s a way to perform the task with some introspection, given the annotation can be used on methods:

Logged defaultLogged; //lazily initialized, as reflection is slow

@Logged // annotation with default log level
private String defaultAnnotationDetailLogLevel() {
        if(defaultLogged == null}{
            class Local {};
            final Method meth = Local.class.getEnclosingMethod(); // this method
            defaultLogged = meth.getAnnotation(Logged.class);
        }
        return defaultLogged.detailValue();
}

In case the annotation will be changed to use any other method name, your compiler will notice immediately and you can fix the broken code before it slips through testing and hits production. Nifty!

Dynamic proxies for classes

December 22, 2012 3 comments

If you have made your hobby a job, like I did, you have possibly seen your hobby die just like me. Because when you come home from work, you are likely to have seen enough code for a day. In such a situation a “be your own boss day” can be a delightful experience. Twice a year my company pays me a whole day to do whatever I like to do, given that I will report what I did. In the course of the past years, they say, the company has seen some interesting projects. I chose to devote some time to enhance my knowledge about the Java technology; more precisely I finally found the time to crack a nut that has been bugging me ever since I wrote the blog “Reflection without Strings”, more than two years ago: Dynamic Proxies for classes. Read more…

Into the Void: Refining Java beans with rare weapons

July 17, 2011 2 comments

Lately I’ve been experimenting with the Void class, because I asked myself what I could do with it that I haven’t yet thought about. I came across a use case that I’d like to share with you, because I think it is possible to beautify one’s code with it. In the process of slightly refactoring a Java bean in order to get a nicer, more refactoring-proof way of dealing with beans and reflection, I’m also using an inline-assignment in a way that you might already have come across, still I have hardly seen it anywhere in the wild, even though I find this particular use case for inline-assignments one of the best there is.
Read more…

Reflection without Strings

August 9, 2010 4 comments

Sometimes reflection is very handy, but using reflection often involves referencing methods by Strings, which can get you into major trouble when you are refactoring your code. If a method belongs to an interface though, there is salvation. But let me first tell you how it started and what I did about it. Read more…

Using annotations for validation

March 20, 2010 2 comments

There is one common case I often come across and that’s validation of parameters. There are different situations in which you like to validate parameters, the most common case is validation of method parameters, usually performed with guard-clauses and assertions. This is not what I’d like to write about in this blog. I may come across this later. This time it’s all about annotation driven validation on fields. Read more…