From 1574eb70c56dc51d7e8aa904d9fca946d7a1e4ac Mon Sep 17 00:00:00 2001 From: Lachlan McKee Date: Mon, 19 Nov 2018 14:32:15 +0000 Subject: [PATCH 1/5] Issue #15 Updated compiler-base to 1.2.1 --- build.gradle | 4 ++-- .../extension/def/intdef/IntDefGsonPathFieldValidator.kt | 6 +++--- .../def/stringdef/StringDefGsonPathFieldValidator.kt | 6 +++--- .../extension/empty/EmptyToNullGsonPathFieldValidator.kt | 4 ++-- .../gsonpath/extension/size/SizeGsonPathFieldValidator.kt | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index dae62a6..3fd8a17 100644 --- a/build.gradle +++ b/build.gradle @@ -1,8 +1,8 @@ buildscript { ext.kotlin_version = '1.2.31' ext.gson_version = '2.8.1' - ext.gsonpath_version = '2.4.0' - ext.gsonpath_compiler_version = '1.2.0' + ext.gsonpath_version = '3.0.0' + ext.gsonpath_compiler_version = '1.2.1' ext.junit_version = '4.12' repositories { diff --git a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/intdef/IntDefGsonPathFieldValidator.kt b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/intdef/IntDefGsonPathFieldValidator.kt index f343e75..f5b489e 100644 --- a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/intdef/IntDefGsonPathFieldValidator.kt +++ b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/intdef/IntDefGsonPathFieldValidator.kt @@ -3,12 +3,12 @@ package gsonpath.extension.def.intdef import com.squareup.javapoet.CodeBlock import gsonpath.compiler.ExtensionFieldMetadata import gsonpath.compiler.GsonPathExtension -import gsonpath.compiler.addNewLine -import gsonpath.compiler.addWithNewLine import gsonpath.extension.addException import gsonpath.extension.def.DefAnnotationMirrors import gsonpath.extension.def.getDefAnnotationMirrors import gsonpath.extension.getAnnotationValueObject +import gsonpath.util.addWithNewLine +import gsonpath.util.newLine import javax.annotation.processing.ProcessingEnvironment import javax.lang.model.element.AnnotationValue @@ -42,7 +42,7 @@ class IntDefGsonPathFieldValidator : GsonPathExtension { validationBuilder.indent() .addStatement("break") .unindent() - .addNewLine() + .newLine() // Create a 'default' that throws an exception if an unexpected integer is found. .addWithNewLine("default:") diff --git a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/stringdef/StringDefGsonPathFieldValidator.kt b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/stringdef/StringDefGsonPathFieldValidator.kt index 27d8848..9a268f4 100644 --- a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/stringdef/StringDefGsonPathFieldValidator.kt +++ b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/stringdef/StringDefGsonPathFieldValidator.kt @@ -10,12 +10,12 @@ import com.sun.source.util.Trees import com.sun.tools.javac.tree.JCTree import gsonpath.compiler.ExtensionFieldMetadata import gsonpath.compiler.GsonPathExtension -import gsonpath.compiler.addNewLine -import gsonpath.compiler.addWithNewLine import gsonpath.extension.addException import gsonpath.extension.def.DefAnnotationMirrors import gsonpath.extension.def.getDefAnnotationMirrors import gsonpath.extension.getAnnotationValue +import gsonpath.util.addWithNewLine +import gsonpath.util.newLine import javax.annotation.processing.ProcessingEnvironment import javax.lang.model.element.AnnotationValue @@ -84,7 +84,7 @@ class StringDefGsonPathFieldValidator : GsonPathExtension { .addStatement("$variableName = $constant") .addStatement("break") .unindent() - .addNewLine() + .newLine() } // Throw an exception if an unexpected String is found. diff --git a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/empty/EmptyToNullGsonPathFieldValidator.kt b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/empty/EmptyToNullGsonPathFieldValidator.kt index 3ee769f..30a0350 100644 --- a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/empty/EmptyToNullGsonPathFieldValidator.kt +++ b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/empty/EmptyToNullGsonPathFieldValidator.kt @@ -5,9 +5,9 @@ import com.squareup.javapoet.CodeBlock import gsonpath.ProcessingException import gsonpath.compiler.ExtensionFieldMetadata import gsonpath.compiler.GsonPathExtension -import gsonpath.compiler.isFieldCollectionType import gsonpath.extension.addException import gsonpath.extension.annotation.EmptyToNull +import gsonpath.util.ProcessorTypeHandler import javax.annotation.processing.ProcessingEnvironment import javax.lang.model.type.ArrayType import javax.lang.model.type.TypeMirror @@ -31,7 +31,7 @@ class EmptyToNullGsonPathFieldValidator : GsonPathExtension { val fieldCollectionType: Boolean = try { - isFieldCollectionType(processingEnvironment, fieldInfo.typeMirror) + ProcessorTypeHandler(processingEnvironment).isMirrorOfCollectionType(fieldInfo.typeMirror) } catch (e: Exception) { false } diff --git a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/size/SizeGsonPathFieldValidator.kt b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/size/SizeGsonPathFieldValidator.kt index 3393f01..e0ed596 100644 --- a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/size/SizeGsonPathFieldValidator.kt +++ b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/size/SizeGsonPathFieldValidator.kt @@ -5,10 +5,10 @@ import com.squareup.javapoet.CodeBlock import gsonpath.ProcessingException import gsonpath.compiler.ExtensionFieldMetadata import gsonpath.compiler.GsonPathExtension -import gsonpath.compiler.isFieldCollectionType import gsonpath.extension.addException import gsonpath.extension.getAnnotationMirror import gsonpath.extension.getAnnotationValueObject +import gsonpath.util.ProcessorTypeHandler import javax.annotation.processing.ProcessingEnvironment import javax.lang.model.element.AnnotationMirror import javax.lang.model.type.ArrayType @@ -34,7 +34,7 @@ class SizeGsonPathFieldValidator : GsonPathExtension { // Ensure that the field is either an array, string or a collection. val fieldCollectionType: Boolean = try { - isFieldCollectionType(processingEnvironment, fieldInfo.typeMirror) + ProcessorTypeHandler(processingEnvironment).isMirrorOfCollectionType(fieldInfo.typeMirror) } catch (e: Exception) { false } From f58b939ee9c5166d8896fdcec82dc1322dc0ca19 Mon Sep 17 00:00:00 2001 From: Lachlan McKee Date: Mon, 19 Nov 2018 14:41:22 +0000 Subject: [PATCH 2/5] Issue #15 Updated code structure based on new gsonpath kotlin extensions --- .../intdef/IntDefGsonPathFieldValidator.kt | 48 ++++------ .../StringDefGsonPathFieldValidator.kt | 76 +++++++-------- .../EmptyToNullGsonPathFieldValidator.kt | 56 +++++------ .../extension/range/RangeFunctions.kt | 29 +++--- .../FloatRangeGsonPathFieldValidator.kt | 32 +++---- .../IntRangeGsonPathFieldValidator.kt | 28 +++--- .../size/SizeGsonPathFieldValidator.kt | 94 +++++++++---------- 7 files changed, 167 insertions(+), 196 deletions(-) diff --git a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/intdef/IntDefGsonPathFieldValidator.kt b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/intdef/IntDefGsonPathFieldValidator.kt index f5b489e..079faa7 100644 --- a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/intdef/IntDefGsonPathFieldValidator.kt +++ b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/intdef/IntDefGsonPathFieldValidator.kt @@ -8,7 +8,9 @@ import gsonpath.extension.def.DefAnnotationMirrors import gsonpath.extension.def.getDefAnnotationMirrors import gsonpath.extension.getAnnotationValueObject import gsonpath.util.addWithNewLine -import gsonpath.util.newLine +import gsonpath.util.case +import gsonpath.util.codeBlock +import gsonpath.util.switch import javax.annotation.processing.ProcessingEnvironment import javax.lang.model.element.AnnotationValue @@ -25,38 +27,26 @@ class IntDefGsonPathFieldValidator : GsonPathExtension { val (fieldInfo, variableName) = extensionFieldMetadata val defAnnotationMirrors: DefAnnotationMirrors = getDefAnnotationMirrors(fieldInfo.element, - "android.support.annotation", "IntDef") ?: return null - - val validationBuilder = CodeBlock.builder() - validationBuilder.beginControlFlow("switch ($variableName)") + "android.support.annotation", "IntDef") ?: return null // The integer constants within the 'IntDef#values' property. val intDefValues: List<*> = getAnnotationValueObject(defAnnotationMirrors.defAnnotationMirror, "value") - as List<*>? ?: return null - - // Create a 'case' for each valid integer. - intDefValues.forEach { it -> - validationBuilder.addWithNewLine("""case ${(it as AnnotationValue).value}:""") - } - - validationBuilder.indent() - .addStatement("break") - .unindent() - .newLine() - - // Create a 'default' that throws an exception if an unexpected integer is found. - .addWithNewLine("default:") - .indent() - .addException("""Unexpected Int '" + $variableName + "' for JSON element '${extensionFieldMetadata.jsonPath}'""") - .unindent() - - validationBuilder.endControlFlow() - - val validationCodeBlock = validationBuilder.build() - if (!validationCodeBlock.isEmpty) { - return validationCodeBlock + as List<*>? ?: return null + + return codeBlock { + switch(variableName) { + // Create a 'case' for each valid integer. + intDefValues.forEach { + case((it as AnnotationValue).value.toString()) {} + } + + // Create a 'default' that throws an exception if an unexpected integer is found. + addWithNewLine("default:") + indent() + addException("""Unexpected Int '" + $variableName + "' for JSON element '${extensionFieldMetadata.jsonPath}'""") + unindent() + } } - return null } } diff --git a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/stringdef/StringDefGsonPathFieldValidator.kt b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/stringdef/StringDefGsonPathFieldValidator.kt index 9a268f4..5444fde 100644 --- a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/stringdef/StringDefGsonPathFieldValidator.kt +++ b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/def/stringdef/StringDefGsonPathFieldValidator.kt @@ -14,8 +14,7 @@ import gsonpath.extension.addException import gsonpath.extension.def.DefAnnotationMirrors import gsonpath.extension.def.getDefAnnotationMirrors import gsonpath.extension.getAnnotationValue -import gsonpath.util.addWithNewLine -import gsonpath.util.newLine +import gsonpath.util.* import javax.annotation.processing.ProcessingEnvironment import javax.lang.model.element.AnnotationValue @@ -39,11 +38,11 @@ class StringDefGsonPathFieldValidator : GsonPathExtension { val (fieldInfo, variableName) = extensionFieldMetadata val defAnnotationMirrors: DefAnnotationMirrors = getDefAnnotationMirrors(fieldInfo.element, - "android.support.annotation", "StringDef") ?: return null + "android.support.annotation", "StringDef") ?: return null // The annotation values reference which contains the String constants. val defAnnotationValues: AnnotationValue = getAnnotationValue(defAnnotationMirrors.defAnnotationMirror, - "value") ?: return null + "value") ?: return null // // The standard javax AnnotationMirror does not expose the ability to retrieve the actual constant variable @@ -53,53 +52,42 @@ class StringDefGsonPathFieldValidator : GsonPathExtension { val annotationElement = defAnnotationMirrors.annotationMirror.annotationType.asElement() val treesInstance = Trees.instance(processingEnvironment) val stringDefConstants: List? = AnnotationValueConstantsVisitor().scan( - treesInstance.getPath(annotationElement, defAnnotationMirrors.defAnnotationMirror, defAnnotationValues), - null) + treesInstance.getPath(annotationElement, defAnnotationMirrors.defAnnotationMirror, defAnnotationValues), + null) // If there are no String constants in the 'StringDef' abort. if (stringDefConstants == null || stringDefConstants.isEmpty()) { return null } - val validationBuilder = CodeBlock.builder() - validationBuilder.beginControlFlow("switch ($variableName)") - - stringDefConstants.forEach { it -> - - val constant = - if (!it.contains(".")) { - // Append the enclosing class name - "$annotationElement.$it" - } else { - it - } - - validationBuilder.addWithNewLine("""case $constant:""") - .indent() - - // - // The StringDef annotation requires the String constant to be referenced, therefore we must - // reassign the String value to the constant. - // - .addStatement("$variableName = $constant") - .addStatement("break") - .unindent() - .newLine() - } - - // Throw an exception if an unexpected String is found. - validationBuilder.addWithNewLine("default:") - .indent() - .addException("""Unexpected String '" + $variableName + "' for JSON element '${extensionFieldMetadata.jsonPath}'""") - .unindent() - - validationBuilder.endControlFlow() - - val validationCodeBlock = validationBuilder.build() - if (!validationCodeBlock.isEmpty) { - return validationCodeBlock + return codeBlock { + switch(variableName) { + stringDefConstants + .map { + if (!it.contains(".")) { + // Append the enclosing class name + "$annotationElement.$it" + } else { + it + } + } + .forEach { + // + // The StringDef annotation requires the String constant to be referenced, therefore we must + // reassign the String value to the constant. + // + case(it) { + assign(variableName, it) + } + } + + // Throw an exception if an unexpected String is found. + addWithNewLine("default:") + indent() + addException("""Unexpected String '" + $variableName + "' for JSON element '${extensionFieldMetadata.jsonPath}'""") + unindent() + } } - return null } /** diff --git a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/empty/EmptyToNullGsonPathFieldValidator.kt b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/empty/EmptyToNullGsonPathFieldValidator.kt index 30a0350..e5d307f 100644 --- a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/empty/EmptyToNullGsonPathFieldValidator.kt +++ b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/empty/EmptyToNullGsonPathFieldValidator.kt @@ -8,6 +8,9 @@ import gsonpath.compiler.GsonPathExtension import gsonpath.extension.addException import gsonpath.extension.annotation.EmptyToNull import gsonpath.util.ProcessorTypeHandler +import gsonpath.util.`if` +import gsonpath.util.assign +import gsonpath.util.codeBlock import javax.annotation.processing.ProcessingEnvironment import javax.lang.model.type.ArrayType import javax.lang.model.type.TypeMirror @@ -30,50 +33,47 @@ class EmptyToNullGsonPathFieldValidator : GsonPathExtension { } val fieldCollectionType: Boolean = - try { - ProcessorTypeHandler(processingEnvironment).isMirrorOfCollectionType(fieldInfo.typeMirror) - } catch (e: Exception) { - false - } + try { + ProcessorTypeHandler(processingEnvironment).isMirrorOfCollectionType(fieldInfo.typeMirror) + } catch (e: Exception) { + false + } val fieldMapType: Boolean = - try { - isFieldMapType(processingEnvironment, fieldInfo.typeMirror) - } catch (e: Exception) { - false - } + try { + isFieldMapType(processingEnvironment, fieldInfo.typeMirror) + } catch (e: Exception) { + false + } val fieldType: FieldType = - when { - (fieldInfo.typeMirror is ArrayType) -> FieldType.ARRAY - fieldCollectionType -> FieldType.COLLECTION - fieldMapType -> FieldType.MAP - (fieldInfo.typeName == ClassName.get(String::class.java)) -> FieldType.STRING + when { + (fieldInfo.typeMirror is ArrayType) -> FieldType.ARRAY + fieldCollectionType -> FieldType.COLLECTION + fieldMapType -> FieldType.MAP + (fieldInfo.typeName == ClassName.get(String::class.java)) -> FieldType.STRING - else -> - throw ProcessingException("Unexpected type found for field annotated with 'EmptyToNull', only " + - "string, array, map, or collection classes may be used.", fieldInfo.element) - } + else -> + throw ProcessingException("Unexpected type found for field annotated with 'EmptyToNull', only " + + "string, array, map, or collection classes may be used.", fieldInfo.element) + } - val validationBuilder = CodeBlock.builder() - .beginControlFlow("if ($variableName${fieldType.emptyCheck})") - .apply { + return codeBlock { + `if`("$variableName${fieldType.emptyCheck}") { if (isRequired) { addException("JSON element '$jsonPath' cannot be blank") } else { - addStatement("$variableName = null") + assign(variableName, "null") } } - .endControlFlow() - - return validationBuilder.build() + } } private fun isFieldMapType(processingEnv: ProcessingEnvironment, typeMirror: TypeMirror): Boolean { val mapTypeElement = processingEnv.elementUtils.getTypeElement(Map::class.java.name) val typeUtils = processingEnv.typeUtils val mapWildcardType = typeUtils.getDeclaredType(mapTypeElement, - typeUtils.getWildcardType(null, null), - typeUtils.getWildcardType(null, null)) + typeUtils.getWildcardType(null, null), + typeUtils.getWildcardType(null, null)) return typeUtils.isSubtype(typeMirror, mapWildcardType) } diff --git a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/range/RangeFunctions.kt b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/range/RangeFunctions.kt index 6fb6ade..1777924 100644 --- a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/range/RangeFunctions.kt +++ b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/range/RangeFunctions.kt @@ -2,6 +2,7 @@ package gsonpath.extension.range import com.squareup.javapoet.CodeBlock import gsonpath.extension.addException +import gsonpath.util.`if` /** * Creates the 'range' code validation code block. @@ -19,23 +20,23 @@ fun CodeBlock.Builder.handleRangeValue(value: String, variableName: String): CodeBlock.Builder { val comparisonOperator: String = - if (isFrom) { - if (isInclusive) "<" else "<=" - } else { - if (isInclusive) ">" else ">=" - } + if (isFrom) { + if (isInclusive) "<" else "<=" + } else { + if (isInclusive) ">" else ">=" + } val expectedOperator: String = - if (isFrom) { - if (isInclusive) ">=" else ">" - } else { - if (isInclusive) "<=" else "<" - } + if (isFrom) { + if (isInclusive) ">=" else ">" + } else { + if (isInclusive) "<=" else "<" + } val label: String = if (isFrom) "from" else "to" - return this.beginControlFlow("if ($variableName $comparisonOperator $value)") - .addException("Invalid '$label' range for JSON element '$jsonPath'. Expected: '$expectedOperator $value', " + - """Found '" + $variableName + "'""") - .endControlFlow() + return `if`("$variableName $comparisonOperator $value") { + addException("Invalid '$label' range for JSON element '$jsonPath'. Expected: '$expectedOperator $value', " + + """Found '" + $variableName + "'""") + } } \ No newline at end of file diff --git a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/range/floatrange/FloatRangeGsonPathFieldValidator.kt b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/range/floatrange/FloatRangeGsonPathFieldValidator.kt index 8ad4227..dd35c5c 100644 --- a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/range/floatrange/FloatRangeGsonPathFieldValidator.kt +++ b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/range/floatrange/FloatRangeGsonPathFieldValidator.kt @@ -8,6 +8,7 @@ import gsonpath.compiler.GsonPathExtension import gsonpath.extension.getAnnotationMirror import gsonpath.extension.getAnnotationValueObject import gsonpath.extension.range.handleRangeValue +import gsonpath.util.codeBlock import javax.annotation.processing.ProcessingEnvironment import javax.lang.model.element.AnnotationMirror @@ -28,28 +29,27 @@ class FloatRangeGsonPathFieldValidator : GsonPathExtension { val (fieldInfo, variableName, jsonPath) = extensionFieldMetadata val floatRangeAnnotation: AnnotationMirror = - getAnnotationMirror(fieldInfo.element, "android.support.annotation", "FloatRange") - ?: getAnnotationMirror(fieldInfo.element, "gsonpath.extension.annotation", "FloatRange") - ?: return null + getAnnotationMirror(fieldInfo.element, "android.support.annotation", "FloatRange") + ?: getAnnotationMirror(fieldInfo.element, "gsonpath.extension.annotation", "FloatRange") + ?: return null // Ensure that the field is either a float, or a double. val typeName = - if (fieldInfo.typeName.isPrimitive) { - fieldInfo.typeName.box() - } else { - fieldInfo.typeName - } + if (fieldInfo.typeName.isPrimitive) { + fieldInfo.typeName.box() + } else { + fieldInfo.typeName + } if (typeName != BOXED_DOUBLE && typeName != BOXED_FLOAT) { throw ProcessingException("Unexpected type found for field annotated with 'FloatRange', only " + - "floats and doubles are allowed.", fieldInfo.element) + "floats and doubles are allowed.", fieldInfo.element) } - val validationBuilder = CodeBlock.builder() - .handleFrom(floatRangeAnnotation, jsonPath, variableName) - .handleTo(floatRangeAnnotation, jsonPath, variableName) - - val validationCodeBlock = validationBuilder.build() + val validationCodeBlock = codeBlock { + handleFrom(floatRangeAnnotation, jsonPath, variableName) + handleTo(floatRangeAnnotation, jsonPath, variableName) + } if (!validationCodeBlock.isEmpty) { return validationCodeBlock } @@ -68,7 +68,7 @@ class FloatRangeGsonPathFieldValidator : GsonPathExtension { val fromValue: Double = getAnnotationValueObject(floatRangeAnnotationMirror, "from") as Double? ?: return this val fromInclusive: Boolean = getAnnotationValueObject(floatRangeAnnotationMirror, "fromInclusive") as Boolean? - ?: true + ?: true if (fromValue == Double.NEGATIVE_INFINITY) { return this @@ -89,7 +89,7 @@ class FloatRangeGsonPathFieldValidator : GsonPathExtension { val toValue: Double = getAnnotationValueObject(floatRangeAnnotationMirror, "to") as Double? ?: return this val toInclusive: Boolean = getAnnotationValueObject(floatRangeAnnotationMirror, "toInclusive") as Boolean? - ?: true + ?: true if (toValue == java.lang.Double.POSITIVE_INFINITY) { return this diff --git a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/range/intrange/IntRangeGsonPathFieldValidator.kt b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/range/intrange/IntRangeGsonPathFieldValidator.kt index 9ff8915..7a8562f 100644 --- a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/range/intrange/IntRangeGsonPathFieldValidator.kt +++ b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/range/intrange/IntRangeGsonPathFieldValidator.kt @@ -8,6 +8,7 @@ import gsonpath.compiler.GsonPathExtension import gsonpath.extension.getAnnotationMirror import gsonpath.extension.getAnnotationValueObject import gsonpath.extension.range.handleRangeValue +import gsonpath.util.codeBlock import javax.annotation.processing.ProcessingEnvironment import javax.lang.model.element.AnnotationMirror @@ -28,28 +29,27 @@ class IntRangeGsonPathFieldValidator : GsonPathExtension { val (fieldInfo, variableName, jsonPath) = extensionFieldMetadata val intRangeAnnotation: AnnotationMirror = - getAnnotationMirror(fieldInfo.element, "android.support.annotation", "IntRange") - ?: getAnnotationMirror(fieldInfo.element, "gsonpath.extension.annotation", "IntRange") - ?: return null + getAnnotationMirror(fieldInfo.element, "android.support.annotation", "IntRange") + ?: getAnnotationMirror(fieldInfo.element, "gsonpath.extension.annotation", "IntRange") + ?: return null // Ensure that the field is either a integer, or a long. val typeName = - if (fieldInfo.typeName.isPrimitive) { - fieldInfo.typeName.box() - } else { - fieldInfo.typeName - } + if (fieldInfo.typeName.isPrimitive) { + fieldInfo.typeName.box() + } else { + fieldInfo.typeName + } if (typeName != BOXED_INT && typeName != BOXED_LONG) { throw ProcessingException("Unexpected type found for field annotated with 'IntRange', only " + - "integers and longs are allowed.", fieldInfo.element) + "integers and longs are allowed.", fieldInfo.element) } - val validationBuilder = CodeBlock.builder() - .handleFrom(intRangeAnnotation, jsonPath, variableName) - .handleTo(intRangeAnnotation, jsonPath, variableName) - - val validationCodeBlock = validationBuilder.build() + val validationCodeBlock = codeBlock { + handleFrom(intRangeAnnotation, jsonPath, variableName) + handleTo(intRangeAnnotation, jsonPath, variableName) + } if (!validationCodeBlock.isEmpty) { return validationCodeBlock } diff --git a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/size/SizeGsonPathFieldValidator.kt b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/size/SizeGsonPathFieldValidator.kt index e0ed596..759a7ae 100644 --- a/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/size/SizeGsonPathFieldValidator.kt +++ b/gsonpath-extensions-compiler/src/main/java/gsonpath/extension/size/SizeGsonPathFieldValidator.kt @@ -9,6 +9,8 @@ import gsonpath.extension.addException import gsonpath.extension.getAnnotationMirror import gsonpath.extension.getAnnotationValueObject import gsonpath.util.ProcessorTypeHandler +import gsonpath.util.`if` +import gsonpath.util.codeBlock import javax.annotation.processing.ProcessingEnvironment import javax.lang.model.element.AnnotationMirror import javax.lang.model.type.ArrayType @@ -27,35 +29,34 @@ class SizeGsonPathFieldValidator : GsonPathExtension { val (fieldInfo, variableName, jsonPath) = extensionFieldMetadata val sizeAnnotation: AnnotationMirror = - getAnnotationMirror(fieldInfo.element, "android.support.annotation", "Size") - ?: getAnnotationMirror(fieldInfo.element, "gsonpath.extension.annotation", "Size") - ?: return null + getAnnotationMirror(fieldInfo.element, "android.support.annotation", "Size") + ?: getAnnotationMirror(fieldInfo.element, "gsonpath.extension.annotation", "Size") + ?: return null // Ensure that the field is either an array, string or a collection. val fieldCollectionType: Boolean = - try { - ProcessorTypeHandler(processingEnvironment).isMirrorOfCollectionType(fieldInfo.typeMirror) - } catch (e: Exception) { - false - } + try { + ProcessorTypeHandler(processingEnvironment).isMirrorOfCollectionType(fieldInfo.typeMirror) + } catch (e: Exception) { + false + } val fieldType: FieldType = - when { - (fieldInfo.typeMirror is ArrayType) -> FieldType.ARRAY - fieldCollectionType -> FieldType.COLLECTION - (fieldInfo.typeName == ClassName.get(String::class.java)) -> FieldType.STRING - - else -> - throw ProcessingException("Unexpected type found for field annotated with 'Size', only " + - "arrays, string, or collection classes may be used.", fieldInfo.element) - } - - val validationBuilder = CodeBlock.builder() - .handleExactLength(sizeAnnotation, jsonPath, variableName, fieldType) - .handleMin(sizeAnnotation, jsonPath, variableName, fieldType) - .handleMax(sizeAnnotation, jsonPath, variableName, fieldType) - .handleMultiple(sizeAnnotation, jsonPath, variableName, fieldType) - - val validationCodeBlock = validationBuilder.build() + when { + (fieldInfo.typeMirror is ArrayType) -> FieldType.ARRAY + fieldCollectionType -> FieldType.COLLECTION + (fieldInfo.typeName == ClassName.get(String::class.java)) -> FieldType.STRING + + else -> + throw ProcessingException("Unexpected type found for field annotated with 'Size', only " + + "arrays, string, or collection classes may be used.", fieldInfo.element) + } + + val validationCodeBlock = codeBlock { + handleExactLength(sizeAnnotation, jsonPath, variableName, fieldType) + handleMin(sizeAnnotation, jsonPath, variableName, fieldType) + handleMax(sizeAnnotation, jsonPath, variableName, fieldType) + handleMultiple(sizeAnnotation, jsonPath, variableName, fieldType) + } if (!validationCodeBlock.isEmpty) { return validationCodeBlock } @@ -79,12 +80,10 @@ class SizeGsonPathFieldValidator : GsonPathExtension { } val lengthProperty = fieldType.lengthProperty - return this.beginControlFlow("if ($variableName.$lengthProperty < $minValue)") - - .addSizeException(fieldType, jsonPath, - """Expected minimum: '$minValue', actual minimum: '" + $variableName.$lengthProperty + "'""") - - .endControlFlow() + return `if`("$variableName.$lengthProperty < $minValue") { + addSizeException(fieldType, jsonPath, + """Expected minimum: '$minValue', actual minimum: '" + $variableName.$lengthProperty + "'""") + } } /** @@ -104,12 +103,10 @@ class SizeGsonPathFieldValidator : GsonPathExtension { } val lengthProperty = fieldType.lengthProperty - return this.beginControlFlow("if ($variableName.$lengthProperty > $maxValue)", variableName, maxValue) - - .addSizeException(fieldType, jsonPath, - """Expected maximum: '$maxValue', actual maximum: '" + $variableName.$lengthProperty + "'""") - - .endControlFlow() + return `if`("$variableName.$lengthProperty > $maxValue", variableName, maxValue) { + addSizeException(fieldType, jsonPath, + """Expected maximum: '$maxValue', actual maximum: '" + $variableName.$lengthProperty + "'""") + } } /** @@ -131,12 +128,10 @@ class SizeGsonPathFieldValidator : GsonPathExtension { } val lengthProperty = fieldType.lengthProperty - return this.beginControlFlow("if ($variableName.$lengthProperty % $multipleValue != 0)", variableName, multipleValue) - - .addSizeException(fieldType, jsonPath, - """$lengthProperty of '" + $variableName.$lengthProperty + "' is not a multiple of $multipleValue""") - - .endControlFlow() + return `if`("$variableName.$lengthProperty % $multipleValue != 0", variableName, multipleValue) { + addSizeException(fieldType, jsonPath, + """$lengthProperty of '" + $variableName.$lengthProperty + "' is not a multiple of $multipleValue""") + } } /** @@ -158,13 +153,10 @@ class SizeGsonPathFieldValidator : GsonPathExtension { } val lengthProperty = fieldType.lengthProperty - return this.beginControlFlow("if ($variableName.$lengthProperty != $exactLengthValue)", variableName, exactLengthValue) - - .addSizeException(fieldType, jsonPath, - "Expected $lengthProperty: '$exactLengthValue', " + + return `if`("$variableName.$lengthProperty != $exactLengthValue", variableName, exactLengthValue) { + addSizeException(fieldType, jsonPath, "Expected $lengthProperty: '$exactLengthValue', " + """actual $lengthProperty: '" + $variableName.$lengthProperty + "'""") - - .endControlFlow() + } } /** @@ -173,8 +165,8 @@ class SizeGsonPathFieldValidator : GsonPathExtension { private fun CodeBlock.Builder.addSizeException(fieldType: FieldType, jsonPath: String, exceptionText: String): CodeBlock.Builder { - return this.addException("Invalid ${fieldType.label} ${fieldType.lengthProperty} for JSON element '$jsonPath'. " + - exceptionText) + return addException("Invalid ${fieldType.label} ${fieldType.lengthProperty} for JSON element '$jsonPath'. " + + exceptionText) } /** From a1f95e43dbf64474f113f99312361f122ae4b940 Mon Sep 17 00:00:00 2001 From: Lachlan McKee Date: Mon, 19 Nov 2018 15:42:13 +0000 Subject: [PATCH 3/5] Issue #15 Updated kotlin to 1.3.0 --- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- gsonpath-extensions-compiler/build.gradle | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 3fd8a17..a2055a1 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.2.31' + ext.kotlin_version = '1.3.0' ext.gson_version = '2.8.1' ext.gsonpath_version = '3.0.0' ext.gsonpath_compiler_version = '1.2.1' diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 4cdb829..0c160ab 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip diff --git a/gsonpath-extensions-compiler/build.gradle b/gsonpath-extensions-compiler/build.gradle index 70c438d..571783c 100644 --- a/gsonpath-extensions-compiler/build.gradle +++ b/gsonpath-extensions-compiler/build.gradle @@ -4,7 +4,7 @@ apply plugin: 'maven' apply plugin: 'signing' group = 'net.lachlanmckee' -version = '1.0.0' +version = '1.1.0' targetCompatibility = JavaVersion.VERSION_1_7 sourceCompatibility = JavaVersion.VERSION_1_7 From 84ee5fe6d22820c567d8fdefd5b2b69258d52bcc Mon Sep 17 00:00:00 2001 From: Lachlan McKee Date: Mon, 19 Nov 2018 15:44:43 +0000 Subject: [PATCH 4/5] Issue #15 Preparing to release 1.1.0 --- CHANGELOG.md | 4 ++-- README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7eb4c30..b00b9ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,10 @@ Change Log ========== -Version 1.1.0 *(2017-02-26)* +Version 1.1.0 *(2018-11-19)* ---------------------------- -* Added support for Gsonpath 2.3.0. +* Added support for Gsonpath 3.0.0 Version 1.0.0 *(2017-02-26)* ---------------------------- diff --git a/README.md b/README.md index 075ccb0..b825755 100644 --- a/README.md +++ b/README.md @@ -236,5 +236,5 @@ if (nonNullMap.size() == 0) { This library is available on Maven, you can add it to your project using the following gradle dependencies: ```gradle -apt 'net.lachlanmckee:gsonpath-extensions:1.0.0' +apt 'net.lachlanmckee:gsonpath-extensions:1.1.0' ``` From 8b2bb1cc27c71ac105cd4f0863ae9a7e1b17b8c8 Mon Sep 17 00:00:00 2001 From: Lachlan McKee Date: Mon, 19 Nov 2018 15:56:28 +0000 Subject: [PATCH 5/5] Updated non-compiler module to 1.1.0 as well --- gsonpath-extensions/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gsonpath-extensions/build.gradle b/gsonpath-extensions/build.gradle index 8f32982..65ec529 100644 --- a/gsonpath-extensions/build.gradle +++ b/gsonpath-extensions/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'maven' apply plugin: 'signing' group = 'net.lachlanmckee' -version = '1.0.0' +version = '1.1.0' targetCompatibility = JavaVersion.VERSION_1_7 sourceCompatibility = JavaVersion.VERSION_1_7