Skip to content

Commit 97e76ea

Browse files
Merge pull request #11254 from samvaity/check-npe
Use singleton pattern for rule loading to avoid NPE
2 parents 88eb406 + a6b6d4e commit 97e76ea

File tree

1 file changed

+22
-4
lines changed
  • PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-java-sdk/src/main/java/com/microsoft/azure/toolkit/intellij/java/sdk/utils

1 file changed

+22
-4
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-java-sdk/src/main/java/com/microsoft/azure/toolkit/intellij/java/sdk/utils/RuleConfigLoader.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,28 @@
2424
@Slf4j
2525
public class RuleConfigLoader implements ProjectActivity {
2626
private static final String CONFIG_FILE_PATH = "./ruleConfigs.json";
27-
private static RuleConfigLoader INSTANCE;
27+
private static volatile RuleConfigLoader INSTANCE = new RuleConfigLoader();
2828
private Map<String, RuleConfig> ruleConfigs;
29+
private volatile boolean initialized = false;
2930

3031
private RuleConfigLoader() {
3132
this.ruleConfigs = new HashMap<>();
3233
}
3334

35+
static {
36+
try {
37+
// Eagerly load configuration at class load time
38+
INSTANCE.initialize();
39+
} catch (Exception e) {
40+
// Never fail class loading; keep instance alive with empty configs
41+
log.warn("Failed to eagerly initialize RuleConfigLoader: " + e.getMessage(), e);
42+
}
43+
}
44+
3445
/**
3546
* Gets the singleton instance of RuleConfigLoader.
3647
*
37-
* @return The singleton instance of RuleConfigLoader.
48+
* @return The singleton instance of RuleConfigLoader (never null).
3849
*/
3950
public static RuleConfigLoader getInstance() {
4051
return INSTANCE;
@@ -56,12 +67,19 @@ public Map<String, RuleConfig> getRuleConfigs() {
5667
return Collections.unmodifiableMap(ruleConfigs);
5768
}
5869

59-
private void initialize(){
70+
private synchronized void initialize() {
71+
if (initialized) {
72+
return;
73+
}
6074
try {
75+
this.ruleConfigs.clear();
6176
this.ruleConfigs.putAll(this.loadRuleConfigurations());
62-
INSTANCE = this;
77+
// INSTANCE is already assigned in the static initializer; avoid reassigning it here
78+
initialized = true;
6379
} catch (IOException e) {
80+
// Keep INSTANCE non-null, but proceed with empty configs
6481
log.warn("Failed to initialize RuleConfigLoader: " + e.getMessage(), e);
82+
initialized = true;
6583
}
6684
}
6785

0 commit comments

Comments
 (0)