java 泛型堆内存泄漏_java - 使用JAXB解组Java泛型对象 - 堆栈内存溢出

我有这样的事情:

@XmlRootElement(name = "attribute", namespace = "ns.attrib")

public class Attribute implements AuthEntity {

private String name;

private T value;

@XmlAttribute

@Override

public String getName() {

return name;

}

@Override

public Optional> getAuthEntities() {

return Optional.empty();

}

@XmlAttribute

public T getValue() {

return value;

}

public void setValue(T value) {

this.value = value;

}

public void setName(String name) {

this.name = name;

}

@Override

public int hashCode() {

return Objects.hash(getName(), getValue());

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

@SuppressWarnings("rawtypes")

GSAttribute other = (GSAttribute) obj;

if (!name.equals(other.name))

return false;

if (value == null) {

if (other.value != null)

return false;

} else if (!value.equals(other.value))

return false;

return true;

}

}

我必须使用一些自定义接口在基于xml的持久层上进行一些CRUD,所以我必须使用JAXB(版本2.2.11)来编组/解编上述类型。 好的,让我们开始进行一些测试来对此充满信心。 我写了几篇:

@Test

public void deserializeEmptyAttribute() throws JAXBException {

String xml = "";

Attribute expected = new Attribute<>();

Attribute actual = JAXB.unmarshal(new StringReader(xml), Attribute.class);

Assert.assertEquals(expected, actual);

}

@Test

public void deserializeIntegerAttribute() throws Exception {

String xml = "";

Attribute expected = new Attribute<>();

expected.setName("somename");

expected.setValue(105);

Attribute actual = JAXB.unmarshal(new StringReader(xml), Attribute.class);

Assert.assertEquals(expected, actual);

}

@Test

public void deserializeStringAttribute() {

String xml = "";

Attribute expected = new Attribute<>();

expected.setName("somename");

expected.setValue("somevalue");

Attribute actual = JAXB.unmarshal(new StringReader(xml), Attribute.class);

Assert.assertEquals(expected, actual);

}

每个测试在此堆栈上均失败:

java.lang.NullPointerException

at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:167)

at com.sun.xml.bind.v2.runtime.property.AttributeProperty.(AttributeProperty.java:91)

at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:108)

at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.(ClassBeanInfoImpl.java:181)

at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:503)

at com.sun.xml.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:320)

at com.sun.xml.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:139)

at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1138)

at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:162)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)

at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)

at javax.xml.bind.ContextFinder.find(ContextFinder.java:441)

at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)

at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)

at javax.xml.bind.JAXB$Cache.(JAXB.java:112)

at javax.xml.bind.JAXB.getContext(JAXB.java:139)

at javax.xml.bind.JAXB.unmarshal(JAXB.java:242)

...

我对JAXB还是很陌生(大多数年前都被遗忘了),但在运行正常的测试时遇到了一些麻烦。 我想念什么? 我看到有很多类似的帖子/答案,但是它们没有帮助(或者我没有得到)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值