If I do a common optimisation, which is to create the regex matcher once, and then reset it every time it is used, the stack overflow problem goes away - I can now compile 1500 archetypes with no errors.
The change looks like this:
private static final Matcher m = archetypeHRIDPattern.matcher(""); // ADDED
@JsonCreator
public ArchetypeHRID(String value) {
// Matcher m = archetypeHRIDPattern.matcher(value); // REMOVED
m.reset(); // ADDED
if(!m.matches()) {
throw new IllegalArgumentException(value + " is not a valid archetype human readable id");
}
namespace = m.group("namespace");
...
}
I read online that Pattern.matcher() is not thread-safe however, so this fix might not be safe for some users.
Anyone on the Archie team got a better solution?
EDIT: looks like I spoke too soon. I missed the argument value to m.reset(). When I put that back, I get the same stack overflow error, with a different archetype id. The ids are clearly matching.
EDIT2: finally figured it out. It’s nothing to do with regex matching, it’s just that the stack runs out when regex matching is going on. The problem is OPT creation, due to our models containing recursive use_archetype referencing. The depth of populating those references has to be limited.