我正在通过编写一个简单的“已记录”来尝试Xtend的活动注释
注释,用于在调用方法时进行跟踪.基本上我想用Xtend编写:
@LoggedoverrIDe onCreate(){ sampleFuncCall()}
并在Java中获得如下内容:
@OverrIDe voID onCreate(){ Log.d("TAG", "onCreate started"); sampleFuncCall(); Log.d("TAG", "onCreate ended");}
这是我的第一次尝试:
@Active(LoggedAnnotationProcessor)@Target(ElementType.METHOD)annotation Logged {}class LoggedAnnotationProcessor extends AbstractMethodProcessor{ overrIDe dotransform(MutableMethodDeclaration method, extension transformationContext context) { val prefix = "wrapped_" method.declaringType.addMethod(prefix + method.simplename) [ m | m.static = method.static m.visibility = Visibility.PRIVATE m.docComment = method.docComment m.exceptions = method.exceptions method.parameters.forEach[ p | m.addParameter(p.simplename, p.type) ] m.body = method.body m.primarySourceElement = method m.returnType = method.returnType ] val voIDMethod = method.returnType === null || method.returnType.voID method.body = [ ''' try { androID.util.Log.d("TAG", "«method.simplename» start"); «IF (!voIDMethod) method.returnType» ret = «ENDIF» «prefix + method.simplename»(«method.parameters.map[simplename].join(", ")»); androID.util.Log.d("TAG", "«method.simplename» end"); «IF (!voIDMethod)»return ret;«ENDIF» } catch(RuntimeException e) { androID.util.Log.d("TAG", "«method.simplename» ended with exception " + e.getClass().getSimplename() + "\n" + e.getMessage()); throw e; } '''] }}
(请注意,我找不到修改method.body的方法,而不得不创建带有“ wrapped_”前缀的新私有方法.我想这对性能不利,因此,如果您知道如何直接修改方法的主体,请分享).
在处理返回voID的方法时,我遇到了问题.
如果未明确声明该方法的返回类型,则会出现以下错误:
Cannot call method ‘isVoID’ on a inferred type reference before the
compilation phase. Check isInferred() before calling any methods.
好的,我可以添加对method.returnType.inferred的检查,但是要怎么做-似乎在这个阶段我们仍然不知道它是否将是无效的,但是知道这对于转发方法的关键返回值.
请告诉我编写这种注释的正确方法是什么,谢谢!
解决方法:
也许您应该推迟计算.到关闭
method.body = [ val voIDMethod = method.returnType === null || method.returnType.voID ''' try { androID.util.Log.d("TAG", "«method.simplename» start"); «IF (!voIDMethod)»«method.returnType» ret = «ENDIF» «prefix + method.simplename»(«method.parameters.map[simplename].join(", ")»); androID.util.Log.d("TAG", "«method.simplename» end"); «IF (!voIDMethod)»return ret;«ENDIF» } catch(RuntimeException e) { androID.util.Log.d("TAG", "«method.simplename» ended with exception " + e.getClass().getSimplename() + "\n" + e.getMessage()); throw e; } ''']
总结 以上是内存溢出为你收集整理的java-如何在Xtend的活动注释中获得完整的返回类型转发?全部内容,希望文章能够帮你解决java-如何在Xtend的活动注释中获得完整的返回类型转发?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)