Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared ambiguity instance #3146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)");
}

}