Skip to content

Commit

Permalink
Add ClassInfo.isTopLevel().
Browse files Browse the repository at this point in the history
Seems like a useful method? I can't remember why I didn't add it in the first place. Perhaps just an oversight.

Partially addresses #3349

RELNOTES=`reflect`: Added `ClassInfo.isTopLevel()`.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=338738055
  • Loading branch information
benyu authored and nick-someone committed Oct 26, 2020
1 parent 33f2b15 commit 4106272
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
38 changes: 27 additions & 11 deletions android/guava/src/com/google/common/reflect/ClassPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@
public final class ClassPath {
private static final Logger logger = Logger.getLogger(ClassPath.class.getName());

private static final Predicate<ClassInfo> IS_TOP_LEVEL =
new Predicate<ClassInfo>() {
@Override
public boolean apply(ClassInfo info) {
return info.className.indexOf('$') == -1;
}
};

/** Separator for the Class-Path manifest attribute value in jar files. */
private static final Splitter CLASS_PATH_ATTRIBUTE_SEPARATOR =
Splitter.on(" ").omitEmptyStrings();
Expand Down Expand Up @@ -137,9 +129,21 @@ public ImmutableSet<ClassInfo> getAllClasses() {
return FluentIterable.from(resources).filter(ClassInfo.class).toSet();
}

/** Returns all top level classes loadable from the current class path. */
/**
* Returns all top level classes loadable from the current class path. Note that "top-level-ness"
* is determined heuristically by class name (see {@link ClassInfo#isTopLevel}).
*/
public ImmutableSet<ClassInfo> getTopLevelClasses() {
return FluentIterable.from(resources).filter(ClassInfo.class).filter(IS_TOP_LEVEL).toSet();
return FluentIterable.from(resources)
.filter(ClassInfo.class)
.filter(
new Predicate<ClassInfo>() {
@Override
public boolean apply(ClassInfo info) {
return info.isTopLevel();
}
})
.toSet();
}

/** Returns all top level classes whose package name is {@code packageName}. */
Expand Down Expand Up @@ -324,6 +328,18 @@ public String getName() {
return className;
}

/**
* Returns true if the class name "looks to be" top level (not nested), that is, it includes no
* '$' in the name, This method may return false for a top-level class that's intentionally
* named with the '$' character. If ths is a concern, you could use {@link #load} and then check
* on the loaded {@link Class} object instead.
*
* @since NEXT
*/
public boolean isTopLevel() {
return className.indexOf('$') == -1;
}

/**
* Loads (but doesn't link or initialize) the class.
*
Expand Down Expand Up @@ -408,7 +424,7 @@ private void scanJar(File file, ClassLoader classloader) throws IOException {
} finally {
try {
jarFile.close();
} catch (IOException ignored) {
} catch (IOException ignored) { // similar to try-with-resources, but don't fail scanning
}
}
}
Expand Down
38 changes: 27 additions & 11 deletions guava/src/com/google/common/reflect/ClassPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@
public final class ClassPath {
private static final Logger logger = Logger.getLogger(ClassPath.class.getName());

private static final Predicate<ClassInfo> IS_TOP_LEVEL =
new Predicate<ClassInfo>() {
@Override
public boolean apply(ClassInfo info) {
return info.className.indexOf('$') == -1;
}
};

/** Separator for the Class-Path manifest attribute value in jar files. */
private static final Splitter CLASS_PATH_ATTRIBUTE_SEPARATOR =
Splitter.on(" ").omitEmptyStrings();
Expand Down Expand Up @@ -137,9 +129,21 @@ public ImmutableSet<ClassInfo> getAllClasses() {
return FluentIterable.from(resources).filter(ClassInfo.class).toSet();
}

/** Returns all top level classes loadable from the current class path. */
/**
* Returns all top level classes loadable from the current class path. Note that "top-level-ness"
* is determined heuristically by class name (see {@link ClassInfo#isTopLevel}).
*/
public ImmutableSet<ClassInfo> getTopLevelClasses() {
return FluentIterable.from(resources).filter(ClassInfo.class).filter(IS_TOP_LEVEL).toSet();
return FluentIterable.from(resources)
.filter(ClassInfo.class)
.filter(
new Predicate<ClassInfo>() {
@Override
public boolean apply(ClassInfo info) {
return info.isTopLevel();
}
})
.toSet();
}

/** Returns all top level classes whose package name is {@code packageName}. */
Expand Down Expand Up @@ -324,6 +328,18 @@ public String getName() {
return className;
}

/**
* Returns true if the class name "looks to be" top level (not nested), that is, it includes no
* '$' in the name, This method may return false for a top-level class that's intentionally
* named with the '$' character. If ths is a concern, you could use {@link #load} and then check
* on the loaded {@link Class} object instead.
*
* @since NEXT
*/
public boolean isTopLevel() {
return className.indexOf('$') == -1;
}

/**
* Loads (but doesn't link or initialize) the class.
*
Expand Down Expand Up @@ -408,7 +424,7 @@ private void scanJar(File file, ClassLoader classloader) throws IOException {
} finally {
try {
jarFile.close();
} catch (IOException ignored) {
} catch (IOException ignored) { // similar to try-with-resources, but don't fail scanning
}
}
}
Expand Down

0 comments on commit 4106272

Please sign in to comment.