NutzCN Logo
问答 mirror.getFields(XXXX.class)无法获取Field[]
发布于 3250天前 作者 loyalove 2607 次浏览 复制 上一个帖子 下一个帖子
标签: lang
Mirror<?> mirror = Mirror.me(this.getClass());
Field[] fields = mirror.getFields(CndType.class); //fileds没有filed对象

public class UserQuery extends BaseQuery {

    @CndType("=")
    private String id;

    @CndType("like")
    private String name;

    @OrderType("asc")
    private String updateTime;

    @OrderType("desc")
    private String createTime;

   //......
}
7 回复
//fileds没有filed对象

我就没看到filed在哪里...

public class UserQuery extends BaseQuery {

    @CndType("=")
    private String id;

    @CndType("like")
    private String name;

    @OrderType("asc")
    private String updateTime;

    @OrderType("desc")
    private String createTime;

Mirror<?> mirror = Mirror.me(this.getClass());
Field[] fields = mirror.getFields(CndType.class); //fileds为空数组
}

public class UserQuery extends BaseQuery {

@CndType("=")
private String id;

@CndType("like")
private String name;

@OrderType("asc")
private String updateTime;

@OrderType("desc")
private String createTime;

public void test(){
    Mirror<?> mirror = Mirror.me(this.getClass());

Field[] fields = mirror.getFields(CndType.class); //fileds为空数组
}
}

@wendal

public class UserQuery extends BaseQuery {

  @CndType("=")
  private String id;
  
  @CndType("like")
  private String name;
  
  @OrderType("asc")
  private String updateTime;
  
  @OrderType("desc")
  private String createTime;

  public void test(){
	  Mirror<?> mirror = Mirror.me(this.getClass());
	  Field[] fields = mirror.getFields(CndType.class); //fileds为空数组
   }
}
Field[] fields = mirror.getFields();  //可以获取到所有Field

我的测试代码

public class UserQuery {

	@Column("=")
	private String id;

	@Column("like")
	private String name;

	@Column("asc")
	private String updateTime;

	@Column("desc")
	private String createTime;

	@Test
	public void test() {
		Mirror<?> mirror = Mirror.me(this.getClass());
		Field[] fields = mirror.getFields(Column.class); // fileds为空数组
		System.out.println(Arrays.toString(fields));
	}
}

输出

[private java.lang.String net.wendal.nutzbook.UserQuery.id, private java.lang.String net.wendal.nutzbook.UserQuery.name, private java.lang.String net.wendal.nutzbook.UserQuery.updateTime, private java.lang.String net.wendal.nutzbook.UserQuery.createTime]

@CndType和@OrderType是不是声明错了, 起码要加上这个才能读取的.

@Retention(RetentionPolicy.RUNTIME)

是没加@Retention(RetentionPolicy.RUNTIME); 多谢兽总,学习了。

添加回复
请先登陆
回到顶部