Unable to expand/explode EnumSet and Guava Immutable Collections
taa-autorola-com opened this issue · comments
Problem
Using an EnumSet (or an Immutable Collection from Google Guava) to expand an exploded parameter in a template fails with a reflection exception.
Given the following test case:
import com.damnhandy.uri.template.UriTemplate;
import java.util.*;
public class DamnHandyBug {
public enum Hello {
World,
Universe
}
public static void main(String[] args) {
// does NOT work
final Set<Hello> someSet = EnumSet.of(Hello.World);
// works fine
// Set<Hello> someSet = new HashSet<>();
// someSet.add(Hello.Universe);
String template = "http://example.com{?someSet*}";
Map<String, Object> map = new HashMap<>();
map.put("someSet", someSet);
String result = UriTemplate.fromTemplate(template)
.expand(map);
System.out.println(result);
}
}
It fails with the following Exception:
Exception in thread "main" com.damnhandy.uri.template.VarExploderException: java.lang.IllegalAccessException: class com.damnhandy.uri.template.DefaultVarExploder cannot access a member of class java.util.RegularEnumSet (in module java.base) with modifiers "public"
at com.damnhandy.uri.template.DefaultVarExploder.getValue(DefaultVarExploder.java:298)
at com.damnhandy.uri.template.DefaultVarExploder.inspectGetters(DefaultVarExploder.java:199)
at com.damnhandy.uri.template.DefaultVarExploder.initValues(DefaultVarExploder.java:141)
at com.damnhandy.uri.template.DefaultVarExploder.setSource(DefaultVarExploder.java:115)
at com.damnhandy.uri.template.DefaultVarExploder.<init>(DefaultVarExploder.java:94)
at com.damnhandy.uri.template.impl.VarExploderFactory.getExploder(VarExploderFactory.java:54)
at com.damnhandy.uri.template.UriTemplate.expandVariables(UriTemplate.java:664)
at com.damnhandy.uri.template.UriTemplate.expressionReplacementString(UriTemplate.java:586)
at com.damnhandy.uri.template.UriTemplate.expand(UriTemplate.java:422)
at com.damnhandy.uri.template.UriTemplate.expand(UriTemplate.java:405)
at DamnHandyBug.main(DamnHandyBug.java:25)
Caused by: java.lang.IllegalAccessException: class com.damnhandy.uri.template.DefaultVarExploder cannot access a member of class java.util.RegularEnumSet (in module java.base) with modifiers "public"
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591)
at java.base/java.lang.reflect.Method.invoke(Method.java:558)
at com.damnhandy.uri.template.DefaultVarExploder.getValue(DefaultVarExploder.java:290)
... 10 more
Likewise for guava's Immutable Collections.
It seems to be an invocation of Collection.isEmpty() that fails for some yet unknown reason. I haven't investigated further.
Workaround
Create a new collection before invoking expand, such as HashSet or ArrayList.
Versions
Tested in
- java 1.8.0_231 (version 2.1.6 and 2.1.8)
- java 11.0.7 (version 2.1.8)