Skip to content

Commit

Permalink
simplified code (#4865)
Browse files Browse the repository at this point in the history
* fix convertMethodConfig2AsyncInfo spelling error

* simplified code

* refact: simplified code

* feat: add unittest of JavaBeanSerializeUtilTest class

* feat: add unittest of JavaBeanSerializeUtilTest class
  • Loading branch information
leechor authored and beiwei30 committed Aug 19, 2019
1 parent b5bfacc commit 00653c3
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 54 deletions.
Expand Up @@ -44,8 +44,8 @@ public abstract class AbstractLoadBalance implements LoadBalance {
* @return weight which takes warmup into account
*/
static int calculateWarmupWeight(int uptime, int warmup, int weight) {
int ww = (int) ((float) uptime / ((float) warmup / (float) weight));
return ww < 1 ? 1 : (ww > weight ? weight : ww);
int ww = (int) ( uptime / ((float) warmup / weight));
return ww < 1 ? 1 : (Math.min(ww, weight));
}

@Override
Expand All @@ -70,7 +70,7 @@ public <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invo
* @param invocation the invocation of this invoker
* @return weight
*/
protected int getWeight(Invoker<?> invoker, Invocation invocation) {
int getWeight(Invoker<?> invoker, Invocation invocation) {
int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), WEIGHT_KEY, DEFAULT_WEIGHT);
if (weight > 0) {
long timestamp = invoker.getUrl().getParameter(REMOTE_TIMESTAMP_KEY, 0L);
Expand All @@ -82,7 +82,7 @@ protected int getWeight(Invoker<?> invoker, Invocation invocation) {
}
}
}
return weight >= 0 ? weight : 0;
return Math.max(weight, 0);
}

}
Expand Up @@ -52,6 +52,7 @@ public boolean isAvailable() {
return available;
}

@Override
public Result invoke(Invocation invocation) throws RpcException {
return null;
}
Expand Down
Expand Up @@ -481,6 +481,7 @@ public boolean isAvailable() {
return false;
}

@Override
public Result invoke(Invocation invocation) throws RpcException {
throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test timeout");
}
Expand Down
Expand Up @@ -24,6 +24,8 @@

