Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException when using Spoon inside an IntelliJ Plugin (works with .jar but not with Maven) #1116

Closed
mduruisseau opened this issue Jan 13, 2017 · 14 comments · Fixed by #1208
Labels

Comments

@mduruisseau
Copy link

mduruisseau commented Jan 13, 2017

Hi !

Context:
We are using Spoon library inside an IntelliJ plugin for many months. Until now, we used version 5.1.1 downloaded from your website ("spoon-core-5.1.1-jar-with-dependencies.jar"). Earlier this week, we updated it on v 5.4, also using the .jar (with dependencies). Till then, no problem.
On the 11th January (v5.5 release), we switched to a maven dependency. Unfortunately, it looks like there is an incompatibility with IntelliJ and the JDT-core jar included by Spoon ?
I also tried Spoon v5.4.0 (from Maven) VS Spoon v5.4.0 (jar-with-dependencies), but it does the same thing: the jar is OK but when using Maven, it throws this exception:

Exception :

java.lang.NullPointerException
	at org.eclipse.jdt.internal.compiler.batch.FileSystem.<init>(FileSystem.java:141)
	at org.eclipse.jdt.internal.compiler.batch.Main.getLibraryAccess(Main.java:3086)
	at spoon.support.compiler.jdt.JDTBatchCompiler.getUnits(JDTBatchCompiler.java:107)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnits(JDTBasedSpoonCompiler.java:410)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnitsAndModel(JDTBasedSpoonCompiler.java:372)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildSources(JDTBasedSpoonCompiler.java:348)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:119)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:102)
	at spoon.Launcher.buildModel(Launcher.java:700)

Using breakpoints and debugger, I found this :
image

When you can see the NullPointerException, apparently because "paths" is null.
However, I am not sure that the problem come from this file. Here is the diff between the two version of the FileSystem.class (decompiled by IntelliJ), on the left side, the one from IntelliJ, on the right side, the Spoon one.
image

I tried several methods to "force use" the .jar from Spoon, without success.
EDIT: Even when deleting the lib from IntelliJ (ecj-4.5.2.jar), the NP Exception is still thrown.

Have you any idea how I could fix that ?

Thanks !

@surli
Copy link
Collaborator

surli commented Jan 13, 2017

It looks like a problem with your classpath: can you give for input on the way you use Spoon in your project?
I don't understand why you have the .jar in a /lib/ directory if you use the maven dependency in your snapshot...

@mduruisseau
Copy link
Author

mduruisseau commented Jan 13, 2017

Maven with IntelliJ ask where to download the files, it has chosen the /lib/ directory by default, but it has no effect (I tried in a folder outside the project, and I got the same exception).

As for my use of Spoon, it didn't change between v 5.1.1 and v5.5, also it works if I import the jar with dependencies (v5.4), so I don't think the problem comes from there.

In summary we use this code:

Launcher launcher = new Launcher();
String[] arguments = {"-x", "-c", "-i", sourceDir}; 

launcher.addProcessor(...); // x7

launcher.setArgs(arguments);
launcher.buildModel();
launcher.process();

The exception is thrown inside the "buildModel" method.

@pvojtechovsky
Copy link
Collaborator

May be I am wrong, but I experienced similar error when I had 2 different JDK-core...jar files on the class path. One JDK-core comes from spoon and second (wrong one) comes from analyzed project.

@mduruisseau
Copy link
Author

Thanks, I will look closer at it, but the -x arg should say to Spoon to not look on classpath ?
Also, the input folder (-i sourceDir) contains only .java files, nothing else, no .jar, no .class ..., so where could these .jar come from ?

@surli
Copy link
Collaborator

surli commented Jan 13, 2017

the -x arg should say to Spoon to not look on classpath ?

It does not look on the classpath to retrieve types reference of the source you want to spoon, but it uses the classpath you give for its own dependencies like jdt.core.

so where could these .jar come from ?

From your classpath: if you run your project through intelliJ it automatically computes the classpath given your module/project dependencies. If you want to debug it, you can put a breakpoint at the begin of your program and in the debugger > Evaluate expression and type something like:

((URLClassLoader)ClassLoader.getSystemClassLoader()).getURLs();

You will see all jar loaded by your project when running.

@surli
Copy link
Collaborator

surli commented Jan 18, 2017

