Is it possible to set properties of annotation programmatically. So if I have :
#interface Author(
String name();
Date date ();
)
I want to be able to set the name property inside the code as if it is a regular class. Is this possible?
You can't use Date in an annotation
To access it at runtime, you need to add the #Retention(RetentionPolicy.RUNTIME) retention policy
The declaration for annotations uses {} for the annotation definition, not ()
No, you can't modify annotations during runtime using normal Java functionality
You may do this using a bytecode manipulator, like Javassist. But that is is not trivial.
Why do you want to do this during runtime? Possibly there are other better solutions to your problem.
Related
I am using Spring and Java for an application, and I want to use the #Value annotation to inject the value of the property, my use case is that I want first to check if that property exists as system property (so it takes priority), and otherwise default to a configuration property (existing in the properties file)
In the commented code you can see what I am trying to achieve, is that possible to default to something else that a simple string? If it is not, how can I achieve this?
//#Value("#{configProperties['local.datasource.username']}") THIS IS THE ORIGINAL CODE
//#Value("#{systemProperties['some.key'] ?: 'my default system property value'}") THIS IS HOW SPRING PROPOSE TO SET A DEFAULT VALUE
//#Value("#{systemProperties['some.key'] ?: #{configProperties['local.datasource.username']}}") THIS IS WHAT I WANT TO ACHIEVE, HOWEVER NOT COMPILING,
private String username;
What you are looking for are simple Property Palceholders.
While the Spring Expression Language supports the #{} syntax for rather complex expressions (ternary, calls, expressions, math), injecting property values and defaults is in most cases better done using a simple property placeholder ${value:defaultValue} approach:
#Property("${some.key:local.datasource.username}")
private String username;
Where some.key is being resolved (independent of its origin), and if that is null, Spring defaults to the value of local.datasource.username.
Please keep in mind, that even if some.key is present, Spring will throw an exception when it can't resolve your default property.
See also:
Spring Expression Language (SpEL) with #Value: dollar vs. hash ($ vs. #) and
A Quick Guide to Spring #Value
I am using Immutables-value for defining my POJO. And when it generates the Immutable* class, it has the #Generated annotation at the top. Is there any way I could disable it?
I checked in their codebase:
https://github.com/immutables/immutables/blob/master/value-annotations/src/org/immutables/value/Generated.java#L22-L27
It is mentioned here that it can be disabled by :
Style#allowedClasspathAnnotations()
I used it on top of POJO interface like this:
#Value.Style(allowedClasspathAnnotations = {org.immutables.value.Generated.class})
But still I am getting the #Generated annotation on top of my generated class. Any idea how can I do this?
#Style's allowedClasspathAnnotations attribute whitelists the annotations you included there (Rather than blacklisting them). See here.
So if you want to only disable org.immutables.value.Generated you should instead do something like:
#Value.Style(allowedClasspathAnnotations = {
javax.annotation.concurrent.Immutable,
javax.annotation.ParametersAreNonnullByDefault,
javax.annotation.CheckReturnValue,
edu.umd.cs.findbugs.annotations.SuppressFBWarnings,
com.google.errorprone.annotations.Var,
com.google.errorprone.annotations.Immutable
})
To whitelist the annotations you want to keep.
The conventional way of java validation is to define a class with properties and annotated with one or many validation constraint annotations such as #NotNull or #Future.
Is it possible to validate an object directly with validator constraint annotation without specifying a class. For example:
String a = "";
validator.validate(a, javax.validation.constraints.NotNull.class);
That's clearly not a use case for which Hibernate Validator has been designed.
What you would have to do is to create a ConstraintValidator from the constraint and the type of the object and then use the isValid() method. The entry point would be ConstraintValidatorManager.
It's an internal class so it's subject to change without warning, even in a micro version so I wouldn't recommend using it.
In java tutorials - Annotations part, question 3 (https://docs.oracle.com/javase/tutorial/java/annotations/QandE/questions.html), an annotation are expected to be used as below:
#Meal("breakfast", mainDish="cereal")
I tried to define the annotation as below but it does not allow the above usage.
public #interface Meal {
String value();
String mainDish();
}
Is it possible to omit the first attribute name as the question suggested?
No, the shortcut only works if you specify the value attribute and nothing else.
Otherwise you must explicitly write value=, that is the correct version would be #Meal(value = "breakfast", mainDish = "cereal")
I'm using JAX-WS api for wsdl generation.
Java-bean class is something like:
public class MyBean {
private String nullableField;
private String notNullableField;
// and here appropriate get/set/ters
}
When wsdl is generated then nullability of this fields is not specified.
Question: what (and where) necessary to specify that fields have corresponding nillable='' value in wsdl? I.e. how can I specify fields nullability in plain java code for wsdl?
At this time I'm generating wsdl and then correcting xml manually for fields nullability. That's not convenient. I want this nillable attribute'll be generated by java-ws automatically.
Any suggestions?
Thanks.
AFAIK, it is still not possible to generate nillable=false when using #WebParam i.e. when using a Java-first approach (as discussed in this thread). Actually, I'd recommend to use a WSDL-first approach if you want fine control.