java:不兼容的类型:T 无法转换为 java.lang.String

java: incompatible types: T cannot be converted to java.lang.String

我目前正在使用 Google Tink 为我的应用程序提供加密和解密服务。
问题如下:我想在不使用(几乎)重复代码的情况下对其进行编程,因此我有了使用泛型的想法。
如果将字符串解析为 byte[] 是唯一的选择,我会这样做,但我宁愿不这样做。
这些是方法和变量:


我正在使用的 3 个堆栈:

private Stack<String> plaintextAccInformation = new Stack<>();
private Stack<byte[]> encryptedAccInformation = new Stack<>();
private Stack<String> decryptedAccInformation = new Stack<>();

用于从 Stack 获取信息的方法(我想用泛型解决但也不起作用)。不可以。解析不起作用,因为该方法必须可以使用两种不同的数据类型进行访问。
private <T> Account getInformation(Stack<T> stack) {
    boolean isApproved = stack.peek();
    stack.pop();
    boolean isAdmin = stack.peek();
    stack.pop();
    double balance = stack.peek();
    stack.pop();
    String password = stack.peek();
    stack.pop();
    String iBan = stack.peek();
    stack.pop();
    String uuid = stack.peek();
    stack.pop();

    return new Account(uuid, iBan, password, balance, isAdmin, isApproved);
}

用于加密Account对象所有数据的方法。
思想是遍历```Stack plaintextAccInformation```并加密Account对象中的每个变量,然后将每个加密变量保存到一个新的```堆栈加密的AccInformation```
public Account encrypt(Account account) throws GeneralSecurityException {
        this.plaintextAccInformation.empty();
        this.encryptedAccInformation.empty();

        agjEncryption = new AesGcmJce(key.getBytes());

        this.plaintextAccInformation.push(account.getUuid());
        this.plaintextAccInformation.push(account.getIban());
        this.plaintextAccInformation.push(account.getPassword());
        this.plaintextAccInformation.push(String.valueOf(account.getBalance()));
        this.plaintextAccInformation.push(String.valueOf(account.isAdmin()));
        this.plaintextAccInformation.push(String.valueOf(account.isApproved()));

        Iterator<String> iterator = plaintextAccInformation.iterator();
        while (iterator.hasNext()) {
            encryptedAccInformation.push(agjEncryption.encrypt(plaintextAccInformation.peek().getBytes(), aad.getBytes()));
            plaintextAccInformation.pop();
        }

        return getInformation(this.encryptedAccInformation);
    }

用于解密```Stack encryptedAccInformation```中保存的变量并保存到```Stack decryptedAccInformation```中的方法
    public Account decrypt() throws GeneralSecurityException {
        this.decryptedAccInformation.empty();

        this.agjDecryption = new AesGcmJce(key.getBytes());

        Iterator<byte[]> iterator2 = encryptedAccInformation.iterator();
        while (iterator2.hasNext()) {
            decryptedAccInformation.push(new String(agjDecryption.decrypt(encryptedAccInformation.peek(), aad.getBytes())));
            encryptedAccInformation.pop();
        }

        return getInformation(this.decryptedAccInformation);
    }

假设您确定堆栈将始终按照您在此处期望的顺序排列(您看起来是这样)。

您可以使用 Object 的堆栈并转换 peek() 函数的结果,而不是通用堆栈(我认为这将您限制为只有一个 T 值)。

private Account getInformation(Stack<Object> stack) {
        Boolean isApproved = (Boolean) stack.peek();
        stack.pop();
        Boolean isAdmin = (Boolean) stack.peek();
        stack.pop();
        Double balance = (Double) stack.peek();
        stack.pop();
        String password = (String) stack.peek();
        stack.pop();
        String iBan = (String) stack.peek();
        stack.pop();
        String uuid = (String) stack.peek();
        stack.pop();

        return new Account(uuid, iBan, password, balance, isAdmin, isApproved);
}