Skip to content

Commit f00da97

Browse files
committed
update error printing
1 parent 641233b commit f00da97

File tree

4 files changed

+60
-25
lines changed

4 files changed

+60
-25
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ org.gradle.jvmargs=-Xmx1536m
1717
RELEASE_REPOSITORY_URL=https://maven.pkg.github.com/bleacherreport/Velocidapter
1818
SNAPSHOT_REPOSITORY_URL=https://maven.pkg.github.com/bleacherreport/Velocidapter
1919

20-
VERSION_NAME=2.0.0.1-SNAPSHOT
20+
VERSION_NAME=2.0.0-SNAPSHOT
2121
GROUP=com.bleacherreport.velocidapter
2222

2323
POM_NAME=Velocidapter

velocidapter-demo/src/main/java/com/bleacherreport/velocidapterdemo/single/NumberViewHolder.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.bleacherreport.velocidapterannotations.ViewHolder
66
import com.bleacherreport.velocidapterdemo.MainActivity
77
import com.bleacherreport.velocidapterdemo.databinding.ItemNumberBinding
88

9+
910
/** ViewBinding Top Level Extension Function **/
1011
@ViewHolder(adapters = [MainActivity.SingleAdapter, MainActivity.MultiAdapter])
1112
fun ItemNumberBinding.bind(item: NumberViewItemBindingExtension) {

velocidapter/src/main/java/com/bleacherreport/velocidapter/FunctionBuilder.kt

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,27 @@ class ViewBindingFunction private constructor(
4040
}
4141

4242
companion object {
43-
fun from(element: Element): ViewBindingFunction {
43+
fun from(element: Element): ViewBindingFunction? {
4444
val executableType = (element.asType() as ExecutableType)
4545
val parameters = executableType.parameterTypes
4646

4747
val viewBindingParamName = executableType.viewBindingParamNameAtPosition(0)
48-
?: throw VelocidapterException("@ViewHolderBind method ${element.methodFullName()} should be an extension method for a ViewBinding. $BIND_INSTRUCTION")
48+
?: kotlin.run {
49+
errors.add("@ViewHolderBind method ${element.methodFullName()} should be an extension method for a ViewBinding. $BIND_INSTRUCTION")
50+
return null
51+
}
4952

5053
val dataParamName = executableType.paramNameAtPosition(1)
51-
?: throw VelocidapterException("@ViewHolderBind method ${element.methodFullName()} second param should be a data param. $BIND_INSTRUCTION")
54+
?: kotlin.run {
55+
errors.add("@ViewHolderBind method ${element.methodFullName()} second param should be a data param. $BIND_INSTRUCTION")
56+
return null
57+
}
5258

5359
if (parameters.size > 2) {
54-
throw VelocidapterException("@ViewHolderBind method ${element.methodFullName()} has too many params. $BIND_INSTRUCTION")
60+
kotlin.run {
61+
errors.add("@ViewHolderBind method ${element.methodFullName()} has too many params. $BIND_INSTRUCTION")
62+
return null
63+
}
5564
}
5665

5766
return ViewBindingFunction(
@@ -102,30 +111,40 @@ class ViewHolderBindFunction private constructor(
102111
}
103112

104113
companion object {
105-
fun from(element: Element, viewHolderName: String, bindingType: String): ViewHolderBindFunction {
114+
fun from(element: Element, viewHolderName: String, bindingType: String): ViewHolderBindFunction? {
106115
val executableType = (element.asType() as ExecutableType)
107116

108117
var startingPosition = 0
109118
val viewBindingParamName = executableType.viewBindingParamNameAtPosition(startingPosition)
110119
if (viewBindingParamName != null) {
111120
if (viewBindingParamName != bindingType)
112-
throw VelocidapterException("@Bind method $viewHolderName.${element.simpleName} viewBinding type was $viewBindingParamName but should be $bindingType. $VIEW_HOLDER_BIND_INSTRUCTION")
121+
kotlin.run {
122+
errors.add("@Bind method $viewHolderName.${element.simpleName} viewBinding type was $viewBindingParamName but should be $bindingType. $VIEW_HOLDER_BIND_INSTRUCTION")
123+
return null
124+
}
113125
startingPosition++
114126
}
115127

116128
val dataParamName = executableType.paramNameAtPosition(startingPosition++)
117-
?: throw VelocidapterException("@Bind method $viewHolderName.${element.simpleName} data param is required. $VIEW_HOLDER_BIND_INSTRUCTION")
129+
?: kotlin.run {
130+
errors.add("@Bind method $viewHolderName.${element.simpleName} data param is required. $VIEW_HOLDER_BIND_INSTRUCTION")
131+
return null
132+
}
118133

119134
val hasPositionParam = executableType.parameterTypes
120135
.getOrNull(startingPosition++)
121136
?.let {
122137
if (it !is PrimitiveType || it.kind != TypeKind.INT)
123-
throw VelocidapterException("@Bind method $viewHolderName.${element.simpleName} second parameter is not an Int. $VIEW_HOLDER_BIND_INSTRUCTION")
138+
kotlin.run {
139+
errors.add("@Bind method $viewHolderName.${element.simpleName} second parameter is not an Int. $VIEW_HOLDER_BIND_INSTRUCTION")
140+
return null
141+
}
124142
true
125143
} ?: false
126144

127145
executableType.paramNameAtPosition(startingPosition)?.let {
128-
throw VelocidapterException("@Bind method $viewHolderName.${element.simpleName}$executableType has too many params = ${executableType.parameterTypes.size}. $VIEW_HOLDER_BIND_INSTRUCTION")
146+
errors.add("@Bind method $viewHolderName.${element.simpleName}$executableType has too many params = ${executableType.parameterTypes.size}. $VIEW_HOLDER_BIND_INSTRUCTION")
147+
return null
129148
}
130149

131150
return ViewHolderBindFunction(element,

velocidapter/src/main/java/com/bleacherreport/velocidapter/VelocidapterProcessor.kt

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ import javax.lang.model.type.DeclaredType
1414
import javax.lang.model.type.ExecutableType
1515
import javax.tools.Diagnostic
1616

17+
val errors = mutableListOf<String>()
1718

1819
@AutoService(Processor::class)
1920
class VelocidapterProcessor : AbstractProcessor() {
2021

22+
init {
23+
errors.clear()
24+
}
25+
2126
override fun getSupportedAnnotationTypes(): MutableSet<String> {
2227
return mutableSetOf(ViewHolder::class.java.name,
2328
Bind::class.java.name,
@@ -42,7 +47,7 @@ class VelocidapterProcessor : AbstractProcessor() {
4247
fun getFunctions(
4348
viewHolderElement: Element,
4449
viewBinding: String,
45-
callback: (bindFunction: ViewHolderBindFunction, unbindFunction: FunctionName?, attachFunction: FunctionName?, detachFunction: FunctionName?) -> Unit,
50+
callback: (bindFunction: ViewHolderBindFunction?, unbindFunction: FunctionName?, attachFunction: FunctionName?, detachFunction: FunctionName?) -> Unit,
4651
) {
4752
var bindFunction: ViewHolderBindFunction? = null
4853
var unbindFunction: FunctionName? = null
@@ -52,7 +57,7 @@ class VelocidapterProcessor : AbstractProcessor() {
5257
viewHolderElement.enclosedElements.forEach { enclosedElement ->
5358
bindFunctionList?.find { it == enclosedElement }?.let { bindElement ->
5459
if (bindFunction != null) {
55-
throw VelocidapterException("${viewHolderElement.simpleName} has multiple @Bind annotated methods. $VIEW_HOLDER_BIND_INSTRUCTION")
60+
errors.add("${viewHolderElement.simpleName} has multiple @Bind annotated methods. $VIEW_HOLDER_BIND_INSTRUCTION")
5661
}
5762
bindFunction = ViewHolderBindFunction.from(
5863
bindElement,
@@ -63,29 +68,29 @@ class VelocidapterProcessor : AbstractProcessor() {
6368

6469
unbindFunctionList?.find { it == enclosedElement }?.let { bindElement ->
6570
if (unbindFunction != null) {
66-
throw VelocidapterException("${viewHolderElement.simpleName} has more than one @Unbind annotated method.")
71+
errors.add("${viewHolderElement.simpleName} has more than one @Unbind annotated method.")
6772
}
6873
unbindFunction = FunctionName.from(bindElement)
6974
}
7075

7176
attachFunctionList?.find { it == enclosedElement }?.let { attachElement ->
7277
if (attachFunction != null) {
73-
throw VelocidapterException("${viewHolderElement.simpleName} has multiple @OnAttachToWindow annotated methods.")
78+
errors.add("${viewHolderElement.simpleName} has multiple @OnAttachToWindow annotated methods.")
7479
}
7580
attachFunction = FunctionName.from(attachElement)
7681
}
7782

7883
detatchFunctionList?.find { it == enclosedElement }?.let { detachElement ->
7984
if (detachFunction != null) {
80-
throw VelocidapterException("${viewHolderElement.simpleName} has multiple @OnDetachFromWindow annotated methods.")
85+
errors.add("${viewHolderElement.simpleName} has multiple @OnDetachFromWindow annotated methods.")
8186
}
8287
detachFunction = FunctionName.from(detachElement)
8388
}
8489
}
8590

91+
8692
callback(
87-
bindFunction
88-
?: throw VelocidapterException("${viewHolderElement.simpleName} is missing an @Bind method. $VIEW_HOLDER_BIND_INSTRUCTION"),
93+
bindFunction,
8994
unbindFunction,
9095
attachFunction,
9196
detachFunction
@@ -121,11 +126,19 @@ class VelocidapterProcessor : AbstractProcessor() {
121126
viewBindingTypeElement
122127
}
123128
.firstOrNull()
124-
?: throw VelocidapterException("@ViewHolder for class ${viewHolderElement.simpleName} must have a constructor with a single param that is of type ViewBinding")
129+
?: kotlin.run {
130+
errors.add("@ViewHolder for class ${viewHolderElement.simpleName} must have a constructor with a single param that is of type ViewBinding")
131+
return@forEach
132+
}
125133

126134
getFunctions(viewHolderElement,
127135
binding.qualifiedName?.toString()!!) { bindFunction, unbindFunction, attachFunction, detachFunction ->
128136

137+
if (bindFunction == null) {
138+
errors.add("${viewHolderElement.simpleName} is missing an @Bind method. $VIEW_HOLDER_BIND_INSTRUCTION")
139+
return@getFunctions
140+
}
141+
129142
val viewHolder = BindMethodViewHolderBuilder(
130143
viewHolderElement,
131144
getFullPath(viewHolderElement),
@@ -158,7 +171,7 @@ class VelocidapterProcessor : AbstractProcessor() {
158171
}
159172
?.forEach { viewHolderBindingElement ->
160173

161-
val bindFunction = ViewBindingFunction.from(viewHolderBindingElement)
174+
val bindFunction = ViewBindingFunction.from(viewHolderBindingElement) ?: return@forEach
162175
val annotation = viewHolderBindingElement.getAnnotation(ViewHolder::class.java)!!
163176

164177
val viewHolder = ClassViewHolderBuilder(
@@ -179,6 +192,13 @@ class VelocidapterProcessor : AbstractProcessor() {
179192
}
180193
}
181194

195+
if (errors.isNotEmpty()) {
196+
printMessage("")
197+
errors.forEach {
198+
printMessage("ERROR = $it")
199+
}
200+
return false
201+
}
182202

183203
adapterList.forEach { entry ->
184204
val builder = FileSpec.builder("com.bleacherreport.velocidapter", entry.name)
@@ -193,11 +213,8 @@ class VelocidapterProcessor : AbstractProcessor() {
193213

194214
true
195215
} catch (e: Exception) {
196-
printMessage("ERROR = ${e.message}")
197216
printMessage("")
198-
e.stackTrace.forEach {
199-
processingEnv.messager.printMessage(Diagnostic.Kind.ERROR, " ${it}\r\n")
200-
}
217+
printMessage("ERROR = ${e.message}")
201218
false
202219
}
203220
}
@@ -347,8 +364,6 @@ class VelocidapterProcessor : AbstractProcessor() {
347364
}
348365
}
349366

350-
class VelocidapterException(override val message: String?) : java.lang.Exception(message)
351-
352367

353368
data class BindableAdapter(val name: String, val viewHolders: MutableSet<BaseViewHolderBuilder> = LinkedHashSet()) {
354369
val dataListName

0 commit comments

Comments
 (0)