Java:将字段名称(“firstName”)转换为访问者方法名称(“getFirstName”)的标准库

是否有一个标准库(例如org.apache.commons.beanutils或java.beans)将采用字符串字段名称并将其转换为标准方法名称? 我正在寻找一个简单的字符串转换实用程序。

出现了狂野的单行!

String fieldToGetter(String name) { return "get" + name.substring(0, 1).toUpperCase() + name.substring(1); } 

JavaBean introspector可能是最好的选择。 它处理布尔类型的“is”getter和带有参数和setter的“getters”,其中包含无论是两个参数和其他边缘情况。 获取类的JavaBean字段列表非常方便。

这是一个例子,

 import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; public class SimpleBean { private final String name = "SimpleBean"; private int size; public String getName() { return this.name; } public int getSize() { return this.size; } public void setSize( int size ) { this.size = size; } public static void main( String[] args ) throws IntrospectionException { BeanInfo info = Introspector.getBeanInfo( SimpleBean.class ); for ( PropertyDescriptor pd : info.getPropertyDescriptors() ) System.out.println( pd.getName() ); } } 

这打印

 class name size 

class来自inheritance自Object getClass()

编辑:获取getter或setter及其名称。

 public static String findGetterName(Class clazz, String name) throws IntrospectionException, NoSuchFieldException, NoSuchMethodException { Method getter = findGetter(clazz, name); if (getter == null) throw new NoSuchMethodException(clazz+" has no "+name+" getter"); return getter.getName(); } public static Method findGetter(Class clazz, String name) throws IntrospectionException, NoSuchFieldException { BeanInfo info = Introspector.getBeanInfo(clazz); for ( PropertyDescriptor pd : info.getPropertyDescriptors() ) if (name.equals(pd.getName())) return pd.getReadMethod(); throw new NoSuchFieldException(clazz+" has no field "+name); } public static String findSetterName(Class clazz, String name) throws IntrospectionException, NoSuchFieldException, NoSuchMethodException { Method setter = findSetter(clazz, name); if (setter == null) throw new NoSuchMethodException(clazz+" has no "+name+" setter"); return setter.getName(); } public static Method findSetter(Class clazz, String name) throws IntrospectionException, NoSuchFieldException { BeanInfo info = Introspector.getBeanInfo(clazz); for ( PropertyDescriptor pd : info.getPropertyDescriptors() ) if (name.equals(pd.getName())) return pd.getWriteMethod(); throw new NoSuchFieldException(clazz+" has no field "+name); } 

您可以使用不带InspectorPropertyDescriptor (由Peter建议):

 final PropertyDescriptor propertyDescriptor = new PropertyDescriptor("name", MyBean.class); System.out.println("getter: " + propertyDescriptor.getReadMethod().getName()); System.out.println("setter: " + propertyDescriptor.getWriteMethod().getName()); 
 String fieldToSetter(String name) { return "set" + name.substring(0, 1).toUpperCase() + name.substring(1); } 

拥有Matt Ball的版权

我修改了上面的方法,删除下划线字符并大写下一个字符…例如,如果字段名称为“validated_id”,则getter方法名称将为“getValidatedId”

 private String fieldToGetter(String name) { Matcher matcher = Pattern.compile("_(\\w)").matcher(name); while (matcher.find()) { name = name.replaceFirst(matcher.group(0), matcher.group(1).toUpperCase()); } return "get" + name.substring(0, 1).toUpperCase() + name.substring(1); } private String fieldToSetter(String name) { Matcher matcher = Pattern.compile("_(\\w)").matcher(name); while (matcher.find()) { name = name.replaceFirst(matcher.group(0), matcher.group(1).toUpperCase()); } return "set" + name.substring(0, 1).toUpperCase() + name.substring(1); } 

在以下示例中,名为exampleeXample两个字段具有由eclipse生成的getter getExamplegeteExample IF 。 但这与PropertyDescriptor("eXample",...).getReadMethod().getName()不兼容,它将getEXample作为有效的getter名称。

 public class XX { private Integer example; private Integer eXample; public Integer getExample() { return example; } public Integer geteXample() { return eXample; } public void setExample(Integer example) { this.example = example; } public void seteXample(Integer eXample) { this.eXample = eXample; } public static void main(String[] args) { try { System.out.println("Getter: " + new PropertyDescriptor("example", ReflTools.class).getReadMethod().getName()); System.out.println("Getter: " + new PropertyDescriptor("eXample", ReflTools.class).getReadMethod().getName()); } catch (IntrospectionException e) { e.printStackTrace(); } } } 

Guava CaseFormat将为您完成。

例如,来自lower_underscore – > LowerUnderscore

 CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, str) 
 String fieldToGetter(Field field) { final String name = field.getName(); final boolean isBoolean = (field.getType() == Boolean.class || field.getType() == boolean.class); return (isBoolean ? "is" : "get") + name.substring(0, 1).toUpperCase() + name.substring(1); } String fieldToGetter(boolean isBoolean, String name) { return (isBoolean ? "is" : "get") + name.substring(0, 1).toUpperCase() + name.substring(1); }