在使用SAX时,我在Java中出现了一些奇怪的行为。正在设置值,但在输出的toString方法中恢复为默认值。这看起来很不寻常。我查看了代码并输出了变量的内存地址,并且所有事情都以正确的顺序进行,看起来是正确的。其他反序列化的XML元素很好,所以我无法解决这个问题。XML反序列化器中的奇怪行为
我的系统的工作方式如下:有用户定义属性助手和元素处理程序的抽象方法的类。元素处理程序根据子XML元素处理子类的生成,而属性handers从构造函数中获取属性列表,并通过属性助手使用属性列表通过属性帮助器中已实现的抽象方法设置字段。
这里所讨论的问题类:
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/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 1
AH packets: 5
AH bytes: 333
AH delayFromFirstProbeSum: 50274395ns
ahaaaa2:: FlowStats/[email protected][flowId: 1, packets: 5, bytes: 333, delayFromFirstProbeSum: 50274395]
ahaaaa1:: FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 2
AH packets: 5
AH bytes: 333
AH delayFromFirstProbeSum: 50274395ns
ahaaaa2:: FlowStats/[email protected][flowId: 2, packets: 5, bytes: 333, delayFromFirstProbeSum: 50274395]
ahaaaa1:: FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 3
AH packets: 5
AH bytes: 1082
AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/[email protected][flowId: 3, packets: 5, bytes: 1082, delayFromFirstProbeSum: 0]
ahaaaa1:: FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 4
AH packets: 4
AH bytes: 762
AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/[email protected][flowId: 4, packets: 4, bytes: 762, delayFromFirstProbeSum: 0]
ahaaaa1:: FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 2
AH packets: 5
AH bytes: 333
AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/[email protected][flowId: 2, packets: 5, bytes: 333, delayFromFirstProbeSum: 0]
ahaaaa1:: FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 4
AH packets: 4
AH bytes: 762
AH delayFromFirstProbeSum: 40615996ns
ahaaaa2:: FlowStats/[email protected][flowId: 4, packets: 4, bytes: 762, delayFromFirstProbeSum: 40615996]
ahaaaa1:: FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 1
AH packets: 5
AH bytes: 333
AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/[email protected][flowId: 1, packets: 5, bytes: 333, delayFromFirstProbeSum: 0]
ahaaaa1:: FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
AH flowId: 3
AH packets: 5
AH bytes: 1082
AH delayFromFirstProbeSum: 51335996ns
ahaaaa2:: FlowStats/[email protected][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/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]>],FlowProbe[index: 1, stats: <FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]>],FlowProbe[index: 2, stats: <FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0],FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]>]]]
注意内存值 - 这是相当奇怪的。整个toString输出(来自已转换的根元素)位于最后一行。
无论如何,感谢您的帮助,
克里斯
编辑:设置在
protected final FlowProbe parent;
protected int flowId = 1000;
protected int packets = 1000;
protected int bytes = 1000;
protected long delayFromFirstProbeSum = 1000;
protected boolean set = false;
这里是结果:我设置的变量,而不是一些不同的默认值0
ahaaaa1:: FlowStats/[email protected][flowId: 0, packets: 0, bytes: 0, delayFromFirstProbeSum: 0]
[email protected]: AH flowId: 4
[email protected]: AH packets: 5
[email protected]: AH bytes: 1082
[email protected]: AH delayFromFirstProbeSum: 0ns
ahaaaa2:: FlowStats/[email protected][flowId: 4, packets: 5, bytes: 1082, delayFromFirstProbeSum: 0]
因此,最初有些奇怪的事情发生,值是0而不是1000.非常奇怪。当涉及到最终输出时,正在输出的对象的值为1000而不是0(来自字段默认值的初始值)。
EDIT2:我现在有标头中的以下内容:
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/[email protected][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/[email protected][flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/[email protected][flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/[email protected][flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/[email protected][flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4]>],FlowProbe[index: 1, stats: <FlowStats/[email protected][flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/[email protected][flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4]>],FlowProbe[index: 2, stats: <FlowStats/[email protected][flowId: 1000, packets: 1000, bytes: 1000, delayFromFirstProbeSum: 1000, countdown: 4],FlowStats/[email protected][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 in thread "FlowMonitor Updater" java.lang.NullPointerException
at kokuks.flowmon.FlowProbeFlowStats.toString(FlowProbeFlowStats.java:95)
编辑4:我从JDK1.7改为1.6,同样的问题,一个稳定的版本。
我刚刚想过,也许这些字段 - 在构造函数期间未设置,然后再设置。我不确定初始化的顺序,但在这种情况下可能会搞砸了。无论如何,我会把它放在一个init方法中,看看它是否有任何作用。奇怪的是,它不适用于单一类的情况。它适用于其他人。
如果您在第二个代码块中看到1和2变量是成对的。在设定操作完成后,我只是声称这个状态。奇怪的是,混乱的类变量只能在构造函数中完成,并且这些字段在其他情况下是隐藏信息的。没有固定器或清晰的方法。所有这些都在一个循环线程中完成,并且这些对象在迭代之间不会被重用。 – 2011-02-02 23:47:24