Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8062630

Incorrect answer in Tutorial for Annotations

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: docs

      Description

      3th question in Annotation Tutorial "Questions and Exercises" section is:

      "Will the following code compile without error? Why or why not?

      public @interface Meal { ... }

      @Meal("breakfast", mainDish="cereal")
      @Meal("lunch", mainDish="pizza")
      @Meal("dinner", mainDish="salad")
      public void evaluateDiet() { ... }
      "
      http://docs.oracle.com/javase/tutorial/java/annotations/QandE/questions.html

      Answer for this question in "Answers to Questions and Exercises" section is:

      "The code fails to compile. Before JDK 8, repeatable annotations are not supported. As of JDK 8, the code fails to compile because the Meal annotation type was not defined to be repeatable. It can be fixed by adding the @Repeatable meta-annotation and defining a container annotation type:

      @java.lang.annotation.Repeatable(MealContainer.class)
      public @interface Meal { ... }

      public @interface MealContainer {
          Meal[] value();
      }
      "

      http://docs.oracle.com/javase/tutorial/java/annotations/QandE/answers.html

      Actually, it's still won't compile after suggested fix.
      Compilation error is:
      "annotation values must be of the form 'name=value'"

      The problem is elements of the annotation:
      @Meal("breakfast", mainDish="cereal")

      From the same tutorial:

      "If there is just one element named value, then the name can be omitted"

      For Meal annotation we have at least 2 elements, so element name can't be omitted even if it is "value".

      Suggested fix:

      Add element's name.
      @Meal(value="breakfast", mainDish="cereal")

      Tested with JDK 8u5

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            snikandrova Svetlana Nikandrova (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: