-
Notifications
You must be signed in to change notification settings - Fork 40.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make equality checks defensive to null reference #19540
Conversation
…, and a constant or a valued object should be used to call equals
@tellmenow Please sign the Contributor License Agreement! Click here to manually synchronize the status of this Pull Request. See the FAQ for frequently asked questions. |
@jason-liu475 Thank you for signing the Contributor License Agreement! |
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 for the PR but we prefer to apply such automated inspection ourselves as usually, a number of false positives surface. This is the case, with a large number of changes not necessary. There are a few worth discussing though. Can you please revert those I've flagged for a start so that we focus on what seems legit?
@@ -82,7 +82,7 @@ private void writeStackTraceElement(PrintWriter writer, StackTraceElement elemen | |||
writer.printf("\tat %s%n", element.toString()); | |||
LockInfo lockInfo = info.getLockInfo(); | |||
if (firstElement && lockInfo != null) { | |||
if (element.getClassName().equals(Object.class.getName()) && element.getMethodName().equals("wait")) { | |||
if (element.getClassName().equals(Object.class.getName()) && "wait".equals(element.getMethodName())) { |
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.
element.getMethodName()
does not return null
so that change is not necessary.
@@ -74,7 +74,7 @@ default String getPrefix() { | |||
* @return the path as a servlet URL mapping | |||
*/ | |||
default String getServletUrlMapping() { | |||
if (getPath().equals("") || getPath().equals("/")) { | |||
if ("".equals(getPath()) || "/".equals(getPath())) { |
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.
getPath
does not return null
.
@@ -74,7 +74,7 @@ default String getUrlMapping() { | |||
if (!path.startsWith("/")) { | |||
path = "/" + path; | |||
} | |||
if (path.equals("/")) { | |||
if ("/".equals(path)) { |
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.
There is a usage of path
right above so that change isn't necessary.
@@ -265,7 +265,7 @@ public void setLoadOnStartup(int loadOnStartup) { | |||
} | |||
|
|||
public String getServletMapping() { | |||
if (this.path.equals("") || this.path.equals("/")) { | |||
if ("".equals(this.path) || "/".equals(this.path)) { |
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.path
is initialized to a value so this is not necessary.
@@ -306,7 +306,7 @@ private void disableGrabResolvers(List<? extends AnnotatedNode> nodes) { | |||
for (AnnotatedNode classNode : nodes) { | |||
List<AnnotationNode> annotations = classNode.getAnnotations(); | |||
for (AnnotationNode node : new ArrayList<>(annotations)) { | |||
if (node.getClassNode().getNameWithoutPackage().equals("GrabResolver")) { | |||
if ("GrabResolver".equals(node.getClassNode().getNameWithoutPackage())) { |
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 doesn't return null either.
@@ -210,7 +210,7 @@ | |||
|
|||
@Override | |||
public void execute() throws MojoExecutionException, MojoFailureException { | |||
if (this.project.getPackaging().equals("pom")) { | |||
if ("pom".equals(this.project.getPackaging())) { |
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.
packaging has a default so there is no need to do that.
@@ -256,7 +256,7 @@ private int hashCode(String protocol, String file) { | |||
|
|||
@Override | |||
protected boolean sameFile(URL u1, URL u2) { | |||
if (!u1.getProtocol().equals("jar") || !u2.getProtocol().equals("jar")) { | |||
if (!"jar".equals(u1.getProtocol()) || !"jar".equals(u2.getProtocol())) { |
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.
protocol is mandatory
@@ -91,7 +91,7 @@ protected RunArguments resolveJvmArguments() { | |||
|
|||
private boolean isJava13OrLater() { | |||
for (Method method : String.class.getMethods()) { | |||
if (method.getName().equals("stripIndent")) { | |||
if ("stripIndent".equals(method.getName())) { |
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.
No need to do this either.
@@ -303,7 +303,7 @@ private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) { | |||
|
|||
private boolean isSet(ConfigurableEnvironment environment, String property) { | |||
String value = environment.getProperty(property); | |||
return (value != null && !value.equals("false")); | |||
return (value != null && !"false".equals(value)); |
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.
Can be replaced by Boolean.parseBoolean
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.
!"false".equals(value) not equals s.equalsIgnoreCase("true"),eg.If enter an empty string, the former return true but the latter return false.
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.
Yeah, you're right there. Maybe not the best idea. I'll make up my mind as part of polishing this.
@@ -82,10 +82,10 @@ public Object getProperty(String name) { | |||
} | |||
|
|||
private Object getRandomValue(String type) { | |||
if (type.equals("int")) { | |||
if ("int".equals(type)) { |
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 don't think those are necessary either.
At least some of these could be considered potential bugs, especially the fact that |
@jason-liu475 thank you for making your first contribution to Spring Boot. |
When I used the "inspection code" function of idea, I found that "the equals method of Object is easy to throw a null pointer exception, and a constant or a valued object should be used to call equals".