java注释的自定义

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java注释的自定义相关的知识,希望对你有一定的参考价值。

参考技术A

它类似于新创建一个接口类文件,但为了区分,我们需要将它声明为@interface,如下例:
Java代码
packagecom.iwtxokhtd.annotation;
public@interfaceNewAnnotation

使用自定义的注解类型
Java代码
packagecom.iwtxokhtd.annotation;
publicclassAnnotationTest
@NewAnnotation
publicstaticvoidmain(String[] args)


为自定义注解添加变量
Java代码
packagecom.iwtxokhtd.annotation;
public@interfaceAnnotation
String value();

Java代码
publicclassAnnotationTest
@NewAnnotation(main method)
publicstaticvoidmain(String[] args)
saying();

@NewAnnotation(value = say method)
publicstaticvoidsaying()


定义一个枚举类型,然后将参数设置为该枚举类型,并赋予默认值
public@interfaceGreeting
publicenum FontColor
BLUE,RED,GREEN
;
String name();
FontColor fontColor()defaultFontColor.RED;

这里有两种选择,其实变数也就是在赋予默认值的参数上,我们可以选择使用该默认值,也可以重新设置一个值来替换默认值
Java代码
publicclassAnnotationTest
@NewAnnotation(main method)
publicstaticvoidmain(String[] args)
saying();
sayHelloWithDefaultFontColor();
sayHelloWithRedFontColor();

@NewAnnotation(say method)
publicstaticvoidsaying()

// 此时的fontColor为默认的RED
@Greeting(name = defaultfontcolor)
publicstaticvoidsayHelloWithDefaultFontColor()

@Greeting(name = notdefault, fontColor = Greeting.FontColor.BLUE)
publicstaticvoidsayHelloWithRedFontColor()
1.1. 限制注解的使用范围
用@Target指定ElementType属性
Java代码(jdk)
packagejava.lang.annotation;
public enum ElementType
TYPE,
// 用于类,接口,枚举但不能是注解
FIELD,
// 字段上,包括枚举值
METHOD,
// 方法,不包括构造方法
PARAMETER,
// 方法的参数
CONSTRUCTOR,
//构造方法
LOCAL_VARIABLE,
// 本地变量或catch语句
ANNOTATION_TYPE,
// 注解类型(无数据)
PACKAGE
// Java包

1.2. 注解保持性策略
Java代码
//限制注解使用范围
@Target(ElementType.METHOD,ElementType.CONSTRUCTOR)
public @interface Greeting
//使用枚举类型
public enum FontColor
BLUE,RED,GREEN
;
String name();
FontColor fontColor() defaultFontColor.RED;

在Java编译器编译时,它会识别在源代码里添加的注解是否还会保留,这就是RetentionPolicy。下面是Java定义的RetentionPolicy枚举:
编译器的处理有三种策略:
将注解保留在编译后的类文件中,并在第一次加载类时读取它
将注解保留在编译后的类文件中,但是在运行时忽略它
按照规定使用注解,但是并不将它保留到编译后的类文件中
Java代码
packagejava.lang.annotation;
public enum RetentionPolicy
SOURCE,
// 此类型会被编译器丢弃
CLASS,
// 此类型注解会保留在class文件中,但JVM会忽略它
RUNTIME
// 此类型注解会保留在class文件中,JVM会读取它

Java代码
//让保持性策略为运行时态,即将注解编码到class文件中,让虚拟机读取
@Retention(RetentionPolicy.RUNTIME)
public @interface Greeting
//使用枚举类型
public enum FontColor
BLUE,RED,GREEN
;
String name();
FontColor fontColor() defaultFontColor.RED;

1.3. 文档化功能
Java提供的Documented元注解跟Javadoc的作用是差不多的,其实它存在的好处是开发人员可以定制Javadoc不支持的文档属性,并在开发中应用。它的使用跟前两个也是一样的,简单代码示例如下:
Java代码
//让它定制文档化功能
//使用此注解时必须设置RetentionPolicy为RUNTIME
@Documented
public @interface Greeting
//使用枚举类型
public enum FontColor
BLUE,RED,GREEN
;
String name();
FontColor fontColor() defaultFontColor.RED;

1.4. 标注继承
Java代码
//让它允许继承,可作用到子类
@Inherited
public @interface Greeting
//使用枚举类型
public enum FontColor
BLUE,RED,GREEN
;
String name();
FontColor fontColor() defaultFontColor.RED;

