是否可以将List转换为JCR值?

时间:2015-09-16 21:55:15

标签: java reflection nodes jcr

所以我使用反射来创建List的新实例。然后我想将该列表设置为Node。问题是节点只采用原语,字符串,值和值[]。当我尝试这样做时,我得到一个ClassCastException:

 Value[] valueArray = (Value[])Array.newInstance(elementType,size);

我想这样做:

node.setProperty(name,valueArray);

有没有人遇到过这样做的方法?或者会让我朝着正确方向前进的地方?这甚至可能吗? 谢谢你的期待。

1 个答案:

答案 0 :(得分:0)

那你为什么不拥有自己的Value实现,例如:

class MyValue implements Value {
    private Object value;
    private int type;

    public MyValue(Object value) throws RepositoryException {
        if (value == null)
            throw new RepositoryException("Value can not be null");
        this.value = value;
        if (value instanceof Boolean) {
            type = PropertyType.BOOLEAN;
        } else if (value instanceof Calendar) {
            type = PropertyType.DATE;
        } else if (value instanceof Double) {
            type = PropertyType.DOUBLE;
        } else if (value instanceof Long) {
            type = PropertyType.LONG;
        } else if (value instanceof String) {
            type = PropertyType.STRING;
        } else {
            throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
        }
    }

    @Override
    public Binary getBinary() throws RepositoryException {
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }

    @Override
    public boolean getBoolean() throws ValueFormatException,
            RepositoryException {
        if (type == PropertyType.BOOLEAN)
            return (Boolean)value;
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }

    @Override
    public Calendar getDate() throws ValueFormatException, RepositoryException {
        if (type == PropertyType.DATE)
            return (Calendar)value;
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }

    @Override
    public BigDecimal getDecimal() throws ValueFormatException,
            RepositoryException {
        if (type == PropertyType.DECIMAL)
            return (BigDecimal)value;
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }

    @Override
    public double getDouble() throws ValueFormatException, RepositoryException {
        if (type == PropertyType.DOUBLE)
            return (Double)value;
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }

    @Override
    public long getLong() throws ValueFormatException, RepositoryException {
        if (type == PropertyType.LONG)
            return (Long)value;
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }

    @Override
    public InputStream getStream() throws RepositoryException {
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }

    @Override
    public String getString() throws ValueFormatException,
            IllegalStateException, RepositoryException {
        if (type == PropertyType.STRING)
            return (String)value;
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }

    @Override
    public int getType() {
        return type;
    }

    @Override
    public String toString() {
        return value.getClass().getSimpleName() + "(" + value + ")";
    }
}

然后你的数组代码就像:

    Value[] valueArray = new Value[size];
    valueArray[0] = new MyValue(123L);
    valueArray[1] = new MyValue(123.45);
    valueArray[2] = new MyValue("12345");