Skip to content

Commit 7de8abb

Browse files
fibberyralf0131
authored andcommitted
Fix telnet not work in some scene(#4007) (#4026)
1 parent e690864 commit 7de8abb

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.lang.reflect.Proxy;
3131
import java.lang.reflect.Type;
3232
import java.util.ArrayList;
33+
import java.util.Arrays;
3334
import java.util.Collection;
3435
import java.util.Collections;
3536
import java.util.HashMap;
@@ -542,7 +543,8 @@ private static Object newInstance(Class<?> cls) {
542543
}
543544
}
544545
constructor.setAccessible(true);
545-
return constructor.newInstance(new Object[constructor.getParameterTypes().length]);
546+
Object[] parameters = Arrays.stream(constructor.getParameterTypes()).map(PojoUtils::getDefaultValue).toArray();
547+
return constructor.newInstance(parameters);
546548
} catch (InstantiationException e) {
547549
throw new RuntimeException(e.getMessage(), e);
548550
} catch (IllegalAccessException e) {
@@ -553,6 +555,21 @@ private static Object newInstance(Class<?> cls) {
553555
}
554556
}
555557

558+
/**
559+
* return init value
560+
* @param parameterType
561+
* @return
562+
*/
563+
private static Object getDefaultValue(Class<?> parameterType) {
564+
if (parameterType.getName().equals("char")) {
565+
return Character.MIN_VALUE;
566+
}
567+
if (parameterType.getName().equals("bool")) {
568+
return false;
569+
}
570+
return parameterType.isPrimitive() ? 0 : null;
571+
}
572+
556573
private static Method getSetterMethod(Class<?> cls, String property, Class<?> valueCls) {
557574
String name = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
558575
Method method = NAME_METHODS_CACHE.get(cls.getName() + "." + name + "(" + valueCls.getName() + ")");
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dubbo.common.model;
19+
20+
import java.util.Objects;
21+
22+
/**
23+
* @author: fibbery
24+
* @date: 2019-05-13 18:41
25+
* @description: this class has no nullary constructor and some field is primitive
26+
*/
27+
public class User {
28+
private int age;
29+
30+
private String name;
31+
32+
public User(int age, String name) {
33+
this.age = age;
34+
this.name = name;
35+
}
36+
37+
public int getAge() {
38+
return age;
39+
}
40+
41+
public void setAge(int age) {
42+
this.age = age;
43+
}
44+
45+
public String getName() {
46+
return name;
47+
}
48+
49+
public void setName(String name) {
50+
this.name = name;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return String.format("User name(%s) age(%d) ", name, age);
56+
}
57+
58+
@Override
59+
public boolean equals(Object o) {
60+
if (this == o) return true;
61+
if (o == null || getClass() != o.getClass()) return false;
62+
User user = (User) o;
63+
if (name == null) {
64+
if (user.name != null) {
65+
return false;
66+
}
67+
} else if (!name.equals(user.name)) {
68+
return false;
69+
}
70+
return Objects.equals(age, user.age);
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
return Objects.hash(age, name);
76+
}
77+
}

dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.apache.dubbo.common.model.Person;
2020
import org.apache.dubbo.common.model.SerializablePerson;
21+
import org.apache.dubbo.common.model.User;
2122
import org.apache.dubbo.common.model.person.BigPerson;
2223
import org.apache.dubbo.common.model.person.FullAddress;
2324
import org.apache.dubbo.common.model.person.PersonInfo;
@@ -132,6 +133,11 @@ public void test_pojo() throws Exception {
132133
assertObject(new SerializablePerson());
133134
}
134135

136+
@Test
137+
public void test_has_no_nullary_constructor_pojo() {
138+
assertObject(new User(1,"fibbery"));
139+
}
140+
135141
@Test
136142
public void test_Map_List_pojo() throws Exception {
137143
Map<String, List<Object>> map = new HashMap<String, List<Object>>();

0 commit comments

Comments
 (0)