Skip to content
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
4 changes: 3 additions & 1 deletion src/main/java/net/datafaker/providers/base/BaseFaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.function.Predicate;
import java.util.function.Supplier;

import static java.util.Collections.synchronizedMap;

/**
* Provides utility methods for generating fake strings, such as names, phone
* numbers, addresses. generate random strings with given patterns
Expand All @@ -32,7 +34,7 @@ public class BaseFaker implements BaseProviders {
private static final Predicate<Class<?>> EVERY_PROVIDER_ALLOWED = t -> true;
private final FakerContext context;
private final FakeValuesService fakeValuesService;
private final Map<Class<?>, AbstractProvider<?>> providersCache = new IdentityHashMap<>();
private final Map<Class<?>, AbstractProvider<?>> providersCache = synchronizedMap(new IdentityHashMap<>());
private final Predicate<Class<?>> whiteListPredicate;

public BaseFaker() {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/datafaker/providers/base/ObjectMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import java.util.Set;
import java.util.stream.Stream;

import static java.util.Collections.synchronizedMap;
import static java.util.stream.Collectors.toMap;

public class ObjectMethods {
private static final Map<Class<?>, Map<String, Method>> METHODS_BY_NAME = new IdentityHashMap<>();
private static final Map<Class<?>, Map<String, Method>> METHODS_BY_RETURN_TYPE = new IdentityHashMap<>();
private static final Map<Class<?>, Map<String, Method>> METHODS_BY_NAME = synchronizedMap(new IdentityHashMap<>());
private static final Map<Class<?>, Map<String, Method>> METHODS_BY_RETURN_TYPE = synchronizedMap(new IdentityHashMap<>());
private static final Set<String> IGNORED_METHODS = Set.of("equals", "hashCode", "toString", "Builder", "stream");

private static synchronized Map<String, Method> scanMethodsByName(Class<?> clazz) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/datafaker/service/FakeValuesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.synchronizedMap;
import static java.util.Locale.ROOT;
import static java.util.Objects.requireNonNull;
import static java.util.logging.Level.FINE;
Expand Down Expand Up @@ -1147,7 +1148,7 @@ private Object[] coerceArguments(Method accessor, String[] args) {
return coerced;
}

private static final Map<Class<?>, Class<?>> PRIMITIVE_WRAPPER_MAP = new IdentityHashMap<>();
private static final Map<Class<?>, Class<?>> PRIMITIVE_WRAPPER_MAP = synchronizedMap(new IdentityHashMap<>());
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PRIMITIVE_WRAPPER_MAP is populated once in the static initializer and then only read. Wrapping it in Collections.synchronizedMap(...) adds unnecessary synchronization overhead; consider making it immutable instead (e.g., build the IdentityHashMap in the static block and then wrap with Collections.unmodifiableMap, or replace with Map.ofEntries if identity semantics aren’t required).

Copilot uses AI. Check for mistakes.

static {
PRIMITIVE_WRAPPER_MAP.put(Boolean.TYPE, Boolean.class);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/datafaker/service/FakerContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Objects;
import java.util.regex.Pattern;

import static java.util.Collections.synchronizedMap;
import static java.util.Locale.ROOT;
import static net.datafaker.service.FakeValuesService.DEFAULT_LOCALE;

Expand All @@ -18,8 +19,8 @@
*/
public class FakerContext {
private static final Pattern LOCALE = Pattern.compile("[-_]");
private static final Map<SingletonLocale, List<SingletonLocale>> LOCALE_2_LOCALES_CHAIN = new IdentityHashMap<>();
private static final Map<SingletonLocale, SingletonLocale> STRING_LOCALE_HASH_MAP = new IdentityHashMap<>();
private static final Map<SingletonLocale, List<SingletonLocale>> LOCALE_2_LOCALES_CHAIN = synchronizedMap(new IdentityHashMap<>());
private static final Map<SingletonLocale, SingletonLocale> STRING_LOCALE_HASH_MAP = synchronizedMap(new IdentityHashMap<>());
Comment on lines +22 to +23
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOCALE_2_LOCALES_CHAIN / STRING_LOCALE_HASH_MAP are now Collections.synchronizedMap(...), but the class still uses synchronized (FakerContext.class) around some accesses. Since synchronizedMap uses the map instance as its mutex, those blocks don’t synchronize with other map operations and won’t make compound actions (e.g., containsKey+put) atomic. Consider either synchronizing on the map for compound operations, or using computeIfAbsent so the get+populate path is atomic under the map’s lock.

Suggested change
private static final Map<SingletonLocale, List<SingletonLocale>> LOCALE_2_LOCALES_CHAIN = synchronizedMap(new IdentityHashMap<>());
private static final Map<SingletonLocale, SingletonLocale> STRING_LOCALE_HASH_MAP = synchronizedMap(new IdentityHashMap<>());
private static final Map<SingletonLocale, List<SingletonLocale>> LOCALE_2_LOCALES_CHAIN = new IdentityHashMap<>();
private static final Map<SingletonLocale, SingletonLocale> STRING_LOCALE_HASH_MAP = new IdentityHashMap<>();

Copilot uses AI. Check for mistakes.
public static final List<SingletonLocale> DEFAULT_SINGLETON_LOCALE_LIST = List.of(DEFAULT_LOCALE);
private static final Map<String, String> LANGUAGE_DEFAULT_COUNTRY = Map.ofEntries(
Map.entry("be", "BY"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Collections.synchronizedMap;

public class JavaObjectTransformer implements Transformer<Object, Object> {
private static final Map<Schema<Object, ?>, Consumer<Object>> SCHEMA2CONSUMER = new IdentityHashMap<>();
private static final Map<Class<?>, Constructor<?>> CLASS2CONSTRUCTOR = new IdentityHashMap<>();
private static final Map<Schema<Object, ?>, Consumer<Object>> SCHEMA2CONSUMER = synchronizedMap(new IdentityHashMap<>());
private static final Map<Class<?>, Constructor<?>> CLASS2CONSTRUCTOR = synchronizedMap(new IdentityHashMap<>());

private Optional<Object> sourceClazz = Optional.empty();

Expand Down
Loading