Results 1 to 6 of 6
  1. #1
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default Annotation definition with multiple parameters

    There is an annotation definition:

    Java Code:
    @Target({ElementType.METHOD, ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface AuthRequired {
        enum AuthType {
            Server(null),
            Student(UserType.Student),
            Teacher(UserType.Teacher),
            SchoolMaster(UserType.SchoolMaster),
            Operator(UserType.Operator);
    
            private UserType userType;
    
            AuthType(UserType userType) {
                this.userType = userType;
            }
    
            public UserType getUserType() {
                return userType;
            }
        }
    
        AuthType[] value() default {};
    
        boolean optional() default false;
    
    }
    and its usage:

    Java Code:
       @AuthRequired(AuthRequired.AuthType.Server)
        @RequestMapping(method = RequestMethod.GET, value = "/isUserSlaveOf")
        boolean isUserSlaveOf(
                @RequestParam String slaveUserIds,
          ...
          ...
    there are two parameters in the definition, value and optional.
    but in the usage, there is only one paramet, so I have two questions:
    1) if there are less parameters in usage than in definition, and the parameter name is absent, how to determine which one is used?
    2) in the above code, value is an array type, but AuthRequired.AuthType.Server is an element, how can they match?

    Thanks!

  2. #2
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default Re: Annotation definition with multiple parameters

    does anyone know?

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Annotation definition with multiple parameters

    I don't. But 6 hours isn't sufficient time for a bump. Maybe within a couple of days or a week.

    Regards,
    JIm
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Annotation definition with multiple parameters

    Well, "optional" has a default value (actually they both do), so you don't have to supply it.
    And the compiler is clever enough to know that an AuthType is not a boolean, so the supplied AuthType is for the "value" parameter.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default Re: Annotation definition with multiple parameters

    how about question 2) in the above code, value is an array type, but AuthRequired.AuthType.Server is an element, how can they match?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Annotation definition with multiple parameters

    It's a short cut.
    A single element passed into the annotation can be interpreted by the compiler as a single element array.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 03-06-2014, 10:46 AM
  2. query with multiple IN parameters
    By learnjava32 in forum JDBC
    Replies: 3
    Last Post: 10-09-2012, 04:44 PM
  3. Replies: 4
    Last Post: 03-24-2011, 11:50 PM
  4. Replies: 5
    Last Post: 11-21-2010, 02:08 PM
  5. Replies: 4
    Last Post: 10-01-2010, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •