Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't load resource bundles during annotation processing (fixes #1876) #1878

Merged
merged 3 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -179,7 +180,7 @@ private static String stacktrace(Exception e) {
}

private boolean tryProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

Model.Messages.setLoadBundles(false);
new AnnotationValidator(processingEnv).validateAnnotations(roundEnv);

Context context = new Context();
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -11599,6 +11599,7 @@ public <T> T getExtension(Class<T> cls) {
* @see CommandSpec#qualifiedName(String)
* @since 3.6 */
public static class Messages {
private static boolean loadBundles = true;
rsenden marked this conversation as resolved.
Show resolved Hide resolved
private final CommandSpec spec;
private final String bundleBaseName;
private final ResourceBundle rb;
Expand All @@ -11620,7 +11621,19 @@ public Messages(CommandSpec spec, String baseName, ResourceBundle rb) {
}
}
private static ResourceBundle createBundle(String baseName) {
return ResourceBundle.getBundle(baseName);
if (loadBundles) {
rsenden marked this conversation as resolved.
Show resolved Hide resolved
return ResourceBundle.getBundle(baseName);
} else {
return new ResourceBundle() {
@Override
protected Object handleGetObject(String key) {
return null;
}
@Override
public Enumeration<String> getKeys() {
return Collections.emptyEnumeration(); }
rsenden marked this conversation as resolved.
Show resolved Hide resolved
};
}
}
private static String extractName(ResourceBundle rb) {
try { // ResourceBundle.getBaseBundleName was introduced in Java 8
Expand Down Expand Up @@ -11648,6 +11661,18 @@ private static Set<String> keys(ResourceBundle rb) {
for (Enumeration<String> 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
Expand Down