java.lang.IllegalAccessException 没有访问权限

java.lang.IllegalAccessException 没有访问权限

今天利用java高级特性反射,操作泛型化对象的私有方法时报错,记录分析过程。

java.lang.IllegalAccessException: Class com.base.Basedao can not access a member of class com.entity.Person with modifiers "private" at  sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)

IllegalAccessException 表示没有访问权限的异常。
com.base.Basedao 类无法访问 com.entity.Person 中的私有方法。

Class com.base.Basedao can not access a member of class com.entity.Person with modifiers “private”

这次权限访问异常,是因为自己在用反射获取实体类对象的时候没有去除权限,也就是method.setAccessible(true);

com.entity.Person

package com.entity.Person;

public class AifacePerson {
	private void setDefaultValue(){
		log.info("do setDefaultValue...");
	}
}

com.entity.Basedao

package com.entity.Basedao;

public abstract class AbstractService{
	public <T extends BaseEntityModel> Serializable save(T entity) {
		Class clz = entity.getClass();
	    String className = clz.getName();
	    try {
	        Method method = clz.getDeclaredMethod("setDefaultValue");
	        Object o = clz.newInstance();
	//      method.setAccessible(true);
	        method.invoke(o);
	    } catch (NoSuchMethodException e) {
	        log.error("Error:",e);
	    } catch (IllegalAccessException e) {
	        log.error("Error:",e);
	    } catch (InstantiationException e) {
	        log.error("Error:",e);
	    } catch (InvocationTargetException e) {
	        log.error("Error:",e);
	    }
	}
}

把上面Basedao代码中的注释打开,问题解决。


天下英雄出我辈,一入江湖岁月催
我是爱生活的「无间行者」,努力把实践过的解决方案分享给大家
如果这篇文章对你有用,一个赞、一个评论、一个关注,我都很开心,给点鼓励吧,让我知道你在看。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要针对判空写一个自定义注解,你可以使用 Java 的注解和反射机制来实现。下面是一个示例代码,演示如何创建一个自定义的 `@NotNull` 注解来进行非空校验: 首先,创建一个 `NotNull` 注解: ```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 NotNull { } ``` 在上述代码中,我们使用 `@Retention(RetentionPolicy.RUNTIME)` 注解指定了注解的保留策略为运行时。而 `@Target(ElementType.FIELD)` 注解表示该注解可以用于字段上。 接下来,创建一个测试类来使用 `@NotNull` 注解: ```java public class Person { @NotNull private String name; public Person(String name) { this.name = name; } public static void main(String[] args) { Person person1 = new Person("John"); validateNotNullFields(person1); Person person2 = new Person(null); validateNotNullFields(person2); } private static void validateNotNullFields(Object object) { for (Field field : object.getClass().getDeclaredFields()) { if (field.isAnnotationPresent(NotNull.class)) { field.setAccessible(true); try { Object value = field.get(object); if (value == null) { throw new IllegalArgumentException("Field '" + field.getName() + "' cannot be null"); } } catch (IllegalAccessException e) { e.printStackTrace(); } } } } } ``` 在上述代码中,我们创建了一个 `Person` 类,并在 `name` 字段上使用了 `@NotNull` 注解。然后,我们在 `main()` 方法中创建了两个 `Person` 对象,一个给了有效的值,另一个给了 `null`。 接着,我们定义了一个 `validateNotNullFields()` 方法,它使用反射来检查对象中带有 `@NotNull` 注解的字段是否为 `null`。如果字段为 `null`,则抛出一个异常。 在上述示例中,`person1` 没有抛出异常,因为 `name` 字段有一个有效的值。而 `person2` 会抛出异常,因为 `name` 字段的值为 `null`。 这只是一个简单的示例,你可以根据自己的需求进行扩展和定制。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

無间行者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值