Skip to content

Commit e7ffc73

Browse files
committed
Merge branch 'shared-ambiguity' of github.com:nieqiurong/mybatis-3 into pr/3146
2 parents 95e067b + 4a9d42c commit e7ffc73

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ protected static class StrictMap<V> extends ConcurrentHashMap<String, V> {
11131113
private static final long serialVersionUID = -4950446264854982944L;
11141114
private final String name;
11151115
private BiFunction<V, V, String> conflictMessageProducer;
1116+
private static final Object AMBIGUITY_INSTANCE = new Object();
11161117

11171118
public StrictMap(String name, int initialCapacity, float loadFactor) {
11181119
super(initialCapacity, loadFactor);
@@ -1162,7 +1163,7 @@ public V put(String key, V value) {
11621163
if (super.get(shortKey) == null) {
11631164
super.put(shortKey, value);
11641165
} else {
1165-
super.put(shortKey, (V) new Ambiguity(shortKey));
1166+
super.put(shortKey, (V) AMBIGUITY_INSTANCE);
11661167
}
11671168
}
11681169
return super.put(key, value);
@@ -1183,25 +1184,13 @@ public V get(Object key) {
11831184
if (value == null) {
11841185
throw new IllegalArgumentException(name + " does not contain value for " + key);
11851186
}
1186-
if (value instanceof Ambiguity) {
1187-
throw new IllegalArgumentException(((Ambiguity) value).getSubject() + " is ambiguous in " + name
1187+
if (AMBIGUITY_INSTANCE == value) {
1188+
throw new IllegalArgumentException(key + " is ambiguous in " + name
11881189
+ " (try using the full name including the namespace, or rename one of the entries)");
11891190
}
11901191
return value;
11911192
}
11921193

1193-
protected static class Ambiguity {
1194-
private final String subject;
1195-
1196-
public Ambiguity(String subject) {
1197-
this.subject = subject;
1198-
}
1199-
1200-
public String getSubject() {
1201-
return subject;
1202-
}
1203-
}
1204-
12051194
private String getShortName(String key) {
12061195
final String[] keyParts = key.split("\\.");
12071196
return keyParts[keyParts.length - 1];

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

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

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

23+
import org.apache.ibatis.builder.StaticSqlSource;
2224
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
2325
import org.apache.ibatis.io.Resources;
26+
import org.apache.ibatis.mapping.MappedStatement;
27+
import org.apache.ibatis.mapping.ResultMap;
28+
import org.apache.ibatis.mapping.SqlCommandType;
2429
import org.apache.ibatis.parsing.PropertyParser;
2530
import org.apache.ibatis.session.Configuration;
2631
import org.apache.ibatis.session.SqlSessionFactory;
@@ -79,4 +84,27 @@ void applyPropertyValueOnXmlConfiguration() throws IOException {
7984

8085
}
8186

87+
@Test
88+
void testAmbiguityCache() {
89+
Configuration configuration = new Configuration();
90+
configuration.addMappedStatement(
91+
new MappedStatement.Builder(configuration,
92+
"org.apache.ibatis.submitted.DemoMapper1.selectById",
93+
new StaticSqlSource(configuration, "select * from test where id = 1"), SqlCommandType.SELECT).build()
94+
);
95+
configuration.addMappedStatement(
96+
new MappedStatement.Builder(configuration,
97+
"org.apache.ibatis.submitted.DemoMapper1.test",
98+
new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build()
99+
);
100+
configuration.addMappedStatement(
101+
new MappedStatement.Builder(configuration,
102+
"org.apache.ibatis.submitted.DemoMapper2.test",
103+
new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build()
104+
);
105+
Assertions.assertThat(configuration.getMappedStatement("selectById")).isNotNull();
106+
Assertions.assertThatThrownBy(() -> configuration.getMappedStatement("test"))
107+
.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)");
108+
}
109+
82110
}

0 commit comments

Comments
 (0)