gpt4 book ai didi

java - XML 反序列化器中的奇怪行为

转载 作者:行者123 更新时间:2023-12-01 15:56:58 25 4
gpt4 key购买 nike

在使用 SAX 时,我在 Java 中遇到了一些奇怪的行为。正在设置值,但随后在输出时在 toString 方法中恢复为其默认值。这看起来确实很不寻常。我查看了代码并输出了变量的内存地址,一切都以正确的顺序发生,并且似乎完成得正确。其他反序列化的 XML 元素都很好,所以我无法解决。

我的系统是这样工作的:有一些带有抽象方法的类,用户在其中定义属性帮助器和元素处理程序。元素处理程序处理基于子 XML 元素的子类的生成,而属性处理程序从构造函数中获取属性列表,并通过属性帮助程序使用该列表,通过属性帮助程序中实现的抽象方法来设置字段。

这是有问题的类:

package kokuks.flowmon;

import kokuks.flowmon.FlowmonParser.Handler;

import org.xml.sax.Attributes;

public class FlowProbeFlowStats extends FlowmonParserElement {
protected final FlowProbe parent;
protected int flowId = 0;
protected int packets = 0;
protected int bytes = 0;
protected long delayFromFirstProbeSum = 0;
protected boolean set = false;

/**
* @param handler
* @param parent
* @param uri
* @param localName
* @param qName
* @param attributes
*/
FlowProbeFlowStats(Handler handler, FlowProbe parent, String uri, String localName, String qName, Attributes attributes) {
super(handler, parent, uri, localName, qName, attributes);
this.parent = parent;
}

/**
* @return the parent
*/
public FlowProbe getParent() {
return parent;
}

/* (non-Javadoc)
* @see kokuks.flowmon.FlowmonParserElement#getXMLName()
*/
@Override
protected String getXMLName() {
return "FlowStats";
}

/* (non-Javadoc)
* @see kokuks.flowmon.FlowmonParserElement#createAttributeHandlers()
*/
@Override
protected IAttributeHandler[] createAttributeHandlers() {
return new IAttributeHandler[] {
new AttributeHandler("flowId") {
@Override
public void perform(String argValue) {
System.out.println("AH flowId: " + argValue);
flowId = Integer.parseInt(argValue);
set = true;
}
},
new AttributeHandler("packets") {
@Override
public void perform(String argValue) {
System.out.println("AH packets: " + argValue);
packets = Integer.parseInt(argValue);
set = true;
}
},
new AttributeHandler("bytes") {
@Override
public void perform(String argValue) {
System.out.println("AH bytes: " + argValue);
bytes = Integer.parseInt(argValue);
set = true;
}
},
new AttributeHandler("delayFromFirstProbeSum") {
@Override
public void perform(String argValue) {
System.out.println("AH delayFromFirstProbeSum: " + argValue);
delayFromFirstProbeSum = Long.parseLong(argValue.substring(0, argValue.length() - 2));
set = true;
}
}
};
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "FlowStats/" + super.toString() + "[flowId: " + flowId + ", packets: " + packets +
", bytes: " + bytes + ", delayFromFirstProbeSum: " + delayFromFirstProbeSum + "]";
}
}

我还有一些调试输出,其中查找属性并调用相关的实现方法。

    if (this instanceof FlowProbeFlowStats) {
System.out.println("ahaaaa1:: " + this.toString());
}

IAttributeHandler[] ahandlers = createAttributeHandlers();

if (ahandlers != null) {
Map<String, IAttributeHandler> attributeHandlers = new HashMap<String, IAttributeHandler>(ahandlers.length);
for (IAttributeHandler ah : ahandlers) {
attributeHandlers.put(ah.getName(), ah);
}

for (int i = 0; i < attributes.getLength(); i++) {
IAttributeHandler ah = attributeHandlers.get(attributes.getQName(i));
if (ah == null) {
throw new IllegalStateException("Attribute helper not found in qName: " + qName + " for attrib with localName: " + attributes.getLocalName(i) + ", qname*: " + attributes.getQName(i));
}

try {
ah.perform(attributes.getValue(i));
} catch (Exception e) {
e.printStackTrace();
}
}
}

