Skip to content

Commit

Permalink
Shared ambiguity instance
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Apr 19, 2024
1 parent 7ae38a2 commit 4a9d42c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
19 changes: 4 additions & 15 deletions src/main/java/org/apache/ibatis/session/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,7 @@ protected static class StrictMap<V> extends ConcurrentHashMap<String, V> {
private static final long serialVersionUID = -4950446264854982944L;
private final String name;
private BiFunction<V, V, String> conflictMessageProducer;
private static final Object AMBIGUITY_INSTANCE = new Object();

public StrictMap(String name, int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
Expand Down Expand Up @@ -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);
Expand All @@ -1176,25 +1177,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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
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.ResultMap;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.parsing.PropertyParser;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
Expand Down Expand Up @@ -79,4 +84,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.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)");
}

}

0 comments on commit 4a9d42c

Please sign in to comment.