1:自定义注解进行字段隐藏(简单版)

java 中对自定义注解的说明请参见:

http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html

http://www.cnblogs.com/peida/archive/2013/04/26/3038503.html

更强大更复杂版本请看:
http://blog.csdn.net/liuc0317/article/details/48787793

下面是我自己写的,还有部分需要完善,比如传入类型等等。
//==============注解================
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SensitiveInfo {

public SensitiveUtils.sensitiveType type();

}
//=============实体类====================
简单,就使用了2个字段
public class User {

@SensitiveInfo(type = sensitiveType.str)
private String name;

private String password;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
@Override
public String toString() {
    return "User [name=" + name + ", password=" + password + "]";
}

}

//==============主要方法====================
比较懒,直接使用main方法测试
public class SensitiveUtils {

public enum sensitiveType {
    str;
}

public static String str(String ss) {
    if (null == ss) {
        return "";
    }
    String s1 = ss.replaceAll("(.)", "*");
    return s1;
}

public static void main(String[] args) {
    User user = new User();
    user.setName("xiaoming");
    user.setPassword("123456");
    System.out.println(user.getName());
    System.out.println("===================");
    System.out.println(SensitiveUtils.str(user.getName()));
    System.out.println("===================");
    System.out.println(user);
}

}
//================结果测试==============

xiaoming

**

User [name=xiaoming, password=123456]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,我们可以通过自定义注解来修改字段的值。首先,我们需要定义一个注解。可以使用@interface关键字来定义注解。例如,假设我们要定义一个名为@CustomAnnotation的注解,该注解用于修改字段的值。 ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface CustomAnnotation { String value() default ""; } ``` 在注解内部,使用了@Retention和@Target元注解来指定注解的保留策略和注解的作用目标。在此例中,我们设置了@Retention(RetentionPolicy.RUNTIME),表示该注解在运行时可见。@Target(ElementType.FIELD)表示该注解可以用于字段上。 接下来,我们可以将@CustomAnnotation应用到字段上,并使用反射机制获取字段并修改其值。 ```java import java.lang.reflect.Field; public class DemoClass { @CustomAnnotation("Hello World") private String value; public void setValueUsingAnnotation() throws IllegalAccessException { Field[] fields = this.getClass().getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(CustomAnnotation.class)) { CustomAnnotation annotation = field.getAnnotation(CustomAnnotation.class); field.setAccessible(true); field.set(this, annotation.value()); } } } public static void main(String[] args) throws IllegalAccessException { DemoClass demo = new DemoClass(); System.out.println("Before modification: " + demo.value); demo.setValueUsingAnnotation(); System.out.println("After modification: " + demo.value); } } ``` 在上述示例中,我们定义了一个名为DemoClass的类,并在其中声明了一个私有字段value。我们将@CustomAnnotation应用到该字段上,并在注解中设置了一个字符串值。 在setValueUsingAnnotation方法中,使用反射机制获取类的所有字段。然后,检查每个字段是否应用了@CustomAnnotation注解。如果有,则使用Field类的setAccessible方法来设置字段可访问,并使用Field类的set方法将注解中的值赋给字段。 在main方法中,我们创建了DemoClass的实例,并输出修改前后的字段值。 通过运行上述代码,我们可以看到输出结果中,字段的值在应用自定义注解后发生了变化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值