如何确保参数是类字段(Java)?

如何确保参数是类字段(Java)?,java,enums,arguments,Java,Enums,Arguments,我正在创建一个API,它返回一个汽车列表。API用户必须能够请求根据Cars类的特定属性(字段)对列表进行过滤和排序。我该怎么做 class Car { public final String model; public final String color; public Car(String m, String c) { model = m; color = c; } } class CarListRequest { public final Stri

我正在创建一个API,它返回一个汽车列表。API用户必须能够请求根据
Cars
类的特定属性(字段)对列表进行过滤和排序。我该怎么做

class Car {
  public final String model;
  public final String color;

  public Car(String m, String c) {
    model = m;
    color = c;
  }
}

class CarListRequest {
  public final String sortBy;
  public final String filterBy;
  public final List<String> filterList;

  public CarListRequest(String s, String f, List<String> list) {
    sortBy = s;
    filterBy = f;
    filterList = list;
  }
}
等级车{
公共最终字符串模型;
公共最终字符串颜色;
公共汽车(m线、c线){
模型=m;
颜色=c;
}
}
职业探索{
公共最终字符串排序;
公共最终字符串过滤器;
公共最终列表过滤器列表;
公共请求(字符串s、字符串f、列表){
sortBy=s;
filterBy=f;
过滤器列表=列表;
}
}
是否有一种方法可以使用Java语言特性限制sortBy和filterBy字符串不能包含Car类的属性(字段)以外的任何其他值


我知道我可以使用枚举来声明Car的所有属性,但是这会导致字符串的重复,这是我希望避免的。

使用Java中的反射,可以检查类的字段

例如,当传入
s
时,可以对参数执行如下检查:

for (Field field : Car.class.getFields()) {
    if (field.getName().equalsIgnoreCase(s)) {
        Do something here to signal that s
        was a valid Field of the Car class.
    }
}
这样做允许您反射性地检查Car类,以验证传入的参数实际上是该类的字段


但是,请注意,如果可能的话,您应该使用枚举或类层次结构,因为反射对于您试图完成的任务来说可能有点过分。

使用Java中的反射,可以检查类的字段

例如,当传入
s
时,可以对参数执行如下检查:

for (Field field : Car.class.getFields()) {
    if (field.getName().equalsIgnoreCase(s)) {
        Do something here to signal that s
        was a valid Field of the Car class.
    }
}
这样做允许您反射性地检查Car类,以验证传入的参数实际上是该类的字段


但是,请注意,如果可能的话,您应该使用枚举或类层次结构,因为对于您试图完成的任务来说,反射可能有点过头了。

@hmc\u jake的反射建议非常有效。但是,如果要避免反射,可以使用类层次结构:

class CarAttribute {
    private String attrib;
    public CarAttribute(String att){
        attrib = att;
    }
    // add getters and/or setters for attrib ... 
}

class CarModel extends CarAttribute {
}

class CarColor extends CarAttribute {
}

class Car {
    public final CarModel model;
    public final CarColor color;

    public Car(CarModel m, CarColor c) {
        model = m;
        color = c;
    }
}

class CarListRequest {
    public final CarAttribute sortBy;
    public final CarAttribute filterBy;
    public final List<CarAttribute> filterList;

    public CarListRequest(CarAttribute s, CarAttribute f, List<CarAttribute> list) {
        sortBy = s;
        filterBy = f;
        filterList = list;
    }
 }
类属性{ 私有字符串属性; 公共属性(字符串附件){ attrib=att; } //为属性添加getter和/或setter。。。 } 类CarModel扩展了CarAttribute{ } 类CarColor扩展了CarAttribute{ } 班车{ 公共最终CarModel模型; 公共最终颜色; 公共汽车(CarModel m、CarColor c){ 模型=m; 颜色=c; } } 职业探索{ 公共最终属性sortBy; 公共最终分配过滤器; 公共最终列表过滤器列表; 公共属性(属性s、属性f、列表){ sortBy=s; filterBy=f; 过滤器列表=列表; } }
@hmc\u杰克的反思建议非常有效。但是,如果要避免反射,可以使用类层次结构:

class CarAttribute {
    private String attrib;
    public CarAttribute(String att){
        attrib = att;
    }
    // add getters and/or setters for attrib ... 
}

class CarModel extends CarAttribute {
}

class CarColor extends CarAttribute {
}

class Car {
    public final CarModel model;
    public final CarColor color;

    public Car(CarModel m, CarColor c) {
        model = m;
        color = c;
    }
}

class CarListRequest {
    public final CarAttribute sortBy;
    public final CarAttribute filterBy;
    public final List<CarAttribute> filterList;

    public CarListRequest(CarAttribute s, CarAttribute f, List<CarAttribute> list) {
        sortBy = s;
        filterBy = f;
        filterList = list;
    }
 }
类属性{ 私有字符串属性; 公共属性(字符串附件){ attrib=att; } //为属性添加getter和/或setter。。。 } 类CarModel扩展了CarAttribute{ } 类CarColor扩展了CarAttribute{ } 班车{ 公共最终CarModel模型; 公共最终颜色; 公共汽车(CarModel m、CarColor c){ 模型=m; 颜色=c; } } 职业探索{ 公共最终属性sortBy; 公共最终分配过滤器; 公共最终列表过滤器列表; 公共属性(属性s、属性f、列表){ sortBy=s; filterBy=f; 过滤器列表=列表; } }
字符串的重复是什么?你能解释清楚吗?如果我创建一个枚举,我会在另一个地方重复“model”、“color”等。如果汽车获得新属性,我必须编辑两个位置。(Car class and CarAttributesEnum)@greppz你应该接受答案。这样做表明问题已经解决。字符串的重复是什么?你能解释清楚吗?如果我创建一个枚举,我会在另一个地方重复“model”、“color”等。如果汽车获得新属性,我必须编辑两个位置。(Car class and CarAttributesEnum)@greppz你应该接受答案。这样做表明问题已经解决了。使用枚举比使用反射要好得多。无论如何,这似乎符合OP的要求。@FedericoPeraltaSchaffner我同意你的看法。我在回答中添加了一个建议。使用枚举比使用反射要好得多。无论如何,这似乎符合OP的要求。@FedericoPeraltaSchaffner我同意你的看法。我在回答中加了一条建议。