0
我的目標是能夠註釋基於TextView的類,以便我可以在它們上注入自定義字體,而無需搜索我的整個(和龐大的)代碼庫。由於我有一個AspectJ Android項目,所以AOP看起來很不錯。使用AspectJ捕獲Android中帶註釋的小部件的初始化/設置
我開始通過定義以下注釋:
@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中定義的建議未應用」警告。
任何人都可以對此有所瞭解嗎?
在此先感謝。