Skip to content

Commit

Permalink
WELD-2618 Make proxy name creation more deterministic.
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Jan 11, 2021
1 parent 00253ae commit 5a689a5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
11 changes: 6 additions & 5 deletions impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
70 changes: 39 additions & 31 deletions impl/src/main/java/org/jboss/weld/util/Proxies.java
Expand Up @@ -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;
Expand All @@ -51,13 +54,38 @@ public class Proxies {

public static class TypeInfo {

private final Set<Class<?>> interfaces;
private final Set<Class<?>> classes;
private final List<Class<?>> interfaces;
private final List<Class<?>> classes;

private TypeInfo() {
super();
this.interfaces = new LinkedHashSet<Class<?>>();
this.classes = new LinkedHashSet<Class<?>>();
private TypeInfo(Set<? extends Type> types) {
Comparator<Class<?>> classComparator = Comparator.comparing(Class::getName);
List<Class<?>> foundInterfaces = new ArrayList<>();
List<Class<?>> 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<Class<?>> foundInterfaces, List<Class<?>> 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() {
Expand Down Expand Up @@ -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<Class<?>> getClasses() {
return classes;
}

public Set<Class<?>> getClasses() {
return Collections.unmodifiableSet(classes);
}

public Set<Class<?>> getInterfaces() {
return Collections.unmodifiableSet(interfaces);
public List<Class<?>> getInterfaces() {
return interfaces;
}

public static TypeInfo of(Set<? extends Type> types) {
TypeInfo typeInfo = new TypeInfo();
for (Type type : types) {
typeInfo.add(type);
}
return typeInfo;
return new TypeInfo(types);
}

}
Expand Down

0 comments on commit 5a689a5

Please sign in to comment.