Skip to content

Commit 8088842

Browse files
authored
Develop (#36)
* #26 #25 * #29 #26 * #23 * #25 (#35)
1 parent 29a2ba1 commit 8088842

File tree

9 files changed

+144
-39
lines changed

9 files changed

+144
-39
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ addons:
1212
script: mvn clean test
1313

1414
after_success:
15-
- mvn clean test jacoco:report coveralls:report
15+
- ./tools/update_version.sh
16+
- mvn clean cobertura:cobertura coveralls:report

pom.xml

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,12 @@
44

55
<groupId>io.github.rhkiswani</groupId>
66
<artifactId>javaff</artifactId>
7-
<version>0.0.22-SNAPSHOT</version>
7+
<version>0.0.23-SNAPSHOT</version>
88
<name>JavaFF: Java Facade/Factories</name>
99
<description>
10-
We all know the Golden Object Oriented rule **Don't talk to strangers**
11-
12-
We all know that the API/frameworks defects or magic or limitations is not showing at the begging of the development.
13-
14-
So imagine that you using an API in all your projects and after spending months or years that API you got a production issue becuase of it ,
15-
changing the that API would be soo hard and costy !!!
16-
17-
18-
Main Features
19-
--------------
20-
- This project offers a standard/clear API for the most used API's in the Java Applications like : Exceptions, Locale, Beans, Formatter's, Json Handlers, Loggers, ReflectionHelpers ...etc
21-
- **you can control the implementation's through the class path without changing line of code**
22-
- Smart Exception handling mechanism
23-
- Default Implementations
24-
- Many Utilises
10+
JavaFF: Java Facade/Factories, serves as a facade or abstraction for various frameworks like Gson ,
11+
Jakson , Log4j , Logback, locale, and many more .
12+
check https://github.com/rhkiswani/JavaFF
2513
</description>
2614
<url>https://github.com/rhkiswani/JavaFF</url>
2715

src/main/java/io/github/rhkiswani/javaff/reflection/DefaultReflectionHelper.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,23 @@ public List<Field> scanFieldsByAnnotation(Class clazz, Class... annotations) thr
6666
@Override
6767
public void setFieldValue(T obj, String fieldName, Object value) throws ReflectionException {
6868
if (obj == null){
69-
throw new ReflectionException(SmartException.NULL_VAL, obj);
69+
throw new ReflectionException(SmartException.NULL_VAL, "target object");
7070
}
7171
Field field = getField(obj.getClass(), fieldName);
7272
if (value != null && !field.getType().isInstance(value)){
73-
throw new ReflectionException(SmartException.TYPE_ERROR, field.getClass(), value.getClass());
73+
throw new ReflectionException(SmartException.TYPE_ERROR, value != null ? value.getClass() : value, field.getType());
7474
}
7575
try {
7676
field.set(obj, value);
77-
} catch (IllegalAccessException e) {
77+
} catch (Exception e) {
7878
throw new ReflectionException(e);
7979
}
8080
}
8181

8282
@Override
8383
public Field getField(Class clazz, String fieldName) throws ReflectionException {
8484
if (clazz == null){
85-
throw new ReflectionException(SmartException.NULL_VAL, clazz);
85+
throw new ReflectionException(SmartException.NULL_VAL, "target class");
8686
}
8787
Field[] fields = clazz.getDeclaredFields();
8888
for (Field field : fields) {
@@ -101,9 +101,9 @@ public Field getField(Class clazz, String fieldName) throws ReflectionException
101101
}
102102

103103
@Override
104-
public T newInstance(String className, Object... constructorParams) throws ReflectionException {
104+
public Class forName(String className) throws ReflectionException {
105105
try {
106-
return (T) Class.forName(className).newInstance();
106+
return Class.forName(className);
107107
} catch (Exception e) {
108108
throw new ReflectionException(e);
109109
}
@@ -141,7 +141,7 @@ private boolean isIgnored(Field field) {
141141
public <V> V getFieldValue(T obj, String fieldName) throws ReflectionException {
142142
try {
143143
return (V) getField(obj.getClass(), fieldName).get(obj);
144-
} catch (IllegalAccessException e) {
144+
} catch (Exception e) {
145145
throw new ReflectionException(e);
146146
}
147147
}

src/main/java/io/github/rhkiswani/javaff/reflection/ReflectionHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface ReflectionHelper<T> {
3535

3636
<V> V getFieldValue(T obj, String fieldName) throws ReflectionException;
3737

38-
T newInstance(String className, Object... constructorParams) throws ReflectionException;
38+
Class forName(String className) throws ReflectionException;
3939

4040
List<Field> getFields(Class clazz);
4141
}

src/main/java/io/github/rhkiswani/javaff/reflection/ReflectionUtil.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import io.github.rhkiswani.javaff.exceptions.SmartException;
1919
import io.github.rhkiswani.javaff.lang.exceptions.IllegalParamException;
20-
import io.github.rhkiswani.javaff.reflection.exception.ReflectionException;
2120

2221
/**
2322
* @author Mohamed Kiswani
@@ -26,10 +25,7 @@
2625
*/
2726
public class ReflectionUtil {
2827

29-
private ReflectionUtil(){
30-
31-
32-
}
28+
private ReflectionUtil(){}
3329

3430
public static boolean isPresent(String className) {
3531
return isPresent(className, Thread.currentThread().getContextClassLoader());
@@ -46,7 +42,7 @@ public static boolean isPresent(String className, ClassLoader classLoader) {
4642
}
4743

4844
public static void setFieldValue(Object obj, String fieldName, Object val) {
49-
new DefaultReflectionHelper().setFieldValue(obj, fieldName, val);
45+
ReflectionHelpersFactory.getReflectionHelper(ReflectionUtil.class).setFieldValue(obj, fieldName, val);
5046
}
5147

5248
public static Class getCallerClass(int numberOfLevels){
@@ -61,10 +57,6 @@ public static Class getCallerClass(int numberOfLevels){
6157
}
6258

6359
public static Class forName(String className) {
64-
try{
65-
return Class.forName(className);
66-
} catch (Exception e){
67-
throw new ReflectionException(e);
68-
}
60+
return ReflectionHelpersFactory.getReflectionHelper(ReflectionUtil.class).forName(className);
6961
}
7062
}

src/test/java/io/github/rhkiswani/javaff/beans/ValuesHolderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ValuesHolderTest {
2020
private EmployeeByIdAnnotation e;
2121
private EmployeeByIdAnnotation e1;
2222
@Before
23-
public void setup(){
23+
public void setUp(){
2424
e = new EmployeeByIdAnnotation();
2525
e.setId(100000);
2626
e.setName("Kiswani");

src/test/java/io/github/rhkiswani/javaff/json/JsonHandlerFactoryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class JsonHandlerFactoryTest {
1616

1717
private EmployeeX employeeX = null;
1818
@Before
19-
public void setup(){
19+
public void setUp(){
2020
employeeX = new EmployeeX();
2121
employeeX.setEmpId(1000);
2222
employeeX.setName("Kiswani");

src/test/java/io/github/rhkiswani/javaff/reflection/ReflectionTest.java

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
import io.github.rhkiswani.javaff.beans.EmployeeX;
44
import io.github.rhkiswani.javaff.exceptions.SmartException;
5+
import io.github.rhkiswani.javaff.lang.exceptions.IllegalParamException;
6+
import io.github.rhkiswani.javaff.reflection.exception.ReflectionException;
57
import org.junit.Before;
68
import org.junit.Test;
9+
710
import static org.assertj.core.api.Assertions.assertThat;
811
public class ReflectionTest {
912

1013
private ReflectionHelper reflectionHelper;
1114

1215
@Before
13-
public void setup(){
16+
public void setUp(){
1417
reflectionHelper = new DefaultReflectionHelper<EmployeeX>();
1518
}
1619

@@ -26,9 +29,93 @@ public void testFieldsByAnnotation() {
2629
public void testGetCallerClass() {
2730
new X().doX();
2831
assertThat(Throwable.class.isInstance(new SmartException(""))).isEqualTo(true);
32+
try{
33+
ReflectionUtil.getCallerClass(-1);
34+
}catch (Exception e){
35+
assertThat(e).isInstanceOf(IllegalParamException.class).hasMessage("Number Of Levels should be greater or equal 0");
36+
}
37+
try{
38+
ReflectionUtil.getCallerClass(10000);
39+
}catch (Exception e){
40+
assertThat(e).isInstanceOf(IllegalParamException.class).hasMessageContaining("StackTrace MaxSize is");
41+
}
42+
}
43+
44+
@Test
45+
public void testIsPresent() {
46+
assertThat(ReflectionUtil.isPresent(Integer.class.getName())).isEqualTo(true);
47+
assertThat(ReflectionUtil.isPresent("com.xyz.xyz")).isEqualTo(false);
48+
}
49+
50+
@Test
51+
public void testExceptions() {
52+
try {
53+
reflectionHelper.getField(ReflectionTest.class, "non_exist_field");
54+
}catch (Exception e){
55+
assertThat(e).isInstanceOf(ReflectionException.class).hasMessage("non_exist_field not found");
56+
}
57+
try {
58+
reflectionHelper.getField(null, "non_exist_field");
59+
}catch (Exception e){
60+
assertThat(e).isInstanceOf(ReflectionException.class).hasMessage("target class cant be null");
61+
}
62+
// these fields will be ignored because they are added by frameworks
63+
try {
64+
reflectionHelper.getField(X.class, "$jacocoData");
65+
}catch (Exception e){
66+
assertThat(e).isInstanceOf(ReflectionException.class).hasMessage("$jacocoData not found");
67+
}
68+
try {
69+
reflectionHelper.getField(X.class, "__cobertura_counters");
70+
}catch (Exception e){
71+
assertThat(e).isInstanceOf(ReflectionException.class).hasMessage("__cobertura_counters not found");
72+
}
73+
try {
74+
reflectionHelper.getField(X.class, "this$");
75+
}catch (Exception e){
76+
assertThat(e).isInstanceOf(ReflectionException.class).hasMessage("this$ not found");
77+
}
78+
assertThat(Throwable.class.isInstance(new SmartException(""))).isEqualTo(true);
79+
}
80+
81+
@Test
82+
public void testSetVal() {
83+
X x = new X();
84+
reflectionHelper.setFieldValue(x, "privateStr", "Value");
85+
assertThat(x.privateStr).isEqualTo("Value");
86+
try{
87+
reflectionHelper.setFieldValue(null, "privateStr", "Value");
88+
}catch (Exception e){
89+
assertThat(e).isInstanceOf(ReflectionException.class).hasMessage("target object cant be null");
90+
}
91+
try{
92+
reflectionHelper.setFieldValue(x, "privateStr", Integer.valueOf(1));
93+
}catch (Exception e){
94+
assertThat(e).isInstanceOf(ReflectionException.class).hasMessage("class java.lang.Integer is not fit to class java.lang.String");
95+
}
96+
try{
97+
reflectionHelper.setFieldValue(x, "privatePrimitive", null);
98+
}catch (Exception e){
99+
assertThat(e).isInstanceOf(ReflectionException.class).hasMessage("java.lang.IllegalArgumentException: Can not set int field io.github.rhkiswani.javaff.reflection.ReflectionTest$X.privatePrimitive to null value");
100+
}
101+
try{
102+
ReflectionUtil.setFieldValue(x, "privatePrimitive", null);
103+
}catch (Exception e){
104+
assertThat(e).isInstanceOf(ReflectionException.class).hasMessage("java.lang.IllegalArgumentException: Can not set int field io.github.rhkiswani.javaff.reflection.ReflectionTest$X.privatePrimitive to null value");
105+
}
106+
}
107+
108+
@Test
109+
public void testFactory() {
110+
assertThat(ReflectionHelpersFactory.instance() == ReflectionHelpersFactory.instance()).isEqualTo(true);
29111
}
30112

31113
private class X {
114+
private Object $jacocoData;
115+
private Object __cobertura_counters;
116+
private Object this$;
117+
private String privateStr;
118+
private int privatePrimitive;
32119
public void doX(){
33120
assertThat(ReflectionUtil.getCallerClass(1)).isEqualTo(ReflectionTest.class);
34121
}

tools/update_version.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
function getVersion(){
4+
mvn_version=$(mvn -q \
5+
-Dexec.executable="echo" \
6+
-Dexec.args='${project.version}' \
7+
--non-recursive \
8+
org.codehaus.mojo:exec-maven-plugin:1.3.1:exec)
9+
10+
if [[ $mvn_version == *"SNAPSHOT"* ]]
11+
then
12+
for str in $(echo "$mvn_version" | grep -o -e "[^-SNAPSHOT]*"); do
13+
version="$str";
14+
done
15+
fi
16+
}
17+
18+
function update(){
19+
mvn release:update-versions -DperformRelease=true -B
20+
getVersion
21+
}
22+
23+
function afterUpdate(){
24+
git commit . -m"develop $version updated"
25+
git pull origin develop
26+
git push origin develop
27+
}
28+
29+
function main(){
30+
if [ "$TRAVIS_BRANCH" = "develop" ]; then
31+
update
32+
afterUpdate
33+
fi
34+
}
35+
36+
main
37+

0 commit comments

Comments
 (0)