BeanIO で CSV ファイルを読み込んでいます。CSV で表されるオブジェクトは Field オブジェクトであり、ファイルを読み取ると、そのように返され、デバッグ ユーティリティですべて問題ないことを確認できます。ただし、オブジェクトのプロパティにアクセスするためにオブジェクトをその型に解析しようとすると、次のようにクラス キャスト例外が発生します。
java.lang.ClassCastException: util.flatfile.model.classes.Field cannot be cast to util.flatfile.model.classes.Field
at util.csvreader.util.CSVBeanReader.read(CSVBeanReader.gs:19)
at program_.__Program__1983.evaluate(Unknown Source)
at gw.internal.gosu.parser.GosuProgram.runProgram(GosuProgram.java:420)
at gw.internal.gosu.parser.GosuProgram.evaluate(GosuProgram.java:253)
at gw.internal.gosu.parser.GosuProgram_Proxy.evaluate(gw.internal.gosu.parser.GosuProgram_Proxy:2)
at gw.internal.gosu.parser.ExecutionEnvironment$1.evaluate(ExecutionEnvironment.java:542)
at gw.internal.gosu.parser.ExecutionEnvironment$1.runScript(ExecutionEnvironment.java:522)
at gw.internal.gosu.parser.ExecutionEnvironment$1.run(ExecutionEnvironment.java:488)
at java.lang.Thread.run(Thread.java:724)
私の読み取り方法は次のとおりです
class CSVBeanReader implements BeanReader {
override function read(): Object {
// create a StreamFactory
var factory = StreamFactory.newInstance()
// load the mapping file
factory.load("C:\\Dev\\modules\\configuration\\gsrc\\util\\csvreader\\util\\fieldMapping.xml")
// use a StreamFactory to create a BeanReader
using (var beanReader = factory.createReader("fields", new File("C:\\Dev\\modules\\configuration\\gsrc\\util\\csvreader\\util\\vendorDef.csv"))) {
try {
var field = beanReader.read()
while (field != null) {
print((field as Field)) //EXCEPTION OCCURS HERE
// process the record...
field = beanReader.read()
}
} catch (var e: InvalidRecordException) {
var context = e.getRecordContext()
if (context.hasRecordErrors()) {
for (error in context.getRecordErrors()) {
print("Error: ${error}")
// handle record errors...
}
}
if (context.hasFieldErrors()) {
for (field in context.getFieldErrors().keySet()) {
for (error in context.getFieldErrors(field)) {
print("Error: ${error}")
// handle field error...
}
}
}
}
}
return null
}
}
私のフィールドクラス:
class Field {
private var _startPos: int as StartPosition
private var _length: int as Length
private var _fieldName: String as Name
private var _param1: String as Parameter1
private var _format: String as Format
private var _justify: String as Justify
private var _fill: String as Fill
private var _truncate: String as Truncate
private var _stripType: String as StripType
private var _strip: String as Strip
private var _data: String as Data
private var _param2: String as Parameter2
construct() {
}
construct(startPos: int, fieldLength: int, fieldName: String,
param1: String, format: String, justify: String, fill: String, truncate: String,
stripType: String, strip: String, data: String, param2: String) {
_startPos = startPos
_length = fieldLength
_fieldName = fieldName
_param1 = param1
_format = format
_justify = justify
_fill = fill
_truncate = truncate
_stripType = stripType
_data = data
_param2 = param2
}
}
私のCSV入力:
Field,File Header,1,1,Constant,'',None,Left,' ',Right,'None','','1',''
Field,File Header,2,2,Constant,'',None,Left,'0',Right,'None','','',''
Field,File Header,4,10,Destination DDA,'',None,Right,'0',Left,'None','','',''
Field,File Header,14,10,Constant,'',None,Left,' ',Right,'None','','0111000025',''
Field,File Header,24,6,File Creation Date,'',YYMMDD,Right,'0',Left,'None','','',''
Field,File Header,30,4,File Creation Date,'',HHMM,Right,'0',Left,'None','','',''
Field,File Header,34,47,Constant,'',None,Left,' ',Right,'None','','',''
そして私のマッピングxml:
<beanio xmlns="http://www.beanio.org/2012/03"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">
<stream name="fields" format="csv">
<record name="field" class="util.flatfile.model.classes.Field">
<field name="entryType" rid="true" literal="Field" ignore="true" />
<field name="recordType" ignore="true" />
<field name="startPosition" type="int" />
<field name="length" type="int"/>
<field name="name" />
<field name="parameter1" />
<field name="format" />
<field name="justify" />
<field name="fill" />
<field name="truncate" />
<field name="stripType" />
<field name="strip" />
<field name="data" />
<field name="parameter2" />
</record>
</stream>
</beanio>
クラスローダーが原因である可能性が高いと読みましたが、ここで問題になる理由がわかりません。何か案は?
ありがとう