Skip to content

Commit

Permalink
More nullability annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Dec 13, 2021
1 parent c2a5679 commit 5d98f97
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4768,6 +4768,7 @@ interface InstallationListener {
/**
* Indicates that an exception is handled.
*/
@Nullable
Throwable SUPPRESS_ERROR = null;

/**
Expand Down Expand Up @@ -4796,6 +4797,7 @@ interface InstallationListener {
* @param throwable The throwable that causes the error.
* @return The error to propagate or {@code null} if the error is handled. Any subsequent listeners are not called if the exception is handled.
*/
@Nullable
Throwable onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable);

/**
Expand Down Expand Up @@ -4919,6 +4921,7 @@ public void onInstall(Instrumentation instrumentation, ResettableClassFileTransf
/**
* {@inheritDoc}
*/
@Nullable
public Throwable onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable) {
return SUPPRESS_ERROR;
}
Expand Down Expand Up @@ -5164,6 +5167,7 @@ public void onInstall(Instrumentation instrumentation, ResettableClassFileTransf
/**
* {@inheritDoc}
*/
@Nullable
public Throwable onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable) {
for (InstallationListener installationListener : installationListeners) {
if (throwable == SUPPRESS_ERROR) {
Expand Down Expand Up @@ -9603,17 +9607,20 @@ class Default implements AgentBuilder {
/**
* Indicator for access to a static member via reflection to make the code more readable.
*/
@Nullable
private static final Object STATIC_MEMBER = null;

/**
* The value that is to be returned from a {@link java.lang.instrument.ClassFileTransformer} to indicate
* that no class file transformation is to be applied.
*/
@Nullable
private static final byte[] NO_TRANSFORMATION = null;

/**
* Indicates that a loaded type should be considered as non-available.
*/
@Nullable
private static final Class<?> NO_LOADED_TYPE = null;

/**
Expand Down Expand Up @@ -11578,6 +11585,7 @@ private static <T> T doPrivileged(PrivilegedAction<T> action, @SuppressWarnings(
/**
* {@inheritDoc}
*/
@Nullable
public byte[] transform(@Nullable ClassLoader classLoader,
@Nullable String internalTypeName,
@Nullable Class<?> classBeingRedefined,
Expand Down Expand Up @@ -11612,6 +11620,7 @@ public byte[] transform(@Nullable ClassLoader classLoader,
* @param binaryRepresentation The class file of the instrumented class in its current state.
* @return The transformed class file or an empty byte array if this transformer does not apply an instrumentation.
*/
@Nullable
protected byte[] transform(@Nullable Object rawModule,
@Nullable ClassLoader classLoader,
@Nullable String internalTypeName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,11 @@ public interface ClassLoadingDelegate {
@HashCodeAndEqualsPlugin.Enhance
class Default implements ClassLoadingDelegate {

/**
* A class loader that does not define resources of its own but allows querying for resources supplied by the boot loader.
*/
private static final ClassLoader BOOT_LOADER_PROXY = doPrivileged(BootLoaderProxyCreationAction.INSTANCE);

/**
* The underlying class loader.
*/
Expand All @@ -1402,7 +1407,7 @@ protected Default(ClassLoader classLoader) {
public static ClassLoadingDelegate of(@Nullable ClassLoader classLoader) {
return ForDelegatingClassLoader.isDelegating(classLoader)
? new ForDelegatingClassLoader(classLoader)
: new Default(classLoader == null ? ClassLoader.getSystemClassLoader() : classLoader);
: new Default(classLoader == null ? BOOT_LOADER_PROXY : classLoader);
}

/**
Expand All @@ -1415,8 +1420,29 @@ public Class<?> locate(String name) throws ClassNotFoundException {
/**
* {@inheritDoc}
*/
@Nullable
public ClassLoader getClassLoader() {
return classLoader;
return classLoader == BOOT_LOADER_PROXY
? ClassLoadingStrategy.BOOTSTRAP_LOADER
: classLoader;
}

/**
* A privileged action for creating a proxy class loader for the boot class loader.
*/
protected enum BootLoaderProxyCreationAction implements PrivilegedAction<ClassLoader> {

/**
* The singleton instance.
*/
INSTANCE;

/**
* {@inheritDoc}
*/
public ClassLoader run() {
return new URLClassLoader(new URL[0], ClassLoadingStrategy.BOOTSTRAP_LOADER);
}
}
}

Expand Down Expand Up @@ -1444,9 +1470,9 @@ class ForDelegatingClassLoader extends Default {
/**
* Creates a class loading delegate for a delegating class loader
*
* @param classLoader The delegating class loader or {@code null} to use the boot loader.
* @param classLoader The delegating class loader.
*/
protected ForDelegatingClassLoader(@Nullable ClassLoader classLoader) {
protected ForDelegatingClassLoader(ClassLoader classLoader) {
super(classLoader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private static <T> T doPrivileged(PrivilegedAction<T> action) {
* @param parent The {@link java.lang.ClassLoader} that is the parent of this class loader.
* @param typeDefinitions A map of fully qualified class names pointing to their binary representations.
*/
public ByteArrayClassLoader(ClassLoader parent, Map<String, byte[]> typeDefinitions) {
public ByteArrayClassLoader(@Nullable ClassLoader parent, Map<String, byte[]> typeDefinitions) {
this(parent, true, typeDefinitions);
}

Expand All @@ -161,7 +161,7 @@ public ByteArrayClassLoader(ClassLoader parent, Map<String, byte[]> typeDefiniti
* @param sealed {@code true} if this class loader is sealed.
* @param typeDefinitions A map of fully qualified class names pointing to their binary representations.
*/
public ByteArrayClassLoader(ClassLoader parent, boolean sealed, Map<String, byte[]> typeDefinitions) {
public ByteArrayClassLoader(@Nullable ClassLoader parent, boolean sealed, Map<String, byte[]> typeDefinitions) {
this(parent, sealed, typeDefinitions, PersistenceHandler.LATENT);
}

Expand All @@ -172,7 +172,7 @@ public ByteArrayClassLoader(ClassLoader parent, boolean sealed, Map<String, byte
* @param typeDefinitions A map of fully qualified class names pointing to their binary representations.
* @param persistenceHandler The persistence handler of this class loader.
*/
public ByteArrayClassLoader(ClassLoader parent, Map<String, byte[]> typeDefinitions, PersistenceHandler persistenceHandler) {
public ByteArrayClassLoader(@Nullable ClassLoader parent, Map<String, byte[]> typeDefinitions, PersistenceHandler persistenceHandler) {
this(parent, true, typeDefinitions, persistenceHandler);
}

Expand All @@ -184,7 +184,7 @@ public ByteArrayClassLoader(ClassLoader parent, Map<String, byte[]> typeDefiniti
* @param typeDefinitions A map of fully qualified class names pointing to their binary representations.
* @param persistenceHandler The persistence handler of this class loader.
*/
public ByteArrayClassLoader(ClassLoader parent, boolean sealed, Map<String, byte[]> typeDefinitions, PersistenceHandler persistenceHandler) {
public ByteArrayClassLoader(@Nullable ClassLoader parent, boolean sealed, Map<String, byte[]> typeDefinitions, PersistenceHandler persistenceHandler) {
this(parent, sealed, typeDefinitions, ClassLoadingStrategy.NO_PROTECTION_DOMAIN, persistenceHandler, PackageDefinitionStrategy.Trivial.INSTANCE);
}

Expand All @@ -197,9 +197,9 @@ public ByteArrayClassLoader(ClassLoader parent, boolean sealed, Map<String, byte
* @param packageDefinitionStrategy The package definer to be queried for package definitions.
* @param persistenceHandler The persistence handler of this class loader.
*/
public ByteArrayClassLoader(ClassLoader parent,
public ByteArrayClassLoader(@Nullable ClassLoader parent,
Map<String, byte[]> typeDefinitions,
ProtectionDomain protectionDomain,
@Nullable ProtectionDomain protectionDomain,
PersistenceHandler persistenceHandler,
PackageDefinitionStrategy packageDefinitionStrategy) {
this(parent, true, typeDefinitions, protectionDomain, persistenceHandler, packageDefinitionStrategy);
Expand Down Expand Up @@ -311,7 +311,7 @@ private static Object methodHandle() throws Exception {
* @param types The unloaded types to be loaded.
* @return A map of the given type descriptions pointing to their loaded representations.
*/
public static Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDescription, byte[]> types) {
public static Map<TypeDescription, Class<?>> load(@Nullable ClassLoader classLoader, Map<TypeDescription, byte[]> types) {
return load(classLoader,
types,
ClassLoadingStrategy.NO_PROTECTION_DOMAIN,
Expand Down Expand Up @@ -1084,7 +1084,7 @@ public ChildFirst(@Nullable ClassLoader parent, boolean sealed, Map<String, byte
* @param typeDefinitions A map of fully qualified class names pointing to their binary representations.
* @param persistenceHandler The persistence handler of this class loader.
*/
public ChildFirst(ClassLoader parent, Map<String, byte[]> typeDefinitions, PersistenceHandler persistenceHandler) {
public ChildFirst(@Nullable ClassLoader parent, Map<String, byte[]> typeDefinitions, PersistenceHandler persistenceHandler) {
super(parent, typeDefinitions, persistenceHandler);
}

Expand All @@ -1109,9 +1109,9 @@ public ChildFirst(@Nullable ClassLoader parent, boolean sealed, Map<String, byte
* @param persistenceHandler The persistence handler of this class loader.
* @param packageDefinitionStrategy The package definer to be queried for package definitions.
*/
public ChildFirst(ClassLoader parent,
public ChildFirst(@Nullable ClassLoader parent,
Map<String, byte[]> typeDefinitions,
ProtectionDomain protectionDomain,
@Nullable ProtectionDomain protectionDomain,
PersistenceHandler persistenceHandler,
PackageDefinitionStrategy packageDefinitionStrategy) {
super(parent, typeDefinitions, protectionDomain, persistenceHandler, packageDefinitionStrategy);
Expand Down Expand Up @@ -1183,7 +1183,7 @@ public ChildFirst(@Nullable ClassLoader parent,
* @param types The unloaded types to be loaded.
* @return A map of the given type descriptions pointing to their loaded representations.
*/
public static Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDescription, byte[]> types) {
public static Map<TypeDescription, Class<?>> load(@Nullable ClassLoader classLoader, Map<TypeDescription, byte[]> types) {
return load(classLoader,
types,
ClassLoadingStrategy.NO_PROTECTION_DOMAIN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2018,7 +2018,7 @@ public Dispatcher initialize() {
/**
* {@inheritDoc}
*/
public Class<?> defineClass(ClassLoader classLoader, String name, byte[] binaryRepresentation, ProtectionDomain protectionDomain) {
public Class<?> defineClass(@Nullable ClassLoader classLoader, String name, byte[] binaryRepresentation, @Nullable ProtectionDomain protectionDomain) {
try {
return (Class<?>) defineClass.invoke(unsafe,
name,
Expand Down Expand Up @@ -2072,7 +2072,7 @@ public Dispatcher initialize() {
/**
* {@inheritDoc}
*/
public Class<?> defineClass(ClassLoader classLoader, String name, byte[] binaryRepresentation, ProtectionDomain protectionDomain) {
public Class<?> defineClass(@Nullable ClassLoader classLoader, String name, byte[] binaryRepresentation, @Nullable ProtectionDomain protectionDomain) {
throw new UnsupportedOperationException("Could not access Unsafe class: " + message);
}
}
Expand Down Expand Up @@ -2223,7 +2223,7 @@ public boolean isAvailable() {
* @param classLoader The class loader to inject into or {@code null} to inject into the bootstrap loader.
* @return An appropriate class injector.
*/
public ClassInjector make(ClassLoader classLoader) {
public ClassInjector make(@Nullable ClassLoader classLoader) {
return make(classLoader, ClassLoadingStrategy.NO_PROTECTION_DOMAIN);
}

Expand All @@ -2234,7 +2234,7 @@ public ClassInjector make(ClassLoader classLoader) {
* @param protectionDomain The protection domain to apply or {@code null} if no protection domain should be used.
* @return An appropriate class injector.
*/
public ClassInjector make(ClassLoader classLoader, ProtectionDomain protectionDomain) {
public ClassInjector make(@Nullable ClassLoader classLoader, @Nullable ProtectionDomain protectionDomain) {
return new UsingUnsafe(classLoader, protectionDomain, dispatcher);
}

Expand Down Expand Up @@ -2552,14 +2552,15 @@ protected void inject(Instrumentation instrumentation, JarFile jarFile) {
/**
* The class loader to load classes from.
*/
@Nullable
private final ClassLoader classLoader;

/**
* Creates a new injection target.
*
* @param classLoader The class loader to load classes from.
*/
Target(ClassLoader classLoader) {
Target(@Nullable ClassLoader classLoader) {
this.classLoader = classLoader;
}

Expand All @@ -2568,6 +2569,7 @@ protected void inject(Instrumentation instrumentation, JarFile jarFile) {
*
* @return The class loader to load classes from.
*/
@Nullable
protected ClassLoader getClassLoader() {
return classLoader;
}
Expand Down Expand Up @@ -2817,7 +2819,7 @@ public boolean isAvailable() {
/**
* {@inheritDoc}
*/
public Class<?> defineClass(ClassLoader classLoader, String name, byte[] binaryRepresentation, ProtectionDomain protectionDomain) {
public Class<?> defineClass(@Nullable ClassLoader classLoader, String name, byte[] binaryRepresentation, @Nullable ProtectionDomain protectionDomain) {
return jvm.JVM_DefineClass(JNIEnv.CURRENT,
name.replace('.', '/'),
classLoader,
Expand Down Expand Up @@ -2857,7 +2859,7 @@ public boolean isAvailable() {
/**
* {@inheritDoc}
*/
public Class<?> defineClass(ClassLoader classLoader, String name, byte[] binaryRepresentation, ProtectionDomain protectionDomain) {
public Class<?> defineClass(@Nullable ClassLoader classLoader, String name, byte[] binaryRepresentation, @Nullable ProtectionDomain protectionDomain) {
throw new UnsupportedOperationException("JNA is not available and JNA-based injection cannot be used: " + error);
}
}
Expand All @@ -2882,10 +2884,10 @@ interface Jvm extends Library {
@SuppressWarnings("checkstyle:methodname")
Class<?> JVM_DefineClass(JNIEnv env,
String name,
ClassLoader classLoader,
@Nullable ClassLoader classLoader,
byte[] binaryRepresentation,
int length,
ProtectionDomain protectionDomain) throws LastErrorException;
@Nullable ProtectionDomain protectionDomain) throws LastErrorException;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ public void reset(Instrumentation instrumentation, ClassFileLocator classFileLoc
/**
* Indicates that a class is not redefined.
*/
@Nullable
private static final byte[] NO_REDEFINITION = null;

/**
Expand Down Expand Up @@ -526,9 +527,10 @@ protected ClassRedefinitionTransformer(Map<Class<?>, ClassDefinition> redefinedC
* {@inheritDoc}
*/
@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Value is always null")
public byte[] transform(ClassLoader classLoader,
String internalTypeName,
Class<?> classBeingRedefined,
@Nullable
public byte[] transform(@Nullable ClassLoader classLoader,
@Nullable String internalTypeName,
@Nullable Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) {
if (internalTypeName == null) {
Expand Down Expand Up @@ -563,9 +565,10 @@ protected enum ClassResettingTransformer implements ClassFileTransformer {
/**
* {@inheritDoc}
*/
public byte[] transform(ClassLoader classLoader,
String internalTypeName,
Class<?> classBeingRedefined,
@Nullable
public byte[] transform(@Nullable ClassLoader classLoader,
@Nullable String internalTypeName,
@Nullable Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) {
return NO_REDEFINITION;
Expand Down
Loading

0 comments on commit 5d98f97

Please sign in to comment.