在使用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