Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Fixed AvoidCatchingNPE and AvoidThrowingRawExceptionTypes issues found by PMD #5318

Merged
merged 1 commit into from
Apr 3, 2018

Conversation

VelinYordanov
Copy link
Contributor

Fixed pmd issues found is smarthome - AvoidCatchingNPE and AvoidThrowingRawExceptionTypes.

Signed-off-by: VelinYordanov [email protected]

Copy link
Contributor

@htreu htreu left a 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
Copy link
Contributor

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()));
Copy link
Contributor

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);
Copy link
Contributor

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.

Copy link
Contributor Author

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);
Copy link
Contributor

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);
Copy link
Contributor

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
Copy link
Contributor

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":
Copy link
Contributor

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.

Copy link
Contributor

@htreu htreu left a 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.

Copy link
Contributor

@maggu2810 maggu2810 left a 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) {
Copy link
Contributor

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();
Copy link
Contributor

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) {
Copy link
Contributor

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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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".

@VelinYordanov VelinYordanov force-pushed the pmd-fixes branch 2 times, most recently from 934dafc to a6f05b6 Compare March 30, 2018 08:30
@VelinYordanov
Copy link
Contributor Author

VelinYordanov commented Mar 30, 2018

I also fixed all SimplifyBooleanExpressions and MoreThanOneLogger issues.

@VelinYordanov VelinYordanov force-pushed the pmd-fixes branch 2 times, most recently from 4a28914 to 1a77092 Compare March 30, 2018 11:24
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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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).

Copy link
Contributor

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);
         }

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor

@maggu2810 maggu2810 left a 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).

@VelinYordanov
Copy link
Contributor Author

I added a suppression for the SecureHttpClientFactory in regards to @htreu 's review

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.

@maggu2810
Copy link
Contributor

  • 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.

@sobimibos can you please give us some details?

@maggu2810
Copy link
Contributor

@VelinYordanov I added another issue, so this one could be merged as soon as Travis CI succeeded.

@maggu2810 maggu2810 merged commit 9828c8f into eclipse-archived:master Apr 3, 2018
@maggu2810 maggu2810 mentioned this pull request Apr 4, 2018
sjsf pushed a commit to sjsf/smarthome that referenced this pull request Apr 9, 2018
...which were introduced by eclipse-archived#5318.

Signed-off-by: Simon Kaufmann <[email protected]>
sjsf pushed a commit to sjsf/smarthome that referenced this pull request Apr 9, 2018
...which were introduced by eclipse-archived#5318.

fixes eclipse-archived#5373
Signed-off-by: Simon Kaufmann <[email protected]>
@sjsf sjsf mentioned this pull request Apr 9, 2018
kaikreuzer pushed a commit that referenced this pull request Apr 9, 2018
...which were introduced by #5318.

fixes #5373
Signed-off-by: Simon Kaufmann <[email protected]>
afuechsel pushed a commit to afuechsel/smarthome that referenced this pull request Apr 13, 2018
...which were introduced by eclipse-archived#5318.

fixes eclipse-archived#5373
Signed-off-by: Simon Kaufmann <[email protected]>
@htreu htreu added this to the 0.10.0 milestone Oct 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants