diff --git a/src/main/java/org/apache/ibatis/session/Configuration.java b/src/main/java/org/apache/ibatis/session/Configuration.java index dfc8dd0218c..8234250a6ed 100644 --- a/src/main/java/org/apache/ibatis/session/Configuration.java +++ b/src/main/java/org/apache/ibatis/session/Configuration.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2024 the original author or authors. + * Copyright 2009-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,20 +15,6 @@ */ package org.apache.ibatis.session; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.locks.ReentrantLock; -import java.util.function.BiFunction; - import org.apache.ibatis.binding.MapperRegistry; import org.apache.ibatis.builder.CacheRefResolver; import org.apache.ibatis.builder.IncompleteElementException; @@ -97,6 +83,20 @@ import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandlerRegistry; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.BiFunction; + /** * @author Clinton Begin */ @@ -1113,6 +1113,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); @@ -1162,7 +1163,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); @@ -1183,25 +1184,13 @@ 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; } - protected static class Ambiguity { - private final String subject; - - public Ambiguity(String subject) { - this.subject = subject; - } - - public String getSubject() { - return subject; - } - } - private String getShortName(String key) { final String[] keyParts = key.split("\\."); return keyParts[keyParts.length - 1]; 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..1550979147c 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 @@ -1,5 +1,5 @@ /* - * Copyright 2009-2023 the original author or authors. + * Copyright 2009-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,11 @@ */ package org.apache.ibatis.submitted.global_variables_defaults; -import java.io.IOException; -import java.io.Reader; -import java.util.Properties; - +import org.apache.ibatis.builder.StaticSqlSource; import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; import org.apache.ibatis.io.Resources; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.parsing.PropertyParser; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; @@ -29,6 +28,10 @@ import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; +import java.io.IOException; +import java.io.Reader; +import java.util.Properties; + class ConfigurationTest { @Test @@ -79,4 +82,27 @@ void applyPropertyValueOnXmlConfiguration() throws IOException { } + @Test + void testAmbiguityCache() { + Configuration configuration = new Configuration(); + + configuration.addMappedStatement( + new MappedStatement.Builder(configuration, "org.apache.ibatis.submitted.DemoMapper1.selectById", + new StaticSqlSource(configuration, "select * from test where id = 1"), SqlCommandType.SELECT).build()); + configuration + .addMappedStatement(new MappedStatement.Builder(configuration, "org.apache.ibatis.submitted.DemoMapper1.test", + new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build()); + configuration + .addMappedStatement(new MappedStatement.Builder(configuration, "org.apache.ibatis.submitted.DemoMapper2.test", + new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build()); + + Assertions.assertThat(configuration.getMappedStatement("selectById")).isNotNull(); + Assertions.assertThat(configuration.getMappedStatement("org.apache.ibatis.submitted.DemoMapper1.test")).isNotNull(); + Assertions.assertThat(configuration.getMappedStatement("org.apache.ibatis.submitted.DemoMapper2.test")).isNotNull(); + + Assertions.assertThatThrownBy(() -> configuration.getMappedStatement("test")) + .isInstanceOf(IllegalArgumentException.class).hasMessage( + "test is ambiguous in Mapped Statements collection (try using the full name including the namespace, or rename one of the entries)"); + } + }