Skip to content

Commit

Permalink
start new version
Browse files Browse the repository at this point in the history
  • Loading branch information
tvd12 committed Nov 8, 2018
1 parent e83c9a5 commit 6f2ad25
Show file tree
Hide file tree
Showing 28 changed files with 642 additions and 409 deletions.
25 changes: 1 addition & 24 deletions pom.xml
Expand Up @@ -9,7 +9,7 @@

<groupId>com.tvd12</groupId>
<artifactId>properties-file</artifactId>
<version>1.0.4-SNAPSHOT</version>
<version>1.0.5-SNAPSHOT</version>
<packaging>jar</packaging>

<name>properties-file</name>
Expand Down Expand Up @@ -71,33 +71,10 @@
</distributionManagement>

<dependencies>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/tvd12/properties/file/bean/Transformer.java
@@ -0,0 +1,7 @@
package com.tvd12.properties.file.bean;

public interface Transformer {

public Object transform(Object input);

}
@@ -1,13 +1,14 @@
package com.tvd12.properties.file.constant;

public interface Constants {
public final class Constants {

static final String COMMENT = "written by com.tvd12:properties-file";

static final String DATE_FORMATS[] = {
public static final String COMMENT = "written by com.tvd12:properties-file";
public static final String DATE_FORMATS[] = {
"yyyy-MM-dd'T'HH:mm:ss.SSSZ",
"YYYY-MM-DD HH:MM:SS",
"EEE MMM dd kk:mm:ss z yyyy"
};

private Constants() {
}
}
Expand Up @@ -122,10 +122,29 @@ public PropertiesMapper reader(FileReader reader) {
* @return object after mapped
*/
public <T> T map() {
PropertiesBean answer =
PropertiesBean mapping =
new PropertiesBean(newBeanInstance());
answer.putAll(getProperties());
return answer.getObject();
mapping.putAll(getProperties());
T answer = mapping.getObject();
return answer;
}

/**
* map properties to object
*
* @param <T> the type of object
* @param mapping a custom of properties bean mapping
* @return object after mapped
*/
public <T> T map(PropertiesBean mapping) {
if(bean != null)
mapping.init(bean);
else
mapping.init(clazz);
mapping.putAll(getProperties());
T answer = mapping.getObject();
return answer;

}

/**
Expand Down
Expand Up @@ -2,10 +2,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import java.util.Base64;
import java.util.Base64.Decoder;

/**
* Support for reading base64 encoded file
Expand All @@ -21,8 +19,11 @@ public class Base64FileReader extends BaseFileReader {
*/
@Override
protected byte[] decode(InputStream inputStream) throws IOException {
String str = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
return Base64.decodeBase64(str);
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
Decoder decoder = Base64.getDecoder();
byte[] answer = decoder.decode(bytes);
return answer;
}

}
21 changes: 5 additions & 16 deletions src/main/java/com/tvd12/properties/file/reader/BaseFileReader.java
Expand Up @@ -11,10 +11,6 @@
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tvd12.properties.file.exception.PropertiesFileException;

/**
Expand Down Expand Up @@ -149,7 +145,9 @@ public List<Properties> loadInputStreams(Collection<InputStream> inputStreams)
* @throws IOException if an I/O error occurs
*/
protected byte[] decode(InputStream inputStream) throws IOException {
return IOUtils.toByteArray(inputStream);
byte[] answer = new byte[inputStream.available()];
inputStream.read(answer);
return answer;
}

/**
Expand All @@ -170,15 +168,11 @@ private InputStream getInputStreamByAbsolutePath(String propertiesFile) {
* @return input stream
*/
private InputStream getInputStreamByAbsolutePath(File file) {
InputStream inputStream = null;
try {
if(file.exists())
inputStream = new FileInputStream(file);
return new FileInputStream(file);
} catch (FileNotFoundException e) {
getLogger().error("can't read file " + file, e);
return null;
}

return inputStream;
}

/**
Expand All @@ -196,9 +190,4 @@ private InputStream getResourceAsStream(Class<?> context,
ip = context.getResourceAsStream(propertiesFile);
return ip;
}

private Logger getLogger() {
return LoggerFactory.getLogger(getClass());
}

}
@@ -0,0 +1,48 @@
package com.tvd12.properties.file.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;

public final class ReflectionClassUtils {

private ReflectionClassUtils() {
}

public static Set<Field> getValidFields(Class<?> clazz) {
Class<?> current = clazz;
Set<Field> answer = new HashSet<>();
while(current != Object.class) {
Field[] fields = current.getDeclaredFields();
for(Field field : fields) {
int modifiers = field.getModifiers();
if((modifiers & Modifier.STATIC) != 0)
continue;
answer.add(field);
}
current = current.getSuperclass();
}
return answer;
}

public static Set<Method> getPublicMethods(Class<?> clazz) {
Class<?> current = clazz;
Set<Method> answer = new HashSet<>();
while(current != Object.class) {
Method[] methods = current.getDeclaredMethods();
for(Method method : methods) {
int modifiers = method.getModifiers();
if((modifiers & Modifier.PUBLIC) == 0)
continue;
if((modifiers & Modifier.ABSTRACT) != 0)
continue;
answer.add(method);
}
current = current.getSuperclass();
}
return answer;
}

}
27 changes: 11 additions & 16 deletions src/main/java/com/tvd12/properties/file/struct/ClassStruct.java
@@ -1,22 +1,18 @@
package com.tvd12.properties.file.struct;

import static org.reflections.ReflectionUtils.withModifier;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.reflections.ReflectionUtils;

import com.google.common.base.Predicates;
import com.tvd12.properties.file.annotation.Property;
import com.tvd12.properties.file.annotation.PropertyWrapper;
import com.tvd12.properties.file.reflect.ReflectionClassUtils;

/**
*
Expand Down Expand Up @@ -191,14 +187,13 @@ protected void addKey(MethodStruct method) {
*
* @return set of java fields
*/
@SuppressWarnings("unchecked")
protected Set<Field> getAnnotatedFields() {
Set<Field> fields = null;
if(isWrapper)
return ReflectionUtils
.getAllFields(clazz, Predicates.not(
withModifier(Modifier.FINAL)));
return new HashSet<>(FieldUtils
.getFieldsListWithAnnotation(clazz, Property.class));
fields = ReflectionClassUtils.getValidFields(clazz);
else
fields = new HashSet<>(FieldUtils.getFieldsListWithAnnotation(clazz, Property.class));
return fields;
}

/**
Expand All @@ -210,13 +205,13 @@ protected Set<Field> getAnnotatedFields() {
*
* @return set of java methods
*/
@SuppressWarnings("unchecked")
protected Set<Method> getAnnotatedMethods() {
Set<Method> methods = null;
if(isWrapper)
return ReflectionUtils
.getAllMethods(clazz, withModifier(Modifier.PUBLIC));
return new HashSet<>(MethodUtils
.getMethodsListWithAnnotation(clazz, Property.class));
methods = ReflectionClassUtils.getPublicMethods(clazz);
else
methods = new HashSet<>(MethodUtils.getMethodsListWithAnnotation(clazz, Property.class));
return methods;
}

/**
Expand Down

0 comments on commit 6f2ad25

Please sign in to comment.