I'm getting S1125 for a code smell:
MyVO vo;
public boolean is() {
return vo == null ? false : vo.is();
}
Remove the unnecessary boolean literal.sonarlint(java:S1125)
But when choosing Quick Fix -> SonarLint:Simplify the expression it return invalid java syntax:
return !vo == null && vo.is();
Is it Sonar Lint bug ? is my vs code settings is wrong?
Obviously it should fix to:
return vo != null && vo.is();
(Using latest VS code and sonar lint plugin)
I didn't find any issue in sonar community/Jira
Opened a question in Sonar community https://community.sonarsource.com/t/sonarlint-simplify-the-expression-wrong-syntax-on-java-s1125/62467
Which reported as SONARJAVA-4241
S1125: erroneous quick fix suggestion when negating a binary operation
and probably will be removed
It seems that covering all the cases seems tricky. We might want to simplify and simply don't suggest a quick fix if the expression is a binary operation.
Related
I am trying to clean up our legacy code, and noticed there are many if conditional code that can be merged.
Example -
if (file != null) {
if (file.isFile() || file.isDirectory()) {
/* ... */
}
}
This can be refactored to,
if (file != null && (file.isFile() || file.isDirectory())) {
/* ... */
}
Manually performing this change is a pain. So, I was trying to check on inspection tool and template refactoring in intelliji to help me with this bulk code refactoring.
Could not locate this eclipse IDE too.
Kindly suggest, is there an option in Intelliji/Eclipse for this
In IntelliJ IDEA put the text cursor on the first if keyword, press Alt+Enter and invoke Merge nested 'if's.
You can also use Structural Search & Replace to perform this operation in bulk. Use the following search pattern:
if ($a$) {
if ($b$) {
$statement$;
} else $void$;
} else $void$;
Click Edit Variables... and set the minimum and maximum count of statement to 0,∞. Also set the minimum and maximum count of void to 0,0
Use the following replacement pattern:
if (($a$) && ($b$)) {
$statement$;
}
Note that this replacement will introduce redundant parentheses in some cases to prevent changes the semantics of the code. These can later be removed again by invoking Run Inspection by Name and running the Unnecessary parentheses inspection.
The AutoRefactor Eclipse plug-in can do this for you in batch.
See http://autorefactor.org/html/samples.html and select CollapseIfStatementSample.java. You can do this on a whole file, package, or even project.
There is a refactoring to remove unnecessary parentheses too: SimplifyExpressionSample.java (see e. g. line 144).
I am using SonarLint plugin (2.1.0) with Eclipse Mars (4.5.0) and I am getting : NullPointerException might be thrown as listToCheck is nullable here in this code:
if (checkListNotNull(listToCheck)) {
listToCheck.get(0); // I get here that warning
}
checkListNotNull is a method that returns true if the list that is passed is not null
Is possible to avoid this sonar warning?
Thanks
Just do that
if(listToCheck != null && !listToCheck.isEmpty()) {
listToCheck.get(0);
}
And I think the warning will disapear
(the use of another method seems to be useless in this case; the warning is just here because of Eclipse cannot understand what does your checkListNotNull do)
How to avoid following error - ""user" is checked for null after being dereferenced.".
Highlighted code
OLCC olcc = saveOLCCRequest.getOLCC();
if (olcc != null) {
Thanks.
This is either a bug in the parasoft analyzer, or it has been able to determine that saveOLCCRequest.getOLCC() cannot return null.
It's not possible to say which from the two line snippet you've posted, but if getOLCC() cannot possibly return null then you can remove the warning by removing the null check.
i want to use and/or in JasperReport expression
i tried the following but it doesn't work:
($P{pId} == $F{id1}) or
($P{pId} == $F{id2} and F{return}=true)
? "good" : "bad"
but i am getting following exception:
Compilation exceptions: com.jaspersoft.ireport.designer.compiler.ErrorsCollector#1c9f37d net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, calculator_report1_1318247835062_860381: 191: unexpected token: or # line 191, column 144. 1 error at net.sf.jasperreports.compilers.JRGroovyCompiler.compileUnits(JRGroovyCompiler.java:88) at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:188) at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:215) at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:131) at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:509) at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572) at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
any ideas why i am getting this exception and how to solve it.
and and or are not valid Java operators. JasperReports uses Java expressions. And since all Jasper parameters and fields are objects, I doubt you want to compare them with ==. Use equals instead.
($P{pId}.equals($F{id1}) ||
($P{pId}.equals($F{id2}) && F{return}.booleanValue()))
? "good" : "bad"
This piece of code is causing a stackOverflow exception:
ISerializer serializer = buildSerializer(TestDataProvider.getAuthor());
ASObject result = (ASObject) serializer.serialize();
assert result.isNotLazyProxy
The StackOverflow is being thrown on this line: assert result.isNotLazyProxy. Note, the isNotLazyProxy method never actually gets called.
isNotLazyProxy is a extension method (what are these called in groovy?) defined as follows:
/**
* Asserts that this ASObject is not a lazy loaded proxy,
* ie - that all of it's properties' values have been included
*/
ASObject.metaClass.isNotLazyProxy = { ->
assert delegate[HibernateProxyConstants.PROXYINITIALIZED] == true
return true;
}
However, setting a breakpoint on the first line of that closure shows that it never gets called.
Instead, there's a StackOverflow thrown:
java.lang.StackOverflowError
at java.lang.System.arraycopy(Native Method)
at java.lang.String.getChars(String.java:855)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:391)
at java.lang.StringBuffer.append(StringBuffer.java:224)
at java.lang.StringBuffer.<init>(StringBuffer.java:104)
at org.codehaus.groovy.runtime.InvokerHelper.formatMap(InvokerHelper.java:557)
at org.codehaus.groovy.runtime.InvokerHelper.format(InvokerHelper.java:530)
at org.codehaus.groovy.runtime.InvokerHelper.formatList(InvokerHelper.java:602)
at org.codehaus.groovy.runtime.InvokerHelper.format(InvokerHelper.java:527)
at org.codehaus.groovy.runtime.InvokerHelper.formatMap(InvokerHelper.java:575)
snip
I'm not sure if it's relevant, but ASObject is a subclass of a Map, and it's contents may have properties that refer to other keys within itself.
I would've have thought it was relevant, except the StackOverflow appears to indicate that groovy is traversing the members of the map.
What's going on? Why is this stackoverflow occurring?
assert result.isNotLazyProxy is probably not doing what you want to do.
In groovy map.somehing is translated to map.get(something). See http://groovy.codehaus.org/JN1035-Maps :
assert map2.class == null
//field syntax always refers to value of key, even if it doesn't exist
//use getClass() instead of class for maps...
assert map2.getClass() == LinkedHashMap //the kind of Map being used
So use assert result.isNotLazyProxy().
Of course result.isNotLazyProxy should return null in your case, and the assert result.isNotLazyProxy assertion should fail. When this assertion fails groovy will display an assertion error, and the map. In your case formatting the map fails for some reason.
Reason it fails:
It is a known bug, see example. As I see, it has nothing to do with ASObject, as it uses no lists.
At first glance, it looks like something else is causing the endless recursion...
Does ASObject have any other metaClass methods? Especially those overriding the getAt( key ) method of Map
I came up with this quick test script, and it seems to work fine as I'd expect (and I think it does the same as what you say you're doing)
class ASObject {
#Delegate Map map = [ 'PROXYINITIALIZED' : true ]
}
ASObject.metaClass.isNotLazyProxy = { ->
assert delegate[ 'PROXYINITIALIZED' ] == true
return true
}
assert new ASObject().isNotLazyProxy()