Java如何使用getDeclaredField(“someVaraible”)方法

Java如何使用getDeclaredField(“someVaraible”)方法,java,jooq,Java,Jooq,我在应用程序中使用Jooq,我使用比较器,我的代码是这样的 public void columnsList(List<TableRecord> records){ Collections.sort(records, new StampDateComparator()); for(TableRecord record : records){ Table table = record.getTable();

我在应用程序中使用Jooq,我使用比较器,我的代码是这样的

public void columnsList(List<TableRecord> records){

         Collections.sort(records, new StampDateComparator());
        for(TableRecord record : records){
            Table table = record.getTable();
            Field[] fields = table.fields();
            for(Field field : fields){
                field.getName();
                record.getValue(field);

            }
        }

    }
但是这行代码

 Date d1 = (Date) o1.getClass().getDeclaredField("StampDate").get(o1);
java.lang.NoSuchFieldException:StamUpdate位于 位于的java.lang.Class.getDeclaredField(未知源) com.csdcsystems.amanda.comp.macro.StampDateComparator.compare(StampDateComparator.java:13) 位于java.util.TimSort.countRunAndMakeAscending(未知源代码) 位于的java.util.TimSort.sort(未知源) 位于的java.util.TimSort.sort(未知源) 位于的java.util.Arrays.sort(未知源) java.util.Collections.sort(未知源)

我做错了什么

对于您的参考
o1
包含如下字段

@Override
public int compare(Object o1, Object o2) {

    try {
        Date d1 = (Date) o1.getClass().getDeclaredField("StampDate").get(o1);
        Date d2 = (Date) o2.getClass().getDeclaredField("StampDate").get(o2);
        return compare(d1, d2);
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException("Missing variable stampDate");
    }catch (ClassCastException e) {
        throw new RuntimeException("stampDate is not a Date");
    } catch (IllegalArgumentException e) {
        //shoud not happen
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
|PeopleRSN|NameTitle|NameFirst|NameLast|OrganizationName|AddrHouse|AddrStreet|AddrStreetType|AddrStreetDirection|AddrUnitType|AddrUnit|AddrCity|AddrProvince|AddrCountry|AddrPostal|Phone1|Phone1Desc|Phone2|Phone2Desc|LicenceNumber|PeopleCode|ContactSex|BirthDate|FamilyRSN|Comments|StampDate|StampUser|AddrPrefix|AddressLine1|AddressLine2|AddressLine3|AddressLine4|AddressLine5|AddressLine6|AddrHouseNumeric|Phone3|Phone3Desc|EmailAddress|NameFirstUpper|NameLastUpper|OrgNameUpper|AddrStreetUpper|AddrStreetPrefix|ParentRSN|SecurityCode|LogType|LogUser|LogDate|ReferenceFile|StatusCode|Nearby|Community|Phone4|Phone4Desc|InternetPassword|InternetAccess|LogDbDate|InternetRegistrationDate|STATUSTYPE|INTERNETQUESTION|INTERNETANSWER|CREDITCARDPROCESSINGFLAG|WEBDISPLAYFLAG|NameMiddle|NameSuffix|AddrCommunityCode|AddrCountyCode|NameMiddleUpper|SMSFlag|EmailFlag|NameLast1|AliasRSN|PeopleRoleType|PeopleRaceType|PeopleHairType|PeopleEyeType|SEX   |Height|HeightInch|Weight|SocialSecurityNumber|LicenceNumberProvince|ConversionFlag|

|   {null}|{null}   |{null}   |{null}  |{null}          |{null}   |{null}    |{null}        |{null}             |{null}      |{null}  |{null}  |{null}      |{null}     |{null}    |{null}|{null}    |{null}|{null}    |{null}       |    {null}|{null}    |{null}   |   {null}|{null}  |{null}   |{null}   |{null}    |{null}      |{null}      |{null}      |{null}      |{null}      |{null}      |          {null}|{null}|{null}    |{null}      |{null}        |{null}       |{null}      |{null}         |{null}          |   {null}|      {null}|{null} |{null} |{null} |{null}       |    {null}|{null}|{null}   |{null}|{null}    |{null}          |{null}        |{null}   |{null}                  |{null}    |{null}          |{null}        |{null}                  |{null}        |{null}    |{null}    |           {null}|        {null}|{null}         |{null} |{null}   |{null}   |  {null}|{null}        |{null}        |{null}        |{null}       |{null}|{null}|    {null}|{null}|{null}              |{null}               |{null}        |
编辑:-

/**
 * This class is generated by jOOQ.
 */
@java.lang.SuppressWarnings({ "all", "unchecked", "rawtypes" })
@javax.persistence.Entity
@javax.persistence.Table(name = "LogFolder", schema = "amanda")
public class LogFolderRecord extends org.jooq.impl.UpdatableRecordImpl<com.csdcsystems.schema.amanda.records.LogFolderRecord> {

    private static final long serialVersionUID = -673142359;

    /**
     * Setter for <code>amanda.LogFolder.FolderRSN</code>. 
     */
    public void setFolderRSN(java.lang.Integer value) {
        setValue(0, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.FolderRSN</code>. 
     */
    @javax.persistence.Id
    @javax.persistence.Column(name = "FolderRSN", unique = true, nullable = false, precision = 10)
    @javax.validation.constraints.NotNull
    public java.lang.Integer getFolderRSN() {
        return (java.lang.Integer) getValue(0);
    }

    /**
     * Setter for <code>amanda.LogFolder.FolderYear</code>. 
     */
    public void setFolderYear(java.lang.String value) {
        setValue(1, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.FolderYear</code>. 
     */
    @javax.persistence.Column(name = "FolderYear", nullable = false, length = 4)
    @javax.validation.constraints.NotNull
    @javax.validation.constraints.Size(max = 4)
    public java.lang.String getFolderYear() {
        return (java.lang.String) getValue(1);
    }

    /**
     * Setter for <code>amanda.LogFolder.FolderSequence</code>. 
     */
    public void setFolderSequence(java.lang.String value) {
        setValue(2, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.FolderSequence</code>. 
     */
    @javax.persistence.Column(name = "FolderSequence", nullable = false, length = 10)
    @javax.validation.constraints.NotNull
    @javax.validation.constraints.Size(max = 10)
    public java.lang.String getFolderSequence() {
        return (java.lang.String) getValue(2);
    }

    /**
     * Setter for <code>amanda.LogFolder.FolderSection</code>. 
     */
    public void setFolderSection(java.lang.String value) {
        setValue(3, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.FolderSection</code>. 
     */
    @javax.persistence.Column(name = "FolderSection", nullable = false, length = 3)
    @javax.validation.constraints.NotNull
    @javax.validation.constraints.Size(max = 3)
    public java.lang.String getFolderSection() {
        return (java.lang.String) getValue(3);
    }

    /**
     * Setter for <code>amanda.LogFolder.FolderRevision</code>. 
     */
    public void setFolderRevision(java.lang.String value) {
        setValue(4, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.FolderRevision</code>. 
     */
    @javax.persistence.Column(name = "FolderRevision", nullable = false, length = 2)
    @javax.validation.constraints.NotNull
    @javax.validation.constraints.Size(max = 2)
    public java.lang.String getFolderRevision() {
        return (java.lang.String) getValue(4);
    }

    /**
     * Setter for <code>amanda.LogFolder.FolderType</code>. 
     */
    public void setFolderType(java.lang.String value) {
        setValue(5, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.FolderType</code>. 
     */
    @javax.persistence.Column(name = "FolderType", length = 4)
    @javax.validation.constraints.Size(max = 4)
    public java.lang.String getFolderType() {
        return (java.lang.String) getValue(5);
    }

    /**
     * Setter for <code>amanda.LogFolder.StatusCode</code>. 
     */
    public void setStatusCode(java.lang.Integer value) {
        setValue(6, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.StatusCode</code>. 
     */
    @javax.persistence.Column(name = "StatusCode", precision = 10)
    public java.lang.Integer getStatusCode() {
        return (java.lang.Integer) getValue(6);
    }

    /**
     * Setter for <code>amanda.LogFolder.PropertyRSN</code>. 
     */
    public void setPropertyRSN(java.lang.Integer value) {
        setValue(7, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.PropertyRSN</code>. 
     */
    @javax.persistence.Column(name = "PropertyRSN", precision = 10)
    public java.lang.Integer getPropertyRSN() {
        return (java.lang.Integer) getValue(7);
    }

    /**
     * Setter for <code>amanda.LogFolder.PropertyLocation</code>. 
     */
    public void setPropertyLocation(java.lang.String value) {
        setValue(8, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.PropertyLocation</code>. 
     */
    @javax.persistence.Column(name = "PropertyLocation", length = 255)
    @javax.validation.constraints.Size(max = 255)
    public java.lang.String getPropertyLocation() {
        return (java.lang.String) getValue(8);
    }

    /**
     * Setter for <code>amanda.LogFolder.InDate</code>. 
     */
    public void setInDate(java.sql.Timestamp value) {
        setValue(9, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.InDate</code>. 
     */
    @javax.persistence.Column(name = "InDate")
    public java.sql.Timestamp getInDate() {
        return (java.sql.Timestamp) getValue(9);
    }

    /**
     * Setter for <code>amanda.LogFolder.IssueDate</code>. 
     */
    public void setIssueDate(java.sql.Timestamp value) {
        setValue(10, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.IssueDate</code>. 
     */
    @javax.persistence.Column(name = "IssueDate")
    public java.sql.Timestamp getIssueDate() {
        return (java.sql.Timestamp) getValue(10);
    }

    /**
     * Setter for <code>amanda.LogFolder.ExpiryDate</code>. 
     */
    public void setExpiryDate(java.sql.Timestamp value) {
        setValue(11, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.ExpiryDate</code>. 
     */
    @javax.persistence.Column(name = "ExpiryDate")
    public java.sql.Timestamp getExpiryDate() {
        return (java.sql.Timestamp) getValue(11);
    }

    /**
     * Setter for <code>amanda.LogFolder.ReferenceFile</code>. 
     */
    public void setReferenceFile(java.lang.String value) {
        setValue(12, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.ReferenceFile</code>. 
     */
    @javax.persistence.Column(name = "ReferenceFile", length = 100)
    @javax.validation.constraints.Size(max = 100)
    public java.lang.String getReferenceFile() {
        return (java.lang.String) getValue(12);
    }

    /**
     * Setter for <code>amanda.LogFolder.IssueUser</code>. 
     */
    public void setIssueUser(java.lang.String value) {
        setValue(13, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.IssueUser</code>. 
     */
    @javax.persistence.Column(name = "IssueUser", length = 128)
    @javax.validation.constraints.Size(max = 128)
    public java.lang.String getIssueUser() {
        return (java.lang.String) getValue(13);
    }

    /**
     * Setter for <code>amanda.LogFolder.FinalDate</code>. 
     */
    public void setFinalDate(java.sql.Timestamp value) {
        setValue(14, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.FinalDate</code>. 
     */
    @javax.persistence.Column(name = "FinalDate")
    public java.sql.Timestamp getFinalDate() {
        return (java.sql.Timestamp) getValue(14);
    }

    /**
     * Setter for <code>amanda.LogFolder.SubCode</code>. 
     */
    public void setSubCode(java.lang.Integer value) {
        setValue(15, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.SubCode</code>. 
     */
    @javax.persistence.Column(name = "SubCode", precision = 10)
    public java.lang.Integer getSubCode() {
        return (java.lang.Integer) getValue(15);
    }

    /**
     * Setter for <code>amanda.LogFolder.WorkCode</code>. 
     */
    public void setWorkCode(java.lang.Integer value) {
        setValue(16, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.WorkCode</code>. 
     */
    @javax.persistence.Column(name = "WorkCode", precision = 10)
    public java.lang.Integer getWorkCode() {
        return (java.lang.Integer) getValue(16);
    }

    /**
     * Setter for <code>amanda.LogFolder.FolderDescription</code>. 
     */
    public void setFolderDescription(java.lang.String value) {
        setValue(17, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.FolderDescription</code>. 
     */
    @javax.persistence.Column(name = "FolderDescription", length = 2147483647)
    @javax.validation.constraints.Size(max = 2147483647)
    public java.lang.String getFolderDescription() {
        return (java.lang.String) getValue(17);
    }

    /**
     * Setter for <code>amanda.LogFolder.FolderCondition</code>. 
     */
    public void setFolderCondition(java.lang.String value) {
        setValue(18, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.FolderCondition</code>. 
     */
    @javax.persistence.Column(name = "FolderCondition", length = 2147483647)
    @javax.validation.constraints.Size(max = 2147483647)
    public java.lang.String getFolderCondition() {
        return (java.lang.String) getValue(18);
    }

    /**
     * Setter for <code>amanda.LogFolder.ParentRSN</code>. 
     */
    public void setParentRSN(java.lang.Integer value) {
        setValue(19, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.ParentRSN</code>. 
     */
    @javax.persistence.Column(name = "ParentRSN", precision = 10)
    public java.lang.Integer getParentRSN() {
        return (java.lang.Integer) getValue(19);
    }

    /**
     * Setter for <code>amanda.LogFolder.CopyFlag</code>. 
     */
    public void setCopyFlag(com.csdcsystems.schema.custom.CopyFlag value) {
        setValue(20, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.CopyFlag</code>. 
     */
    @javax.persistence.Column(name = "CopyFlag", length = 7)
    public com.csdcsystems.schema.custom.CopyFlag getCopyFlag() {
        return (com.csdcsystems.schema.custom.CopyFlag) getValue(20);
    }

    /**
     * Setter for <code>amanda.LogFolder.StampDate</code>. 
     */
    public void setStampDate(java.sql.Timestamp value) {
        setValue(21, value);
    }

    /**
     * Getter for <code>amanda.LogFolder.StampDate</code>. 
     */
    @javax.persistence.Column(name = "StampDate")
    public java.sql.Timestamp getStampDate() {
        return (java.sql.Timestamp) getValue(21);
    }

您正在尝试获取一个不存在的字段。

您正在尝试获取一个不存在的字段。

getClass()。getDeclaredField
用于以反射方式获取
字段的值。语句

 Date d1 = (Date) o1.getClass().getDeclaredField("StampDate").get(o1);
将返回字段
StampDate
的值。当它抛出
java.lang.NoSuchFieldException
时,意味着该字段不在类中,或者存在一些拼写错误。尝试将
StampDate
更改为
StampDate


getDeclaredField
提供当前类中声明的所有字段(不属于任何基类),并且
getField
提供整个类层次结构中的所有公共字段。如果在任何超类中声明了
StampDate
,则可以使用
getField

getClass()访问该超类。getDeclaredField
用于以反射方式获取
字段的值

 Date d1 = (Date) o1.getClass().getDeclaredField("StampDate").get(o1);
将返回字段
StampDate
的值。当它抛出
java.lang.NoSuchFieldException
时,意味着该字段不在类中,或者存在一些拼写错误。尝试将
StampDate
更改为
StampDate


getDeclaredField
提供当前类中声明的所有字段(不属于任何基类),并且
getField
提供整个类层次结构中的所有公共字段。如果在任何超类中声明了
StampDate
,则可以使用
getField
getDeclaredField
作为的方法部分来访问该超类,其目的是查找已在要查询字段的特定类中定义的java字段(又称实例属性),与
getField
相反,它将尝试在整个类层次结构中查找指定的字段

如果您的类
TableRecord
的定义如下,您就可以使用它:

public class TableRecord {

   public Date StampDate;
   // other fields

}
然而,正如代码所示,您正在寻找其他东西您的类
TableRecord
没有名为
StampDate
的字段,而是有一个名为
StampDate
的属性。这里有两种方法,一种是使用反射,另一种是使用反射,但获取方法
getStampDate
,而不是字段

使用内省,您可以执行以下操作:

BeanInfo bi = Introspector.getBeanInfo(o1.getClass());
PropertyDescriptor[] properties = bi.getPropertyDescriptors();
// iterate over 'properties' and look for a property named 'stampDate'
// you then need to do the following
Date stampDate = (Date) stampDateProperty.getReadMethod().invoke(o1);
使用反射(可能更容易,因为您已经完成了一半):


作为参考,请查看。

getDeclaredField
是的方法部分,其目的是查找已在要查询字段的特定类中定义的java字段(又称实例属性),与
getField
相反,它将尝试在整个类层次结构中查找指定的字段

如果您的类
TableRecord
的定义如下,您就可以使用它:

public class TableRecord {

   public Date StampDate;
   // other fields

}
然而,正如代码所示,您正在寻找其他东西您的类
TableRecord
没有名为
StampDate
的字段,而是有一个名为
StampDate
的属性。这里有两种方法,一种是使用反射,另一种是使用反射,但获取方法
getStampDate
,而不是字段

使用内省,您可以执行以下操作:

BeanInfo bi = Introspector.getBeanInfo(o1.getClass());
PropertyDescriptor[] properties = bi.getPropertyDescriptors();
// iterate over 'properties' and look for a property named 'stampDate'
// you then need to do the following
Date stampDate = (Date) stampDateProperty.getReadMethod().invoke(o1);
使用反射(可能更容易,因为您已经完成了一半):


为了便于参考,请查看。

但是O1包含该字段您可以通过Eclipse在O1对象上的MouseOver检查O1输出显示Java运行时说它不包含。O1包含StampDate varaible但是该变量是由您查询它的类声明的吗?您需要读取Javadoc。但是O1包含该字段您可以检查O1对象上Eclipse onMouseOver的O1输出显示Java运行时说它不包含。O1包含StampDate varaible但该变量是由您查询它的类声明的吗?您需要阅读Javadoc。也许您应该检查参数是否为预期类型。请注意,Comparator接口允许通过其泛型类型参数缩小接受的参数。您应该尝试使用它,而不是容易出错的反射。您应该发布类的源代码
o1
。我发现这个表真的无法访问该字段不是用名称“StampDate”创建的!我建议对
getDeclaredFields()
进行迭代,它返回一个数组,查看
field.getName()
中的字段名。也许您应该检查参数是否为预期类型。请注意,Comparator接口允许通过其泛型类型参数缩小接受的参数。您应该尝试使用它,而不是容易出错的反射。您应该发布类的源代码
o1
。我发现这个表真的无法访问该字段不是用名称“StampDate”创建的!我建议在
getDeclaredFields()
上使用ITERATION,它返回一个数组,查看
field.getName()
中的字段名,但我已经在问题中打印了o1值,其中包含