Java里的annotation的实现原理?
最近使用ajax往前台传送json格式的数据时,会使用@JSON(serialize=false)放在get函数前来把阻止这个对象被加入json数据中。
但是现在有一个疑问是这句话的作用是直接把这个函数注释掉了,还是在因为这一句而在生成json数据时把这个数据去除掉了?而实际这个函数还在?
可能叙述的不是很清楚。。
另外还想问一下@的实现原理是什么?可以自己定义@的格式吗?
GlkGlk
10 years, 9 months ago
Answers
可以自定义注解,比如下面的例子:
/*
* @(#)AndroidRes.java Project:com.sinaapp.msdxblog.androidkit
* Date:2012-11-18
*
* Copyright (c) 2011 CFuture09, Institute of Software,
* Guangdong Ocean University, Zhanjiang, GuangDong, China.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lurencun.cfuture09.androidkit.ui.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Android资源。
*
* @author Geek_Soledad ([email protected])
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AndroidRes {
public int id();
public ResType type();
/**
* 资源类型。
*
* @author msdx
*/
public enum ResType {
BOOLEAN, COLOR, DRAWABLE, INT, INT_ARRAY, STRING, STRING_ARRAY, TEXT, TEXT_ARRAY
}
}
这是我在我的一个android快速开发工具包里面的代码,这里就自定义了一个注解。Retention表示这个注解保留到什么时候,有编译时期(仅编译有效),运行时间等。。
Target是定义它的作用域,比如类,变量(如我上面的例子),方法等。
然后还要再写一个类,去解析它,通过反射。
代码如下:
/*
* @(#)ResBindUtil.java Project:com.sinaapp.msdxblog.androidkit
* Date:2012-11-18
*
* Copyright (c) 2011 CFuture09, Institute of Software,
* Guangdong Ocean University, Zhanjiang, GuangDong, China.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lurencun.cfuture09.androidkit.ui;
import java.lang.reflect.Field;
import android.content.Context;
import android.content.res.Resources;
import com.lurencun.cfuture09.androidkit.ui.annotation.AndroidRes;
import com.lurencun.cfuture09.androidkit.ui.annotation.AndroidRes.ResType;
/**
* @author Geek_Soledad ([email protected])
*/
public class ResBindUtil {
/**
* 绑定资源。
*
* @param context
*/
public static void bindAllRes(Context context) {
Resources reses = context.getResources();
Class<?> cl = context.getClass();
Field[] fields = cl.getDeclaredFields();
try {
for (Field field : fields) {
AndroidRes ar = field.getAnnotation(AndroidRes.class);
if (ar != null) {
field.setAccessible(true);
ResType type = ar.type();
int id = ar.id();
if (type == ResType.BOOLEAN) {
field.set(context, reses.getBoolean(id));
} else if (type == ResType.COLOR) {
field.set(context, reses.getColor(id));
} else if (type == ResType.DRAWABLE) {
field.set(context, reses.getDrawable(id));
} else if (type == ResType.INT) {
field.set(context, reses.getInteger(id));
} else if (type == ResType.INT_ARRAY) {
field.set(context, reses.getIntArray(id));
} else if (type == ResType.STRING) {
field.set(context, reses.getString(id));
} else if (type == ResType.STRING_ARRAY) {
field.set(context, reses.getStringArray(id));
} else if (type == ResType.TEXT) {
field.set(context, reses.getText(id));
} else if (type == ResType.TEXT_ARRAY) {
field.set(context, ResType.TEXT_ARRAY);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
你可以看下《JAVA 编程思想》(即Thinking in java)。里面有讲解到。
守护我们的萝莉
answered 10 years, 9 months ago