Skip to content

Commit 4a9d42c

Browse files
committed
Shared ambiguity instance
1 parent 7ae38a2 commit 4a9d42c

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
@@ -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,25 +1177,13 @@ 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

1186-
protected static class Ambiguity {
1187-
private final String subject;
1188-
1189-
public Ambiguity(String subject) {
1190-
this.subject = subject;
1191-
}
1192-
1193-
public String getSubject() {
1194-
return subject;
1195-
}
1196-
}
1197-
11981187
private String getShortName(String key) {
11991188
final String[] keyParts = key.split("\\.");
12001189
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)