About Question enthuware.ocpjp.v11.2.3393 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Riccardomz
Posts: 4
Joined: Sun May 15, 2022 10:18 am
Contact:

About Question enthuware.ocpjp.v11.2.3393 :

Post by Riccardomz »

Dear Enthuware,

This question is clear but I still don't understand the answer and I can't find many documentation about this edge case of Repeatable container annotation.

In my opinion it could make sense either if the annotation container is present as follows:

Code: Select all

@Authors(value = {@Author("Bob"), @Author("Alice")} )
Or the way it should be with the repeated annotation:

Code: Select all

@Author("Bob")
@Author("Alice")
What I really don't understand is why is it legal to have a mix with both?

Code: Select all

@Authors(value={@Author("bob"), @Author("alice") }) 
@Author("Charlie")
void myMethod(){}
And also in this scenario if I add another @Author annotation it doesn't compile anymore, I really don't understand this behaviour.
In the last example the method myMethod how many @Author annotations has? bob, alice and Charlie? Only bob and alice? Only Charlie?

Thanks in advance for helping me understand this particular behaviour.
Best regards

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3393 :

Post by admin »

You can do this in either of the following two ways:

1. You can have one container annotation i.e. @Authors (which can contain one or more Author annotations) and 0 or 1 @Author annotation.
2. You can omit the container annotation altogether and have multiple repeatable annotations i.e. have multiple @Author annotations.

You cannot have the container annotation and more than one repeatable annotation ( i.e. @Authors + more than one @Author) because it is explicitly prohibited by the language designers. Section 9.7.5 of JLS says:
It is a compile-time error if, in a declaration context or type context, there are multiple annotations of a repeatable annotation type T and any annotations of the containing annotation type of T.
In your last example, myMethod has one Authors and one Author annotation. i.e. effectively, it has three @Author annotations - bob, charlie, and alice.

You can run the following test program to confirm:

Code: Select all

public class TestClass {
    @Authors(value={ @Author("bob"), @Author("charlie")}, team="java") 
    @Author("alice")       
    //@Author("dave")     //NOT ALLOWED

    void someMethod(){ }    
    
    public static void main(String[] args) throws Exception {
        Class c = annotation.TestClass.class;
        Method m = c.getDeclaredMethod("someMethod");
        Annotation[] aa = m.getDeclaredAnnotations();
        for(Annotation a :  aa)  System.out.println(a); //This will print two annotations - one Authors and one Author(alice)
        
        Authors containerOfAuthors = m.getDeclaredAnnotation(Authors.class);
        Author[] authors = containerOfAuthors.value(); //this has two author annotations - bob and charlie
        for(Author a : authors){
            System.out.println(a+" id : "+a.id()+" value : "+List.of(a.value()));
        }
    }
}
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 34 guests