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 32f8012af..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 @@ -4,6 +4,7 @@ import picocli.CommandLine.Command; import picocli.CommandLine.IFactory; import picocli.CommandLine.Mixin; +import picocli.CommandLine.Model; import picocli.CommandLine.Model.ArgGroupSpec; import picocli.CommandLine.Model.CommandSpec; import picocli.CommandLine.Model.IAnnotatedElement; @@ -179,7 +180,7 @@ private static String stacktrace(Exception e) { } private boolean tryProcess(Set annotations, RoundEnvironment roundEnv) { - + 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 d366723aa..2e8a24877 100644 --- a/src/main/java/picocli/CommandLine.java +++ b/src/main/java/picocli/CommandLine.java @@ -11599,6 +11599,7 @@ public T getExtension(Class cls) { * @see CommandSpec#qualifiedName(String) * @since 3.6 */ public static class Messages { + private static boolean loadBundles = true; private final CommandSpec spec; private final String bundleBaseName; private final ResourceBundle rb; @@ -11620,7 +11621,19 @@ public Messages(CommandSpec spec, String baseName, ResourceBundle rb) { } } private static ResourceBundle createBundle(String baseName) { - return ResourceBundle.getBundle(baseName); + if (loadBundles) { + return ResourceBundle.getBundle(baseName); + } else { + return new ResourceBundle() { + @Override + protected Object handleGetObject(String key) { + return null; + } + @Override + public Enumeration getKeys() { + return Collections.emptyEnumeration(); } + }; + } } private static String extractName(ResourceBundle rb) { try { // ResourceBundle.getBaseBundleName was introduced in Java 8 @@ -11648,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