Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,9 @@ public static boolean generateJavaTrie(
int chunkSize = 0;
StringBuilder buf = new StringBuilder();
buf.append(" \"");
if (trie.isEmpty()) {
buf.append("\\000"); // encode an empty root node with zero branches
}
for (int i = 0; i < trie.trieLength; i++) {
char c = trie.trieData[i];
if (++chunkSize > 10_000) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,28 @@ void generateJavaSource(@TempDir File classesDir) throws Exception {
assertEquals(5, trieApply.invoke(null, "com.foobar.Five"));
}

@Test
void generateEmptyJavaSource(@TempDir File classesDir) throws Exception {
Files.createFile(new File(classesDir, "empty.trie").toPath());
ClassNameTrie.JavaGenerator.main(
new String[] {classesDir.toString(), classesDir.toString(), "empty.trie"});

assertEquals(
0,
getSystemJavaCompiler()
.run(null, null, null, new File(classesDir, "EmptyTrie.java").toString()));

try (URLClassLoader cl = URLClassLoader.newInstance(new URL[] {classesDir.toURI().toURL()})) {
Class<?> trieClass = cl.loadClass("EmptyTrie");
Method apply = trieClass.getMethod("apply", String.class);
Method applyFromIndex = trieClass.getMethod("apply", String.class, int.class);
for (String key : new String[] {"", "a", "com.example.Type"}) {
assertEquals(-1, apply.invoke(null, key));
assertEquals(-1, applyFromIndex.invoke(null, "prefix/" + key, 7));
}
}
}

@Test
@SuppressWarnings("UnnecessaryUnicodeEscape")
void manual() throws IOException {
Expand Down
Loading