Skip to content

Commit b331513

Browse files
authored
Merge pull request #31305 from mkouba/qute-message-annotation-optional
Qute type-safe messages - make the Message annotation optional
2 parents 245d062 + f67b69b commit b331513

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/MessageBundleProcessor.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,10 @@ private Map<String, String> getLocalizedFileKeyToTemplate(MessageBundleBuildItem
699699
} else {
700700
messageAnnotation = method.annotation(Names.MESSAGE);
701701
}
702+
if (messageAnnotation == null) {
703+
messageAnnotation = AnnotationInstance.builder(Names.MESSAGE).value(Message.DEFAULT_VALUE)
704+
.add("name", Message.DEFAULT_NAME).build();
705+
}
702706
return getMessageAnnotationValue(messageAnnotation) != null;
703707
})
704708
.map(MethodInfo::name)
@@ -760,7 +764,7 @@ private String adaptLine(String line) {
760764

761765
private boolean hasMessageBundleMethod(ClassInfo bundleInterface, String name) {
762766
for (MethodInfo method : bundleInterface.methods()) {
763-
if (method.name().equals(name) && method.hasAnnotation(Names.MESSAGE)) {
767+
if (method.name().equals(name)) {
764768
return true;
765769
}
766770
}
@@ -813,8 +817,8 @@ private String generateImplementation(MessageBundleBuildItem bundle, ClassInfo d
813817
for (MethodInfo method : methods) {
814818
if (!method.returnType().name().equals(DotNames.STRING)) {
815819
throw new MessageBundleException(
816-
String.format("A message bundle interface method must return java.lang.String on %s: %s",
817-
bundleInterface, method));
820+
String.format("A message bundle method must return java.lang.String: %s#%s",
821+
bundleInterface, method.name()));
818822
}
819823
LOGGER.debugf("Found message bundle method %s on %s", method, bundleInterface);
820824

@@ -834,9 +838,9 @@ private String generateImplementation(MessageBundleBuildItem bundle, ClassInfo d
834838
}
835839

836840
if (messageAnnotation == null) {
837-
throw new MessageBundleException(
838-
"A message bundle interface method must be annotated with @Message: " +
839-
bundleInterface.name() + "#" + method.name());
841+
LOGGER.debugf("@Message not declared on %s#%s - using the default key/value", bundleInterface, method);
842+
messageAnnotation = AnnotationInstance.builder(Names.MESSAGE).value(Message.DEFAULT_VALUE)
843+
.add("name", Message.DEFAULT_NAME).build();
840844
}
841845

842846
String key = getKey(method, messageAnnotation, defaultKeyValue);

extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/i18n/LocalizedFileBundleLocaleMergeTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public interface Messages {
6868
@Message("Nazdar!")
6969
String hello();
7070

71-
@Message
7271
String goodbye();
7372

7473
@Message("Greetings!")

extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/i18n/MessageBundleMethodMissingAnnotationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public interface MyBundle {
2929
@Message("Hello {name}")
3030
String hello(String name);
3131

32+
// this fails because there is no value specified in a localized file
3233
String notAllowed();
3334

3435
}

extensions/qute/runtime/src/main/java/io/quarkus/qute/i18n/Message.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,20 @@
77
import java.lang.annotation.Target;
88

99
/**
10-
* Identifies a message bundle method.
10+
* Identifies a message bundle method that represents a single message of the enclosing bundle.
1111
* <p>
12-
* Each method of a message bundle interface annotated with {@link MessageBundle} must be annotated with this annotation.
12+
* The value of the {@link #key()} can be used to reference a message in a template. It is possible to specify an explicit key
13+
* or a strategy to extract the default key. The default strategy is defined by the enclosing
14+
* {@link MessageBundle#defaultKey()}.
15+
* <p>
16+
* The {@link #value()} defines the template of a message. The method parameters can be used in this template. All the message
17+
* templates are validated at build time.
18+
* <p>
19+
* Note that any method declared on a message bundle interface is consireded a message bundle method. If not annotated with this
20+
* annotation then the defaulted values are used for the key and template.
21+
* <p>
22+
* All message bundle methods must return {@link String}. If a message bundle method does not return string then the build
23+
* fails.
1324
*
1425
* @see MessageBundle
1526
*/
@@ -47,8 +58,9 @@
4758
String UNDERSCORED_ELEMENT_NAME = "<<underscored element name>>";
4859

4960
/**
61+
* The key of a message.
5062
*
51-
* @return the key
63+
* @return the message key
5264
*/
5365
String key() default DEFAULT_NAME;
5466

0 commit comments

Comments
 (0)