Android中的注解
16lz
2021-12-04
package com.cooliris.picasa;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.xml.sax.Attributes;public abstract class Entry { public static final String[] ID_PROJECTION = { "_id" }; // The primary key of the entry. by_zh @Column("_id") //使用 public long id = 0; @Retention(RetentionPolicy.RUNTIME) //使用失效。 注解存在于类文件中,而且在运行时虚拟机可以获取注解信息。 by_zh @Target(ElementType.TYPE) //此注解只能用来对类、接口以及枚举类型进行注解 by_zh public @interface Table { String value(); } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) //此注解只能用来对成员变量进行注解 public @interface Column { String value(); //注解属性,value是获取此属性值的方法的名称。 boolean indexed() default false; boolean fullText() default false; } public void clear() { id = 0; } public void setPropertyFromXml(String uri, String localName, Attributes attrs, String content) { throw new UnsupportedOperationException("Entry class does not support XML parsing"); }}
private static final class PhotoProjection extends Entry { public static final EntrySchema SCHEMA = new EntrySchema(PhotoProjection.class); @Column("edit_uri") public String editUri; @Column("title") public String title; @Column("summary") public String summary; @Column("date_taken") public long dateTaken; @Column("latitude") public double latitude; @Column("longitude") public double longitude; @Column("thumbnail_url") public String thumbnailUrl; @Column("screennail_url") public String screennailUrl; @Column("content_url") public String contentUrl; @Column("content_type") public String contentType; @Column("html_page_url") public String htmlPageUrl; }
下面是在另一个类中使用注解:
private ColumnInfo[] parseColumnInfo(Class<? extends Object> clazz) { // Gather metadata from each annotated field. ArrayList<ColumnInfo> columns = new ArrayList<ColumnInfo>(); Field[] fields = clazz.getFields(); for (int i = 0; i != fields.length; ++i) { // Get column metadata from the annotation. Field field = fields[i]; Entry.Column info = ((AnnotatedElement) field).getAnnotation(Entry.Column.class); if (info == null) { continue; } // Determine the field type. int type; Class<?> fieldType = field.getType(); if (fieldType == String.class) { type = TYPE_STRING; } else if (fieldType == boolean.class) { type = TYPE_BOOLEAN; } else if (fieldType == short.class) { type = TYPE_SHORT; } else if (fieldType == int.class) { type = TYPE_INT; } else if (fieldType == long.class) { type = TYPE_LONG; } else if (fieldType == float.class) { type = TYPE_FLOAT; } else if (fieldType == double.class) { type = TYPE_DOUBLE; } else if (fieldType == byte[].class) { type = TYPE_BLOB; } else { throw new IllegalArgumentException("Unsupported field type for column: " + fieldType.getName()); } // Add the column to the array. int index = columns.size(); columns.add(new ColumnInfo(info.value(), type, info.indexed(), info.fullText(), field, index)); //使用注解 by_zh }
@Entry.Table("photos")public final class PhotoEntry extends Entry { public static final EntrySchema SCHEMA = new EntrySchema(PhotoEntry.class); /** * The user account that is the sync source for this entry. Must be set * before insert/update. */ @Column("sync_account") public String syncAccount; /** * The "edit" URI of the photo. */ @Column("edit_uri") public String editUri;......}
更多相关文章
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- android ImageView的scaleType属性
- android SwitchPreference ListPreference使用
- Android(安卓)8.0新特性调研
- xpose框架使用android studio
- 关于Android(安卓)相对布局中的属性 的介绍和运用