0

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

我读过它可能是由类加载器引起的,但我不明白为什么这会是一个问题。有任何想法吗?

谢谢

4

0 回答 0