This is an automated email from the ASF dual-hosted git repository. rec pushed a commit to branch bugfix/UIMA-6390-NPE-when-trying-to-access-config-names-of-fresh-context in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git
commit 112c5d24c5a041526fed32164e5d74a6b09524cb Author: Richard Eckart de Castilho <r...@apache.org> AuthorDate: Fri Oct 22 11:22:11 2021 +0200 [UIMA-6390] NPE when trying to access config names of fresh context - Fix NPE in both versions of the getConfigParameterNames() method - Added test --- .../org/apache/uima/impl/UimaContext_ImplBase.java | 41 +++++++++++++--------- .../apache/uima/resource/ConfigurationManager.java | 2 +- .../org/apache/uima/impl/UimaContext_implTest.java | 10 ++++++ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java b/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java index dd3be78..e8fdee8 100644 --- a/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java +++ b/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java @@ -572,17 +572,19 @@ public abstract class UimaContext_ImplBase implements UimaContextAdmin { */ @Override public String[] getConfigParameterNames() { - ConfigurationParameter[] params = getConfigurationManager().getConfigParameterDeclarations( - getQualifiedContextName()).getConfigurationParameters(); - if (params == null) { + ConfigurationParameterDeclarations paramDecls = getConfigurationManager() + .getConfigParameterDeclarations(getQualifiedContextName()); + + if (paramDecls == null) { return Constants.EMPTY_STRING_ARRAY; - } else { - String[] names = new String[params.length]; - for (int i = 0; i < params.length; i++) { - names[i] = params[i].getName(); } - return names; + + ConfigurationParameter[] params = paramDecls.getConfigurationParameters(); + if (params == null || params.length == 0) { + return Constants.EMPTY_STRING_ARRAY; } + + return Arrays.stream(params).map(ConfigurationParameter::getName).toArray(String[]::new); } /* @@ -592,29 +594,34 @@ public abstract class UimaContext_ImplBase implements UimaContextAdmin { */ @Override public String[] getConfigParameterNames(String aGroup) { - ConfigurationGroup[] groups = getConfigurationManager().getConfigParameterDeclarations( - getQualifiedContextName()).getConfigurationGroupDeclarations(aGroup); + ConfigurationParameterDeclarations paramDecls = getConfigurationManager() + .getConfigParameterDeclarations(getQualifiedContextName()); + + if (paramDecls == null) { + return Constants.EMPTY_STRING_ARRAY; + } + + ConfigurationGroup[] groups = paramDecls.getConfigurationGroupDeclarations(aGroup); if (groups.length == 0) { return Constants.EMPTY_STRING_ARRAY; - } else { + } + List<String> names = new ArrayList<>(); - ConfigurationParameter[] commonParams = getConfigurationManager() - .getConfigParameterDeclarations(getQualifiedContextName()).getCommonParameters(); + ConfigurationParameter[] commonParams = paramDecls.getCommonParameters(); if (commonParams != null) { for (int i = 0; i < commonParams.length; i++) { names.add(commonParams[i].getName()); } } + for (int i = 0; i < groups.length; i++) { ConfigurationParameter[] groupParams = groups[i].getConfigurationParameters(); for (int j = 0; j < groupParams.length; j++) { names.add(groupParams[j].getName()); } } - String[] nameArray = new String[names.size()]; - names.toArray(nameArray); - return nameArray; - } + + return names.toArray(new String[names.size()]); } /** diff --git a/uimaj-core/src/main/java/org/apache/uima/resource/ConfigurationManager.java b/uimaj-core/src/main/java/org/apache/uima/resource/ConfigurationManager.java index d9a5fbf..83eebf7 100644 --- a/uimaj-core/src/main/java/org/apache/uima/resource/ConfigurationManager.java +++ b/uimaj-core/src/main/java/org/apache/uima/resource/ConfigurationManager.java @@ -144,7 +144,7 @@ public interface ConfigurationManager { * @param aContextName * the name for which to get the parameter declarations * - * @return parameter declarations for the context + * @return parameter declarations for the context or {@code null} if there are no declarations. */ public ConfigurationParameterDeclarations getConfigParameterDeclarations(String aContextName); diff --git a/uimaj-core/src/test/java/org/apache/uima/impl/UimaContext_implTest.java b/uimaj-core/src/test/java/org/apache/uima/impl/UimaContext_implTest.java index 8824d40..a384983 100644 --- a/uimaj-core/src/test/java/org/apache/uima/impl/UimaContext_implTest.java +++ b/uimaj-core/src/test/java/org/apache/uima/impl/UimaContext_implTest.java @@ -19,6 +19,8 @@ package org.apache.uima.impl; +import static org.assertj.core.api.Assertions.assertThat; + import java.io.InputStream; import java.net.URI; import java.net.URL; @@ -298,6 +300,14 @@ public class UimaContext_implTest extends TestCase { Assert.assertEquals("GrouplessParam2", names[1]); } + public void testGetConfigParameterNamesWorksWhenNoParametersHaveBeenDeclared() { + UimaContext emptyContext = UIMAFramework.newUimaContext(UIMAFramework.getLogger(), + UIMAFramework.newDefaultResourceManager(), UIMAFramework.newConfigurationManager()); + + assertThat(emptyContext.getConfigParameterNames()).isEmpty(); + assertThat(emptyContext.getConfigParameterNames("blah")).isEmpty(); + } + public void testGetConfigParameterNamesString() { String[] names = mContext2.getConfigParameterNames("en"); Assert.assertEquals(4, names.length);