I am having issues with intellij intellisense sometimes where the code compiles fine and works, but intellij is showing that the code is invalid.
In this particular case the error is saying the following:
Cannot resolve method 'javascriptRouter(java.lang.String,
play.core.Router.JavascriptReverseRoute,
play.core.Router.javascriptReverseRoute)
Is this a bug in intellij, or do I need to adjust some of my settings?
It looks like jetbrains is aware of the issue and are looking into fixing it. Here is the link to the issue:
https://youtrack.jetbrains.com/oauth?state=%2Fissue%2FSCL-9930
All things being equal, it's not a bug in IntelliJ.
IntelliSense is going to give you possible methods, but if you plug in mismatched parameters it's not going to compile.
If you want to know for sure, double-click on the method name and type {Ctrl-B} and it'll go to the definition of the method and you should be able to see what the method parameters are (and there could be multiple methods with slightly different signatures).
Related
I'm very new to java. starting from yesterday! i installed eclipse and imported spring libraries inside it. but a weird problem happened. in import statement there was an error telling The type org.springframework.context.ApplicationContext is not accessible. after running the project another error happened (related to same subject ApplicationContext cannot be resolved to a type).
Anyway! i was confused. it took one day for me. searching forums such as stackoverflow and googling didn't resolve my problem. suddenly i saw a popup message in eclipse suggesting some solutions. ignoring some worthless one of them was adding module to module-info.java file. unbelievably that solved the problem! strangest thing was that i never saw this solution in related forums! and most annoying thing is that what if i never saw that popup? where did i do a mistake? and why this solution doesn't exist on the internet?
Lots of thanks!
Java modules are a new feature of Java 9; they allow you to specify exactly which dependencies you need for your program, hence allowing greater control of the size of your application - no need to bring along library code you don't use. That seems to imply that you need to explicitly say what you want, I guess so that you realise that you are pulling in more modules.
So the reason that you don't see much information about modules in general is that they are comparatively new and doubt that I'm alone in being a long-standing Java developer who has never seen a module-info file!
I wonder whether Eclipse gave you some options when you were creating your project and you inadvertently took an option meaning "yes I want the extra control of doing mocules and I don't mind doing a bit more work."
I am building a web app in Java using the Play! framework and Twirl template engine, using IntelliJ IDEA. In the template there are a number of implicit variables which are available(flash, session, requestHeader). I can successfully reference any of these variables and my app will compile and run as expected. However, as these variables have not been explicitly declared, IDEA shows an error(eg. Cannot resolve symbol flash) and does not provide any code assistance. While it is not a huge problem I would like to fix this if possible.
I have searched quite a bit for a solution, and the JetBrains blog has a post on how to do this for Velocity templates, if someone could help me do the same for Twirl templates I would be very thankful!
Jackson
I have found a solution to this problem, which is to refer to the fully qualified name within the code, ie. to access the flash variable use
#play.mvc.Http.Context.Implicit.flash
This can also be imported at the top of a template, then you can simply refer to #flash in the code.
This should not be necessary- Play! is aware of these variables without explicitly importing them, this is simply for IDEA's sake. Strangely enough, I found that after creating a new project through IDEA it actually did provide the expected behaviour- I could simply use #flash without the import and IDEA would work as expected. However once I closed and reopened the project the same problem would occur.
At the moment this is the best solution I have found, although I have contacted IntelliJ support and if they offer a better solution I will update my answer accordingly.
So like probably many people out there I usually comment out code temporarily, mostly for debugging purposes. I currently put something like **DEBUG** or whatever that is easily searched for, but I thought having the compiler output a warning (or even an error) whenever it finds code that is temporarily commented out could be useful. I thought of using an annotation, but annotations can't be used to mark comments.
Does anyone know of a better approach than putting an easily searchable string in the commented-out section of code?
there are plenty of code inspection tools out there that can alert you to the presence of code patterns that you define. most of them have built-in support for detecting common stuff like "//todo" comments left in code etc.
most IDEs support auto-detection of //todo as well (intellij idea, for example).
a common command-line tool for this is checkstyle. you could run it as part of your build and have it point these things out to you
At least Eclipse allows you to use (and define your own) markers put in comments, that can be easily listed afterwards. There's at least TODO and XXX, but I believe you could make your own as well.
If you're using Maven, consider to use the taglist-maven-plugin.
In my case there are two reason for doing that:
Sometimes people by mistake import classes which present in macbooks JDKs but absent in Linux. That causes build to fail on ci servers which are Linux based boxes. I doesn’t happen frequently, but when it does happened I'm thinking that there should be some smarter way to find out that earlier.
Unused imports trigger warning in IDE/code analysis. From time to time somebody need to spend time on cleaning up this stuff. Even if its just single right click in IDE you still need to commit your changes and make sure everything alright on build.
I'm curious if there is any way to find unused imports programmatically (lets say from unit test) and fail locally if there are any.
Maybe failing a build because of unused import sounds harsh, but if it saves time for team overall it makes sens to do so (would love to hear opinion on that as well).
UPDATE:
I followed yegor256 suggestion and incorporated Checkstyle task with initially small subset of Sun Code Conventions (unused imports is one of them) and made it break a build if violations found.
After one week of trial we've got zero unused imports in our codebase and surprisingly zero complaints about this rule (by the way, Checkstyle is really fast: analyzing ~100KLoc taking less than one second).
As for using IDE for such analysis: yes, it good choice, but having this kind of checks run as part of automated build is better.
What you're trying to do is called static code analysis. Checkstyle can help you. If you're using Maven, this plugin will do the automation for you: http://maven.apache.org/plugins/maven-checkstyle-plugin/
You can also take a look at qulice.com (I'm one of its developers), which integrates a few static analysis tools and pre-configures them (incl. Checkstyle, PMD, FindBugs).
If you are using eclipse IDE or IntelliJ IDEA, you can configure them to
1a. organize imports / remove unused imports on save or before commit (see cleanup preferences)
1b. switch the "unused imports" warning to an error (see error settings)
2a. configure a jre which does not include com.* stuff
2b. configure the warning of proprietary api usage from the jre to be an error
You might still want to check that on the build server, though. In this case the more complicated stuff like configuring CheckStyle would still be necessary.
I'm curious if there is any way to find unused imports programmatically (lets say from unit test) and fail build locally if there are any.
I use IntelliJ to organise imports, this removes all the unused imports. You can do this with one hot key from the top of you code base to correct all the imports. (It also has over 700 other types of static checks and fixes)
Maybe failing a build because of unused import sounds harsh, but if it saves time for team overall it makes sens to do so (would love to hear opinion on that as well).
I have IntelliJ check in code which formatted and with imports organised so the issue never arises in the first place. ;)
In Computer Science the name given to such a process of analyzing the code without executing is known as static code analysis.
Try using an IDE, I am using Eclipse, which marks all the Unused imports and Unused Variables or methods in with a Yellow color underline....
Aren't these unrelated questions? If you import classes only present in the local JDK, these imports are used (just unsatisfied). For either problem, I recommend solving it in the IDE so the problem will be detected when code is written, rather than prior to checkin (the earlier the detection, the easier the fix ...).
In eclipse, you could prevent unsatisfied imports with access rules, and automatically fix imports whenever a source file is saved by enabling the appropriate save action. If you check these settings into version control, you can easily share them with the team.
I see lot of comments in same way that use this IDE or that IDE. But all my friends try to understand the difference. Doing something programmatically is different and using IDE is different.
If I want a process to be programmatic then suggestion of IDE is not useful. It might be possible some one is asking this question because he is building complete process and this step is part of it. How opening IDE would help him on different machines and OS where CI is working?
I too building one tool on similar lines. I achieved it up to some level but it programmatically open IDE and close it automatically and fixes source code too. But opening same in Linux might be a question for me.
Understanding some one's view before answering is really very important.
While determining the impact of various bug fixes, I often find myself trying to find all the routes through the codebase which can end up in my changed code. This is a tedious manual process, involving lots of clicking on method names in Eclipse and searching for 'References...' while I draw a tree structure of calls in my notepad.
This seems like it should be automatable, but whenever I search for such tools I fail to find such a thing. There seem to be tools which will analyse code for bugs, race-conditions, etc (FindBugs, PMD, Checkstyle, for example) but nothing which will simply output potential routes through the code. It's quite possible I'm not using the correct search terms.
Does such a tool exist for Java or I have overlooked a key reason why this is not possible?
Isn't the "Open Call Hierarchy" Command what you are searching for?
To use this select a method, field, constructor, etc. and select "Open Call Hierarchy". Here you are able to browse through the places where your code references the selected element.
There is a shortcut, too: Ctrl+Alt+H
I believe you are looking for a call hierarchy. It is built in Eclipse, IntelliJ and Netbeans.
https://www.google.co.uk/search?q=eclipse+call+hierarchy