There is no getter for property named 'delegate' in 'class com.sun.proxy.$Proxy398'

Viewed 10

when I add Interceptor to my project.I found an error which says There is no getter for property named 'delegate' in 'class com.sun.proxy.$Proxy398'

the detail:

        @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class,
                Integer.class})
})
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
 
        MetaObject metaObject = SystemMetaObject.forObject(statementHandler);

        //obtain mappedStatement
        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");

the error occur in the lastest code

MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");

1 Answers

why this error occur. I try to find the difference between add it and before. I found there has a same type Intercepts Signature in the project.

as we know, the mybatis plugin will wrap itself, so the way to solve it is to obtain the final target.

 StatementHandler statementHandler;
    Object expectedStatementHandler = invocation.getTarget();
    while (Proxy.isProxyClass(expectedStatementHandler.getClass())) {
        MetaObject metaObject = SystemMetaObject.forObject(expectedStatementHandler);
        //fastReturn
        if (BooleanUtils.isNotTrue(metaObject.hasGetter("h.target"))) {
            log.error("cant find mappedStatement h.get method");
            break;
        }
        expectedStatementHandler = metaObject.getValue("h.target");
    }
    //failFast
    if (!(expectedStatementHandler instanceof StatementHandler)) {
        log.error("sorry,expectedStatementHandler not instanceof StatementHandler!");
        return;
    }

    statementHandler = (StatementHandler) expectedStatementHandler;

    MetaObject metaObject = SystemMetaObject.forObject(statementHandler);

    MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
Related