Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用AspectJ捕获Android中带注释小部件的初始化/设置_Android_Android Widget_Aspectj - Fatal编程技术网

使用AspectJ捕获Android中带注释小部件的初始化/设置

使用AspectJ捕获Android中带注释小部件的初始化/设置,android,android-widget,aspectj,Android,Android Widget,Aspectj,我的目标是能够对基于TextView的类进行注释,这样我就可以在它们上注入自定义字体,而不必搜索整个(庞大的)代码库。因为我有一个AspectJ Android项目,所以对于AOP来说这似乎是一个不错的工作 我首先定义了以下注释: @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface InsertFontTypeFace { String typeFacenamePathInAs

我的目标是能够对基于TextView的类进行注释,这样我就可以在它们上注入自定义字体,而不必搜索整个(庞大的)代码库。因为我有一个AspectJ Android项目,所以对于AOP来说这似乎是一个不错的工作

我首先定义了以下注释:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface InsertFontTypeFace 
{
    String typeFacenamePathInAssets() default "";
}
在我的活动中,我有以下几点:

@InsertFontTypeFace(typeFacenamePathInAssets="fonts/myCustomFont.ttf")
private Button myButton;
最后,在我这方面,我有:

pointcut textViewBasedWidgetInitialization(TextView thisObject, InsertFontTypeFace   annotation): initialization(TextView+.new(..)) && @annotation(annotation) && target(thisObject);

after(TextView thisObject, InsertFontTypeFace annotation) : textViewBasedWidgetInitialization(thisObject, annotation)
{
    String pathToFont = annotation.typeFacenamePathInAssets();

    if(! EMPTY_STRING.equals(pathToFont))
    {
        Typeface myTypeface = Typeface.createFromAsset(thisObject.getContext().getAssets(), pathToFont);
        thisObject.setTypeface(myTypeface);
    }
}
我还尝试了以下切入点来捕获字段设置:

pointcut textViewBasedWidgetInitialization(TextView thisObject, InsertFontTypeFace annotation): set(TextView+ *.*) && @annotation(annotation) && target(thisObject);
这两个选项都会在Eclipse中产生“XXX中定义的通知尚未应用”警告

有人能解释一下吗


提前谢谢

好吧,我不认为我有完美的解决方案,但我有一个变通办法,就是做我想做的事情。代码如下:

pointcut textViewBasedWidgetInitialization(InsertFontTypeFace annotation): set(TextView+ Activity+.*) && @annotation(annotation);

after (InsertFontTypeFace annotation, Object targetObject): textViewBasedWidgetInitialization(annotation) && args(targetObject)
{
    if(targetObject != null)
    {
        TextView widget = (TextView) targetObject;

        String pathToFont = annotation.typeFacenamePathInAssets();

        if(! EMPTY_STRING.equals(pathToFont))
        {
            Typeface myTypeface = Typeface.createFromAsset(widget.getContext().getAssets(), pathToFont);
            widget.setTypeface(myTypeface);
        }
    }
}
我只是捕获设置一个TextView子类,该子类在节上用@InsertFontTypeFace注释。稍后,我将建议切入点并捕获args(这将是小部件!!)。我尝试了this()和target(),但它们总是捕获活动,而不是小部件。我还尝试了()returning()和variants,但无法实现,因为continue()必须使用与point cut相同数量的参数,而且我确实需要捕获注释

嗯,这是一种解决办法。如果有人有更好的贡献,我想看看