如何使泛型函数根据 Java 中的输入获取对象属性

我有如下的 Java 实体


@Entity

@Table(name = "user", schema = "xxxx")

public class User extends AuditableBase<Long> {


  /** The Constant serialVersionUID. */

  private static final long serialVersionUID = -5827659402853250569L;


  public static final String FTL = "FTL";


  /** The refer number. */

  @Column(name = "reference_number")

  private String referenceNumber;


  /** The customer id. */

  @OneToOne(fetch = FetchType.LAZY)

  @JoinColumn(name = "customer_id")

  @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)

  private Customer customer;


  /** The load Notes. */

  @OneToMany(cascade = CascadeType.ALL, mappedBy = "load")

  @JsonBackReference

  private Set<Notes> notes = new HashSet<>();


  /** The customer location. */

  @OneToOne(fetch = FetchType.LAZY)

  @JoinColumn(name = "customer_location_id")

  @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)

  private CustomerLocation customerLocation;


  /** The invoice number. */

  @Column(name = "invoice_number")

  private String invoiceNumber;


  // Lot going


  /**

   * Instantiates a new User.

   */

  public User() {

    super();

  }


  /**

   * Instantiates a new User.

   *

   * @param id

   *          the id

   */

  public User(Long id) {

    super(id);

  }


}

我在实体内部有很多孩子,我从 DAO 调用中获取对象


User user = userRepository.findById(userId);

现在我想根据一些引用 Map 的 if 条件获取一些用户详细信息。


Map<Integer, String> cc = new HashMap<Integer, String>();

cc.put(1, "getCustomer()");

cc.put(2, "getNotes()");

cc.put(3, "getCustomerLocation()");

cc.put(4, "getReferenceNumber()");



for (Entry<Integer, String> map : cc.entrySet()) {


    user.map.getvalue();


}

我需要创建一个通用方法来根据地图获取用户对象。


我怎样才能做到这一点。


繁华开满天机
浏览 117回答 1
1回答

牧羊人nacy

不确定我是否完全理解你的问题,但你可以这样做:&nbsp; &nbsp; &nbsp; &nbsp; Map<Integer,Method> cc = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; cc.put(1, User.class.getDeclaredMethod("getCustomer"));&nbsp; &nbsp; &nbsp; &nbsp; cc.put(2, User.class.getDeclaredMethod("getNotes"));&nbsp; &nbsp; &nbsp; &nbsp; cc.put(3, User.class.getDeclaredMethod("getCustomerLocation"));&nbsp; &nbsp; &nbsp; &nbsp; cc.put(4, User.class.getDeclaredMethod("getReferenceNumber"));&nbsp; &nbsp; &nbsp; &nbsp; for (Entry<Integer, Method> map : cc.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Integer index = map.getKey();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Method getter = map.getValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object value = getter.invoke(user);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doSomethingUsefulWith(index, value);&nbsp; &nbsp; &nbsp; &nbsp; }更新:你可以Getter像这样声明一个接口:public interface Getter<T,R> {&nbsp; &nbsp; public R get(T obj);}然后做这样的事情:&nbsp; &nbsp; Map<Integer,Getter<User,?>> cc = new HashMap<>();&nbsp; &nbsp; cc.put(1, (u) -> u.getCustomer().getName());&nbsp; &nbsp; cc.put(2, (u) -> u.getNotes());&nbsp; &nbsp; cc.put(3, (u) -> u.getCustomerLocation());&nbsp; &nbsp; cc.put(4, (u) -> u.getReferenceNumber());&nbsp; &nbsp; for (Entry<Integer,Getter<User,?>> map : cc.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; Integer index = map.getKey();&nbsp; &nbsp; &nbsp; &nbsp; Getter<User,?> getter = map.getValue();&nbsp; &nbsp; &nbsp; &nbsp; Object value = getter.get(user);&nbsp; &nbsp; &nbsp; &nbsp; doSomethingUsefulWith(index, value);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java