diff --git a/android-example/src/main/java/pl/droidsonroids/jspoon/android/ExampleActivity.java b/android-example/src/main/java/pl/droidsonroids/jspoon/android/ExampleActivity.java index 22df5e7..23fd4ac 100644 --- a/android-example/src/main/java/pl/droidsonroids/jspoon/android/ExampleActivity.java +++ b/android-example/src/main/java/pl/droidsonroids/jspoon/android/ExampleActivity.java @@ -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 htmlAdapter = jspoon.adapter(Animals.class); diff --git a/jspoon/build.gradle b/jspoon/build.gradle index 41d218b..b65339c 100644 --- a/jspoon/build.gradle +++ b/jspoon/build.gradle @@ -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" } diff --git a/jspoon/src/main/java/pl/droidsonroids/jspoon/ArrayUtils.java b/jspoon/src/main/java/pl/droidsonroids/jspoon/ArrayUtils.java index 36959e4..08bf6df 100644 --- a/jspoon/src/main/java/pl/droidsonroids/jspoon/ArrayUtils.java +++ b/jspoon/src/main/java/pl/droidsonroids/jspoon/ArrayUtils.java @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -160,7 +160,7 @@ static int[] toPrimitive(Integer[] array) { result[i] = 0; continue; } - result[i] = array[i].intValue(); + result[i] = array[i]; } return result; } diff --git a/jspoon/src/main/java/pl/droidsonroids/jspoon/FieldType.java b/jspoon/src/main/java/pl/droidsonroids/jspoon/FieldType.java index c5e95da..742e99d 100644 --- a/jspoon/src/main/java/pl/droidsonroids/jspoon/FieldType.java +++ b/jspoon/src/main/java/pl/droidsonroids/jspoon/FieldType.java @@ -21,22 +21,19 @@ 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) { @@ -44,25 +41,24 @@ public class FieldType { 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); } } } @@ -117,7 +113,7 @@ private Class resolveClass(Type type, Class subType) { } private static Map, Type> getTypeVariableMap(final Class targetType) { - Map, Type> map = new HashMap, Type>(); + Map, Type> map = new HashMap<>(); // Populate interfaces populateSuperTypeArgs(targetType.getGenericInterfaces(), map); // Populate super classes and interfaces @@ -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]; diff --git a/jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlAdapter.java b/jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlAdapter.java index d7a724a..fae881f 100644 --- a/jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlAdapter.java +++ b/jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlAdapter.java @@ -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); } diff --git a/jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlField.java b/jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlField.java index 20a60f9..452a7a1 100644 --- a/jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlField.java +++ b/jspoon/src/main/java/pl/droidsonroids/jspoon/HtmlField.java @@ -22,7 +22,7 @@ abstract class HtmlField { protected final FieldType field; - protected final SelectorSpec spec; + final SelectorSpec spec; HtmlField(FieldType field, SelectorSpec spec) { this.field = field; @@ -31,11 +31,11 @@ abstract class HtmlField { 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()) { @@ -56,7 +56,7 @@ static void setFieldOrThrow(FieldType field, Object newInstance, Object value) { } } - protected U instanceForNode(Element node, Class fieldType) { + U instanceForNode(Element node, Class fieldType) { // if clazz.isPrimitive convert it to it's Object counterpart fieldType = Utils.wrapToObject(fieldType); @@ -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()); diff --git a/jspoon/src/main/java/pl/droidsonroids/jspoon/SelectorSpec.java b/jspoon/src/main/java/pl/droidsonroids/jspoon/SelectorSpec.java index 5ec5b5a..9d6ef04 100644 --- a/jspoon/src/main/java/pl/droidsonroids/jspoon/SelectorSpec.java +++ b/jspoon/src/main/java/pl/droidsonroids/jspoon/SelectorSpec.java @@ -79,8 +79,8 @@ boolean shouldSkipOn(Throwable conversionException) { if (conversionException != null && skipAnnotation != null && skipAnnotation.value() != null) { - for (Class trowableToSkip : skipAnnotation.value()) { - if (trowableToSkip.isAssignableFrom(conversionException.getClass())) { + for (Class throwableToSkip : skipAnnotation.value()) { + if (throwableToSkip.isAssignableFrom(conversionException.getClass())) { return true; } } diff --git a/jspoon/src/main/java/pl/droidsonroids/jspoon/Utils.java b/jspoon/src/main/java/pl/droidsonroids/jspoon/Utils.java index 4ef235b..5d54865 100644 --- a/jspoon/src/main/java/pl/droidsonroids/jspoon/Utils.java +++ b/jspoon/src/main/java/pl/droidsonroids/jspoon/Utils.java @@ -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; @@ -17,7 +18,7 @@ class Utils { /** Map of class representing primitives and their object counterparts. */ - private final static Map, Class> PRIMITIVE_WRAPPERS = new HashMap, Class>(); + private final static Map, Class> PRIMITIVE_WRAPPERS = new HashMap<>(); static { PRIMITIVE_WRAPPERS.put(boolean.class, Boolean.class); @@ -69,9 +70,7 @@ public static List 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; } diff --git a/jspoon/src/main/java/pl/droidsonroids/jspoon/annotation/Selector.java b/jspoon/src/main/java/pl/droidsonroids/jspoon/annotation/Selector.java index a6f48b6..a576c19 100644 --- a/jspoon/src/main/java/pl/droidsonroids/jspoon/annotation/Selector.java +++ b/jspoon/src/main/java/pl/droidsonroids/jspoon/annotation/Selector.java @@ -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(); diff --git a/jspoon/src/test/java/pl/droidsonroids/jspoon/DefaultValueTest.java b/jspoon/src/test/java/pl/droidsonroids/jspoon/DefaultValueTest.java index 8de2b93..ce20ca3 100644 --- a/jspoon/src/test/java/pl/droidsonroids/jspoon/DefaultValueTest.java +++ b/jspoon/src/test/java/pl/droidsonroids/jspoon/DefaultValueTest.java @@ -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; } @@ -31,7 +31,7 @@ public void defaultValueTest() { Model model = htmlAdapter.fromHtml("
"); 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); } diff --git a/jspoon/src/test/java/pl/droidsonroids/jspoon/FieldInheritenceTest.java b/jspoon/src/test/java/pl/droidsonroids/jspoon/FieldInheritanceTest.java similarity index 97% rename from jspoon/src/test/java/pl/droidsonroids/jspoon/FieldInheritenceTest.java rename to jspoon/src/test/java/pl/droidsonroids/jspoon/FieldInheritanceTest.java index fc7995c..6d87385 100644 --- a/jspoon/src/test/java/pl/droidsonroids/jspoon/FieldInheritenceTest.java +++ b/jspoon/src/test/java/pl/droidsonroids/jspoon/FieldInheritanceTest.java @@ -11,7 +11,7 @@ import pl.droidsonroids.jspoon.annotation.Selector; -public class FieldInheritenceTest { +public class FieldInheritanceTest { private final static String HTML_CONTENT = "
" + "Test1" @@ -61,7 +61,7 @@ public void testInheritedFields() { @SuppressWarnings("static-access") @Test - public void testDowncastingInstanceFields() { + public void testDownCastingInstanceFields() { HtmlAdapter htmlAdapter = jspoon.adapter(ParentModel.class); ChildModel subclass = new ChildModel(); ChildModel model = (ChildModel) htmlAdapter.fromHtml(HTML_CONTENT, subclass); diff --git a/jspoon/src/test/java/pl/droidsonroids/jspoon/FieldTypeTest.java b/jspoon/src/test/java/pl/droidsonroids/jspoon/FieldTypeTest.java index 7f1633b..dc1e601 100644 --- a/jspoon/src/test/java/pl/droidsonroids/jspoon/FieldTypeTest.java +++ b/jspoon/src/test/java/pl/droidsonroids/jspoon/FieldTypeTest.java @@ -53,7 +53,7 @@ static class StringListModel extends GenericModel> { } @Test - public void testGenericStringList() throws Exception { + public void testGenericStringList() { HtmlAdapter htmlAdapter = Jspoon.create().adapter(StringListModel.class); StringListModel model = htmlAdapter.fromHtml(HTML_CONTENT); assertNotNull(model.item); @@ -65,7 +65,7 @@ static class StringModel extends GenericModel{ } @Test - public void testGenericString() throws Exception { + public void testGenericString() { HtmlAdapter htmlAdapter = Jspoon.create().adapter(StringModel.class); StringModel model = htmlAdapter.fromHtml(HTML_CONTENT); assertNotNull(model.item); @@ -76,7 +76,7 @@ static class StringArrayModel extends GenericModel{ } @Test - public void testGenericStringArray() throws Exception { + public void testGenericStringArray() { HtmlAdapter htmlAdapter = Jspoon.create().adapter(StringArrayModel.class); StringArrayModel model = htmlAdapter.fromHtml(HTML_CONTENT); assertNotNull(model.item); @@ -87,7 +87,7 @@ static class PrimitiveArrayModel extends GenericModel{ } @Test - public void testGenericPrimitivesArray() throws Exception { + public void testGenericPrimitivesArray() { HtmlAdapter htmlAdapter = Jspoon.create().adapter(PrimitiveArrayModel.class); PrimitiveArrayModel model = htmlAdapter.fromHtml(HTML_CONTENT); assertNotNull(model.item); @@ -99,7 +99,7 @@ static class LinkedListModel { } @Test - public void testLinkedList() throws Exception { + public void testLinkedList() { HtmlAdapter htmlAdapter = Jspoon.create().adapter(LinkedListModel.class); LinkedListModel model = htmlAdapter.fromHtml(HTML_CONTENT); assertNotNull(model.list); @@ -113,7 +113,7 @@ static class RawModel { } @Test - public void testRawList() throws Exception { + public void testRawList() { HtmlAdapter htmlAdapter = Jspoon.create().adapter(RawModel.class); RawModel model = htmlAdapter.fromHtml(HTML_CONTENT); assertNotNull(model.list); @@ -126,7 +126,7 @@ static class WildcardModel { } @Test - public void testObjectList() throws Exception { + public void testObjectList() { HtmlAdapter htmlAdapter = Jspoon.create().adapter(WildcardModel.class); WildcardModel model = htmlAdapter.fromHtml(HTML_CONTENT); assertNotNull(model.list); @@ -139,7 +139,7 @@ static class BoundWildcardModel { } @Test - public void testStringList() throws Exception { + public void testStringList() { HtmlAdapter htmlAdapter = Jspoon.create().adapter(BoundWildcardModel.class); BoundWildcardModel model = htmlAdapter.fromHtml(HTML_CONTENT); assertNotNull(model.list); @@ -162,7 +162,7 @@ static class ModelBigDecimal extends ModelGeneric { } @Test - public void genericFieldsTest() throws Exception { + public void genericFieldsTest() { @SuppressWarnings("rawtypes") HtmlAdapter htmlAdapter = jspoon.adapter(ModelGeneric.class); ModelGeneric model = htmlAdapter.fromHtml(HTML_CONTENT); @@ -174,7 +174,7 @@ public void genericFieldsTest() throws Exception { } @Test - public void genericStringFieldsTest() throws Exception { + public void genericStringFieldsTest() { HtmlAdapter htmlAdapter = jspoon.adapter(ModelString.class); ModelString model = htmlAdapter.fromHtml(HTML_CONTENT); assertListEquals(model.justList, "3366", "-4", "9.98"); @@ -185,7 +185,7 @@ public void genericStringFieldsTest() throws Exception { } @Test - public void genericBigDecimalFieldsTest() throws Exception { + public void genericBigDecimalFieldsTest() { HtmlAdapter htmlAdapter = jspoon.adapter(ModelBigDecimal.class); ModelBigDecimal model = htmlAdapter.fromHtml(HTML_CONTENT); assertListEquals(model.justList, "3366", "-4", "9.98"); diff --git a/jspoon/src/test/java/pl/droidsonroids/jspoon/HtmlAdapterTest.java b/jspoon/src/test/java/pl/droidsonroids/jspoon/HtmlAdapterTest.java index cf45585..a2d0a95 100644 --- a/jspoon/src/test/java/pl/droidsonroids/jspoon/HtmlAdapterTest.java +++ b/jspoon/src/test/java/pl/droidsonroids/jspoon/HtmlAdapterTest.java @@ -13,14 +13,14 @@ public class HtmlAdapterTest { private static final String HTML_CONTENT = "
" - + "John" - + "Doe" + + "John" + + "Doe" + "
"; private static class Profile { - @Selector(value = "#firstname") String firstname; - @Selector(value = "#lastname") String lastname; + @Selector(value = "#firstName") String firstName; + @Selector(value = "#lastName") String lastName; } private Jspoon jspoon; @@ -38,8 +38,8 @@ public void testFromInputStream() throws IOException { = new ByteArrayInputStream(HTML_CONTENT.getBytes())) { Profile profile = adapter.fromInputStream(inputStream, baseUrl); - assertEquals("John", profile.firstname); - assertEquals("Doe", profile.lastname); + assertEquals("John", profile.firstName); + assertEquals("Doe", profile.lastName); } } } diff --git a/retrofit-converter-jspoon/src/main/java/pl/droidsonroids/retrofit2/JspoonResponseBodyConverter.java b/retrofit-converter-jspoon/src/main/java/pl/droidsonroids/retrofit2/JspoonResponseBodyConverter.java index 65de336..db80aa5 100644 --- a/retrofit-converter-jspoon/src/main/java/pl/droidsonroids/retrofit2/JspoonResponseBodyConverter.java +++ b/retrofit-converter-jspoon/src/main/java/pl/droidsonroids/retrofit2/JspoonResponseBodyConverter.java @@ -3,7 +3,6 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import okhttp3.HttpUrl; import okhttp3.MediaType; diff --git a/retrofit-converter-jspoon/src/test/java/pl/droidsonroids/retrofit2/JspoonConverterFactoryTest.java b/retrofit-converter-jspoon/src/test/java/pl/droidsonroids/retrofit2/JspoonConverterFactoryTest.java index 535e6ec..7fb2e26 100644 --- a/retrofit-converter-jspoon/src/test/java/pl/droidsonroids/retrofit2/JspoonConverterFactoryTest.java +++ b/retrofit-converter-jspoon/src/test/java/pl/droidsonroids/retrofit2/JspoonConverterFactoryTest.java @@ -9,7 +9,6 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; -import java.util.List; import org.junit.Test; diff --git a/versions.gradle b/versions.gradle index 34f1a0a..2fc483b 100644 --- a/versions.gradle +++ b/versions.gradle @@ -8,7 +8,7 @@ ext { constraint : '1.1.2', jsoup : '1.11.3', junit : '4.12', - kotlin : '1.2.51', + kotlin : '1.2.60', mockito : '2.20.1', mockwebserver: '3.11.0', retrofit : '2.4.0',