if (this instanceof FlowProbeFlowStats) {
System.out.println("ahaaaa2:: " + this.toString());
}

无论如何,这是输出:

ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@89cd21[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 1
AH packets: 5
AH bytes: 333
AH delayFromFirstProbeSum: 50274395ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@89cd21[flowId: 1, packets: 5, bytes: 333, delayFromFirstProbeSum: 50274395]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@849f16[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 2
AH packets: 5
AH bytes: 333
AH delayFromFirstProbeSum: 50274395ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@849f16[flowId: 2, packets: 5, bytes: 333, delayFromFirstProbeSum: 50274395]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@825c9d[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 3
AH packets: 5
AH bytes: 1082
AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@825c9d[flowId: 3, packets: 5, bytes: 1082, delayFromFirstProbeSum: 0]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@1577d9b[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 4
AH packets: 4
AH bytes: 762
AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@1577d9b[flowId: 4, packets: 4, bytes: 762, delayFromFirstProbeSum: 0]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@8097e5[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 2
AH packets: 5
AH bytes: 333
AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@8097e5[flowId: 2, packets: 5, bytes: 333, delayFromFirstProbeSum: 0]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@745c78[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 4
AH packets: 4
AH bytes: 762
AH delayFromFirstProbeSum: 40615996ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@745c78[flowId: 4, packets: 4, bytes: 762, delayFromFirstProbeSum: 40615996]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@162e843[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 1
AH packets: 5
AH bytes: 333
AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@162e843[flowId: 1, packets: 5, bytes: 333, delayFromFirstProbeSum: 0]
ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@7e9dfc[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 3
AH packets: 5
AH bytes: 1082
AH delayFromFirstProbeSum: 51335996ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@7e9dfc[flowId: 3, packets: 5, bytes: 1082, delayFromFirstProbeSum: 51335996]
[kks-j~KokuKS~/Names] fmxml:FlowMonitor[FlowStats[Flow[flowID: 1,timeFirstTxPacket: 1011735011,timeLastTxPacket: 1343457103,timeFirstRxPacket: 1021768610,timeLastRxPacket: 1353490702,delaySum: 50274395,jitterSum: 212800,lastDelay: 10033599,txBytes: 333, rxBytes: 333,txPackets: 5,rxPackets: 5,lostPackets: 0,timesForwarded: 0],Flow[flowID: 2,timeFirstTxPacket: 1015259481,timeLastTxPacket: 1543226881,timeFirstRxPacket: 1025293080,timeLastRxPacket: 1553260480,delaySum: 50274395,jitterSum: 212800,lastDelay: 10033599,txBytes: 333, rxBytes: 333,txPackets: 5,rxPackets: 5,lostPackets: 0,timesForwarded: 0],Flow[flowID: 3,timeFirstTxPacket: 1021768610,timeLastTxPacket: 1332941904,timeFirstRxPacket: 1031802209,timeLastRxPacket: 1343457103,delaySum: 51335996,jitterSum: 481600,lastDelay: 10515199,txBytes: 1082, rxBytes: 1082,txPackets: 5,rxPackets: 5,lostPackets: 0,timesForwarded: 0],Flow[flowID: 4,timeFirstTxPacket: 1025293080,timeLastTxPacket: 1332969282,timeFirstRxPacket: 1035326679,timeLastRxPacket: 1343226881,delaySum: 40615996,jitterSum: 291200,lastDelay: 10257599,txBytes: 762, rxBytes: 762,txPackets: 4,rxPackets: 4,lostPackets: 0,timesForwarded: 0]],Ipv4FlowClassifier[Flow[flowId: 4, sourceAddress: 10.1.0.1, destinationAddress: 10.1.0.2, protocol: 6],Flow[flowId: 3, sourceAddress: 10.1.0.1, destinationAddress: 10.1.1.2, protocol: 6],Flow[flowId: 2, sourceAddress: 10.1.0.2, destinationAddress: 10.1.0.1, protocol: 6],Flow[flowId: 1, sourceAddress: 10.1.1.2, destinationAddress: 10.1.0.1, protocol: 6]],FlowProbes[FlowProbe[index: 0, stats: <FlowStats/kokuks.flowmon.FlowProbeFlowStats@89cd21[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/kokuks.flowmon.FlowProbeFlowStats@849f16[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/kokuks.flowmon.FlowProbeFlowStats@825c9d[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/kokuks.flowmon.FlowProbeFlowStats@1577d9b[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]>],FlowProbe[index: 1, stats: <FlowStats/kokuks.flowmon.FlowProbeFlowStats@8097e5[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/kokuks.flowmon.FlowProbeFlowStats@745c78[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]>],FlowProbe[index: 2, stats: <FlowStats/kokuks.flowmon.FlowProbeFlowStats@162e843[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/kokuks.flowmon.FlowProbeFlowStats@7e9dfc[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]>]]]

注意内存值——这很奇怪。整个 toString 输出(来自转换后的根 XML 元素)位于最后一行。

无论如何,感谢您的帮助,
克里斯

编辑:我为变量设置了一些不同的默认值而不是 0:

protected final FlowProbe parent;
protected int flowId = 1000;
protected int packets = 1000;
protected int bytes = 1000;
protected long delayFromFirstProbeSum = 1000;
protected boolean set = false;

这是设置过程中的结果:

ahaaaa1:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@1156508[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
kokuks.flowmon.FlowProbeFlowStats@1156508: AH flowId: 4
kokuks.flowmon.FlowProbeFlowStats@1156508: AH packets: 5
kokuks.flowmon.FlowProbeFlowStats@1156508: AH bytes: 1082
kokuks.flowmon.FlowProbeFlowStats@1156508: AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@1156508[flowId: 4, packets: 5, bytes: 1082, delayFromFirstProbeSum: 0]

所以最初发生了一些奇怪的事情,这些值是 0 而不是 1000。非常奇怪。当涉及到最终输出时,正在输出的对象的值为 1000 而不是 0(字段默认值的初始值)。

编辑2:我现在标题中有以下内容:

protected int countdown = 4;

设置属性时会递减。一个例子是:

        new AttributeHandler("packets") {
@Override
public void perform(String argValue) {
synchronized (FlowProbeFlowStats.this) {
System.out.println(FlowProbeFlowStats.super.toString() + ": AH packets: " + argValue);
packets = Integer.parseInt(argValue);
System.out.println("packets set to " + packets);
set = true;
countdown--;
}
}
},

oString 语句现在是:

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "FlowStats/" + super.toString() + "[flowId: " + flowId + ", packets: " + packets +
", bytes: " + bytes + ", delayFromFirstProbeSum: " + delayFromFirstProbeSum +
", countdown: " + countdown + "]";
}

打印输出如下:

ahaaaa2:: FlowStats/kokuks.flowmon.FlowProbeFlowStats@1b27882[flowId: 1, packets: 5, bytes: 333, delayFromFirstProbeSum: 0, countdown: -4]

...

[kks-j~KokuKS~/Names] fmxml:FlowMonitor[FlowStats[Flow[flowID: 1,timeFirstTxPacket: 1007829288,timeLastTxPacket: 1524157706,timeFirstRxPacket: 1017862887,timeLastRxPacket: 1534191305,delaySum: 50274395,jitterSum: 212800,lastDelay: 10033599,txBytes: 333, rxBytes: 333,txPackets: 5,rxPackets: 5,lostPackets: 0,timesForwarded: 0],Flow[flowID: 2,timeFirstTxPacket: 1010210025,timeLastTxPacket: 1324387511,timeFirstRxPacket: 1020243624,timeLastRxPacket: 1334421110,delaySum: 50274395,jitterSum: 212800,lastDelay: 10033599,txBytes: 333, rxBytes: 333,txPackets: 5,rxPackets: 5,lostPackets: 0,timesForwarded: 0],Flow[flowID: 3,timeFirstTxPacket: 1017862887,timeLastTxPacket: 1313900107,timeFirstRxPacket: 1027896486,timeLastRxPacket: 1324157706,delaySum: 40615996,jitterSum: 291200,lastDelay: 10257599,txBytes: 762, rxBytes: 762,txPackets: 4,rxPackets: 4,lostPackets: 0,timesForwarded: 0],Flow[flowID: 4,timeFirstTxPacket: 1020243624,timeLastTxPacket: 1313872312,timeFirstRxPacket: 1030277223,timeLastRxPacket: 1324387511,delaySum: 51335996,jitterSum: 481600,lastDelay: 10515199,txBytes: 1082, rxBytes: 1082,txPackets: 5,rxPackets: 5,lostPackets: 0,timesForwarded: 0]],Ipv4FlowClassifier[Flow[flowId: 4, sourceAddress: 10.1.0.1, destinationAddress: 10.1.0.2, protocol: 6],Flow[flowId: 3, sourceAddress: 10.1.0.1, destinationAddress: 10.1.1.2, protocol: 6],Flow[flowId: 2, sourceAddress: 10.1.0.2, destinationAddress: 10.1.0.1, protocol: 6],Flow[flowId: 1, sourceAddress: 10.1.1.2, destinationAddress: 10.1.0.1, protocol: 6]],FlowProbes[FlowProbe[index: 0, stats: <FlowStats/kokuks.flowmon.FlowProbeFlowStats@1b0d2d0[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/kokuks.flowmon.FlowProbeFlowStats@83df14[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/kokuks.flowmon.FlowProbeFlowStats@1aabc29[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/kokuks.flowmon.FlowProbeFlowStats@2758d0[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4]>],FlowProbe[index: 1, stats: <FlowStats/kokuks.flowmon.FlowProbeFlowStats@137e19e[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/kokuks.flowmon.FlowProbeFlowStats@1bd5f28[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4]>],FlowProbe[index: 2, stats: <FlowStats/kokuks.flowmon.FlowProbeFlowStats@1b27882[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/kokuks.flowmon.FlowProbeFlowStats@5b84b[flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4]>]]]

属性 setter 和 toString 中的同步语句是为了检查 JVM 错误,但它没有执行任何操作。奇怪吧?

编辑3:

我将字段更改为原子*。我必须认真地将其归因于 JVM 错误,因为原子字段是最终的并且在内部设置为默认值,并且我收到了 NullPointerException。

protected final AtomicInteger flowId = new AtomicInteger();
protected final AtomicInteger packets = new AtomicInteger();
protected final AtomicInteger bytes = new AtomicInteger();
protected final AtomicLong delayFromFirstProbeSum = new AtomicLong();

异常(exception):

Exception in thread "FlowMonitor Updater" java.lang.NullPointerException
at kokuks.flowmon.FlowProbeFlowStats.toString(FlowProbeFlowStats.java:95)

编辑4:我从JDK1.7更改为1.6的稳定版本,同样的问题。

我刚刚想到,也许这些字段在构造函数期间未设置,然后稍后设置。我不确定初始化的顺序,但在这种情况下可能会搞砸。不管怎样,我会把它放在一个 init 方法中,看看它是否有作用。但奇怪的是,它在单个类的情况下不起作用。它适用于其他人。

最佳答案

您的输出有些奇怪:以 ahaaaa2 为前缀的行似乎在以 ahaaaa1 为前缀的行之前打印。但事实上,ahaaaa2 行来自前一组值,而 ahaaaa1 行来自下一组值。

ahaaaa1 为前缀的行是具有默认值 (0) 的行。正如我所期望的,带有 ahaaaa2 的行似乎具有正确的值。我认为如果您在 System.out.println("ahaaaa2::"+ this.toString()) 之后添加一两个换行符,您会看到输出是正确的。

<小时/>

编辑:好的,我终于看到这个问题了。在(非常难以阅读!)最后一行输出的末尾,您再次打印出所有统计数据。您希望在哪里看到此内容:

FlowStats/kokuks.flowmon.FlowProbeFlowStats@1577d9b[flowId: 4, packets: 4, bytes: 762, delayFromFirstProbeSum: 0]

你会得到:

FlowStats/kokuks.flowmon.FlowProbeFlowStats@1577d9b[flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]

其他 FlowProbeFlowStats 对象依此类推

我不知道发生了什么,但我也看不到最后一行是在哪里生成的,以及同时 FlowProbeFlowStats 对象可能会发生什么。如果没有看到更多代码,我认为我无法提供帮助。

关于java - XML 反序列化器中的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4879550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com