Java提供了4中注解,专门负责新注解的创建。
@Target
表示该注解可以用于什么地方,可选的ElementType参数有:
CONSTRUCTOR:构造器的声明
FIELD:域声明(包括enum实例)
LOCAL_VARIABLE:局部变量声明
METHOD:方法声明
PACKAGE:包声明
PARAMETER:参数声明
TYPE:类、接口(包括注解类型)或enum声明
@Retention
表示需要在什么级别保存该注解信息。可选的RetentionPolicy参数包括:
SOURCE:注解将被编译器丢弃
CLASS:注解在class文件中可用,但会被VM丢弃
RUNTIME:VM将在运行期间保留注解,因此可以通过反射机制读取注解的信息。
@Document
将注解包含在Javadoc中
@Inherited
允许子类继承父类中的注解
afinal中注解ViewInject 的创建
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ViewInject {
int id();
String click() default “”;
String longClick() default “”;
String itemClick() default “”;
String itemLongClick() default “”;
Select select() default @Select( selected = “” );
}
ViewInject 注解处理器
public abstract class FinalActivity extends Activity {
public FinalActivity() {
}
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
initInjectedView(this);
}
public void setContentView(View view, LayoutParams params) {
super.setContentView(view, params);
initInjectedView(this);
}
public void setContentView(View view) {
super.setContentView(view);
initInjectedView(this);
}
public static void initInjectedView(Activity activity) {
initInjectedView(activity, activity.getWindow().getDecorView());
}
public static void initInjectedView(Object injectedSource, View sourceView) {
Field[] fields = injectedSource.getClass().getDeclaredFields();
if(fields != null && fields.length > 0) {
Field[] var6 = fields;
int var5 = fields.length;
for(int var4 = 0; var4 < var5; ++var4) {
Field field = var6[var4];
try {
field.setAccessible(true);
if(field.get(injectedSource) == null) {
ViewInject e = (ViewInject)field.getAnnotation(ViewInject.class);
if(e != null) {
int viewId = e.id();
field.set(injectedSource, sourceView.findViewById(viewId));
setListener(injectedSource, field, e.click(), FinalActivity.Method.Click);
setListener(injectedSource, field, e.longClick(), FinalActivity.Method.LongClick);
setListener(injectedSource, field, e.itemClick(), FinalActivity.Method.ItemClick);
setListener(injectedSource, field, e.itemLongClick(), FinalActivity.Method.itemLongClick);
Select select = e.select();
if(!TextUtils.isEmpty(select.selected())) {
setViewSelectListener(injectedSource, field, select.selected(), select.noSelected());
}
}
}
} catch (Exception var10) {
var10.printStackTrace();
}
}
}
}
private static void setViewSelectListener(Object injectedSource, Field field, String select, String noSelect) throws Exception {
Object obj = field.get(injectedSource);
if(obj instanceof View) {
((AbsListView)obj).setOnItemSelectedListener((new EventListener(injectedSource)).select(select).noSelect(noSelect));
}
}
private static void setListener(Object injectedSource, Field field, String methodName, FinalActivity.Method method) throws Exception {
if(methodName != null && methodName.trim().length() != 0) {
Object obj = field.get(injectedSource);
switch($SWITCH_TABLE$net$tsz$afinal$FinalActivity$Method()[method.ordinal()]) {
case 1:
if(obj instanceof View) {
((View)obj).setOnClickListener((new EventListener(injectedSource)).click(methodName));
}
break;
case 2:
if(obj instanceof View) {
((View)obj).setOnLongClickListener((new EventListener(injectedSource)).longClick(methodName));
}
break;
case 3:
if(obj instanceof AbsListView) {
((AbsListView)obj).setOnItemClickListener((new EventListener(injectedSource)).itemClick(methodName));
}
break;
case 4:
if(obj instanceof AbsListView) {
((AbsListView)obj).setOnItemLongClickListener((new EventListener(injectedSource)).itemLongClick(methodName));
}
}
}
}
public static enum Method {
Click,
LongClick,
ItemClick,
itemLongClick;
private Method() {
}
}
}
ViewInject 应用
public class MyAcitity extends BaseActivity {
@ViewInject(id = R.id.tv_title)
private TextView titleTv;
@ViewInject(id = R.id.ibtn_left, click = “onBackClick”)
private ImageButton backIbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
FinalActivity.initInjectedView(this);
}
public void onBackClick(View view) {
finish();
}
}