From 443d7b03b80a03d503abc4a903100366cae5ee59 Mon Sep 17 00:00:00 2001 From: Dung Ta Van Date: Tue, 15 Dec 2020 23:04:04 +0700 Subject: [PATCH] map by constructor --- .../file/mapping/PropertiesMapper.java | 62 ++++++------ .../properties/file/struct/ClassStruct.java | 41 ++++++-- .../properties/file/struct/MethodStruct.java | 10 +- .../file/struct/PropertiesBean.java | 69 +++++++++++-- .../properties/file/util/PropertiesUtil.java | 52 ++++++++++ .../ReflectionClassUtil.java} | 45 ++++++--- .../file/testing/PropertiesMapper2Test.java | 11 +-- .../file/testing/PropertiesMapperTest.java | 58 +++++++++++ .../file/testing/ReflectionClassUtilTest.java | 96 +++++++++++++++++++ .../testing/ReflectionClassUtilsTest.java | 44 --------- 10 files changed, 372 insertions(+), 116 deletions(-) rename src/main/java/com/tvd12/properties/file/{reflect/ReflectionClassUtils.java => util/ReflectionClassUtil.java} (80%) create mode 100644 src/test/java/com/monkey/properties/file/testing/ReflectionClassUtilTest.java delete mode 100644 src/test/java/com/monkey/properties/file/testing/ReflectionClassUtilsTest.java diff --git a/src/main/java/com/tvd12/properties/file/mapping/PropertiesMapper.java b/src/main/java/com/tvd12/properties/file/mapping/PropertiesMapper.java index 574e518..776a927 100644 --- a/src/main/java/com/tvd12/properties/file/mapping/PropertiesMapper.java +++ b/src/main/java/com/tvd12/properties/file/mapping/PropertiesMapper.java @@ -11,7 +11,6 @@ import com.tvd12.properties.file.io.ValueConverter; import com.tvd12.properties.file.reader.BaseFileReader; import com.tvd12.properties.file.reader.FileReader; -import com.tvd12.properties.file.reflect.ReflectionClassUtils; import com.tvd12.properties.file.struct.PropertiesBean; import com.tvd12.properties.file.util.PropertiesUtil; @@ -223,18 +222,34 @@ public T map() { @SuppressWarnings("unchecked") public T map(Class clazz) { this.clazz(clazz); + if(reader == null) + reader = new BaseFileReader(); + this.readProperties(); if(clazz != null && Map.class.isAssignableFrom(clazz)) - return (T)getProperties(); + return (T)properties; if(propertyAnnotations == null) propertyAnnotations = new PropertyAnnotations(); - return map( - new PropertiesBean( - newBeanInstance(), - mappingLevel, - valueConverter, - propertyAnnotations, - classLoader - )); + if(bean != null) { + return map( + new PropertiesBean( + bean, + mappingLevel, + valueConverter, + propertyAnnotations, + classLoader + )); + } + else { + return map( + new PropertiesBean( + clazz, + properties, + mappingLevel, + valueConverter, + propertyAnnotations, + classLoader + )); + } } /** @@ -244,44 +259,29 @@ public T map(Class clazz) { * @param mapping a custom of properties bean mapping * @return object after mapped */ - public T map(PropertiesBean mapping) { - if(reader == null) - reader = new BaseFileReader(); - mapping.putAll(getProperties()); + private T map(PropertiesBean mapping) { + mapping.putAll(properties); T answer = mapping.getObject(); return answer; } - /** - * create object to hold data - * - * @return - */ - private Object newBeanInstance() { - if(bean == null) - bean = ReflectionClassUtils.newInstance(clazz); - else - clazz = bean.getClass(); - return bean; - } - /** * read properties file if properties object null * - * @return properties object */ - private Properties getProperties() { + private void readProperties() { try { if(properties == null) - properties = reader.read(classLoader, propertiesFile); + properties = new Properties(); + if(propertiesFile != null) + properties.putAll(reader.read(classLoader, propertiesFile)); if(propertyPrefix != null) properties = PropertiesUtil.getPropertiesByPrefix(properties, propertyPrefix); } catch (PropertiesFileException e) { throw new IllegalStateException(e); } PropertiesUtil.decorateProperties(properties); - return properties; } } diff --git a/src/main/java/com/tvd12/properties/file/struct/ClassStruct.java b/src/main/java/com/tvd12/properties/file/struct/ClassStruct.java index eceeb23..f3e44c1 100644 --- a/src/main/java/com/tvd12/properties/file/struct/ClassStruct.java +++ b/src/main/java/com/tvd12/properties/file/struct/ClassStruct.java @@ -1,14 +1,18 @@ package com.tvd12.properties.file.struct; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; import com.tvd12.properties.file.annotation.PropertyAnnotations; import com.tvd12.properties.file.mapping.MappingLevel; -import com.tvd12.properties.file.reflect.ReflectionClassUtils; +import com.tvd12.properties.file.util.ReflectionClassUtil; /** * @@ -30,6 +34,8 @@ public abstract class ClassStruct { //map of method structure and key protected final Map methods; + protected final List declaredFieldStructs; + protected final PropertyAnnotations propertyAnnotations; /** @@ -46,9 +52,11 @@ public ClassStruct( this.mappingLevel = mappingLevel; this.propertyAnnotations = propertyAnnotations; this.methods = new HashMap<>(); + this.declaredFieldStructs = new ArrayList<>(); this.methodFilter = methodFilter(); this.initWithFields(); this.initWithMethods(); + this.initWithDeclaredFields(); } @@ -73,6 +81,14 @@ private void initWithMethods() { addMethod(initWithMethod(method)); } } + + private void initWithDeclaredFields() { + Field[] fields = clazz.getDeclaredFields(); + for(Field field : fields) { + if(!Modifier.isStatic(field.getModifiers())) + declaredFieldStructs.add(initWithField(field)); + } + } /** * Initialize a MethodStruct object of java field @@ -133,7 +149,7 @@ protected MethodStruct getMethodStruct(String key) { * @param method MethodStruct object to add */ protected void addMethod(MethodStruct method) { - methods.put(method.getKey(), method); + methods.put(method.getKey(), method); } /** @@ -146,9 +162,9 @@ protected void addMethod(MethodStruct method) { protected Set getAcceptedFields() { Set fields; if(mappingLevel == MappingLevel.ALL) - fields = ReflectionClassUtils.getValidFields(clazz); + fields = ReflectionClassUtil.getValidFields(clazz); else - fields = ReflectionClassUtils.getFieldsWithAnnotations( + fields = ReflectionClassUtil.getFieldsWithAnnotations( clazz, propertyAnnotations.getAnnotationClasses()); return fields; @@ -164,9 +180,9 @@ protected Set getAcceptedFields() { protected Set getAcceptedMethods() { Set methods; if(mappingLevel == MappingLevel.ALL) - methods = ReflectionClassUtils.getPublicMethods(clazz); + methods = ReflectionClassUtil.getPublicMethods(clazz); else - methods = ReflectionClassUtils.getMethodsWithAnnotations( + methods = ReflectionClassUtil.getMethodsWithAnnotations( clazz, propertyAnnotations.getAnnotationClasses()); return methods; @@ -194,5 +210,18 @@ public int methodCount() { protected MethodFilter methodFilter() { return method -> validateMethod(method) && !containsMethod(method); } + + @SuppressWarnings("rawtypes") + public Constructor getNoArgsDeclaredConstructor() { + return ReflectionClassUtil.getNoArgsDeclaredConstructor(clazz); + } + + public Constructor getMaxArgsDeclaredConstructor() { + return ReflectionClassUtil.getMaxArgsDeclaredConstructor(clazz); + } + + public Object newObjectInstance() { + return ReflectionClassUtil.newInstance(clazz); + } } diff --git a/src/main/java/com/tvd12/properties/file/struct/MethodStruct.java b/src/main/java/com/tvd12/properties/file/struct/MethodStruct.java index 8682825..0377c23 100644 --- a/src/main/java/com/tvd12/properties/file/struct/MethodStruct.java +++ b/src/main/java/com/tvd12/properties/file/struct/MethodStruct.java @@ -6,6 +6,7 @@ import java.lang.reflect.Method; import com.tvd12.properties.file.annotation.PropertyAnnotations; +import com.tvd12.properties.file.util.PropertiesUtil; import lombok.AccessLevel; import lombok.Getter; @@ -167,14 +168,7 @@ protected String getPropertyPrefix(boolean guess) { protected String guestPropertyPrefix() { String key = getKey(); - StringBuilder builder = new StringBuilder(); - for(int i = 0 ; i < key.length() ; ++i) { - char ch = key.charAt(i); - if(Character.isUpperCase(ch) && i > 0) - builder.append("."); - builder.append(Character.toLowerCase(ch)); - } - return builder.toString(); + return PropertiesUtil.getPropertyNameInDotCase(key); } } diff --git a/src/main/java/com/tvd12/properties/file/struct/PropertiesBean.java b/src/main/java/com/tvd12/properties/file/struct/PropertiesBean.java index 7d13131..02ccda2 100644 --- a/src/main/java/com/tvd12/properties/file/struct/PropertiesBean.java +++ b/src/main/java/com/tvd12/properties/file/struct/PropertiesBean.java @@ -1,7 +1,10 @@ package com.tvd12.properties.file.struct; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.lang.reflect.Parameter; +import java.util.List; import java.util.Properties; import com.tvd12.properties.file.annotation.PropertyAnnotations; @@ -9,8 +12,9 @@ import com.tvd12.properties.file.io.ValueConverter; import com.tvd12.properties.file.mapping.MappingLevel; import com.tvd12.properties.file.mapping.PropertiesMapper; -import com.tvd12.properties.file.reflect.ReflectionClassUtils; import com.tvd12.properties.file.util.Logger; +import com.tvd12.properties.file.util.PropertiesUtil; +import com.tvd12.properties.file.util.ReflectionClassUtil; /** * @@ -37,7 +41,7 @@ public PropertiesBean( Class clazz, PropertyAnnotations propertyAnnotations) { this( - ReflectionClassUtils.newInstance(clazz), + ReflectionClassUtil.newInstance(clazz), propertyAnnotations ); } @@ -67,6 +71,20 @@ public PropertiesBean( this.valueConverter = valueConverter != null ? valueConverter : DefaultValueConverter.getInstance(); } + public PropertiesBean( + Class clazz, + Properties properties, + MappingLevel mappingLevel, + ValueConverter valueConverter, + PropertyAnnotations propertyAnnotations, + ClassLoader classLoader) { + this.wrapper = new ClassWrapper(clazz, mappingLevel, propertyAnnotations); + this.valueConverter = valueConverter != null ? valueConverter : DefaultValueConverter.getInstance(); + this.classLoader = classLoader; + this.propertyAnnotations = propertyAnnotations; + this.bean = createBean(properties); + } + public T getObject() { return (T)bean; } @@ -92,6 +110,8 @@ public void put(Object key, Object value, Properties properties) { } else { Field field = methodStruct.getField(); + if(Modifier.isFinal(field.getModifiers())) + return; if(!Modifier.isPublic(field.getModifiers())) field.setAccessible(true); field.set(bean, argument); @@ -105,7 +125,7 @@ public void put(Object key, Object value, Properties properties) { public void putAll(Properties properties) { for(Object key : wrapper.keySet()) { - Object value = properties.get(key); + Object value = PropertiesUtil.getValue(properties, key); put(key, value, properties); } } @@ -127,16 +147,21 @@ protected Object transform( Object v = value; if(v instanceof String) v = ((String) value).trim(); - return transform(argumentType, v); + return transform(v, argumentType); } try { - return new PropertiesMapper() + if(PropertiesUtil.containsPrefix(properties, prefix)) { + return new PropertiesMapper() .data(properties) .classLoader(classLoader) .propertyPrefix(prefix) .valueConverter(valueConverter) .propertyAnnotations(propertyAnnotations) .map(argumentType); + } + else { + return null; + } } catch (Exception e) { if(guessPrefix) @@ -146,10 +171,40 @@ protected Object transform( } } - protected Object transform(Class newType, Object value) { + protected Object transform(Object value, Class newType) { return valueConverter.convert(value, newType); } - + + protected Object getAndTransform(Properties properties, String key, Class newType) { + Object value = PropertiesUtil.getValue(properties, key); + if(value != null) + value = transform(value, newType); + return value; + } + + protected Object createBean(Properties properties) { + Constructor constructor = wrapper.getNoArgsDeclaredConstructor(); + if(constructor != null) + return wrapper.newObjectInstance(); + return createBeanByMaxArgsConstructor(properties); + } + + protected Object createBeanByMaxArgsConstructor(Properties properties) { + Constructor constructor = wrapper.getMaxArgsDeclaredConstructor(); + Parameter[] parameters = constructor.getParameters(); + List declaredFieldStructs = wrapper.declaredFieldStructs; + Object[] args = new Object[constructor.getParameterCount()]; + for(int i = 0 ; i < args.length ; ++i) { + Class parameterType = parameters[i].getType(); + if(i >= declaredFieldStructs.size()) { + args[i] = PropertiesUtil.defaultValueOf(parameterType); + continue; + } + String key = declaredFieldStructs.get(i).getKey(); + args[i] = getAndTransform(properties, key, parameterType); + } + return ReflectionClassUtil.newInstance(constructor, args); + } protected void printError(String message, Throwable throwable) { Logger.print(message, throwable); diff --git a/src/main/java/com/tvd12/properties/file/util/PropertiesUtil.java b/src/main/java/com/tvd12/properties/file/util/PropertiesUtil.java index 54dfb83..083af62 100644 --- a/src/main/java/com/tvd12/properties/file/util/PropertiesUtil.java +++ b/src/main/java/com/tvd12/properties/file/util/PropertiesUtil.java @@ -73,6 +73,18 @@ public static Properties getPropertiesByPrefix(Map properties, String prefix) { return answer; } + @SuppressWarnings("rawtypes") + public static boolean containsPrefix(Map properties, String prefix) { + for(Object key : properties.keySet()) { + String keyString = key.toString(); + if(keyString.startsWith(prefix) && + keyString.length() > prefix.length()) { + return true; + } + } + return false; + } + /** * Decorate properties, add new keys (replace _ and - by .) * @@ -91,4 +103,44 @@ public static void decorateProperties(Properties properties) { properties.putAll(newKeyValues); } + public static String getPropertyNameInDotCase(String key) { + StringBuilder builder = new StringBuilder(); + for(int i = 0 ; i < key.length() ; ++i) { + char ch = key.charAt(i); + if(Character.isUpperCase(ch) && i > 0) + builder.append("."); + builder.append(Character.toLowerCase(ch)); + } + return builder.toString(); + } + + public static Object getValue(Properties properties, Object key) { + Object value = properties.get(key); + if(value == null) { + String keyInDotCase = getPropertyNameInDotCase(key.toString()); + value = properties.get(keyInDotCase); + } + return value; + } + + public static Object defaultValueOf(Class type) { + if(type == boolean.class) + return false; + if(type == byte.class) + return (byte)0; + if(type == char.class) + return (char)0; + if(type == double.class) + return 0.0D; + if(type == float.class) + return 0.0F; + if(type == int.class) + return 0; + if(type == long.class) + return 0L; + if(type == short.class) + return (short)0; + return null; + } + } diff --git a/src/main/java/com/tvd12/properties/file/reflect/ReflectionClassUtils.java b/src/main/java/com/tvd12/properties/file/util/ReflectionClassUtil.java similarity index 80% rename from src/main/java/com/tvd12/properties/file/reflect/ReflectionClassUtils.java rename to src/main/java/com/tvd12/properties/file/util/ReflectionClassUtil.java index a2b4ea1..0d98232 100644 --- a/src/main/java/com/tvd12/properties/file/reflect/ReflectionClassUtils.java +++ b/src/main/java/com/tvd12/properties/file/util/ReflectionClassUtil.java @@ -1,6 +1,7 @@ -package com.tvd12.properties.file.reflect; +package com.tvd12.properties.file.util; import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -9,15 +10,24 @@ import java.util.List; import java.util.Set; -public final class ReflectionClassUtils { +@SuppressWarnings({ "rawtypes", "unchecked" }) +public final class ReflectionClassUtil { - private ReflectionClassUtils() {} + private ReflectionClassUtil() {} public static Object newInstance(Class clazz) { try { return clazz.newInstance(); } catch (Exception e) { - throw new IllegalStateException("Can not create instance of class " + clazz, e); + throw new IllegalStateException("Can not create instance of classs: " + clazz.getName(), e); + } + } + + public static Object newInstance(Constructor constructor, Object... args) { + try { + return constructor.newInstance(args); + } catch (Exception e) { + throw new IllegalStateException("Can not create instance of constructor: " + constructor, e); } } @@ -37,7 +47,6 @@ public static Set getValidFields(Class clazz) { return answer; } - @SuppressWarnings({ "rawtypes", "unchecked" }) public static Set getFieldsWithAnnotations( Class clazz, List annotationClasses) { @@ -47,7 +56,6 @@ public static Set getFieldsWithAnnotations( return answer; } - @SuppressWarnings("rawtypes") public static Set getFieldsWithAnnotation( Class clazz, Class annotationClass) { @@ -84,7 +92,6 @@ public static Set getPublicMethods(Class clazz) { return answer; } - @SuppressWarnings({ "rawtypes", "unchecked" }) public static Set getMethodsWithAnnotations( Class clazz, List annotationClasses) { @@ -94,7 +101,6 @@ public static Set getMethodsWithAnnotations( return answer; } - @SuppressWarnings("rawtypes") public static Set getMethodsWithAnnotation( Class clazz, Class annotationClass) { @@ -113,12 +119,10 @@ public static Set getMethodsWithAnnotation( return answer; } - @SuppressWarnings("rawtypes") public static Set flatSuperClasses(Class clazz) { return flatSuperClasses(clazz, false); } - @SuppressWarnings("rawtypes") public static Set flatSuperClasses(Class clazz, boolean includeObject) { Set classes = new HashSet<>(); Class superClass = clazz.getSuperclass(); @@ -131,7 +135,6 @@ public static Set flatSuperClasses(Class clazz, boolean includeObject) { return classes; } - @SuppressWarnings("rawtypes") public static Set flatInterfaces(Class clazz) { Class[] interfaces = clazz.getInterfaces(); Set classes = new HashSet<>(Arrays.asList(interfaces)); @@ -140,12 +143,10 @@ public static Set flatInterfaces(Class clazz) { return classes; } - @SuppressWarnings("rawtypes") public static Set flatSuperAndInterfaceClasses(Class clazz) { return flatSuperAndInterfaceClasses(clazz, false); } - @SuppressWarnings("rawtypes") public static Set flatSuperAndInterfaceClasses(Class clazz, boolean includeObject) { Set interfaces = flatInterfaces(clazz); Set superClasses = flatSuperClasses(clazz, includeObject); @@ -158,4 +159,22 @@ public static Set flatSuperAndInterfaceClasses(Class clazz, boolean inclu return classes; } + public static Constructor getNoArgsDeclaredConstructor(Class clazz) { + for(Constructor constructor : clazz.getDeclaredConstructors()) { + if(constructor.getParameterCount() == 0) + return constructor; + } + return null; + } + + public static Constructor getMaxArgsDeclaredConstructor(Class clazz) { + Constructor[] constructors = clazz.getDeclaredConstructors(); + Constructor max = constructors[0]; + for(int i = 1 ; i < constructors.length ; ++i) { + if(constructors[i].getParameterCount() > max.getParameterCount()) + max = constructors[i]; + } + return max; + } + } diff --git a/src/test/java/com/monkey/properties/file/testing/PropertiesMapper2Test.java b/src/test/java/com/monkey/properties/file/testing/PropertiesMapper2Test.java index 18021d8..331d32a 100644 --- a/src/test/java/com/monkey/properties/file/testing/PropertiesMapper2Test.java +++ b/src/test/java/com/monkey/properties/file/testing/PropertiesMapper2Test.java @@ -12,7 +12,6 @@ import com.tvd12.properties.file.annotation.Property; import com.tvd12.properties.file.io.Dates; import com.tvd12.properties.file.mapping.PropertiesMapper; -import com.tvd12.properties.file.struct.PropertiesBean; import lombok.Data; @@ -45,9 +44,8 @@ public void testMapPropertiesToBean() { PropertiesMapper propertiesMapper = new PropertiesMapper() .data(properties) .clazz(ClassA.class); - PropertiesBean pb = new PropertiesBean(ClassA.class); - propertiesMapper.map(pb); - ClassA objectx = propertiesMapper.map(pb); + propertiesMapper.map(ClassA.class); + ClassA objectx = propertiesMapper.map(ClassA.class); assertEquals(objectx.getName(), "hello"); assertEquals(objectx.getAge(), 24); assertEquals(objectx.getMoney(), 10); @@ -60,9 +58,8 @@ public void testMapPropertiesToBean() { PropertiesMapper propertiesMapperx = new PropertiesMapper() .data(properties) .bean(new ClassA()); - PropertiesBean pbx = new PropertiesBean(ClassA.class); - propertiesMapper.map(pb); - ClassA objectxx = propertiesMapperx.map(pbx); + propertiesMapper.map(ClassA.class); + ClassA objectxx = propertiesMapperx.map(ClassA.class); assertEquals(objectxx.getName(), "hello"); assertEquals(objectxx.getAge(), 24); assertEquals(objectxx.getMoney(), 10); diff --git a/src/test/java/com/monkey/properties/file/testing/PropertiesMapperTest.java b/src/test/java/com/monkey/properties/file/testing/PropertiesMapperTest.java index c222a98..82cc9ef 100644 --- a/src/test/java/com/monkey/properties/file/testing/PropertiesMapperTest.java +++ b/src/test/java/com/monkey/properties/file/testing/PropertiesMapperTest.java @@ -18,6 +18,7 @@ import com.tvd12.properties.file.util.PropertiesUtil; import lombok.Data; +import lombok.Getter; import lombok.Setter; public class PropertiesMapperTest { @@ -98,6 +99,7 @@ public void testMapPropertiesToBeanWithFields() { assertEquals(object.age, 24); assertEquals(object.money, 10); assertEquals(object.clazz, ClassC.class); + assertEquals(object.datasourceUsername, "hello"); assertNotNull(object.date); assertEquals(object.dataSourceConfig.username, "hello"); @@ -114,6 +116,7 @@ public void testMapPropertiesToBeanWithFields() { public void testMapPropertiesToBeanWithAnntations() { Properties properties = new Properties(); properties.setProperty("name", "hello"); + properties.put("value", "world"); properties.put("age", 24); properties.put("clazz", ClassC.class.getName()); properties.put("date", new SimpleDateFormat(Dates.getPattern()).format(new Date())); @@ -134,9 +137,11 @@ public void testMapPropertiesToBeanWithAnntations() { a -> ((PropertyForTest)a).prefix())) .map(ClassD.class); assertEquals(object.name, "hello"); + assertEquals(object.value, "value"); assertEquals(object.age, 24); assertEquals(object.money, 10); assertEquals(object.clazz, ClassC.class); + assertEquals(object.datasourceDriver, "dahlia"); assertNotNull(object.date); assertEquals(object.dataSourceConfig.username, "hello"); @@ -183,6 +188,21 @@ public void mapBase64FileToObject() { .map(); } + @Test + public void mapByConstructorTest() { + Properties properties = new Properties(); + properties.put("host", "0.0.0.0"); + properties.put("port", 3005); + properties.put("admin.name", "Admin"); + ServerConfig output = new PropertiesMapper() + .data(properties) + .map(ServerConfig.class); + assert output.getHost().equals("0.0.0.0"); + assert output.getPort() == 3005; + assert output.getAdminName().equals("Admin"); + assert output.getAdminPassword() == null; + } + @Data public static class ClassA { private String name; @@ -217,6 +237,8 @@ public static class ClassC { @Property(prefix = "cors.config.detail.enable") protected boolean corsConfigDetailEnable; + + protected String datasourceUsername; } public static class ClassD extends ClassDBase implements IClassD { @@ -234,6 +256,9 @@ public static class ClassD extends ClassDBase implements IClassD { @Setter protected Properties dataSourceProperties; + + @Property + protected String datasourceDriver; } public static class ClassDBase { @@ -241,6 +266,8 @@ public static class ClassDBase { public String name; @Property public int age; + @Property + public final String value = "value"; } public static interface IClassD extends IIClassD { @@ -272,4 +299,35 @@ public static class Cors { protected boolean enable; protected String host; } + + @Getter + public static class ServerConfig { + public static String DEFAULT1 = "1"; + private String host; + private int port; + private String adminName; + private String adminPassword; + public static String DEFAULT2 = "2"; + + public ServerConfig( + String host, + int port, + String adminName, + String adminPassword, + boolean booleanValue, + byte byteValue, + char charValue, + double doubleValue, + float floatValue, + int intValue, + long longValue, + short shortValue, + Boolean wrapperBooleanValue + ) { + this.host = host; + this.port = port; + this.adminName = adminName; + this.adminPassword = adminPassword; + } + } } diff --git a/src/test/java/com/monkey/properties/file/testing/ReflectionClassUtilTest.java b/src/test/java/com/monkey/properties/file/testing/ReflectionClassUtilTest.java new file mode 100644 index 0000000..fa88a2c --- /dev/null +++ b/src/test/java/com/monkey/properties/file/testing/ReflectionClassUtilTest.java @@ -0,0 +1,96 @@ +package com.monkey.properties.file.testing; + +import static org.testng.Assert.assertEquals; + +import java.lang.reflect.Method; +import java.util.Set; + +import org.testng.annotations.Test; + +import com.tvd12.properties.file.util.ReflectionClassUtil; +import com.tvd12.test.base.BaseTest; + +public class ReflectionClassUtilTest extends BaseTest { + + @Override + public Class getTestClass() { + return ReflectionClassUtil.class; + } + + @Test + public void test1() { + Set publicMethods = ReflectionClassUtil.getPublicMethods(ClassA.class); + assertEquals(publicMethods.size(), 1); + } + + @Test + public void test2() { + ReflectionClassUtil.flatSuperClasses(InterfaceA.class); + ReflectionClassUtil.flatSuperClasses(ClassA.class, true); + } + + @Test + public void getNoArgsDeclaredConstructorTest() { + assert ReflectionClassUtil + .getNoArgsDeclaredConstructor(BX.class) + .getParameterCount() == 0; + } + + @Test + public void getNoArgsDeclaredConstructorWithDefaultConstructorTest() { + assert ReflectionClassUtil + .getNoArgsDeclaredConstructor(CX.class) + .getParameterCount() == 0; + } + + @Test + public void getNoArgsDeclaredConstructorNullTest() { + assert ReflectionClassUtil + .getNoArgsDeclaredConstructor(DX.class) == null; + } + + @Test + public void getMaxArgsDeclaredConstructor() { + assert ReflectionClassUtil + .getMaxArgsDeclaredConstructor(BX.class) + .getParameterCount() == 2; + } + + @Test(expectedExceptions = IllegalStateException.class) + public void newInstanceByConstructorError() { + ReflectionClassUtil.newInstance( + ReflectionClassUtil.getMaxArgsDeclaredConstructor(BX.class) + , 1, 2, 3); + } + + public static interface InterfaceA { + } + + public abstract class ClassA { + + public abstract void love(); + + public void setString() { + } + + } + + public static class BX { + protected BX(String name) { + this(name, "value"); + } + + private BX(String name, String value) {} + + public BX() { + this("name"); + } + } + + public static class CX {} + + public static class DX { + public DX(String name) {} + } + +} diff --git a/src/test/java/com/monkey/properties/file/testing/ReflectionClassUtilsTest.java b/src/test/java/com/monkey/properties/file/testing/ReflectionClassUtilsTest.java deleted file mode 100644 index b1b86d9..0000000 --- a/src/test/java/com/monkey/properties/file/testing/ReflectionClassUtilsTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.monkey.properties.file.testing; - -import static org.testng.Assert.assertEquals; - -import java.lang.reflect.Method; -import java.util.Set; - -import org.testng.annotations.Test; - -import com.tvd12.properties.file.reflect.ReflectionClassUtils; -import com.tvd12.test.base.BaseTest; - -public class ReflectionClassUtilsTest extends BaseTest { - - @Override - public Class getTestClass() { - return ReflectionClassUtils.class; - } - - @Test - public void test1() { - Set publicMethods = ReflectionClassUtils.getPublicMethods(ClassA.class); - assertEquals(publicMethods.size(), 1); - } - - @Test - public void test2() { - ReflectionClassUtils.flatSuperClasses(InterfaceA.class); - ReflectionClassUtils.flatSuperClasses(ClassA.class, true); - } - - public static interface InterfaceA { - } - - public abstract class ClassA { - - public abstract void love(); - - public void setString() { - } - - } - -}