2. 读取注解信息
属于重点,在系统中用到注解权限时非常有用,可以精确控制权限的粒度
注意:要想使用反射去读取注解,必须将Retention的值选为Runtime
Java代码
packagecom.iwtxokhtd.annotation;
importjava.lang.annotation.Annotation;
importjava.lang.reflect.Method;
//读取注解信息
publicclassReadAnnotationInfoTest
publicstaticvoidmain(String[] args)throws Exception
// 测试AnnotationTest类,得到此类的类对象
Class c = Class.forName(com.iwtxokhtd.annotation.AnnotationTest);
// 获取该类所有声明的方法
Method[] methods =c.getDeclaredMethods();
// 声明注解集合
Annotation[] annotations;
// 遍历所有的方法得到各方法上面的注解信息
for(Method method : methods)
// 获取每个方法上面所声明的所有注解信息
annotations =method.getDeclaredAnnotations();
// 再遍历所有的注解,打印其基本信息
System.out.println(method.getName());
for(Annotation an :annotations)
System.out.println(方法名为: + method.getName()+ 其上面的注解为:
+an.annotationType().getSimpleName());
Method[] meths =an.annotationType().getDeclaredMethods();
// 遍历每个注解的所有变量
for(Method meth :meths)
System.out.println(注解的变量名为: + meth.getName 注释有三种:// /* */ /** */ 前两种编译器直接跳过,从来不阅读,第三种编译器是可以看懂的,当你使用javadoc这样的命令时会用到,用来生成API时用的。
注解:这东东完全就是给编译器看的。 比如@Ovrride表示这个方法是重写了父类中的方法,而不是自定义的,所以这个时候编译器会去检查你的方法名是否和父类一样,是否写错了。
以上是不同的概念

如何将默认蓝色注释用于用户位置而不是我的自定义注释?

【中文标题】如何将默认蓝色注释用于用户位置而不是我的自定义注释?【英文标题】:How do I use the default blue annotation for user location rather than my custom annotation? 【发布时间】:2020-10-25 09:04:56 【问题描述】:

我的地图上有自定义注释,现在我希望能够显示用户位置以及自定义注释。但是,当我显示用户位置时,它使用自定义注释而不是默认的蓝色注释。有没有办法让用户位置使用默认值?

我使用以下内容显示用户位置:

locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
map.showsUserLocation = true

自定义注解使用以下设置:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 
    let reuseIdentifier = "pin"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)

    if annotationView == nil 
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView?.canShowCallout = false
     else 
        annotationView?.annotation = annotation
    

    annotationView?.image = UIImage(named: "Marker")

    return annotationView

谢谢

【问题讨论】:

如果您不想使用自定义注释,为什么不删除? 我想对除显示用户位置的注释之外的所有注释使用我的自定义注释 - 当我调用 showUserLocation 它自然会使用此自定义注释 那么您可以使用不同的自定义注释吗? 我发现我需要检查进入 viewFor 的注释以检查它是否属于 MKUserLocation 类型 - 如果是,那么我只需返回 nil。 它是通过以下方式实现的:***.com/questions/61785563/… 【参考方案1】:

如下所示:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 

    yourCustomAnnotation = CustomPointAnnotation()
    yourCustomAnnotation.pinCustomImageName = "Marker"
    yourCustomAnnotation.coordinate = location

    yourCustomAnnotationView = MKPinAnnotationView(annotation: yourCustomAnnotation, reuseIdentifier: "pin")
    map.addAnnotation(yourCustomAnnotationView.annotation!)

【讨论】:

【参考方案2】:

我处理同样的问题。在“viewFor annotation”委托方法的顶部使用此检查:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 
        guard annotation as? MKUserLocation != mapView.userLocation else  return 

这将阻止自定义您的用户位置

【讨论】:

以上是关于java注释的自定义的主要内容,如果未能解决你的问题,请参考以下文章

如何将默认蓝色注释用于用户位置而不是我的自定义注释?

请求参数的自定义 Spring 注释 - 从未调用自定义解析器

@Security 注释的自定义消息

用户位置的自定义注释视图不移动地图视图

具有客户端验证的自定义数据注释验证属性

使用数据注释和代码的自定义验证属性