From 5317dd48022f38fbfab2a035cf5bbb561bbda819 Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Wed, 2 Nov 2022 20:46:28 +0530 Subject: [PATCH] Avoid reflection when creating attribute adapters --- .../AttrAdapterAnnotationProcessor.kt | 12 +++--- tests/test-home/.androidide/init/model.jar | Bin 65261 -> 65261 bytes .../inflater/internal/AbstractView.kt | 2 +- .../inflater/internal/AdapterProvider.kt | 39 ------------------ 4 files changed, 6 insertions(+), 47 deletions(-) delete mode 100644 xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/AdapterProvider.kt diff --git a/annotation-processors/src/main/java/com/itsaky/androidide/annotations/inflater/AttrAdapterAnnotationProcessor.kt b/annotation-processors/src/main/java/com/itsaky/androidide/annotations/inflater/AttrAdapterAnnotationProcessor.kt index d956cca8c..1f790bdb7 100644 --- a/annotation-processors/src/main/java/com/itsaky/androidide/annotations/inflater/AttrAdapterAnnotationProcessor.kt +++ b/annotation-processors/src/main/java/com/itsaky/androidide/annotations/inflater/AttrAdapterAnnotationProcessor.kt @@ -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()) diff --git a/tests/test-home/.androidide/init/model.jar b/tests/test-home/.androidide/init/model.jar index 758f910d9a8999bf3d3adafb382631362ee87890..237f0e5de102b03b4cf5aec39791fca882096040 100644 GIT binary patch delta 31 ncmaF+m-+2qW}X0VW)?061`ZB}#Y;Ex_`PRZymWKS`-%JjxMd5? delta 31 ncmaF+m-+2qW}X0VW)?061`ZB}3A`-%JjxLOO# diff --git a/xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/AbstractView.kt b/xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/AbstractView.kt index 7a276f16c..2b4ed3e88 100644 --- a/xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/AbstractView.kt +++ b/xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/AbstractView.kt @@ -32,6 +32,6 @@ abstract class AbstractView( protected val attributes = mutableListOf() override fun addAttribute(attribute: IAttribute) { - AttributeAdapterIndex.getAdapter(name)?.apply { } + } } diff --git a/xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/AdapterProvider.kt b/xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/AdapterProvider.kt deleted file mode 100644 index e0def9484..000000000 --- a/xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/AdapterProvider.kt +++ /dev/null @@ -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 . - */ - -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 - } - } -} \ No newline at end of file