How to add multiple values to CtAnnotation in inria Spoon

184 views Asked by At

I am using Spoon 8.2.0 and have tried with Spoon 8.3.0 beta 10 as well: In the code below I try to add more than one value to an annotation, but it does not work as expected. I have a Java annotation Summary in Summary.java:

package tryspoon;

public @interface Summary {
    String title();
    String date();
}

In Spooner Spooner.java

package tryspoon;

import spoon.Launcher;
import spoon.SpoonAPI;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.reference.CtTypeReference;

public class Spooner {
    public static void main(String[] args) {
        SpoonAPI spoon = new Launcher();

        CtTypeReference<Summary> typeRef = spoon.getFactory().createCtTypeReference(Summary.class);
        CtAnnotation<Summary> annotation = spoon.getFactory().createAnnotation(typeRef);

        annotation.addValue("title", "First summary");
        annotation.addValue("date", "2020-10-05");

        // This outputs @tryspoon.Summary(date = "2020-10-05")
        System.out.println(annotation);

        // This outputs {date="2020-10-05", title=null}
        System.out.println(annotation.getAllValues());
    }
}

Swapping the order of annotation.addValue("title", "First Summary") and annotation.addValue("date", "2020-10-05") will make it only the title key and value be displayed in the annotation, and the date key being null in the map from getAllValues().

0

There are 0 answers