public final class JavaBeanDescriptor implements Serializable, Iterable<Map.Entry<Object, Object>> {

private static final long serialVersionUID = -8505586483570518029L;

public static final int TYPE_CLASS = 1;
public static final int TYPE_ENUM = 2;
public static final int TYPE_COLLECTION = 3;
Expand All @@ -34,11 +36,9 @@ public final class JavaBeanDescriptor implements Serializable, Iterable<Map.Entr
*/
public static final int TYPE_PRIMITIVE = 6;
public static final int TYPE_BEAN = 7;
private static final long serialVersionUID = -8505586483570518029L;
private static final String ENUM_PROPERTY_NAME = "name";

private static final String ENUM_PROPERTY_NAME = "name";
private static final String CLASS_PROPERTY_NAME = "name";

private static final String PRIMITIVE_PROPERTY_VALUE = "value";

/**
Expand All @@ -47,7 +47,6 @@ public final class JavaBeanDescriptor implements Serializable, Iterable<Map.Entr
* @see #isValidType(int)
*/
private static final int TYPE_MAX = TYPE_BEAN;

/**
* Used to define a type is valid.
*
Expand All @@ -56,13 +55,11 @@ public final class JavaBeanDescriptor implements Serializable, Iterable<Map.Entr
private static final int TYPE_MIN = TYPE_CLASS;

private String className;

private int type;

private Map<Object, Object> properties = new LinkedHashMap<Object, Object>();
private Map<Object, Object> properties = new LinkedHashMap<>();

public JavaBeanDescriptor() {
}
public JavaBeanDescriptor() {}

public JavaBeanDescriptor(String className, int type) {
notEmpty(className, "class name is empty");
Expand Down Expand Up @@ -120,9 +117,7 @@ public void setClassName(String className) {

public Object setProperty(Object propertyName, Object propertyValue) {
notNull(propertyName, "Property name is null");

Object oldValue = properties.put(propertyName, propertyValue);
return oldValue;
return properties.put(propertyName, propertyValue);
}

public String setEnumNameProperty(String name) {
Expand Down Expand Up @@ -173,8 +168,7 @@ public Object getPrimitiveProperty() {

public Object getProperty(Object propertyName) {
notNull(propertyName, "Property name is null");
Object propertyValue = properties.get(propertyName);
return propertyValue;
return properties.get(propertyName);
}

public boolean containsProperty(Object propertyName) {
Expand Down
Expand Up @@ -62,8 +62,7 @@ private JavaBeanSerializeUtil() {
}

public static JavaBeanDescriptor serialize(Object obj) {
JavaBeanDescriptor result = serialize(obj, JavaBeanAccessor.FIELD);
return result;
return serialize(obj, JavaBeanAccessor.FIELD);
}

public static JavaBeanDescriptor serialize(Object obj, JavaBeanAccessor accessor) {
Expand All @@ -74,8 +73,7 @@ public static JavaBeanDescriptor serialize(Object obj, JavaBeanAccessor accessor
return (JavaBeanDescriptor) obj;
}
IdentityHashMap<Object, JavaBeanDescriptor> cache = new IdentityHashMap<Object, JavaBeanDescriptor>();
JavaBeanDescriptor result = createDescriptorIfAbsent(obj, accessor, cache);
return result;
return createDescriptorIfAbsent(obj, accessor, cache);
}

private static JavaBeanDescriptor createDescriptorForSerialize(Class<?> cl) {
Expand Down Expand Up @@ -189,10 +187,9 @@ private static void serializeInternal(JavaBeanDescriptor descriptor, Object obj,
} // ~ end of method serializeInternal

public static Object deserialize(JavaBeanDescriptor beanDescriptor) {
Object result = deserialize(
return deserialize(
beanDescriptor,
Thread.currentThread().getContextClassLoader());
return result;
}

public static Object deserialize(JavaBeanDescriptor beanDescriptor, ClassLoader loader) {
Expand Down Expand Up @@ -331,11 +328,7 @@ private static Object instantiate(Class<?> cl) throws Exception {
try {
constructor.setAccessible(true);
return constructor.newInstance(constructorArgs);
} catch (InstantiationException e) {
LogHelper.warn(logger, e.getMessage(), e);
} catch (IllegalAccessException e) {
LogHelper.warn(logger, e.getMessage(), e);
} catch (InvocationTargetException e) {
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
LogHelper.warn(logger, e.getMessage(), e);
}
}
Expand Down Expand Up @@ -369,27 +362,31 @@ private static Object instantiateForDeserialize(JavaBeanDescriptor beanDescripto
if (cache.containsKey(beanDescriptor)) {
return cache.get(beanDescriptor);
}
Object result = null;

if (beanDescriptor.isClassType()) {
try {
result = name2Class(loader, beanDescriptor.getClassNameProperty());
return result;
return name2Class(loader, beanDescriptor.getClassNameProperty());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
}
} else if (beanDescriptor.isEnumType()) {
}

if (beanDescriptor.isEnumType()) {
try {
Class<?> enumType = name2Class(loader, beanDescriptor.getClassName());
Method method = getEnumValueOfMethod(enumType);
result = method.invoke(null, enumType, beanDescriptor.getEnumPropertyName());
return result;
return method.invoke(null, enumType, beanDescriptor.getEnumPropertyName());
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
} else if (beanDescriptor.isPrimitiveType()) {
result = beanDescriptor.getPrimitiveProperty();
return result;
} else if (beanDescriptor.isArrayType()) {
}

if (beanDescriptor.isPrimitiveType()) {
return beanDescriptor.getPrimitiveProperty();
}

Object result;
if (beanDescriptor.isArrayType()) {
Class<?> componentType;
try {
componentType = name2Class(loader, beanDescriptor.getClassName());
Expand All @@ -403,8 +400,6 @@ private static Object instantiateForDeserialize(JavaBeanDescriptor beanDescripto
Class<?> cl = name2Class(loader, beanDescriptor.getClassName());
result = instantiate(cl);
cache.put(beanDescriptor, result);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
Expand Down
Expand Up @@ -80,8 +80,6 @@ public class ExtensionLoader<T> {

private static final ConcurrentMap<Class<?>, Object> EXTENSION_INSTANCES = new ConcurrentHashMap<>();

// ==============================

private final Class<?> type;

private final ExtensionFactory objectFactory;
Expand Down
Expand Up @@ -75,7 +75,8 @@ public void testDeserialize_Primitive() {
@Test
public void testDeserialize_Primitive0() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_BEAN + 1);
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(),
JavaBeanDescriptor.TYPE_BEAN + 1);
});
}

Expand All @@ -89,55 +90,81 @@ public void testDeserialize_Null() {
@Test
public void testDeserialize_containsProperty() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE);
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(),
JavaBeanDescriptor.TYPE_PRIMITIVE);
descriptor.containsProperty(null);
});
}

@Test
public void testSetEnumNameProperty() {
Assertions.assertThrows(IllegalStateException.class, () -> {
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE);
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(),
JavaBeanDescriptor.TYPE_PRIMITIVE);
descriptor.setEnumNameProperty(JavaBeanDescriptor.class.getName());
});

JavaBeanDescriptor descriptor = new JavaBeanDescriptor(JavaBeanDescriptor.class.getName(),
JavaBeanDescriptor.TYPE_ENUM);

String oldValueOrigin = descriptor.setEnumNameProperty(JavaBeanDescriptor.class.getName());
Assertions.assertNull(oldValueOrigin);

String oldValueNext = descriptor.setEnumNameProperty(JavaBeanDescriptor.class.getName());
Assertions.assertEquals(oldValueNext, descriptor.getEnumPropertyName());
}

@Test
public void testGetEnumNameProperty() {
Assertions.assertThrows(IllegalStateException.class, () -> {
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE);
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(),
JavaBeanDescriptor.TYPE_PRIMITIVE);
descriptor.getEnumPropertyName();
});
}

@Test
public void testSetClassNameProperty() {

Assertions.assertThrows(IllegalStateException.class, () -> {
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE);
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(),
JavaBeanDescriptor.TYPE_PRIMITIVE);
descriptor.setClassNameProperty(JavaBeanDescriptor.class.getName());
});

JavaBeanDescriptor descriptor = new JavaBeanDescriptor(JavaBeanDescriptor.class.getName(),
JavaBeanDescriptor.TYPE_CLASS);

String oldValue1 = descriptor.setClassNameProperty(JavaBeanDescriptor.class.getName());
Assertions.assertNull(oldValue1);

String oldValue2 = descriptor.setClassNameProperty(JavaBeanDescriptor.class.getName());
Assertions.assertEquals(oldValue2, descriptor.getClassNameProperty());
}

@Test
public void testGetClassNameProperty() {
Assertions.assertThrows(IllegalStateException.class, () -> {
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(), JavaBeanDescriptor.TYPE_PRIMITIVE);
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(long.class.getName(),
JavaBeanDescriptor.TYPE_PRIMITIVE);
descriptor.getClassNameProperty();
});
}

@Test
public void testSetPrimitiveProperty() {
Assertions.assertThrows(IllegalStateException.class, () -> {
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(JavaBeanDescriptor.class.getName(), JavaBeanDescriptor.TYPE_BEAN);
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(JavaBeanDescriptor.class.getName(),
JavaBeanDescriptor.TYPE_BEAN);
descriptor.setPrimitiveProperty(JavaBeanDescriptor.class.getName());
});
}

@Test
public void testGetPrimitiveProperty() {
Assertions.assertThrows(IllegalStateException.class, () -> {
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(JavaBeanDescriptor.class.getName(), JavaBeanDescriptor.TYPE_BEAN);
JavaBeanDescriptor descriptor = new JavaBeanDescriptor(JavaBeanDescriptor.class.getName(),
JavaBeanDescriptor.TYPE_BEAN);
descriptor.getPrimitiveProperty();
});
}
Expand Down Expand Up @@ -171,7 +198,8 @@ public void testSerialize_Array() {
if (integers[i] == null) {
Assertions.assertSame(integers[i], descriptor.getProperty(i));
} else {
Assertions.assertEquals(integers[i], ((JavaBeanDescriptor) descriptor.getProperty(i)).getPrimitiveProperty());
Assertions.assertEquals(integers[i], ((JavaBeanDescriptor) descriptor.getProperty(i))
.getPrimitiveProperty());
}
}

Expand Down Expand Up @@ -254,7 +282,8 @@ public void testDeserialize_Array() {
}

descriptor = new JavaBeanDescriptor(BigPerson[].class.getName(), JavaBeanDescriptor.TYPE_ARRAY);
JavaBeanDescriptor innerDescriptor = new JavaBeanDescriptor(BigPerson.class.getName(), JavaBeanDescriptor.TYPE_ARRAY);
JavaBeanDescriptor innerDescriptor = new JavaBeanDescriptor(BigPerson.class.getName(),
JavaBeanDescriptor.TYPE_ARRAY);
innerDescriptor.setProperty(0, JavaBeanSerializeUtil.serialize(createBigPerson(), JavaBeanAccessor.METHOD));
descriptor.setProperty(0, innerDescriptor);

Expand Down Expand Up @@ -318,7 +347,8 @@ public void testBeanSerialize() {
assertEqualsPrimitive(bean.getDate(), descriptor.getProperty("date"));
assertEqualsEnum(bean.getStatus(), descriptor.getProperty("status"));
Assertions.assertTrue(((JavaBeanDescriptor) descriptor.getProperty("type")).isClassType());
Assertions.assertEquals(Bean.class.getName(), ((JavaBeanDescriptor) descriptor.getProperty("type")).getClassNameProperty());
Assertions.assertEquals(Bean.class.getName(), ((JavaBeanDescriptor) descriptor.getProperty("type"))
.getClassNameProperty());
Assertions.assertTrue(((JavaBeanDescriptor) descriptor.getProperty("array")).isArrayType());
Assertions.assertEquals(0, ((JavaBeanDescriptor) descriptor.getProperty("array")).propertySize());

Expand All @@ -335,7 +365,6 @@ public void testBeanSerialize() {
Assertions.assertEquals(bean.getAddresses().getClass().getName(), property.getClassName());
Assertions.assertEquals(1, property.propertySize());


Map.Entry<Object, Object> entry = property.iterator().next();
Assertions.assertTrue(((JavaBeanDescriptor) entry.getKey()).isPrimitiveType());
Assertions.assertEquals("first", ((JavaBeanDescriptor) entry.getKey()).getPrimitiveProperty());
Expand Down
Expand Up @@ -66,6 +66,7 @@ public boolean isAvailable() {
return true;
}

@Override
public Result invoke(Invocation invocation) throws RpcException {
return null;
}
Expand Down
Expand Up @@ -76,6 +76,7 @@ public boolean isAvailable() {
return false;
}

@Override
public Result invoke(Invocation invocation) throws RpcException {
lastInvocation = invocation;
return AsyncRpcResult.newDefaultAsyncResult(invocation);
Expand Down
Expand Up @@ -70,6 +70,7 @@ public boolean isAvailable() {
return false;
}

@Override
public Result invoke(Invocation invocation) throws RpcException {
return null;
}
Expand Down
Expand Up @@ -103,6 +103,7 @@ public boolean isAvailable() {
return false;
}

@Override
public Result invoke(Invocation invocation) throws RpcException {
throw new RpcException(RpcException.TIMEOUT_EXCEPTION);
}
Expand Down
Expand Up @@ -58,6 +58,7 @@ public boolean isAvailable() {
return false;
}

@Override
public Result invoke(Invocation invocation) throws RpcException {
AppResponse result = new AppResponse();
if (!hasException) {
Expand Down

0 comments on commit 00653c3

Please sign in to comment.