Skip to content

Commit

Permalink
Merge pull request #152 from MikeEdgar/148_smallrye
Browse files Browse the repository at this point in the history
Support top-level class name with leading inner class delimiter
  • Loading branch information
Ladicek authored Oct 5, 2021
2 parents aac1389 + 91710c8 commit be03767
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
13 changes: 10 additions & 3 deletions core/src/main/java/org/jboss/jandex/NameTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ DotName convertToName(String name, char delim) {
if (result != null)
return result;

int loc = lastIndexOf(name, delim, '$');
int loc = lastIndexOf(name, delim);
String local = intern(name.substring(loc + 1));
DotName prefix = loc < 1 ? null : convertToName(intern(name.substring(0, loc)), delim);
result = new DotName(prefix, local, true, loc > 0 && name.charAt(loc) == '$');
Expand All @@ -55,16 +55,23 @@ DotName convertToName(String name, char delim) {
return result;
}

private int lastIndexOf(String name, char delim1, char delim2) {
private int lastIndexOf(String name, char delim) {
// Begin at second last position to avoid empty local name
int pos = name.length() - 1;
while (--pos >= 0) {
char c = name.charAt(pos);
if (c == delim1 || c == delim2) {
if (c == delim || c == '$') {
break;
}
}

// avoid splitting on '$' if previous char is a delimiter or the '$'
// is in position 0, because subsequent split would produce an empty
// local name
if (pos >=0 && name.charAt(pos) == '$' && (pos == 0 || name.charAt(pos - 1) == delim)) {
pos--;
}

return pos;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package $delimiters$.test;

public class $SurroundedByDelimiters$ {

}
5 changes: 5 additions & 0 deletions core/src/test/java/$pkg/test/$LeadingDelimiter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package $pkg.test;

public class $LeadingDelimiter {

}
29 changes: 29 additions & 0 deletions core/src/test/java/org/jboss/jandex/test/DotNameTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.junit.Assert;
import org.junit.Test;

import $pkg.test.$LeadingDelimiter;

/**
* Since DotName is often used as a key in collections and implements Comparable,
* make sure the #compareTo, hashCode, equals and toString are strictly consistent.
Expand Down Expand Up @@ -231,6 +233,33 @@ public static class Test$ {

}

@Test
public void testLeadingInnerClassDelimiterOnClass() throws IOException {
DotName pkg = DotName.createComponentized(null, "$pkg");
DotName test = DotName.createComponentized(pkg, "test");
DotName testName = DotName.createComponentized(test, $LeadingDelimiter.class.getSimpleName());

Index index = Index.of($LeadingDelimiter.class);
assertEquals(testName, index.getKnownClasses().iterator().next().name());
assertNotNull(index.getClassByName(DotName.createSimple($LeadingDelimiter.class.getName())));
assertNotNull(index.getClassByName(testName));
}

@Test
public void testClassNameWithDelimitersFirstAndLast() throws IOException {
DotName pkg = DotName.createComponentized(null, "$delimiters$");
DotName test = DotName.createComponentized(pkg, "test");
DotName testName = DotName.createComponentized(test, $delimiters$.test.$SurroundedByDelimiters$.class.getSimpleName());
DotName testNameSimple = DotName.createSimple($delimiters$.test.$SurroundedByDelimiters$.class.getName());

Index index = Index.of($delimiters$.test.$SurroundedByDelimiters$.class);
DotName indexedName = index.getKnownClasses().iterator().next().name();
assertEquals(testName, indexedName);
assertNotNull(index.getClassByName(testNameSimple));
assertNotNull(index.getClassByName(testName));
assertEquals("$delimiters$.test.$SurroundedByDelimiters$", indexedName.toString());
}

private static DotName createRandomDotName() {
return r.nextBoolean() ? createRandomComponentised() : createRandomSimple();
}
Expand Down

0 comments on commit be03767

Please sign in to comment.