android 注解异常,Android 注解处理器中的MirroredTypeException异常处理

在AOP开发中我们经常通过Element的getAnnotation(Class var1)方法去获取自定义注解中的传入的属性

例如:

@Target(AnnotationTarget.FIELD)

@Retention(AnnotationRetention.SOURCE)

@MustBeDocumented

annotation class TTEventType(val value: KClass)

复制代码

当我们获取KClass类型时会出现javax.lang.model.type.MirroredTypeException,这是因为

The annotation returned by this method could contain an element whose value is of type Class. This value cannot be returned directly: information necessary to locate and load a class (such as the class loader to use) is not available, and the class might not be loadable at all. Attempting to read a Class object by invoking the relevant method on the returned annotation will result in a MirroredTypeException, from which the corresponding TypeMirror may be extracted. Similarly, attempting to read a Class[]-valued element will result in a MirroredTypesException.

处理一

如果硬要从getAnnotation()获取则可以利用MirroredTypeException

public class MirroredTypeException

extends MirroredTypesException

Thrown when an application attempts to access the Class object corresponding to a TypeMirror.

复制代码

从异常捕获中获取TypeMirror

inline fun Element.getAnnotationClassValue(f: T.() -> KClass) =

try {

getAnnotation(T::class.java).f()

throw Exception("Expected to get a MirroredTypeException")

} catch (e: MirroredTypeException) {

e.typeMirror

}

复制代码

处理二

我们从AnnotationMirror下手 在List extends AnnotationMirror> getAnnotationMirrors()方法中获取:

private fun getEventTypeAnnotationMirror(typeElement: VariableElement, clazz: Class): AnnotationMirror? {

val clazzName = clazz.name

for (m in typeElement.annotationMirrors) {

if (m.annotationType.toString() == clazzName) {

return m

}

}

return null

}

private fun getAnnotationValue(annotationMirror: AnnotationMirror, key: String): AnnotationValue? {

for ((key1, value) in annotationMirror.elementValues) {

if (key1!!.simpleName.toString() == key) {

return value

}

}

return null

}

private fun getMyValue(foo: VariableElement, clazz: Class, key: String): TypeMirror? {

val am = getEventTypeAnnotationMirror(foo, clazz) ?: return null

val av = getAnnotationValue(am, key)

return if (av == null) {

null

} else {

av.value as TypeMirror

}

}

复制代码val typeMirror = getMyValue(variableElement,TTEventType::class.java,"value")

messager.printMessage(Diagnostic.Kind.NOTE, " --typeMirror-- $typeMirror")

复制代码

参考

Getting Class values from Annotations in an AnnotationProcessor

b739ec46bb5c46d9c0aa4ce35ba1ea56.png

关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。

本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。

[Android 注解处理器中的MirroredTypeException异常处理]http://www.zyiz.net/tech/detail-134349.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值