Skip to content

Commit

Permalink
Refactor and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Wieczorek committed Aug 6, 2018
1 parent 77bdc2c commit 812395b
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static class AnimalsDiv {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
LinearLayout linearLayout = findViewById(R.id.linearLayout);

Jspoon jspoon = Jspoon.create();
HtmlAdapter<Animals> htmlAdapter = jspoon.adapter(Animals.class);
Expand Down
1 change: 1 addition & 0 deletions jspoon/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
dependencies {
api "org.jsoup:jsoup:$versions.jsoup"
compileOnly "org.jetbrains:annotations:$versions.annotations"
testCompileOnly "org.jetbrains:annotations:$versions.annotations"
testImplementation "junit:junit:$versions.junit"
}

Expand Down
32 changes: 16 additions & 16 deletions jspoon/src/main/java/pl/droidsonroids/jspoon/ArrayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static Object toPrimitive(final Object[] array, Class<?> primitiveType) {
return array;
}

static char[] toPrimitive(Character[] array) {
private static char[] toPrimitive(Character[] array) {
if (array == null) {
return null;
}
Expand All @@ -55,12 +55,12 @@ static char[] toPrimitive(Character[] array) {
result[i] = '\u0000';
continue;
}
result[i] = array[i].charValue();
result[i] = array[i];
}
return result;
}

static byte[] toPrimitive(Byte[] array) {
private static byte[] toPrimitive(Byte[] array) {
if (array == null) {
return null;
}
Expand All @@ -70,12 +70,12 @@ static byte[] toPrimitive(Byte[] array) {
result[i] = 0;
continue;
}
result[i] = array[i].byteValue();
result[i] = array[i];
}
return result;
}

static boolean[] toPrimitive(Boolean[] array) {
private static boolean[] toPrimitive(Boolean[] array) {
if (array == null) {
return null;
}
Expand All @@ -85,12 +85,12 @@ static boolean[] toPrimitive(Boolean[] array) {
result[i] = false;
continue;
}
result[i] = array[i].booleanValue();
result[i] = array[i];
}
return result;
}

static float[] toPrimitive(Float[] array) {
private static float[] toPrimitive(Float[] array) {
if (array == null) {
return null;
}
Expand All @@ -100,12 +100,12 @@ static float[] toPrimitive(Float[] array) {
result[i] = 0.0f;
continue;
}
result[i] = array[i].floatValue();
result[i] = array[i];
}
return result;
}

static double[] toPrimitive(Double[] array) {
private static double[] toPrimitive(Double[] array) {
if (array == null) {
return null;
}
Expand All @@ -115,12 +115,12 @@ static double[] toPrimitive(Double[] array) {
result[i] = 0.0d;
continue;
}
result[i] = array[i].doubleValue();
result[i] = array[i];
}
return result;
}

static short[] toPrimitive(Short[] array) {
private static short[] toPrimitive(Short[] array) {
if (array == null) {
return null;
}
Expand All @@ -130,12 +130,12 @@ static short[] toPrimitive(Short[] array) {
result[i] = 0;
continue;
}
result[i] = array[i].shortValue();
result[i] = array[i];
}
return result;
}

static long[] toPrimitive(Long[] array) {
private static long[] toPrimitive(Long[] array) {
if (array == null) {
return null;
}
Expand All @@ -145,12 +145,12 @@ static long[] toPrimitive(Long[] array) {
result[i] = 0L;
continue;
}
result[i] = array[i].longValue();
result[i] = array[i];
}
return result;
}

static int[] toPrimitive(Integer[] array) {
private static int[] toPrimitive(Integer[] array) {
if (array == null) {
return null;
}
Expand All @@ -160,7 +160,7 @@ static int[] toPrimitive(Integer[] array) {
result[i] = 0;
continue;
}
result[i] = array[i].intValue();
result[i] = array[i];
}
return result;
}
Expand Down
22 changes: 9 additions & 13 deletions jspoon/src/main/java/pl/droidsonroids/jspoon/FieldType.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,44 @@ public class FieldType {
private final String name;
private final int localHashCode;
private Class<?> typeClass;
private Type genericType;
private boolean isArray = false;
private Class<?> arrayContentType = null;
private boolean hasTypeArguments = false;
private Class<?>[] typeArguments = null;
private Class<?> subType;

FieldType(Class<?> fieldClass, Field field) {
if (fieldClass == null || field == null) {
throw new IllegalArgumentException("Field and its class cannot be null");
}
this.subType = fieldClass;
this.wrapped = field;
this.name = field.getName();
this.typeClass = field.getType();
this.genericType = field.getGenericType();
Type genericType = field.getGenericType();
this.localHashCode = field.hashCode() + name.hashCode();

if (genericType instanceof Class) {
Class<?> classType = (Class<?>) genericType;
this.isArray = classType.isArray();
this.arrayContentType = classType.getComponentType();
} else if (genericType instanceof ParameterizedType) {
processParametrizedType((ParameterizedType) genericType, subType);
processParametrizedType((ParameterizedType) genericType, fieldClass);
} else if (genericType instanceof GenericArrayType) {
GenericArrayType typeVarArray = (GenericArrayType) genericType;
this.isArray = true;
Type component = typeVarArray.getGenericComponentType();
this.arrayContentType = resolveClass(component, subType);
this.arrayContentType = resolveClass(component, fieldClass);
if (component instanceof ParameterizedType) {
processParametrizedType((ParameterizedType) component, subType);
processParametrizedType((ParameterizedType) component, fieldClass);
}
} else if (genericType instanceof TypeVariable<?>) {
TypeVariable<?> variable = (TypeVariable<?>) genericType;
Type resolvedType = getTypeVariableMap(subType).get(variable);
Type resolvedType = getTypeVariableMap(fieldClass).get(variable);
resolvedType = (resolvedType == null) ? resolveBound(variable) : resolvedType;
this.genericType = resolvedType;
this.typeClass = resolveClass(resolvedType, subType);
this.typeClass = resolveClass(resolvedType, fieldClass);
this.isArray = typeClass.isArray();
this.arrayContentType = typeClass.getComponentType();
if (resolvedType instanceof ParameterizedType) {
processParametrizedType((ParameterizedType) resolvedType, subType);
processParametrizedType((ParameterizedType) resolvedType, fieldClass);
}
}
}
Expand Down Expand Up @@ -117,7 +113,7 @@ private Class<?> resolveClass(Type type, Class<?> subType) {
}

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType) {
Map<TypeVariable<?>, Type> map = new HashMap<TypeVariable<?>, Type>();
Map<TypeVariable<?>, Type> map = new HashMap<>();
// Populate interfaces
populateSuperTypeArgs(targetType.getGenericInterfaces(), map);
// Populate super classes and interfaces
Expand Down Expand Up @@ -288,7 +284,7 @@ public int getTypeArgumentCount() {
public Class<?> getTypeArgument(int index) {
if (index < 0 || typeArguments == null || index > typeArguments.length) {
throw new IndexOutOfBoundsException(String.format(
"There are %s type argumens, want to retrieve at %s",
"There are %s type arguments, want to retrieve at %s",
(typeArguments == null ? "none" : typeArguments.length), index));
}
return typeArguments[index];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ T loadFromNode(Element node) {
return loadFromNode(node, null);
}

T loadFromNode(Element node, T instance) {
private T loadFromNode(Element node, T instance) {
if (instance == null) {
instance = Utils.constructInstance(clazz);
}
Expand Down
10 changes: 5 additions & 5 deletions jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlField.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
abstract class HtmlField<T> {

protected final FieldType field;
protected final SelectorSpec spec;
final SelectorSpec spec;

HtmlField(FieldType field, SelectorSpec spec) {
this.field = field;
Expand All @@ -31,11 +31,11 @@ abstract class HtmlField<T> {

protected abstract void setValue(Jspoon jspoon, Element node, T newInstance);

protected Elements selectChildren(Element node) {
Elements selectChildren(Element node) {
return node.select(spec.getCssQuery());
}

protected Element selectChild(Element parent) {
Element selectChild(Element parent) {
Elements elements = selectChildren(parent);
int size = elements.size();
if (size == 0 || size <= spec.getIndex()) {
Expand All @@ -56,7 +56,7 @@ static void setFieldOrThrow(FieldType field, Object newInstance, Object value) {
}
}

protected <U> U instanceForNode(Element node, Class<U> fieldType) {
<U> U instanceForNode(Element node, Class<U> fieldType) {
// if clazz.isPrimitive convert it to it's Object counterpart
fieldType = Utils.wrapToObject(fieldType);

Expand Down Expand Up @@ -185,7 +185,7 @@ private Double getDouble(String value) {

private Float getFloat(String value) {
try {
return Float.valueOf(NumberFormat.getInstance(spec.getLocale()).parse(value).floatValue());
return NumberFormat.getInstance(spec.getLocale()).parse(value).floatValue();
}
catch (ParseException e) {
throw new FloatParseException(value, spec.getLocale());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ boolean shouldSkipOn(Throwable conversionException) {
if (conversionException != null && skipAnnotation != null
&& skipAnnotation.value() != null) {

for (Class<? extends Throwable> trowableToSkip : skipAnnotation.value()) {
if (trowableToSkip.isAssignableFrom(conversionException.getClass())) {
for (Class<? extends Throwable> throwableToSkip : skipAnnotation.value()) {
if (throwableToSkip.isAssignableFrom(conversionException.getClass())) {
return true;
}
}
Expand Down
7 changes: 3 additions & 4 deletions jspoon/src/main/java/pl/droidsonroids/jspoon/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
Expand All @@ -17,7 +18,7 @@
class Utils {

/** Map of class representing primitives and their object counterparts. */
private final static Map<Class<?>, Class<?>> PRIMITIVE_WRAPPERS = new HashMap<Class<?>, Class<?>>();
private final static Map<Class<?>, Class<?>> PRIMITIVE_WRAPPERS = new HashMap<>();

static {
PRIMITIVE_WRAPPERS.put(boolean.class, Boolean.class);
Expand Down Expand Up @@ -69,9 +70,7 @@ public static List<Field> getAllDeclaredFields(Class<?> target) {
return declaredFields;
}
declaredFields = getAllDeclaredFields(target.getSuperclass());
for (Field field : target.getDeclaredFields()) {
declaredFields.add(field);
}
declaredFields.addAll(Arrays.asList(target.getDeclaredFields()));
return declaredFields;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@Target({ ElementType.TYPE, ElementType.FIELD })
public @interface Selector {

static final String NO_VALUE = "NO_VALUE";
String NO_VALUE = "NO_VALUE";

/** @return Css query */
String value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static class Model {
@Selector(value = "p", defValue = "-100") int number;

@Selector(value = "span", defValue = "hello") String text2 = "world";
@Selector(value = "span") String text3 = "helloworld";
@Selector(value = "span") String text3 = "hello world";
@Selector(value = "span") String text4;
}

Expand All @@ -31,7 +31,7 @@ public void defaultValueTest() {
Model model = htmlAdapter.fromHtml("<div></div>");
assertEquals("DEFAULT_VALUE", model.text); // since defValue explicitly defined
assertEquals("hello", model.text2); // defValue takes precedent as its whatever would be parsed from Element
assertEquals("helloworld", model.text3); // no defValue, let's leave whatever is set
assertEquals("hello world", model.text3); // no defValue, let's leave whatever is set
assertNull(model.text4); // should not be set to anything silently if developer did not set a defValue
assertEquals(-100, model.number);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import pl.droidsonroids.jspoon.annotation.Selector;

public class FieldInheritenceTest {
public class FieldInheritanceTest {

private final static String HTML_CONTENT = "<div>"
+ "<span class='string'>Test1</span>"
Expand Down Expand Up @@ -61,7 +61,7 @@ public void testInheritedFields() {

@SuppressWarnings("static-access")
@Test
public void testDowncastingInstanceFields() {
public void testDownCastingInstanceFields() {
HtmlAdapter<ParentModel> htmlAdapter = jspoon.adapter(ParentModel.class);
ChildModel subclass = new ChildModel();
ChildModel model = (ChildModel) htmlAdapter.fromHtml(HTML_CONTENT, subclass);
Expand Down
Loading

0 comments on commit 812395b

Please sign in to comment.