Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen and how are annotations initialized in Java? Can I control this?
    primarykey
    data
    text
    <p>I have specified a custom annotation for use in Java bean-validation. I use the same annotation class for all of my fields and pass a string parameter with the name of the field, like this:</p> <pre><code>@CustomConstraint(value="name") private String name; @CustomConstraint(value="address1") private String addressLine1; </code></pre> <p>My constraint validator maintains a HashMap that maps String keys (name of field) to an Interface for my validation classes. I statically initialize this map by pairing particular field names with concrete subclasses of the interface that will be used to validate the field, like this:</p> <pre><code>private static final Map&lt;String, CustomValidatorClass&gt; fieldMapper; static { Map&lt;String, CustomValidatorClass&gt; map = new HashMap&lt;String, CustomValidatorClass&gt;(); map.put("name", new NameValidator()); map.put("address1", new Address1Validator()); fieldMapper = Collections.unmodifiableMap(map); } </code></pre> <p>My concern is that this initialization process could be quite expensive (I have dozens of fields), and that it may be initialized for every single field which I am validating. I am hoping that there is some way to specify that the annotation should be initialized once and reused, or that this is the default behavior. </p> <p>EDIT: I am using JSF 2.0 along with bean-validation, so I am not directly reading or calling these annotations. That is handled by the framework, which is why I could not determine exactly how it was operating.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload