INRIA / INRIA/spoon

NullPointerException when parameter type not declared in analyzed source files (MethodTypingContext.adaptTypeParameter)

Open
#3,231 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2k
Forks
392
Avg merge
11h 24m
Merged PRs (30d)
36

Description

When trying to analyze the parameters of a method, a NullPointerException will be thrown in cases where the type of a parameter is not part of the analyzed source code. This will also happen, when e.g. simply calling toString() on a CtClass.

**Problem:**
In the first line of the function `adaptTypeParameter` the program likes to call a function on the CTTypeParameter function parameter object `typeParam`. In cases where the type is an 'external' (e.g. of a library), this object does not exists (intended behavior), a `NullPointerException` will be thrown.

```
protected CtTypeReference adaptTypeParameter(CtTypeParameter typeParam) {
CtFormalTypeDeclarer typeParamDeclarer = typeParam.getTypeParameterDeclarer();
```

This function will only be called in the return statement of `AbstractTypingContext.adaptTypeParameterReference` like this: `return adaptTypeParameter(typeParamRef.getDeclaration());`. In cases, where the type declaration is not part of the source code, getDeclaration will return `null`. This value will be passed to adaptTypeParameter and the NullPointerException has to be thrown.

**Solution:**
Currently `adaptTypeParameter` only will be called by `adaptTypeParameterReference`. So it would be possible to add a null check to `adaptTypeParameterReference` before calling the function like this: `return (typeParamRef.getDeclaration() != null) adaptTypeParameter(typeParamRef.getDeclaration()): null;` or including the check for a null value into `adaptTypeParameter` as first line like this: `if (typeParam == null) return null;`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.