Skip to content

Commit b16b4d1

Browse files
committed
Shared ambiguity instance
1 parent 7ae38a2 commit b16b4d1

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,7 @@ protected static class StrictMap<V> extends ConcurrentHashMap<String, V> {
11061106
private static final long serialVersionUID = -4950446264854982944L;
11071107
private final String name;
11081108
private BiFunction<V, V, String> conflictMessageProducer;
1109+
private static final Object AMBIGUITY_INSTANCE = new Object();
11091110

11101111
public StrictMap(String name, int initialCapacity, float loadFactor) {
11111112
super(initialCapacity, loadFactor);
@@ -1155,7 +1156,7 @@ public V put(String key, V value) {
11551156
if (super.get(shortKey) == null) {
11561157
super.put(shortKey, value);
11571158
} else {
1158-
super.put(shortKey, (V) new Ambiguity(shortKey));
1159+
super.put(shortKey, (V) AMBIGUITY_INSTANCE);
11591160
}
11601161
}
11611162
return super.put(key, value);
@@ -1176,14 +1177,21 @@ public V get(Object key) {
11761177
if (value == null) {
11771178
throw new IllegalArgumentException(name + " does not contain value for " + key);
11781179
}
1179-
if (value instanceof Ambiguity) {
1180-
throw new IllegalArgumentException(((Ambiguity) value).getSubject() + " is ambiguous in " + name
1180+
if (AMBIGUITY_INSTANCE == value) {
1181+
throw new IllegalArgumentException(key + " is ambiguous in " + name
11811182
+ " (try using the full name including the namespace, or rename one of the entries)");
11821183
}
11831184
return value;
11841185
}
11851186

1187+
/**
1188+
* remove class code
1189+
*
1190+
* @deprecated 3.5.17
1191+
*/
1192+
@Deprecated
11861193
protected static class Ambiguity {
1194+
11871195
private final String subject;
11881196

11891197
public Ambiguity(String subject) {

src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
import java.io.IOException;
1919
import java.io.Reader;
20+
import java.util.ArrayList;
2021
import java.util.Properties;
2122

2223
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
2324
import org.apache.ibatis.io.Resources;
25+
import org.apache.ibatis.mapping.ResultMap;
2426
import org.apache.ibatis.parsing.PropertyParser;
2527
import org.apache.ibatis.session.Configuration;
2628
import org.apache.ibatis.session.SqlSessionFactory;
@@ -79,4 +81,28 @@ void applyPropertyValueOnXmlConfiguration() throws IOException {
7981

8082
}
8183

84+
@Test
85+
void testAmbiguityCache() {
86+
Configuration configuration = new Configuration();
87+
configuration.addResultMap(
88+
new ResultMap.Builder(configuration,
89+
"org.apache.ibatis.submitted.DemoMapper1.insert",
90+
Object.class, new ArrayList<>()).build()
91+
);
92+
configuration.addResultMap(
93+
new ResultMap.Builder(configuration,
94+
"org.apache.ibatis.submitted.DemoMapper1.test",
95+
Object.class, new ArrayList<>()).build()
96+
);
97+
configuration.addResultMap(
98+
new ResultMap.Builder(configuration,
99+
"org.apache.ibatis.submitted.DemoMapper2.test",
100+
Object.class, new ArrayList<>()).build()
101+
);
102+
Assertions.assertThat(configuration.getResultMap("insert")).isNotNull();
103+
Assertions.assertThatThrownBy(() -> configuration.getResultMap("test"))
104+
.isInstanceOf(IllegalArgumentException.class).hasMessage("test is ambiguous in Result Maps collection " +
105+
"(try using the full name including the namespace, or rename one of the entries)");
106+
}
107+
82108
}

0 commit comments

Comments
 (0)