Avoid reflection when creating attribute adapters

This commit is contained in:
Akash Yadav
2022-11-02 20:46:28 +05:30
parent 857b10c6aa
commit 5317dd4802
4 changed files with 6 additions and 47 deletions
@@ -26,7 +26,6 @@ import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.ParameterizedTypeName
import com.squareup.javapoet.TypeName
import com.squareup.javapoet.TypeSpec
import com.squareup.javapoet.WildcardTypeName
import java.io.File
import javax.annotation.processing.AbstractProcessor
import javax.annotation.processing.Processor
@@ -55,14 +54,12 @@ class AttrAdapterAnnotationProcessor : AbstractProcessor() {
private val classBuilder = TypeSpec.classBuilder(INDEX_CLASS_NAME).addModifiers(PUBLIC, FINAL)
private val addStatements = CodeBlock.builder()
init {
val wildcard =
WildcardTypeName.subtypeOf(ClassName.get(ADAPTER_BASE_CLASS_PCK, ADAPTER_BASE_CLASS_NAME))
val klass = ParameterizedTypeName.get(ClassName.get(Class::class.java), wildcard)
val adpaterType = ClassName.get(ADAPTER_BASE_CLASS_PCK, ADAPTER_BASE_CLASS_NAME)
val type =
ParameterizedTypeName.get(
ClassName.get(java.util.Map::class.java),
ClassName.get(String::class.java),
klass
adpaterType
)
classBuilder.addField(
FieldSpec.builder(type, INDEX_MAP_FIELD, PRIVATE, STATIC, FINAL)
@@ -83,9 +80,10 @@ class AttrAdapterAnnotationProcessor : AbstractProcessor() {
classBuilder.addMethod(
MethodSpec.methodBuilder("getAdapter")
.addAnnotation(ClassName.get("androidx.annotation", "Nullable"))
.addModifiers(PUBLIC, STATIC)
.addParameter(String::class.java, "view", FINAL)
.returns(klass)
.returns(adpaterType)
.addStatement("return \$L.get(\$L)", INDEX_MAP_FIELD, "view")
.build()
)
@@ -179,7 +177,7 @@ class AttrAdapterAnnotationProcessor : AbstractProcessor() {
val annotation =
element.getAnnotation(AttributeAdapter::class.java) ?: throw IllegalStateException()
addStatements.addStatement(
"\$L.put(\$S, \$T.class)",
"\$L.put(\$S, new \$T())",
INDEX_MAP_FIELD,
getViewName(annotation),
TypeName.get(element.asType())
Binary file not shown.
@@ -32,6 +32,6 @@ abstract class AbstractView(
protected val attributes = mutableListOf<IAttribute>()
override fun addAttribute(attribute: IAttribute) {
AttributeAdapterIndex.getAdapter(name)?.apply { }
}
}
@@ -1,39 +0,0 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.inflater.internal
import com.itsaky.androidide.inflater.IAttributeAdapter
import com.itsaky.androidide.utils.ILogger
/**
* @author Akash Yadav
*/
object AdapterProvider {
private val log = ILogger.newInstance("AdapterProvider")
@JvmStatic
fun getAdapter(name: String) : IAttributeAdapter? {
return try {
val klass = AttributeAdapterIndex.getAdapter(name) ?: return null
klass.getConstructor().newInstance()
} catch (err: Throwable) {
null
}
}
}