有没有标准库(例如org.apache.commons.beanutils或java.beans),可以将一个字符串字段名转换为标准方法名?我已经寻找了很久,但找不到一个简单的字符串转换实用程序。
JavaBean Introspector是可能最好的选择。它处理布尔类型的“is” getter以及带有一个参数的“getter”和没有或两个参数的setter以及其他边缘情况。它很适合获取类的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
来自于从 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);
}
Introspector
从字段名获取方法名的代码示例吗? - Matt BallisFlag
或getFlag
,这样内省器就可以为您解决。 - Peter Lawrey出现了一条野生的单行代码!
String fieldToGetter(String name)
{
return "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
}
isFoo()
命名约定来表示 boolean 类型字段的 getter 方法。 - Matt Ball您可以使用PropertyDescriptor
而不需要Inspector
(这是由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);
}
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);
}
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, str)
example
和eXample
,它们的getter方法是getExample
和geteExample
,但只有当它们是由Eclipse生成时才成立。然而,这与PropertyDescriptor("eXample",...).getReadMethod().getName()
不兼容,因为它期望的有效getter名称是getEXample
。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();
}
}
}