Skip to content

NPE from getDirectDependenciesFromSelf() called on a JavaClass stub #397

@hankem

Description

@hankem

I've run into an issue with transitive dependencies not being imported completely.

Here's a simple example (which, admittedly, looks strange on its own 😃) to illustrate it:

new ClassFileImporter().importClasses(java.lang.Object.class)
        .get(java.lang.Object.class)
        .getMethod("finalize").getThrowsClause().getTypes().get(0)  // java.lang.Throwable
        .getMethod("getStackTrace").getRawReturnType().getComponentType()  // java.lang.StackTraceElement
        .getDirectDependenciesFromSelf();

Since 85506e2, I'm getting a

java.lang.NullPointerException
	at com.tngtech.archunit.core.domain.JavaClass.getDirectDependenciesFromSelf(JavaClass.java:894)

The line number is for ArchUnit 0.14.1, but getDirectDependenciesFromSelf is currently a one-liner anyways; the issue is that javaClassDependencies is not initialized, i.e. that JavaClass.completeFrom(ImportContext) was not called. But here, I don't know what's expected for stubs.

Should I be able to getDirectDependenciesFromSelf() from a stub JavaClass? (Anyways, the NPE isn't a nice result for sure.)

The hypothesis that the error might be because Throwable.getStackTrace() returns StackTraceElement[] (which seems to be imported correctly), whereas I'm asking for dependencies from StackTraceElement, is supported by the following simpler example:

new ClassFileImporter().importClasses(DependsOnArray.class)
        .get(DependsOnArray.class)
        .getField("array").getRawType().getComponentType()  // Element
        .getDirectDependenciesFromSelf();

with

static class DependsOnArray {
    Element[] array;
}

static class Element {
}

I'm still puzzled though why the same issue does not show up in:

new ClassFileImporter().importClasses(java.lang.Throwable.class)
        .get(java.lang.Throwable.class)
        .getMethod("getStackTrace").getRawReturnType().getComponentType()  // java.lang.StackTraceElement
        .getDirectDependenciesFromSelf();

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions