-
Notifications
You must be signed in to change notification settings - Fork 782
Fixed AvoidCatchingNPE and AvoidThrowingRawExceptionTypes issues found by PMD #5318
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most changes are unexpected formatter changes. Please check your formatter setting. I expect no JavaDoc & switch/case reformattings in this PR.
@@ -29,20 +29,28 @@ | |||
import org.slf4j.LoggerFactory; | |||
|
|||
/** | |||
* This is an abstract class that can be used when implementing any module handler that handles scripts. | |||
* This is an abstract class that can be used when implementing any module |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check your formatter settings. The maximum line width for ESH is 120.
String.format("Type is missing in the configuration of module '%s'.", module.getId())); | ||
} else if (script == null || !(script instanceof String) || ((String) script).trim().isEmpty()) { | ||
throw new RuntimeException( | ||
String.format("Script is missing in the configuration of module '%s'.", module.getId())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this removal the individual error description gets lost.
@@ -174,7 +174,7 @@ public Void run() { | |||
if (cause instanceof RuntimeException) { | |||
throw (RuntimeException) cause; | |||
} else { | |||
throw new RuntimeException(cause); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the lines above this looks like the RuntimeException is thrown on purpose here. @sobimibos can you clarify here what we can do alternatively? Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I discarded the changes and added an exception for this class for now.
@@ -232,7 +232,7 @@ public HttpClient run() { | |||
if (cause instanceof RuntimeException) { | |||
throw (RuntimeException) cause; | |||
} else { | |||
throw new RuntimeException(cause); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
@@ -164,7 +164,7 @@ private Response createResponse(Status status, Object entity) { | |||
PipedInputStream in = new PipedInputStream(out); | |||
rp.entity(in); | |||
} catch (IOException e) { | |||
throw new RuntimeException(e); | |||
throw new IllegalStateException("I/O error occurred"+e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add the original exception as a parameter here.
* @param state1 Reference state | ||
* @param state2 State which is checked for equality. | ||
* @param state1 | ||
* Reference state |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this line break is also not standard with the ESH code formatter.
@@ -161,24 +168,25 @@ public boolean isAuthenticated() { | |||
String lastScan = safeFromJson(result.getBody(), NewLightsResponse.class).lastscan; | |||
|
|||
switch (lastScan) { | |||
case "none": | |||
case "none": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is also not ESH formatter conform.
f8c8ee6
to
9e72e13
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, lgtm. Lets wait for Travis' opinion on this before merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added my review comments.
} else { | ||
this.type = (String) type; | ||
this.script = (String) script; | ||
} | ||
} | ||
|
||
private boolean isNotValid(Object parameter) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that we would like to check if the parameter is not valid in the cases above.
But I don't think that methods that check that something is not true should be used.
If we want to check if a collection is not empty, we call !collection.isEmpty()
instead of collection.isNotEmpty()
.
I would also prefer to provide a method isValid(Object parameter)
and use !isValid(type)
above.
} else { | ||
this.type = (String) type; | ||
this.script = (String) script; | ||
} | ||
} | ||
|
||
private boolean isNotValid(Object parameter) { | ||
return (parameter == null) || !(parameter instanceof String) || ((String) parameter).trim().isEmpty(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we add braces that are not necessary? At least the one around parameter == null
could be removed.
@@ -873,6 +873,10 @@ private void handleErrors(Result result) throws IOException, ApiException { | |||
} | |||
|
|||
for (ErrorResponse error : errors) { | |||
if(error == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the formatter is still not correctly applied to all changes.
@@ -3,3 +3,8 @@ org.eclipse.smarthome.io.console.karaf.internal.OSGiConsole=SystemPrintln | |||
org.eclipse.smarthome.io.console.rfc147.internal.CommandWrapper=SystemPrintln | |||
org.eclipse.smarthome.io.console.rfc147.internal.OSGiConsole=SystemPrintln | |||
org.eclipse.smarthome.core.internal.events.ThreadedEventHandler=EmptyIfStmt | |||
org.eclipse.smarthome.config.xml.ConfigDescriptionConverter=AvoidCatchingNPE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it still necessary to catch NPE?
Can you point me to the places where it cannot be checked before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the check should be suppressed for ModelRepositoryImpl and JavaTest. The only reason for the rest is that removing the catching of NPE would lead to longer and a bit uglier code. I could refactor them as well if you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay...
The only reason for the rest is that removing the catching of NPE would lead to longer and a bit uglier code. I could refactor them as well if you prefer.
I cannot judge without having a look at.
Test for null and prevent NPEs in front of instead of catching it normally result into a cleaner (perhaps this depends on the POV) code. I will have a short look at that classes and try to understand why the code gets "uglier".
934dafc
to
a6f05b6
Compare
I also fixed all SimplifyBooleanExpressions and MoreThanOneLogger issues. |
4a28914
to
1a77092
Compare
org.eclipse.smarthome.config.xml.ConfigDescriptionConverter=AvoidCatchingNPE | ||
org.eclipse.smarthome.io.rest.core.internal.thing.ThingResource=AvoidCatchingNPE | ||
org.eclipse.smarthome.test.java.JavaTest=AvoidCatchingNPE | ||
org.eclipse.smarthome.model.core.internal.ModelRepositoryImpl=AvoidCatchingNPE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WRT #3335 and the Xtext magic, I don't care -- it's black magic for me 😉
@@ -3,3 +3,8 @@ org.eclipse.smarthome.io.console.karaf.internal.OSGiConsole=SystemPrintln | |||
org.eclipse.smarthome.io.console.rfc147.internal.CommandWrapper=SystemPrintln | |||
org.eclipse.smarthome.io.console.rfc147.internal.OSGiConsole=SystemPrintln | |||
org.eclipse.smarthome.core.internal.events.ThreadedEventHandler=EmptyIfStmt | |||
org.eclipse.smarthome.config.xml.ConfigDescriptionConverter=AvoidCatchingNPE | |||
org.eclipse.smarthome.io.rest.core.internal.thing.ThingResource=AvoidCatchingNPE | |||
org.eclipse.smarthome.test.java.JavaTest=AvoidCatchingNPE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps okay for relaxed test writings, but hey, we are using pure Java tests and no Groovy, so should be easy to fix (you could also do it in a separate PR and create an tracker issue now if too much tests needs to be fixed).
@@ -3,3 +3,8 @@ org.eclipse.smarthome.io.console.karaf.internal.OSGiConsole=SystemPrintln | |||
org.eclipse.smarthome.io.console.rfc147.internal.CommandWrapper=SystemPrintln | |||
org.eclipse.smarthome.io.console.rfc147.internal.OSGiConsole=SystemPrintln | |||
org.eclipse.smarthome.core.internal.events.ThreadedEventHandler=EmptyIfStmt | |||
org.eclipse.smarthome.config.xml.ConfigDescriptionConverter=AvoidCatchingNPE | |||
org.eclipse.smarthome.io.rest.core.internal.thing.ThingResource=AvoidCatchingNPE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be easy to fix? At which places do we expect that an NPE is thrown?
@@ -3,3 +3,8 @@ org.eclipse.smarthome.io.console.karaf.internal.OSGiConsole=SystemPrintln | |||
org.eclipse.smarthome.io.console.rfc147.internal.CommandWrapper=SystemPrintln | |||
org.eclipse.smarthome.io.console.rfc147.internal.OSGiConsole=SystemPrintln | |||
org.eclipse.smarthome.core.internal.events.ThreadedEventHandler=EmptyIfStmt | |||
org.eclipse.smarthome.config.xml.ConfigDescriptionConverter=AvoidCatchingNPE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is rather easy to fix.
The URI constructor will throw a NPE if the provided text is null only (AFAIK).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this already do the job?
@@ -62,12 +62,15 @@ public class ConfigDescriptionConverter extends GenericUnmarshaller<ConfigDescri
// the URI could be overridden by a context field if it could be
// automatically extracted
uriText = (String) context.get("config-description.uri");
+ if (uriText == null) {
+ throw new ConversionException("Missing URI.");
+ }
}
URI uri = null;
try {
uri = new URI(uriText);
- } catch (NullPointerException | URISyntaxException ex) {
+ } catch (URISyntaxException ex) {
throw new ConversionException(
"The URI '" + uriText + "' in node '" + reader.getNodeName() + "' is invalid!", ex);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I updated the PR with the required changes.
SimplifyBooleanExpressions, MoreThanOneLogger and AvoidThrowingRawExceptionTypes. Signed-off-by: VelinYordanov <[email protected]>
@@ -3,3 +3,5 @@ org.eclipse.smarthome.io.console.karaf.internal.OSGiConsole=SystemPrintln | |||
org.eclipse.smarthome.io.console.rfc147.internal.CommandWrapper=SystemPrintln | |||
org.eclipse.smarthome.io.console.rfc147.internal.OSGiConsole=SystemPrintln | |||
org.eclipse.smarthome.core.internal.events.ThreadedEventHandler=EmptyIfStmt | |||
org.eclipse.smarthome.model.core.internal.ModelRepositoryImpl=AvoidCatchingNPE | |||
org.eclipse.smarthome.io.net.http.internal.SecureHttpClientFactory=AvoidThrowingRawExceptionTypes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question remaining about that one (so, this is perhaps more addresses to the author of that file and other ones caring about the API):
- Why not using a more specific exception instead of encapsulate all ones into
RuntimeException
(at the relevant places)? - Why not using a checked exception instead of an unchecked? If there is a potential error the caller should handle it correctly. Now, the caller gets not informed (without reading the called function implementation) that he needs to handle that error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your fixed.
I have just one remaining question that addresses the SecureHttpClientFactory
implementation (so it address not directly your changed, but you point me to that as you introduced a suppression).
I added a suppression for the
|
@sobimibos can you please give us some details? |
@VelinYordanov I added another issue, so this one could be merged as soon as Travis CI succeeded. |
...which were introduced by eclipse-archived#5318. Signed-off-by: Simon Kaufmann <[email protected]>
...which were introduced by eclipse-archived#5318. fixes eclipse-archived#5373 Signed-off-by: Simon Kaufmann <[email protected]>
...which were introduced by #5318. fixes #5373 Signed-off-by: Simon Kaufmann <[email protected]>
...which were introduced by eclipse-archived#5318. fixes eclipse-archived#5373 Signed-off-by: Simon Kaufmann <[email protected]>
Fixed pmd issues found is smarthome - AvoidCatchingNPE and AvoidThrowingRawExceptionTypes.
Signed-off-by: VelinYordanov [email protected]