Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehutch committed Oct 6, 2021
1 parent c7ad4f5 commit 80fa028
Show file tree
Hide file tree
Showing 29 changed files with 1,095 additions and 990 deletions.
6 changes: 3 additions & 3 deletions src/main/java/io/github/classgraph/AnnotationInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import java.util.Map.Entry;
import java.util.Set;

import nonapi.io.github.classgraph.reflection.ReflectionUtils;
import nonapi.io.github.classgraph.utils.LogNode;
import nonapi.io.github.classgraph.utils.ReflectionUtils;

/** Holds metadata about a specific annotation instance on a class, method, method parameter or field. */
public class AnnotationInfo extends ScanResultObject implements Comparable<AnnotationInfo>, HasName {
Expand Down Expand Up @@ -354,8 +354,8 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg
for (final Entry<String, Object> ent : annotationParameterValuesInstantiated.entrySet()) {
final String paramName = ent.getKey();
final Object paramVal = ent.getValue();
final Object otherParamVal = ReflectionUtils.invokeMethod(args[0], paramName,
/* throwException = */ false);
final Object otherParamVal = ReflectionUtils.invokeMethod(/* throwException = */ false,
args[0], paramName);
if ((paramVal == null) != (otherParamVal == null)) {
// Annotation values should never be null, but just to be safe
return false;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/github/classgraph/ModulePathInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import java.util.List;
import java.util.Set;

import nonapi.io.github.classgraph.reflection.ReflectionUtils;
import nonapi.io.github.classgraph.utils.JarUtils;
import nonapi.io.github.classgraph.utils.ReflectionUtils;
import nonapi.io.github.classgraph.utils.StringUtils;

/**
Expand Down Expand Up @@ -135,12 +135,12 @@ public ModulePathInfo() {
final Class<?> managementFactory = ReflectionUtils
.classForNameOrNull("java.lang.management.ManagementFactory");
final Object runtimeMXBean = managementFactory == null ? null
: ReflectionUtils.invokeStaticMethod(managementFactory, "getRuntimeMXBean",
/* throwException = */ false);
: ReflectionUtils.invokeStaticMethod(/* throwException = */ false, managementFactory,
"getRuntimeMXBean");
@SuppressWarnings("unchecked")
final List<String> commandlineArguments = runtimeMXBean == null ? null
: (List<String>) ReflectionUtils.invokeMethod(runtimeMXBean, "getInputArguments",
/* throwException = */ false);
: (List<String>) ReflectionUtils.invokeMethod(/* throwException = */ false, runtimeMXBean,
"getInputArguments");
if (commandlineArguments != null) {
for (final String arg : commandlineArguments) {
for (int i = 0; i < fields.size(); i++) {
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/io/github/classgraph/ModuleReaderProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.nio.ByteBuffer;
import java.util.List;

import nonapi.io.github.classgraph.utils.ReflectionUtils;
import nonapi.io.github.classgraph.reflection.ReflectionUtils;

/** A ModuleReader proxy, written using reflection to preserve backwards compatibility with JDK 7 and 8. */
public class ModuleReaderProxy implements Closeable {
Expand All @@ -51,8 +51,8 @@ public class ModuleReaderProxy implements Closeable {
collectorClass = ReflectionUtils.classForNameOrNull("java.util.stream.Collector");
final Class<?> collectorsClass = ReflectionUtils.classForNameOrNull("java.util.stream.Collectors");
if (collectorsClass != null) {
collectorsToList = ReflectionUtils.invokeStaticMethod(collectorsClass, "toList",
/* throwException = */ true);
collectorsToList = ReflectionUtils.invokeStaticMethod(/* throwException = */ true, collectorsClass,
"toList");
}
}

Expand All @@ -66,8 +66,8 @@ public class ModuleReaderProxy implements Closeable {
*/
ModuleReaderProxy(final ModuleRef moduleRef) throws IOException {
try {
moduleReader = (AutoCloseable) ReflectionUtils.invokeMethod(moduleRef.getReference(), "open",
/* throwException = */ true);
moduleReader = (AutoCloseable) ReflectionUtils.invokeMethod(/* throwException = */ true,
moduleRef.getReference(), "open");
if (moduleReader == null) {
throw new IllegalArgumentException("moduleReference.open() should not return null");
}
Expand Down Expand Up @@ -104,13 +104,13 @@ public List<String> list() throws SecurityException {
if (collectorsToList == null) {
throw new IllegalArgumentException("Could not call Collectors.toList()");
}
final Object /* Stream<String> */ resourcesStream = ReflectionUtils.invokeMethod(moduleReader, "list",
/* throwException = */ true);
final Object /* Stream<String> */ resourcesStream = ReflectionUtils
.invokeMethod(/* throwException = */ true, moduleReader, "list");
if (resourcesStream == null) {
throw new IllegalArgumentException("Could not call moduleReader.list()");
}
final Object resourcesList = ReflectionUtils.invokeMethod(resourcesStream, "collect", collectorClass,
collectorsToList, /* throwException = */ true);
final Object resourcesList = ReflectionUtils.invokeMethod(/* throwException = */ true, resourcesStream,
"collect", collectorClass, collectorsToList);
if (resourcesList == null) {
throw new IllegalArgumentException("Could not call moduleReader.list().collect(Collectors.toList())");
}
Expand All @@ -133,13 +133,13 @@ public List<String> list() throws SecurityException {
*/
private Object openOrRead(final String path, final boolean open) throws SecurityException {
final String methodName = open ? "open" : "read";
final Object /* Optional<InputStream> */ optionalInputStream = ReflectionUtils.invokeMethod(moduleReader,
methodName, String.class, path, /* throwException = */ true);
final Object /* Optional<InputStream> */ optionalInputStream = ReflectionUtils
.invokeMethod(/* throwException = */ true, moduleReader, methodName, String.class, path);
if (optionalInputStream == null) {
throw new IllegalArgumentException("Got null result from moduleReader." + methodName + "(name)");
}
final Object /* InputStream */ inputStream = ReflectionUtils.invokeMethod(optionalInputStream, "get",
/* throwException = */ true);
final Object /* InputStream */ inputStream = ReflectionUtils.invokeMethod(/* throwException = */ true,
optionalInputStream, "get");
if (inputStream == null) {
throw new IllegalArgumentException("Got null result from moduleReader." + methodName + "(name).get()");
}
Expand Down Expand Up @@ -182,7 +182,7 @@ public ByteBuffer read(final String path) throws SecurityException, OutOfMemoryE
* The {@link ByteBuffer} to release.
*/
public void release(final ByteBuffer byteBuffer) {
ReflectionUtils.invokeMethod(moduleReader, "release", ByteBuffer.class, byteBuffer,
/* throwException = */ true);
ReflectionUtils.invokeMethod(/* throwException = */ true, moduleReader, "release", ByteBuffer.class,
byteBuffer);
}
}
38 changes: 19 additions & 19 deletions src/main/java/io/github/classgraph/ModuleRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import java.util.List;
import java.util.Set;

import nonapi.io.github.classgraph.reflection.ReflectionUtils;
import nonapi.io.github.classgraph.utils.CollectionUtils;
import nonapi.io.github.classgraph.utils.ReflectionUtils;

/** A ModuleReference proxy, written using reflection to preserve backwards compatibility with JDK 7 and 8. */
public class ModuleRef implements Comparable<ModuleRef> {
Expand Down Expand Up @@ -88,46 +88,46 @@ public ModuleRef(final Object moduleReference, final Object moduleLayer) {
this.reference = moduleReference;
this.layer = moduleLayer;

this.descriptor = ReflectionUtils.invokeMethod(moduleReference, "descriptor", /* throwException = */ true);
this.descriptor = ReflectionUtils.invokeMethod(/* throwException = */ true, moduleReference, "descriptor");
if (this.descriptor == null) {
// Should not happen
throw new IllegalArgumentException("moduleReference.descriptor() should not return null");
}
this.name = (String) ReflectionUtils.invokeMethod(this.descriptor, "name", /* throwException = */ true);
this.name = (String) ReflectionUtils.invokeMethod(/* throwException = */ true, this.descriptor, "name");
@SuppressWarnings("unchecked")
final Set<String> modulePackages = (Set<String>) ReflectionUtils.invokeMethod(this.descriptor, "packages",
/* throwException = */ true);
final Set<String> modulePackages = (Set<String>) ReflectionUtils.invokeMethod(/* throwException = */ true,
this.descriptor, "packages");
if (modulePackages == null) {
// Should not happen
throw new IllegalArgumentException("moduleReference.descriptor().packages() should not return null");
}
this.packages = new ArrayList<>(modulePackages);
CollectionUtils.sortIfNotEmpty(this.packages);
final Object optionalRawVersion = ReflectionUtils.invokeMethod(this.descriptor, "rawVersion",
/* throwException = */ true);
final Object optionalRawVersion = ReflectionUtils.invokeMethod(/* throwException = */ true, this.descriptor,
"rawVersion");
if (optionalRawVersion != null) {
final Boolean isPresent = (Boolean) ReflectionUtils.invokeMethod(optionalRawVersion, "isPresent",
/* throwException = */ true);
final Boolean isPresent = (Boolean) ReflectionUtils.invokeMethod(/* throwException = */ true,
optionalRawVersion, "isPresent");
if (isPresent != null && isPresent) {
this.rawVersion = (String) ReflectionUtils.invokeMethod(optionalRawVersion, "get",
/* throwException = */ true);
this.rawVersion = (String) ReflectionUtils.invokeMethod(/* throwException = */ true,
optionalRawVersion, "get");
}
}
final Object moduleLocationOptional = ReflectionUtils.invokeMethod(moduleReference, "location",
/* throwException = */ true);
final Object moduleLocationOptional = ReflectionUtils.invokeMethod(/* throwException = */ true,
moduleReference, "location");
if (moduleLocationOptional == null) {
// Should not happen
throw new IllegalArgumentException("moduleReference.location() should not return null");
}
final Object moduleLocationIsPresent = ReflectionUtils.invokeMethod(moduleLocationOptional, "isPresent",
/* throwException = */ true);
final Object moduleLocationIsPresent = ReflectionUtils.invokeMethod(/* throwException = */ true,
moduleLocationOptional, "isPresent");
if (moduleLocationIsPresent == null) {
// Should not happen
throw new IllegalArgumentException("moduleReference.location().isPresent() should not return null");
}
if ((Boolean) moduleLocationIsPresent) {
this.location = (URI) ReflectionUtils.invokeMethod(moduleLocationOptional, "get",
/* throwException = */ true);
this.location = (URI) ReflectionUtils.invokeMethod(/* throwException = */ true, moduleLocationOptional,
"get");
if (this.location == null) {
// Should not happen
throw new IllegalArgumentException("moduleReference.location().get() should not return null");
Expand All @@ -137,8 +137,8 @@ public ModuleRef(final Object moduleReference, final Object moduleLayer) {
}

// Find the classloader for the module
this.classLoader = (ClassLoader) ReflectionUtils.invokeMethod(moduleLayer, "findLoader", String.class,
this.name, /* throwException = */ true);
this.classLoader = (ClassLoader) ReflectionUtils.invokeMethod(/* throwException = */ true, moduleLayer,
"findLoader", String.class, this.name);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

import nonapi.io.github.classgraph.classpath.ClassLoaderOrder;
import nonapi.io.github.classgraph.classpath.ClasspathOrder;
import nonapi.io.github.classgraph.reflection.ReflectionUtils;
import nonapi.io.github.classgraph.scanspec.ScanSpec;
import nonapi.io.github.classgraph.utils.LogNode;
import nonapi.io.github.classgraph.utils.ReflectionUtils;

/** Extract classpath entries from the Ant ClassLoader. */
class AntClassLoaderHandler implements ClassLoaderHandler {
Expand Down Expand Up @@ -84,7 +84,7 @@ public static void findClassLoaderOrder(final ClassLoader classLoader, final Cla
public static void findClasspathOrder(final ClassLoader classLoader, final ClasspathOrder classpathOrder,
final ScanSpec scanSpec, final LogNode log) {
classpathOrder.addClasspathPathStr(
(String) ReflectionUtils.invokeMethod(classLoader, "getClasspath", false), classLoader, scanSpec,
(String) ReflectionUtils.invokeMethod(false, classLoader, "getClasspath"), classLoader, scanSpec,
log);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

import nonapi.io.github.classgraph.classpath.ClassLoaderOrder;
import nonapi.io.github.classgraph.classpath.ClasspathOrder;
import nonapi.io.github.classgraph.reflection.ReflectionUtils;
import nonapi.io.github.classgraph.scanspec.ScanSpec;
import nonapi.io.github.classgraph.utils.LogNode;
import nonapi.io.github.classgraph.utils.ReflectionUtils;

/** ClassLoaderHandler that is able to extract the URLs from a CxfContainerClassLoader. */
class CxfContainerClassLoaderHandler implements ClassLoaderHandler {
Expand Down Expand Up @@ -74,7 +74,7 @@ public static void findClassLoaderOrder(final ClassLoader classLoader, final Cla
// Ignore
}
// tccl = TomcatClassLoader
classLoaderOrder.delegateTo((ClassLoader) ReflectionUtils.invokeMethod(classLoader, "tccl", false),
classLoaderOrder.delegateTo((ClassLoader) ReflectionUtils.invokeMethod(false, classLoader, "tccl"),
/* isParent = */ false, log);
// This classloader doesn't actually load any classes, but add it to the order to improve logging
classLoaderOrder.add(classLoader, log);
Expand Down

0 comments on commit 80fa028

Please sign in to comment.