diff --git a/src/main/java/org/apache/ibatis/session/Configuration.java b/src/main/java/org/apache/ibatis/session/Configuration.java index 72b50d3808f..18c0b926d31 100644 --- a/src/main/java/org/apache/ibatis/session/Configuration.java +++ b/src/main/java/org/apache/ibatis/session/Configuration.java @@ -1106,6 +1106,7 @@ protected static class StrictMap extends ConcurrentHashMap { private static final long serialVersionUID = -4950446264854982944L; private final String name; private BiFunction conflictMessageProducer; + private static final Object AMBIGUITY_INSTANCE = new Object(); public StrictMap(String name, int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor); @@ -1155,7 +1156,7 @@ public V put(String key, V value) { if (super.get(shortKey) == null) { super.put(shortKey, value); } else { - super.put(shortKey, (V) new Ambiguity(shortKey)); + super.put(shortKey, (V) AMBIGUITY_INSTANCE); } } return super.put(key, value); @@ -1176,14 +1177,21 @@ public V get(Object key) { if (value == null) { throw new IllegalArgumentException(name + " does not contain value for " + key); } - if (value instanceof Ambiguity) { - throw new IllegalArgumentException(((Ambiguity) value).getSubject() + " is ambiguous in " + name + if (AMBIGUITY_INSTANCE == value) { + throw new IllegalArgumentException(key + " is ambiguous in " + name + " (try using the full name including the namespace, or rename one of the entries)"); } return value; } + /** + * remove class code + * + * @deprecated 3.5.17 + */ + @Deprecated protected static class Ambiguity { + private final String subject; public Ambiguity(String subject) { diff --git a/src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java b/src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java index 6f940466f5d..ec0474cded1 100644 --- a/src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java +++ b/src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java @@ -17,10 +17,12 @@ import java.io.IOException; import java.io.Reader; +import java.util.ArrayList; import java.util.Properties; import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; import org.apache.ibatis.io.Resources; +import org.apache.ibatis.mapping.ResultMap; import org.apache.ibatis.parsing.PropertyParser; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; @@ -79,4 +81,28 @@ void applyPropertyValueOnXmlConfiguration() throws IOException { } + @Test + void testAmbiguityCache() { + Configuration configuration = new Configuration(); + configuration.addResultMap( + new ResultMap.Builder(configuration, + "org.apache.ibatis.submitted.DemoMapper1.insert", + Object.class, new ArrayList<>()).build() + ); + configuration.addResultMap( + new ResultMap.Builder(configuration, + "org.apache.ibatis.submitted.DemoMapper1.test", + Object.class, new ArrayList<>()).build() + ); + configuration.addResultMap( + new ResultMap.Builder(configuration, + "org.apache.ibatis.submitted.DemoMapper2.test", + Object.class, new ArrayList<>()).build() + ); + Assertions.assertThat(configuration.getResultMap("insert")).isNotNull(); + Assertions.assertThatThrownBy(() -> configuration.getResultMap("test")) + .isInstanceOf(IllegalArgumentException.class).hasMessage("test is ambiguous in Result Maps collection " + + "(try using the full name including the namespace, or rename one of the entries)"); + } + }