diff --git a/impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java b/impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java index 1b4e3117167..38ab7bdfa24 100644 --- a/impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java +++ b/impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java @@ -35,7 +35,6 @@ import java.security.ProtectionDomain; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; @@ -277,10 +276,12 @@ private static String createCompoundProxyName(String contextId, Bean bean, Ty for (Class type : typeInfo.getInterfaces()) { interfaces.add(type.getSimpleName()); } - Collections.sort(interfaces); - for (final String iface : interfaces) { - name.append(iface); - name.append('$'); + // no need to sort the set, because we copied and already sorted one + for (int i = 0; i < interfaces.size(); i++) { + name.append(interfaces.get(i)); + if (i < interfaces.size() - 1) { + name.append("$"); + } } //there is a remote chance that this could generate the same //proxy name for two interfaces with the same simple name. diff --git a/impl/src/main/java/org/jboss/weld/util/Proxies.java b/impl/src/main/java/org/jboss/weld/util/Proxies.java index 766ad027b3a..b80fafb473d 100644 --- a/impl/src/main/java/org/jboss/weld/util/Proxies.java +++ b/impl/src/main/java/org/jboss/weld/util/Proxies.java @@ -21,10 +21,13 @@ import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; +import java.util.List; import java.util.Set; import jakarta.enterprise.inject.spi.Bean; @@ -51,13 +54,38 @@ public class Proxies { public static class TypeInfo { - private final Set> interfaces; - private final Set> classes; + private final List> interfaces; + private final List> classes; - private TypeInfo() { - super(); - this.interfaces = new LinkedHashSet>(); - this.classes = new LinkedHashSet>(); + private TypeInfo(Set types) { + Comparator> classComparator = Comparator.comparing(Class::getName); + List> foundInterfaces = new ArrayList<>(); + List> foundClasses = new ArrayList<>(); + + types.stream().forEach(type -> add(type, foundInterfaces, foundClasses)); + + // sort both collections and create unmodifiable lists + Collections.sort(foundClasses, classComparator); + Collections.sort(foundInterfaces, classComparator); + this.interfaces = Collections.unmodifiableList(foundInterfaces); + this.classes = Collections.unmodifiableList(foundClasses); + } + + // only invoked during object construction, arrays are then immutable + private TypeInfo add(Type type, List> foundInterfaces, List> foundClasses) { + if (type instanceof Class) { + Class clazz = (Class) type; + if (clazz.isInterface()) { + foundInterfaces.add(clazz); + } else { + foundClasses.add(clazz); + } + } else if (type instanceof ParameterizedType) { + add(((ParameterizedType) type).getRawType(), foundInterfaces, foundClasses); + } else { + throw UtilLogger.LOG.cannotProxyNonClassType(type); + } + return this; } public Class getSuperClass() { @@ -90,36 +118,16 @@ public Class getSuperInterface() { return superclass; } - private TypeInfo add(Type type) { - if (type instanceof Class) { - Class clazz = (Class) type; - if (clazz.isInterface()) { - interfaces.add(clazz); - } else { - classes.add(clazz); - } - } else if (type instanceof ParameterizedType) { - add(((ParameterizedType) type).getRawType()); - } else { - throw UtilLogger.LOG.cannotProxyNonClassType(type); - } - return this; + public List> getClasses() { + return classes; } - public Set> getClasses() { - return Collections.unmodifiableSet(classes); - } - - public Set> getInterfaces() { - return Collections.unmodifiableSet(interfaces); + public List> getInterfaces() { + return interfaces; } public static TypeInfo of(Set types) { - TypeInfo typeInfo = new TypeInfo(); - for (Type type : types) { - typeInfo.add(type); - } - return typeInfo; + return new TypeInfo(types); } }