Skip to content

NullPointerException in ClassReferenceFixer on unresolved Kotlin metadata references #174

Description

@ianbrandt

ClassReferenceFixer's KotlinReferenceFixer assumes every Kotlin metadata reference resolved, and NPEs when one didn't. Several spots dereference the referenced* field without a null check:

// ClassReferenceFixer.java:773  visitAnyType
kotlinTypeMetadata.className = kotlinTypeMetadata.referencedClass.getName();

// :746  visitAnyFunction
kotlinFunctionMetadata.referencedMethod.getName(kotlinFunctionMetadata.referencedMethodClass)

// :905  visitAnyArgument
argument.referencedAnnotationMethod.getName(annotation.referencedAnnotationClass);

// :917 / :929  visitClassArgument / visitEnumArgument
value.className = value.referencedClass.getName();

If the class pool is incomplete — e.g. a class referenced only from @Metadata that isn't on the classpath — ClassReferenceInitializer leaves the matching referenced* null, and these lines throw instead of leaving the reference as-is. (visitAnyType does have a guard, but it's on className != null, not the referencedClass it actually dereferences.)

The annotation path right above already does the right thing:

// :888  visitAnyAnnotation
if (annotation.referencedAnnotationClass != null) {
  annotation.className = annotation.referencedAnnotationClass.getName();
}

Could the type / function / argument paths get the same null guard? Today an incomplete classpath crashes the fixer rather than degrading gracefully.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions