The type java.lang.reflect.AnnotatedElement cannot be resolved. It is indirectly referenced from req

JDK从1.7切换到1.8,Myeclipse10.0导入之前运行正常的工程提示如下错误:

Description Resource    Path    Location    Type
The project was not built since its build path is incomplete. Cannot find the class file for java.lang.reflect.AnnotatedElement. Fix the build path then try building this project  analysis_system     Unknown Java Problem
Description Resource    Path    Location    Type
The type java.lang.reflect.AnnotatedElement cannot be resolved. It is indirectly referenced from required .class files  SpringContextsUtil.java /analysis_system/src/main/java/cn/geekview/analysisSystem/utils line 1  Java Problem

解答:

When using JDK 8 and an IDE with its own compiler, like Eclipse, you have to update the IDE to a version with Java 8 support, even if you are not using the newer Java 8 features.

The reason is that the compiler must be able to load the newer class files of the JRE in order to compile your software which references these classes.

Sometimes you can get away with an older compiler when it ignores the newer version number of the class files. But some types will confuse older class file parsers as they use new features, notably AnnotatedElement, which now has default methods, and Map.Entry, an interface which now has static methods.

It seems that Eclipse does not make a difference between references for which no class file could be found and class files it failed to read when saying “«classname» cannot be resolved”.

实际上就是Myeclipse的版本太低不支持JDK1.8,所以更新开发工具的版本即可解决,我个人切换到IDEA上运行就解决这个问题了!

地址:https://stackoverflow.com/questions/26102115/error-when-using-logmanager-l4j2-with-java-8-java-lang-reflect-annotatedeleme

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误是因为在Java中,一个类不能被强制转换为ParameterizedType类型。通常是因为在使用反射时,尝试将一个Class对象转换为ParameterizedType对象,但是这个Class对象实际上不是一个ParameterizedType类型。要解决这个问题,需要检查代码中的反射使用,确保正确地使用了ParameterizedType类型。 ### 回答2: java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType。 在Java中,java.lang.Class类表示运行时类的信息和属性,而java.lang.reflect.ParameterizedType接口表示带有参数的类型(泛型类型)。 这个错误是因为在尝试将Class对象强制转换为ParameterizedType对象时,类型转换发生了错误。 要解决这个问题,我们需要理解java.lang.reflect.ParameterizedType的使用情况。 ParameterizedType接口是Type接口的子接口,可以用于获取带有参数的类型信息,例如泛型类或泛型接口的类型参数信息。 如果我们想要获得一个类的泛型参数类型信息,我们可以使用java.lang.Class类中的getGenericSuperclass()方法或getGenericInterfaces()方法。 但是要注意,这些方法返回的类型是java.lang.reflect.Type,而不是java.lang.reflect.ParameterizedType。 如果我们想要将Type对象转换为ParameterizedType对象,我们需要进行类型检查和类型转换。 示例代码: import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; public class MyClass<T> { public MyClass() { Type type = getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; // 在这里可以使用ParameterizedType对象来获取泛型参数类型信息 } } } 总结来说,java.lang.Class不能直接强制转换为java.lang.reflect.ParameterizedType,我们需要根据具体的使用情况进行类型检查和转换才能正确地获取泛型参数类型信息。 ### 回答3: java.lang.Class不能被转换为java.lang.reflect.ParameterizedType的原因是两者表示的是不同的类型信息。java.lang.Class用于表示类的信息,而java.lang.reflect.ParameterizedType用于表示泛型类型的信息。 当使用强制类型转换将一个对象转换为java.lang.reflect.ParameterizedType时,如果该对象实际上是java.lang.Class类型的对象,就会抛出ClassCastException。这是因为java.lang.Class和java.lang.reflect.ParameterizedType是不兼容的类型。 如果需要获取一个类中的泛型类型信息,应该使用java.lang.Class对象的getGenericSuperclass()方法来获取其父类的泛型类型信息,再通过强制类型转换获取到java.lang.reflect.ParameterizedType对象。 例如: ``` class MyGenericClass<T> { } class MyClass extends MyGenericClass<String> { } public static void main(String[] args) { MyClass myClass = new MyClass(); ParameterizedType parameterizedType = (ParameterizedType) myClass.getClass().getGenericSuperclass(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); Type actualTypeArgument = actualTypeArguments[0]; String genericType = actualTypeArgument.getTypeName(); System.out.println(genericType); } ``` 如果在强制类型转换时抛出了java.lang.ClassCastException,可以检查代码中的类型转换部分,并确保正确使用泛型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值