该注解用来修饰其他注解,并标明被修饰注解的作用域。其 value 的属性值包含3种:
SOURCE:注解仅在源代码中可用。编译器和 JVM 会忽略此注解,因此在运行时不可用;CLASS:编译器会处理该注解,但 JVM 不会处理,因此在运行时不可用;RUNTIME:JVM 会处理该注解,可以在运行时使用。该注解标记可以应用的目标元素:
ANNOTATION_TYPE:可修饰其他注解;CONSTRUCTOR:可以修饰构造函数;FIELD:可以修饰字段或属性;LOCAL_VARIABLE:可以修饰局部变量;METHOD:可以修饰 method;PACKAGE:可以修饰 package 声明;PARAMETER:可以修饰方法参数;TYPE:可以修饰 Class、Interface、Annotation 或 enum 声明;TYPE_PARAMETER:可以修饰参数声明;TYPE_USE:可以修饰任何类型。该注解可以修饰其他注解,表示将使用 Javadoc 记录被注解的元素。
默认情况下,注解不会被子类继承。但是,如果把注解标记为 @Inherited,那么使用注解修饰 class 时,子类也会继承该注解。该注解仅适用于 class。注意:使用该注解修饰接口时,实现类不会继承该注解。
标明不应该使用带此注解的元素。使用这个注解,编译器会对应生成告警。该注解可以应用于 method、class 和字段。
告诉编译器由于特定原因不产生告警。
该注解通知编译器,该元素正在覆盖(Override)父类中的元素。覆盖元素时,不强制要求加上该注解。但是当覆盖没有正确完成时,例如子类方法的参数与父类参数不同或者返回类型不匹配时,可以帮助编译器生成错误。
该注解断言(Assert)方法或构造函数代码不会对其参数执行不安全(Unsafe)操作。
该注解表示可以对同一个元素多次使用相同的注解。
根据前端传入币种修改对应币金额
自定义注解 import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import com.well.utils.CurrencyType; /**币种注解 * @author yauquenlay * {@see com.well.model.Obtain} * */ @Documented @Target(ElementType.FIELD) @Inherited @Retention(RetentionPolicy.RUNTIME) public @interface Currency { CurrencyType type() default CurrencyType.BTC; } 限定注解参数 /**币种 * @author yauquenlay * */ public enum CurrencyType { BTC, ETH, USD, EOS } 注解标记对应字段 import java.util.Date; import com.well.annotation.Currency; import com.well.utils.CurrencyType; import lombok.Data; /**用户钱包 * @author yauquenlay * */ @Data public class Wallet extends Obtain { private Integer walletId; private Long userId; @Currency(type=CurrencyType.EOS) private String eosAmount; @Currency(type=CurrencyType.USD) private String usdAmount; @Currency(type=CurrencyType.ETH) private String ethAmount; @Currency(type=CurrencyType.BTC) private String btcAmount; private Date updateTime; } 获取注解操作 import java.lang.reflect.Field; import com.well.annotation.Currency; import com.well.exception.CurrencyException; import com.well.utils.CurrencyType; /**对实体类币种做标记,限定前端输入币种 * @author yauquenlay * */ public class Obtain { //获取金额 public String obtainValue(String currency) throws InstantiationException, IllegalAccessException, CurrencyException { Class<? extends Obtain> clazz = this.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if(field.isAnnotationPresent(Currency.class)) { CurrencyType type = annotation.type(); if(currency.equalsIgnoreCase(type.name())) { field.setAccessible(true); String obtainMoney = (String) field.get(this); return obtainMoney; } } } throw new CurrencyException("error currency:"+currency); } //设置金额 public void setValue(String currency,String value) throws InstantiationException, IllegalAccessException, CurrencyException { Class<? extends Obtain> clazz = this.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if(field.isAnnotationPresent(Currency.class)) { CurrencyType type = annotation.type(); if(currency.equalsIgnoreCase(type.name())) { field.setAccessible(true); field.set(this, value); return; } } } throw new CurrencyException("error currency:"+currency); } }