Intellij IDEA Editor: Class Order

I tried to find a solution for this, but had no joy: in Intellij IDEA I created a set of modules so that I had the following:

  • My web project (for all module dependencies set to "Runtime")
  • My Web Project Dependency # 1 (+ artifact that copies the JAR to MWP / WEB-INF / lib)
  • My Web Project Dependency # 2 (+ artifact that copies the JAR to MWP / WEB-INF / lib)

Dependency # 1 has the class org.acme.foo, but also the MWP and definition are different (oh, joy). It builds ok - all code in MWP correctly builds against classes in MWP instead of Dependency # 1.

But ... in his editor, Intellij gives me a red curve because he prefers to refer to a class in Dependency # 1 with its incompatible definition.

Ignoring the fact that there really should not be this class / package collision, can someone help me push Intellij to view the class in the local module and not the class depending?

EDIT: I am using the latest version of Intellij 11.

EDIT: for those who find this question, these issues are gone in Intellij 12+

+6
source share
4 answers

I ran into the same issue in IntelliJ 11 (Mac OS X version).

I manually edited the .iml project file and redid the dependencies in the correct order: I put the orderEntry element containing the correct version of the .jar file on top.

Hope this helps.

+8
source

I had this problem and I used Vladimir’s answer, which worked (hence my upvote), but unfortunately I found that I had to redo the .iml file as it constantly corresponded. After some digging, I found how to change this forever:

Go to the file β†’ Project Structure β†’ Modules β†’ -> Dependencies Tab

IntelliJ will add the dependencies to the class path in the order in which they are listed. To move them, simply select the dependency you want to move and use the up and down arrows on the toolbar below.

+11
source

In my case, this worked well in Eclipse, but happened in IntelliJ. The reason is that the tools load libs / jars depending on different mechanisms:

  • Eclipse Files: .classpath
  • IntelliJ: * .iml files

I have a bunch of libs / jars in a directory. The directory is included in a * .iml file like this <orderEntry type="library" name="external-libs" level="project" /> There are 2 libs / jars libraries that contain a conflict (both contain "org.joda.time .DateTime "in my case). But 2 DateTimes have different functions. I must indicate that one of them should be loaded before the other.

Decision:

  • Go to "File-> Project Structure-> Dependencies"
  • Click the "+" button below to enable the lib / jar that I want to load first.
  • Use the up / down buttons to move the newly enabled lib / jar over the / jars / libs conflict directory.
  • Restore the project.

See screenshot. joda-time-2.4.jar is added on top of external libraries to control the loading sequence. joda-time-2.4.jar is added before loading external-libs.

+1
source

I have a similar problem I'm trying to work with.

I discovered two situations where I know this is happening, and found a way around one of them.

Scenario 1) The class refers to the class in the path to the chain further down the chain than the one also found in the bank, however, in the import statement, the import statement has the form

 import com.company.classes.to.use.* 

By combining them, the IDE seems to take all the classes in this package from the same jar location. By dividing them into separate import class instructions, the IDE will select them separately.

Scenario 2). You are cool chaining methods for an overridden class.

 something.getSomethingElse().getAnotherThing().getYetAnotherThing(); 

if getSomethingElse () returns an object that is not different in the class (so you do not need to import it), you will still get an error. (When adding imports, the line is grayed out because it recognizes that it is not in use, so it does not help). In any case, I would not indulge in this method.

edit: An obvious alternative to this is code refactoring for line breaks to

 ObjectToImport obj = something.getSomethingElse(); result = obj.getAnotherThing.getYetAnotherThing(); 

And then import the temporary variable ...

Obviously, this is not ideal, and you should not reorganize your code for the sake of your development environment, but you need to, these red lines annoy me.

I hope this helps. If you have found the best solution, please share!

Regards, M

0
source

Source: https://habr.com/ru/post/903617/


All Articles