Skip to content
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

[Dubbo-3069] Fix NumberFormatException for input string "" #3076

Closed
wants to merge 10 commits into from

Conversation

maoyiz
Copy link
Contributor

@maoyiz maoyiz commented Dec 27, 2018

Fix #3069

What is the purpose of the change

Fix NumberFormatException for input string "" or " " or null in convertPrimitive(Class<?> type, String value)

Brief changelog

Update files:

ClassHelper
StringUtils
ClassHelperTest
StringUtilsTest

Verifying this change

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a GITHUB_issue field for the change (usually before you start working on it). Trivial changes like typos do not require a GITHUB issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [Dubbo-XXX] Fix UnknownException when host config not exist #XXX. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn clean install -DskipTests & mvn clean test-compile failsafe:integration-test to make sure unit-test and integration-test pass.
  • If this contribution is large, please follow the Software Donation Guide.

@cdfive
Copy link

cdfive commented Dec 27, 2018

Hi, @zhaixiaoxiang

if (type == char.class || type == Character.class) {
    return value.length() > 0 ? value.charAt(0) : '\0';
}

Return '\0' may be confusing.

How about put the if StringUtils.isRealBlank(value) in the first line of this method?
But be careful if the '\0' is used in other place.

@carryxyh carryxyh mentioned this pull request Dec 27, 2018
6 tasks
@carryxyh
Copy link
Member

duplicate: #3075

@maoyiz
Copy link
Contributor Author

maoyiz commented Dec 27, 2018

Hi, @zhaixiaoxiang

if (type == char.class || type == Character.class) {
    return value.length() > 0 ? value.charAt(0) : '\0';
}

Return '\0' may be confusing.

How about put the if StringUtils.isRealBlank(value) in the first line of this method?
But be careful if the '\0' is used in other place.

Yes, ClassHelper.convertPrimitive() is only used in one place, and '\0' is not used, so I remove '\0'.

if (StringUtils.isNotEmpty(value) && ClassHelper.isTypeMatch(method.getParameterTypes()[0], value)) {
    method.invoke(this, ClassHelper.convertPrimitive(method.getParameterTypes()[0], value));
}

@@ -343,6 +343,20 @@ public static boolean isBlank(String str) {
return isEmpty(str);
}

public static boolean isRealBlank(CharSequence cs) {
int strLen;
if (cs != null && (strLen = cs.length()) != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be replaced by 'StringUtils.isNotEmpty(cs)'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz ignore my comment.

@@ -343,6 +343,20 @@ public static boolean isBlank(String str) {
return isEmpty(str);
}

public static boolean isRealBlank(CharSequence cs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be replace is

public static boolean isRealBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }
        }
        return true;
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, what can be replaced by what?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

existing isRealBlank can be change with given one. It is not exact replacement, I have paste the whole code of isRealBlank with modified one so that you don't have to spend time modifying line by line. In the modified one I have remove the extra return statement because the return at the end of method is good enough for the purpose.

@codecov-io
Copy link

Codecov Report

❗ No coverage uploaded for pull request base (master@0282a42). Click here to learn what that means.
The diff coverage is 84.21%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #3076   +/-   ##
=========================================
  Coverage          ?   63.56%           
  Complexity        ?       75           
=========================================
  Files             ?      653           
  Lines             ?    28228           
  Branches          ?     4807           
=========================================
  Hits              ?    17943           
  Misses            ?     8010           
  Partials          ?     2275
Impacted Files Coverage Δ Complexity Δ
...java/org/apache/dubbo/rpc/cluster/RouterChain.java 96.87% <ø> (ø) 0 <0> (?)
...ster/router/condition/config/AppRouterFactory.java 88.88% <ø> (ø) 0 <0> (?)
.../dubbo/registry/integration/RegistryDirectory.java 80% <0%> (ø) 0 <0> (?)
...ava/org/apache/dubbo/common/utils/StringUtils.java 86.85% <100%> (ø) 0 <0> (?)
...ava/org/apache/dubbo/common/utils/ClassHelper.java 69.72% <83.33%> (ø) 0 <0> (?)
...ster/router/condition/config/ListenableRouter.java 28.57% <83.33%> (ø) 0 <0> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0282a42...925ccfb. Read the comment docs.

Copy link
Contributor

@ralf0131 ralf0131 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, there are too many unrelated changes.
Please focus on one single improvement at a time.

} else if (type == boolean.class || type == Boolean.class) {
return Boolean.valueOf(value);
} else if (StringUtils.isRealBlank(value)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

I think we don't need to call isRealBlank, because there is too much to check. If we really want to check, we need to make sure the input value is numeric. I have left comment on #3093

return false;
}
}
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this return (after for loop)is not requires and remove the else and keep just return true;

@maoyiz maoyiz closed this Jan 3, 2019
@maoyiz maoyiz deleted the issue-3069 branch January 3, 2019 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

java.lang.NumberFormatException: For input string: ""
7 participants