Do you manage to have more information about your bug?

@surli surli added the bug label Jan 18, 2017
@mduruisseau
Copy link
Author

Hi !

I've taken a look on the ClassLoader as you said.
The SystemClassLoader returns this list of packages:
image

However, when I retrieve the ClassLoader of any class of my project, I get these results :
image

I'm searching in IntelliJ documentation if there is some information about how a plugin loads its different dependencies ..

@surli
Copy link
Collaborator

surli commented Jan 18, 2017

I'm not sure to understand why it does not show your local .m2 repository for the libs? Have you checked that your IntelliJ use maven dependencies in your project configuration?

@mduruisseau
Copy link
Author

As I said on a previous post, it seems that IntelliJ does not use a .m2 repository. Instead, when I want to add a maven dependency, it asks me where I want to put the files.
In my project I use only Spoon with Maven so here are my config files:
A file named "fr_inria_gforge_spoon_spoon_core_5_5_0.xml", inside the .idea/librairies folder

<component name="libraryTable">
  <library name="fr.inria.gforge.spoon:spoon-core:5.5.0" type="repository">
    <properties maven-id="fr.inria.gforge.spoon:spoon-core:5.5.0" />
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/../spoon_maven/spoon-core-5.5.0.jar!/" />
      <root url="jar://$PROJECT_DIR$/../spoon_maven/org.eclipse.jdt.core-3.12.0.v20160516-2131.jar!/" />
      <root url="jar://$PROJECT_DIR$/../spoon_maven/commons-io-2.5.jar!/" />
      <root url="jar://$PROJECT_DIR$/../spoon_maven/jsap-2.1.jar!/" />
      <root url="jar://$PROJECT_DIR$/../spoon_maven/ant-1.6.5.jar!/" />
      <root url="jar://$PROJECT_DIR$/../spoon_maven/log4j-1.2.17.jar!/" />
    </CLASSES>
    <JAVADOC>
      <root url="jar://$PROJECT_DIR$/../spoon_maven/spoon-core-5.5.0-javadoc.jar!/" />
    </JAVADOC>
    <SOURCES>
      <root url="jar://$PROJECT_DIR$/../spoon_maven/spoon-core-5.5.0-sources.jar!/" />
    </SOURCES>
  </library>
</component>

As well as a new line in my IML config file :
<orderEntry type="library" exported="" name="fr.inria.gforge.spoon:spoon-core:5.5.0" level="project"/>

(This file also contains other orderEntry, "inheritedJdk", "source-folder" for example, I tried to change the order of these lines, but it does the same error)

@surli
Copy link
Collaborator

surli commented Jan 18, 2017

Ok I found it weird that IntelliJ does not use .m2 but I never use IntelliJ on windows.
Besides, it seems that your fr_inria_gforge_spoon_spoon_core_5_5_0.xml file express too much dependencies. Here is my own configuration for one project:

<component name="libraryTable">
  <library name="Maven: fr.inria.gforge.spoon:spoon-core:5.1.1">
    <CLASSES>
      <root url="jar://$MAVEN_REPOSITORY$/fr/inria/gforge/spoon/spoon-core/5.1.1/spoon-core-5.1.1.jar!/" />
    </CLASSES>
    <JAVADOC>
      <root url="jar://$MAVEN_REPOSITORY$/fr/inria/gforge/spoon/spoon-core/5.1.1/spoon-core-5.1.1-javadoc.jar!/" />
    </JAVADOC>
    <SOURCES>
      <root url="jar://$MAVEN_REPOSITORY$/fr/inria/gforge/spoon/spoon-core/5.1.1/spoon-core-5.1.1-sources.jar!/" />
    </SOURCES>
  </library>
</component>

So try maybe to remove extra dependencies in that specific file to see what happens?

@surli
Copy link
Collaborator

surli commented Feb 6, 2017

I close this issue for now. Don't hesitate to reopen it if you have more information.

@surli
Copy link
Collaborator

surli commented Mar 7, 2017

Actually there was a bug, we finally spot and fix it @mduruisseau :)

@mduruisseau
Copy link
Author

Oh nice ! Thank you ;-)
I guess this fix will be included in version 5.6 ?

@surli
Copy link
Collaborator

surli commented Mar 7, 2017

I guess this fix will be included in version 5.6 ?

Yep!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants