Get the App
SLTechnology News&Howtos  ›  Development  › 

Analysis of StringUtils trampling problem in Spring

Shulou Source: shulou.com Published: 2022-06-02 17:41:20 09月26日 Update

This article mainly explains "analyzing the StringUtils trampling problem of Spring". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "analyzing the StringUtils trampling problem of Spring".

cause

Recently, when I was writing CRUD, I found that the robustness of a paged VO was relatively poor. I temporarily itched to change it, but I didn't expect that there were problems with several functions after the change.

The key code of the original VO is as follows:

Public class PageVo implements Serializable {/ /... Omit all irrelevant codes Map query}

This VO is used to pass parameters when paging queries from the front end, while query is used to pass query conditions (whether passing parameters with Map is reasonable is not discussed here). If there is no query condition at the current end, it will cause query to be null. If you don't pay attention to it, it is easy to see NPE.

So I transformed it into something like this.

Public class PageVo implements Serializable {/ /... Omit all irrelevant codes Map query=new HashMap}

But I didn't expect that such a simple transformation turned over (ε').

I have no choice but to troubleshoot the problem.

Find a problem

I have thought about many reasons, but I really didn't expect it to be because of this (/ 'destroy') / ~ ╧╧. Without saying more, the key code of the problem is as follows:

If (StringUtils.isEmpty (page.getQuery () {/ / omit processing logic}

Unexpectedly use StringUtils to judge whether a Map is empty or not, at least change the CollectionUtils (╬ dish) convex.

Although the predecessors dug the hole, why is the `StringUtils of Spring designed to support Object participation?

After thinking about it, let's take a look at the source code.

Source code analysis

The source code of StringUtils's isEmpty () method is super simple, with three lines of code done (in fact, strictly speaking, one line of code):

Public static boolean isEmpty (@ Nullable Object str) {return (str = = null | | ".equals (str));}

Since my Map object is not null, the problem should be because of the equals () method of String. Don't say much, keep tracking the source code.

Public boolean equals (Object anObject) {if (this = = anObject) {return true;} / / the problem is here if (anObject instanceof String) {String anotherString = (String) anObject; int n = value.length; if (n = = anotherString.value.length) {char v1 [] = value; char v2 [] = anotherString.value; int I = 0; while (NMAV -! = 0) {if (v1 [I]! = v2 [I]) return false; iTunes;} return true;}} return false }

The logic of this equals () method is simple.

If the input parameter is null, return true input parameter. No return false input parameter for String type. Both the input parameter object and this object are String. Compare whether the length of their built-in char [] array and each char element are the same. If it is satisfied, return true, otherwise return false.

And my problem is caused by the second point, because the type is different.

Thank you for your reading. The above is the content of "analyzing the StringUtils trampling problem of Spring". After the study of this article, I believe you have a deeper understanding of the problem of analyzing the StringUtils trampling problem of Spring, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Problem code analysis source code unexpected object method learning query same key content front end that is condition type logic transformation robust one line Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Apple Docker Linux