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 18, 2024
1 parent 7ae38a2 commit b16b4d1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
14 changes: 11 additions & 3 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,14 +1177,21 @@ 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;
}

/**
* remove class code
*
* @deprecated 3.5.17
*/
@Deprecated
protected static class Ambiguity {

private final String subject;

public Ambiguity(String subject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.parsing.PropertyParser;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
Expand Down Expand Up @@ -79,4 +81,28 @@ void applyPropertyValueOnXmlConfiguration() throws IOException {

}

@Test
void testAmbiguityCache() {
Configuration configuration = new Configuration();
configuration.addResultMap(
new ResultMap.Builder(configuration,
"org.apache.ibatis.submitted.DemoMapper1.insert",
Object.class, new ArrayList<>()).build()
);
configuration.addResultMap(
new ResultMap.Builder(configuration,
"org.apache.ibatis.submitted.DemoMapper1.test",
Object.class, new ArrayList<>()).build()
);
configuration.addResultMap(
new ResultMap.Builder(configuration,
"org.apache.ibatis.submitted.DemoMapper2.test",
Object.class, new ArrayList<>()).build()
);
Assertions.assertThat(configuration.getResultMap("insert")).isNotNull();
Assertions.assertThatThrownBy(() -> configuration.getResultMap("test"))
.isInstanceOf(IllegalArgumentException.class).hasMessage("test is ambiguous in Result Maps collection " +
"(try using the full name including the namespace, or rename one of the entries)");
}

}

0 comments on commit b16b4d1

Please sign in to comment.