在Java中使用反射获取无效的类名称

时间:2018-12-26 07:26:20

标签: java reflection

我在代码中使用java反射,并在类中进行迭代。 在迭代中,我得到一个特定对象的无效的类名。 PFB反射代码和我得到的日志。

记录器:

2018-12-26 12:38:04,878 INFO  [http-nio-8180-exec-4] [] [] [diy-CJ] []- given class is : class com.paytm.oe.entity.RelatedBusinessSolutionMapping_$$_jvst3bb_5e
2018-12-26 12:38:04,879 INFO  [http-nio-8180-exec-4] [] [] [diy-CJ] []- fields are : private javassist.util.proxy.MethodHandler com.paytm.oe.entity.RelatedBusinessSolutionMapping_$$_jvst3bb_5e.handler
2018-12-26 12:38:04,879 INFO  [http-nio-8180-exec-4] [] [] [diy-CJ] []- fields are : public static byte[] com.paytm.oe.entity.RelatedBusinessSolutionMapping_$$_jvst3bb_5e._filter_signature
2018-12-26 12:38:04,879 INFO  [http-nio-8180-exec-4] [] [] [diy-CJ] []- fields are : public static final long com.paytm.oe.entity.RelatedBusinessSolutionMapping_$$_jvst3bb_5e.serialVersionUID
2018-12-26 12:38:04,879 INFO  [http-nio-8180-exec-4] [] [] [diy-CJ] []- fields are : private static java.lang.reflect.Method[] com.paytm.oe.entity.RelatedBusinessSolutionMapping_$$_jvst3bb_5e._methods_
2018-12-26 12:38:04,879 INFO  [http-nio-8180-exec-4] [] [] [diy-CJ] []- field is : handler path is : relatedBusinessSolutionMapping
2018-12-26 12:38:04,879 INFO  [http-nio-8180-exec-4] [] [] [diy-CJ] []- field is : _filter_signature path is : relatedBusinessSolutionMapping
2018-12-26 12:38:04,880 INFO  [http-nio-8180-exec-4] [] [] [diy-CJ] []- field is : serialVersionUID path is : relatedBusinessSolutionMapping
2018-12-26 12:38:04,880 INFO  [http-nio-8180-exec-4] [] [] [diy-CJ] []- field is : _methods_ path is : relatedBusinessSolutionMapping
2018-12-26 12:38:04,880 INFO  [http-nio-8180-exec-4] [] [] [diy-CJ] []- field is : solutionType path is : null
2018-12-26 12:38:04,880 INFO  [http-nio-8180-exec-4] [] [] [diy-CJ] []- Path in non-primitive type is : null for field : solutionType

您可以清楚地看到,记录器的第一行将类名称打印为:com.paytm.oe.entity.RelatedBusinessSolutionMapping_$$_jvst3bb_5e 而是:

com.paytm.oe.entity.RelatedBusinessSolutionMapping

反射代码:

public static void setNullFieldInAnObject(Object object, Set<String> value, String path) throws Exception {
    LOGGER.info("In merge diff according to the given set function");
    LOGGER.info("Set is : " + value);
    LOGGER.info("Object is : " + object.toString());
    Class classA = object.getClass();
    LOGGER.info("given class is : " + classA.toString());
    Field fields[] = classA.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        LOGGER.info("fields are : " + fields[i].toString());
    }
}

仅在这种情况下,它给出了错误的类名称,这妨碍了我在类内部进行迭代。对于其他所有情况,它都可以正常工作。 请帮忙。

3 个答案:

答案 0 :(得分:1)

正如注释中指出的那样,您的代码失败的类不是因为类名错误,而是因为它是javaassist代理类。据我所知,javaassist仅通过创建子类来代理,因此,如果您知道该类是代理,则只需要调用Class.getSuperclass()。要检索原始类,javaassist代理都实现了javassist Proxy接口,因此类似这样的事情可能满足您的要求:

Class<?> classA = object.getClass();
if(javassist.util.proxy.Proxy.isAssignableFrom(classA)) {
    classA = classA.getSuperclass();
}

答案 1 :(得分:0)

您的对象应该是由spring生成的动态代理目标(我想,因为没有相关代码)。

然后您可以尝试

data: ''

AopUtils在org.springframework.aop.support下

答案 2 :(得分:0)

if (object instanceof HibernateProxy) {
            object = ((HibernateProxy) object).getHibernateLazyInitializer()
                    .getImplementation();
        }

这段代码对我有用!