API设计和impl:从对象数组接收特定类型和返回实例

时间:2016-08-26 07:04:47

标签: java

org.aspectj.lang.JoinPoint我们可以执行此操作Object[] args = joinPoint.getArgs();

现在,我想实现一个可以实现此目的的API:

// This method receive a variable which can help me know which type of instance he want
public String getInstanceStr(JoinPoint joinPoint, ??customerType??/*?how to design here?*/){
    String requestStr = null;
    Object[] os = joinPoint.getArgs();
    for (int i = 0; i < os.length; i++) {
        /*?How to determine the type here?*/
        if (os[i] instanceof ??cusomerType??) {
            requestStr = os[i].toString();
        }
    }
    return requestStr;
}

然后,来电者可以这样做:

ClientMethod(){
    String str = API.getInstanceStr(joinPoint, ??customerType??);
}

在这种情况下,客户端可以确定从args获取的内容。

我已经尝试了Class<T>,你看,我失败了。

请告诉我是否或如何做到这一点?

1 个答案:

答案 0 :(得分:0)

这应该有效:

    // This method receive a variable which can help me know which type of instance he want
    public String getInstanceStr(JoinPoint joinPoint, Class<? extends Object> clazz){
        String requestStr = null;
        Object[] os = joinPoint.getArgs();
        for (int i = 0; i < os.length; i++) {
            /*?How to determine the type here?*/
             if(clazz.isInstance(os[i])){
                requestStr = os[i].toString();
            }

        }
        return requestStr;
    }