Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:18:57+00:00 2026-06-13T21:18:57+00:00

I want to validate my arguments with Java Annotation. I don’t know how to

  • 0

I want to validate my arguments with Java Annotation. I don’t know how to use write a own Annotation for Lists.

Here an simple example:

class test{

    @myAnnotation
    List<myObject> myElements =new List<>(); // validated List
}

class myObject{

        String name;
 }

my Annotation Interface:

  @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD}) 
  @Retention(RetentionPolicy.RUNTIME)
  @Constraint(validatedBy=myAnnotation_Validator.class)
  @Documented


public @interface myAnnotation {
          String message() default "List is not valid";
          Class <?>[] groups() default {};
          Class <?extends Payload>[] payload() default{};

          String[] namen();
    }

public class myAnnotation_Validator implements ConstraintValidator<myAnnotation, Collection> {

    @Override
    public void initialize(Kredite_List_Check a) {
        // What to do here???
    }


    @Override
    public boolean isValid(Collection t, ConstraintValidatorContext cvc) {
        // What to do here???
        // How could i get the names from my List "myElements" ?
        return false;
    }

}

In this Example my List is valid, if each element from my List has another name. I don’t know how I get the listobject in my Validator class and the names of myObject-elements.

UPDATE:

I try to describe my question in another way:

my list is not valid if two elements from type “myObject” in my list (“myElements”) have the same name!

How could I realize this with Annotations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:18:58+00:00Added an answer on June 13, 2026 at 9:18 pm
    public class myAnnotation_Validator implements ConstraintValidator<myAnnotation, Collection> {
         private String[] names;
    
         @Override
         public void initialize(myAnnotation a) {
            //get values which are defined in the annotation
            names = myAnnotation.namen();
         } 
    
         @Override
         public boolean isValid(Collection objectToValidate, ConstraintValidatorContext cvc) {
    
            if(objectToValidate == null) return true; // use the @NotNull annotation for null checks 
    
            for(Object o : objectToValidate) {
               //check if value is valid
            }
    
            return false; 
        } 
    }
    

    In the initialize method you can get the values, which are defined in the annotation.
    The isValid method is used to validate the object (objectToValidate -> your list object).

    For more information on how to write a custom validator see http://docs.jboss.org/hibernate/validator/4.3/reference/en-US/html/validator-customconstraints.html#validator-customconstraints-validator

    Also the Hibernate-Validator implementation is a good reference.
    https://github.com/hibernate/hibernate-validator/tree/master/engine/src/main/java/org/hibernate/validator/internal/constraintvalidators

    I hope this answer helps you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use the System.ComponentModel.DataAnnotations assembly to validate arguments (mapped to properties) for
I have a FluentValidation validator that I want to use to validate a booking.
I have a very generic validator and I want to pass it arguments. Here
I want to write a function validate() which will take some pattern or regular
I want to validate a phone number input. The phone numbers should be mix
I want to validate mathematical expressions using regular expression. The mathematical expression can be
I want to validate a form fields (text) input. It should only be a
I want to validate my email address through jquery unobtrusive validation . Like to
I want to validate if the field really exist in the table in the
I want to validate string only if contains numerics and/or 'h' or 'H' character

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.