Can Intellij recognize unused instantiated code in tests? - intellij-idea

Intellij, of course, highlights my unused code. If I write something like this in the test:
String something= "testValue";
something will be grayed out.
But , if I have some instantiation like:
DccData dccData = new DccData();
even with
dccData.setAmount(12);
If dccData is never assigned to some other object, my FindBugs will eventually find that dccData is useless since it is not assigned or used as a return (thenReturn), etc. and this will crash my build, because that is how my build is set. I need to keep it that way because of the company's policy.
My question is:
Is there a way for Intellij to notice and gray-out this?

Unfortunately you can't set "Unused declaration" inspection to trigger dccData variable from your sample as unused. It will be difficult to prove that dccData is not used anywhere else, i.e. in setter method.

Related

Are ByteBuddy's field setting checks too strict?

I am using MethodCall.setsField() to try to set an instance field on another instance.
My generated class that is doing the field-setting, GC, is trying to set the value of an instance field in an instance of something it has created (CI). So the field's declaring type is CI; my field-setting code resides in GC (which is in the same package as CI but otherwise unrelated to it).
The ByteBuddy checks seem to indicate that although GC and CI are in the same package, GC must be assignable to CI in order to set this field! That greatly surprised me, but I am not a bytecode expert, and I might very well be overlooking something obvious. Could someone kindly explain why this check is necessary?
The method call sets the field implicitly on the this instance on which the method is invoked. For this to be possible, a non-static field must be declared by a super type of the type on which the method is invoked.
If you think this is too strict, please file an issue with an example of the code you are trying to generate, including the code to generate it which is currently failing. Maybe I am not thinking straight about this and if there's a restriction to be lifted, I would surely do it.

Dynamic project-wide variable in Emacs

I'd like to have a project-wide variable which I can change during looking at that project. In other words, I'd like to get it affected whenever opening a file. Yes, I know .dir-locals.el exist in Emacs world. But I think it would be reset to the value set in .dir-locals.el whenever opening a file under that project.
Could I get some hints from you, please?
For this kind of thing you might want to use a function instead of a variable (directly). Specifically, use a getter and setter function.
All of your project code can invoke the getter function to get the value (which can be cached in a variable). And all of your code can invoke the setter function to change the value (which, again, can be cached in a variable).
These functions can be visible globally to your project. The cache variable would be accessed only by the getter and setter functions.
But as for code everywhere in your project being informed when the value gets updated and do what's appropriate with the new value whenever that happens, see #Phil's comment about the use of a variable - the same considerations apply.
You can have a hook in the setter function (or advise it), so that it does something additional (e.g. informs interested/subscribed code) whenever it updates the value.
For a variable you can do something similar using, as #Phils said in a comment, using add-variable-watcher.
For a user-option variable you can do something similar using :set and :get functions in the defcustom. (But those take effect only if changes are made using appropriate Customize functions or the Customize UI.)
You can eval in the dir-locals.el So, if you have a variable my-var that you want to be able to change with setq you could do something like
((nil . ((eval . (or (boundp 'my-var) (setq my-var 'default))))))
There are warnings about using eval in a dir-local though, since any code could be run there.

How to make the IDEA to recognize Spock's with() method?

I write on Groovy and use Spock framework for testing.
Some time ago, IDEA completely supports Spock.
When I've writing the code inside feature (test method) in where-block something like this:
with(someObject) {
intField == 1
...
}
IDEA correctly recognize the someObject and understand that intField is field of the object, also, it was offering me other fields of the object. So, inside Spock's with() block I've felt like inside any closure from DefaultGroovyMethods (with{}, each{}, find{}, etc.), but haven't need to use explicit it param.
(So, I's writing with(someObject) { intField == 1 } what is the same is someObject.with { assert it.intField == 1 }.
After a moment IDEA missed Spock's with() method support.
Now, it don't recognize the class of the parameter (ex. someObject) in think that it's object of Object class. Any fields inside closure don't be recognized. Explicit it usage didn't help.
After some IDEA update everything have repaired, but after reboot the problem has come back.
Does anybody know how to fix it??
I'm using the last version of the IDE - 2018.2
I just tested this out with 2018.1 and have the same issue as you. I didn't run the code but I am fairly certain that the code would be recognized and executed. This to me looks like a bug with Intellij, and it might be worth it to submit a bug report in their ticket tracking system.

IntelliJ - completion suggestions order

I'm wondering if there is a way to let IDE suggest methods before variables?
When writing code like this it's a pain to select methods yourself (builder class example)
Variable example:
var message = "message"
private set
Thank you
Sorry, it appears you can't reconfigure the IDE or Kotlin plugin in this way. You can file a feature suggestion for Kotlin plugin, but I believe there are cases when variables are preferred over same-named methods, so just reordering them would break someone else's habits, so it's tricky.
You can consider making the variables private (so they won't be shown in the completion list at all) or selecting them and then typing an opening parenthesis manually.

Can not build thisJoinPoint lazily for this advice since it has no suitable guard

What is a "suitable guard" and what does it look like?
Linked this question because it refers to the same compiler message and the answer mentions a guard but not how to create one. Looked through the AspectJ docs but did not find and answer there.
This Lint warning is usually switched off in AJDT (AspectJ Development Tools) within Eclipse, but you can activate it as a warning or even error like this (I had to do it to actually see it at all when trying to reproduce your issue):
You can just ignore the Lint warning because basically it only says that there is no way for certain pointcuts to populate the thisJoinPoint object lazily during runtime because the pointcut has no dynamic component like if(), cflow() or similar, which is actually good news because it means that all your joinpoints can be determined statically during compile/weave time and are thus faster than dynamic pointcuts. On the other hand, the warning says that the tjp object always has to be created because for some reason it is also always needed during runtime and thus cannot be instantiated lazily.