INRIA / INRIA/spoon

NPE when missing return type in a method and pretty-printing it.

Open
#2,915 9 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
2k
Forks
392
Avg merge
11h 24m
Merged PRs (30d)
36

Description

Bonjour,

Je voudrais utiliser Spoon pour faire analyser des dépendances entre deux classes, créer une interface pour l'une d'entre elles et passer par l'interface pour la manipuler. L'analyse avec l'outil Spoon se passe sans aucun soucis, or lorsque j'essaie de faire ma transformation je rencontre un problème. Je n'utilise pas de Processors ni de Template pour cette transformation, voici mon code:

I would like to use Spoon to analyse dependencies between two classes (A and B), create an interface that represents class A and use the interface (IA) in class B instead of referencing A directly. The analysis works without problems, however when I try to transform my project I have a problem. I am not using any Processors or Templates in my code as of now to transform, here is my code:

```JAVA
// Creation d'une interface pour représenter une classe nommé invokedClass
CtInterface commonInterface = new CtInterfaceImpl();
commonInterface.setSimpleName("I"+invokedClass.getSimpleName());

// Creation des methodes de l'interface à partir des méthodes de la classe.
List methods = invokedClass.getElements(new TypeFilter(CtMethod.class));
// Parcours des méthodes pour en extraire leurs signatures pour l'ajouter à l'interface.
for(CtMethod method : methods) {
CtMethod m = new CtMethodImpl(); // méthode pour l'interface
m.setSimpleName(method.getSimpleName()); // on copie le nom de la méthode
// On parcours les paramètres pour les ajouter à la nouvelle méthode
for(CtParameter param : params) {
CtParameter p = new CtParameterImpl();
p.setSimpleName(param.getSimpleName());
p.setType(param.getType()); // ligne qui casse
m.addParameter(p);
}
commonInterface.addMethod(m);
}
// commonInterface est ajouté au model au même niveau que invokedClass dans l'AST
```

Lors de la construction de mon sous arbre et l'ajout au modèle il n'y a aucun soucis. Mais lorsque je souhaite utiliser le prettyprint() de Launcher je rencontre l'erreur suivante:

I do not encounter any problems while adding to the AST, but when I use the prettyprint() of Launcher I have the following problem.

`
Exception in thread "main" spoon.SpoonException: java.lang.NullPointerException
at spoon.Launcher.prettyprint(Launcher.java:791)
at migration.Main.main(Main.java:68)
Caused by: java.lang.NullPointerException
at spoon.reflect.factory.ExecutableFactory.createReferenceInternal(ExecutableFactory.java:113)
at spoon.reflect.factory.ExecutableFactory.createReference(ExecutableFactory.java:99)
at spoon.support.reflect.declaration.CtExecutableImpl.getReference(CtExecutableImpl.java:191)
at spoon.reflect.visitor.ImportScannerImpl.addClassImport(ImportScannerImpl.java:398)
...
`

C'est la ligne suivante qui créer cet effet mais je n'arrive pas à savoir pourquoi:
The following line creates this error and I cannot see why:

`
p.setType(param.getType());
`

Je pense que c'est lié au CtTypeReference qui lie les classes entre elles mais je ne trouve pas de documentation pour les créer proprement. Pouvez vous m'indiquer où est-ce que je pourrais me renseigner pour avoir plus d'informations, ou m'aider à mieux comprendre mon erreur?

I think it is linked to the creation of the CtTypeReference that links the classes between each other ut I cannot find the documentation to do this properly. Can someone show me where I can find more information on how to properly perform this action, or help me understand my error?

J'ai déjà lu votre papier “Spoon: A Library for Implementing Analyses and Transformations of Java Source Code” ainsi que les codes que l'on trouve sur votre site mais je n'arrive toujours pas à résoudre ce problème.

I have already read the paper on “Spoon: A Library for Implementing Analyses and Transformations of Java Source Code” as well as the sample code found on the main site but I still cannot find a solution

Merci.

After writing this up, I now realize that it might be helpful to write this in english as well (for the potential contributor or someone having the same problem as me). A quick summary of my problem is that I would like to create an Interface from the AST of a Class navigating through its nodes. I am able to succesfully do this but when I want to copy/clone/create a CtTypReference for the parameter of my new method (of my new interface), I have the previously-stated error. I believe I am incorrectly using CtTypeReference but I do not know how to do so correctly... Any help would be appreciated, thank you.

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.