From fa994b241401d69b95aff2859890f7a6c5b6b502 Mon Sep 17 00:00:00 2001 From: Ruud Senden <8635138+rsenden@users.noreply.github.com> Date: Fri, 16 Dec 2022 10:06:14 +0100 Subject: [PATCH] Implement review comments for #1876 --- .../AbstractCommandSpecProcessor.java | 2 +- src/main/java/picocli/CommandLine.java | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/picocli-codegen/src/main/java/picocli/codegen/annotation/processing/AbstractCommandSpecProcessor.java b/picocli-codegen/src/main/java/picocli/codegen/annotation/processing/AbstractCommandSpecProcessor.java index 741462bf6..1be570e4e 100644 --- a/picocli-codegen/src/main/java/picocli/codegen/annotation/processing/AbstractCommandSpecProcessor.java +++ b/picocli-codegen/src/main/java/picocli/codegen/annotation/processing/AbstractCommandSpecProcessor.java @@ -180,7 +180,7 @@ private static String stacktrace(Exception e) { } private boolean tryProcess(Set annotations, RoundEnvironment roundEnv) { - Model.Messages.loadBundles = false; + Model.Messages.setLoadBundles(false); new AnnotationValidator(processingEnv).validateAnnotations(roundEnv); Context context = new Context(); diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java index f4b302339..2e8a24877 100644 --- a/src/main/java/picocli/CommandLine.java +++ b/src/main/java/picocli/CommandLine.java @@ -11599,7 +11599,7 @@ public T getExtension(Class cls) { * @see CommandSpec#qualifiedName(String) * @since 3.6 */ public static class Messages { - public static boolean loadBundles = true; + private static boolean loadBundles = true; private final CommandSpec spec; private final String bundleBaseName; private final ResourceBundle rb; @@ -11621,7 +11621,7 @@ public Messages(CommandSpec spec, String baseName, ResourceBundle rb) { } } private static ResourceBundle createBundle(String baseName) { - if ( loadBundles ) { + if (loadBundles) { return ResourceBundle.getBundle(baseName); } else { return new ResourceBundle() { @@ -11631,8 +11631,7 @@ protected Object handleGetObject(String key) { } @Override public Enumeration getKeys() { - return new Vector().elements(); - } + return Collections.emptyEnumeration(); } }; } } @@ -11662,6 +11661,18 @@ private static Set keys(ResourceBundle rb) { for (Enumeration k = rb.getKeys(); k.hasMoreElements(); keys.add(k.nextElement())); return keys; } + + /** + * During annotation processing, resource bundles may not be available on the + * classpath and thereby cause failures. This method allows for disabling + * loading of resource bundles during annotation processing, preventing such + * errors. + * @since 4.7.1 + * @param loadBundles true if bundles should be loaded (default), false if bundles should not be loaded + */ + public static final void setLoadBundles(boolean loadBundles) { + Messages.loadBundles = loadBundles; + } /** Returns a copy of the specified Messages object with the CommandSpec replaced by the specified one. * @param spec the CommandSpec of the returned Messages