Returning the result of a JPA native query as a simple Map or POJO

July 14, 2016 Leave a comment

I’ve spent quite some time on StackOverflow to answer one simple question: Can I make a JPA NativeQuery return the results as a Map or as a simple POJO? The common consensus looks like it’s not possible. This is unfortunate, but sometimes when people say something cannot be done, I get fired up. And I’ve found a solution, even though it involves a little Hibernate-Vendor-lock-in and a detour.
Read more…

Categories: JPA Tags: , , , , ,

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

June 27, 2016 Leave a comment

So you have an annotation like this:

@Target({ METHOD, TYPE, FIELD })
public @interface Logged {

	String value() default "debug";

	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 // annotation with default log level
private String defaultAnnotationDetailLogLevel() {
	class Local {};
	final Method meth = Local.class.getEnclosingMethod(); // this method
	final Logged annotation = meth.getAnnotation(Logged.class);
	return annotation.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!

Functional Java – partial function application, the LOD and structure agnostic access

March 21, 2016 Leave a comment

It’s been a while since I last wrote something about programming on my private blog and since a lot has been said about Java 8 lambdas, I did not feel to add a lot more. But I recently saw a colleague’s blog on Intel’s Inside Blue about Lambdas and why a functional programming style matters. That motivated me to write something again, and tackle this topic, putting an emphasis on a specific problem and how Lambdas helped me solve it.

Read more…

Multiple floating toolbars with Swing

March 18, 2013 1 comment

These are my holidays, I should be sitting at the beach, not coding. Unfortunately I can’t; and since when I am bored I do things I like, coding is still one good way to kill some time. So I was restarting an old pet project of mine, some kind of editor and I pretty early ran into a problem: The Java gods have brought us floating toolbars that can be docked to the sides of a BorderLayout. Unfortunately the BorderLayout is the only layout they work with und unfortunately a BorderLayout can just hold one component in each of its border regions, so you can’t have more than one JToolbar on each side, which unfortunately renders the floating functionality pretty useless by default. They might have thought “YAGNI”, when the idea of multiple toolbars next to each other crossed their minds, I don’t know. Unfortunately “I am gonna need it”, but fortunately there is remedy. Read more…

Categories: Swing Tags: , ,

A properly sized JTable

February 16, 2013 2 comments

Task: Create a table with two columns. The columns should fit the size of the content. If the table’s width is smaller than the panel, make the last column fill the panel, if the table is wider than the panel, show horizontal scrollbars. Sounds so easy? How many of you have wasted a good amount of time making a table behave like this? The good thing is: it is easy. And here is how it works. Read more…

Categories: Swing Tags: , , , ,

Listing a directory on the class path when it’s in a Jar archive

January 27, 2013 Leave a comment

Today I was revisiting an old memory game that I wrote almost seven years ago and I must admit that I was partly horrified by the code I have written back in the days. So I started to change small parts of it. For example I was loading a set of images from a directory on the class path, by generating the name of the image, like “card1.jpg”, “card2.jpg” and getting these via getClass().getResource(name). Now, as I was changing some images, and files happened to be “.jpg” and others “.png”, my old name-generator code failed. Not a good solution, I thought, and planned to load all files from a directory that would just contain card images – didn’t work as expected though, but I found a solution on the net, which I had to change a bit to make it work for me. Here’s the result that I’d like to share. Read more…

Categories: File handling Tags: , , ,

Dynamic proxies for classes

December 22, 2012 1 comment

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…


Get every new post delivered to your Inbox.

Join 54 other followers