Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Filter anonymous classes in summarizer itself
Browse files Browse the repository at this point in the history
  • Loading branch information
mahesh-hegde committed Jul 4, 2023
1 parent 2a2741a commit 82881ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
import javax.tools.StandardJavaFileManager;

public class ClassFinder {
// If class is A$B$C, simpleName can be B$C or A$B$C. Doesn't matter much, because
// A can't be anonymous class.
private static boolean isNonAnonymousNestedClassName(String simpleName) {
String[] nestedParts = simpleName.split("\\$");
return Arrays.stream(nestedParts).allMatch(part -> part.matches("[a-zA-Z_][a-zA-Z0-9_]*"));
}

public static boolean isNestedClassOf(String pathString, String fqnWithSlashes, String suffix) {
var fqnWithSlashesDollarSign = fqnWithSlashes + "$";
if (!pathString.startsWith(fqnWithSlashesDollarSign) || !pathString.endsWith(suffix)) {
Expand All @@ -25,8 +32,16 @@ public static boolean isNestedClassOf(String pathString, String fqnWithSlashes,
String nested =
pathString.substring(
fqnWithSlashesDollarSign.length(), pathString.length() - suffix.length());
String[] nestedParts = nested.split("\\$");
return Arrays.stream(nestedParts).allMatch(part -> part.matches("[a-zA-Z_][a-zA-Z0-9_]*"));
return isNonAnonymousNestedClassName(nested);
}

private static boolean isNonAnonymousClassFullPath(String path) {
String[] pathParts = path.split("[/\\\\]");
String simpleNameWithExt = pathParts[pathParts.length - 1];
int extIndex = simpleNameWithExt.indexOf('.');
assert extIndex != -1 : "Should've passed full path with extension to this method";
String simpleName = simpleNameWithExt.substring(0, extIndex);
return isNonAnonymousNestedClassName(simpleName);
}

// Finds [fqn] and its children with [suffix] in [entries].
Expand Down Expand Up @@ -56,6 +71,7 @@ public static Optional<List<String>> findClassAndChildren(
// so always use takeWhile when doing a treeSet subset stream.
.takeWhile(entry -> entry.startsWith(fqnWithSlashesSlash))
.filter(entry -> entry.endsWith(suffix))
.filter(ClassFinder::isNonAnonymousClassFullPath)
.collect(Collectors.toList());
return children.isEmpty() ? Optional.empty() : Optional.of(children);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,21 @@ public void testFindChildren() {
new TreeSet<>(
List.of(
"random/os/App.class",
"random/os/App$1.class",
"random/os/App$1$3.class",
"random/os/Process.class",
"random/os/Process$Fork.class",
"random/os/Process$Fork$1.class",
"random/os/Process$Fork$A$B$C$2.class",
"random/widget/Dialog.class",
"random/widget/Dialog$Button.class",
"random/widget/Dialog$Button$2.class",
"random/widget/Dialog$Button$Color.class",
"random/widget/Dialogue$Button.class",
"random/time/Clock.class",
"random/time/Clock$1.class",
"random/time/Calendar.class",
"random/time/Calendar$Month$1.class",
"random/time/Calendar$Month.class"));
TreeSet<String> entriesWithBackslash =
entriesWithSlash.stream()
Expand Down

0 comments on commit 82881ea

Please sign in to comment.