Introducing AnnotationInjector

Let me introduce you to AnnotationInjector, a small library that allows to inject annotations on classes at runtime.
It began as sample code for the conference on annotations I presented at several JUGs, and was extracted later as a standalone library for general consumption.

What does it do ?

AnnotationInjector allows you to inject annotations on classes at runtime.
It works only on classes though, not on fields, methods, or parameters, due to some gory implementation details in the Class class on the Sun/Oracle JRE.

Let's see it in action :

// Pojo does not bear the MyAnnotation annotation
MyAnnotation existingAnnot = Pojo.class.getAnnotation(MyAnnotation.class);
assertNull(existingAnnot);
 
// Instanciate and inject a new MyAnnotation on Pojo
MyAnnotation injectedAnnot = new MyAnnotation() { ... };
AnnotationInjector injector = new AnnotationInjector(Pojo.class);
injector.injectAnnotation(injectedAnnot);
 
// Now Pojo bears our new annotation
existingAnnot = Pojo.class.getAnnotation(MyAnnotation.class);
assertNotNull(existingAnnot);
assertEquals(existingAnnot, injectedAnnot);
Lire la suite...

Java Quiz #43

FR :
Cela faisait longtemps que je n'avais pas publié de quiz ! L'inspiration vient à manquer, le temps aussi... Mais en voici un tout neuf, présenté en exclusivité lors de ma conférene au YAJUG, le JUG du Luxembourg.
Pouvez-vous repérer le problème de ce code (bien entendu, sans le compiler ni le coller dans votre éditeur préféré) ?

EN :
This quiz has been presented in exclusive to the YAJUG, the java User Group of Luxembourg.
Can you spot what is wrong with this code without compiling it or pasting it in your favorite IDE ?

public class DataSetCreator {
 
    public static void main(String[] args) throws IOException {
        new DataSetCreator().createDataSet(20, new FileOutputStream("test.dat"));
    }
 
    public void createDataSet(int dataSetSize, OutputStream os) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(os);
        Random rand = new Random(System.currentTimeMillis());
        for (int i = 0; i < dataSetSize; i++) {
            oos.writeObject(new Data(rand.nextInt(100)));
        }
        oos.close();
    }
 
    private class Data implements Serializable {
        int number;
        private Data(int number) {
            this.number = number;
        }
        public int getNumber() {
            return number;
        }
    }
 
}
Lire la suite...

Le Paris JUG fête son 3° anniversaire !

A moins que vous n'ayez passé le dernier mois en Egypte, ou que vous ayez été noyé sous un projet web 3.0 hypercritique de gestion de la machine à café, vous savez sans doute que le Paris JUG fête son 3° anniversaire à la fin du mois.

Hé oui, 3 ans ! C'est passé vite.
Je me rappelle, j'ai pris le train en route au cours de la première année, un peu par hasard, et je n'en suis plus jamais descendu. J'ai même fait un tour dans la loco, en passant de l'autre côté du micro (et sérieux, les gars, vous faites peur parfois :p).

Lire la suite...

Un petit tour au YaJUG

Oyez, oyez !

Je serai la semaine prochaine au YAJUG, pour animer ma conférence sur les annotations.
Hé oui, fini la France, je peux maintenant raconter des bêtises à dimension européenne :)

Ils ont même préparé une chouette affiche pour l'événement :)

yajug.jpg

Alors, si vous habitez dans le coin, je vous dis à la semaine prochaine !