Java 如何将MyPojo对象作为引用传递给map方法?

Java 如何将MyPojo对象作为引用传递给map方法?,java,reflection,methods,mapping,Java,Reflection,Methods,Mapping,我有一个名为注释的WsField WsField.java import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang

我有一个名为注释的WsField

WsField.java

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Target(ElementType.FIELD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface WsField 
{
    String fieldName();
}
我在MyPojo类中使用这个WsField注释

MyPojo.java

public class MyPojo
{
    @WsField(fieldName="Column1")
    private String fullName;


    public String getFullName()
    {
        return fullName;
    }

    public void setFullName(String fullName)
    {
        this.fullName = fullName;
    }
}
我想在map方法中设置具有WsField注释的字段的值

WsMapper.java

public class WsMapper
{
    public static void map(Object instance,String attributeName, Object value) 
    {
        Class clsMeta = instance.getClass();
        for (Field field : clsMeta.getFields()) 
        {
            if (field.isAnnotationPresent(WsField.class)) 
            {
                field.setAccessible(true);
                String fieldName = field.getAnnotation(WsField.class).fieldName();
                if (fieldName.contains(attributeName)) 
                {
                   try 
                   {
                     field.set(instance, value);
                   } catch (IllegalAccessException e) 
                   {
                     e.printStackTrace();

                   } catch (IllegalArgumentException e) 
                   {
                     e.printStackTrace();
                   }
                }
            }
        }
    }
}
Application.java

import java.lang.reflect.Field;
public class Application
{
    public static void main(String[] args)
    {    
        MyPojo obj = new MyPojo();
        WsMapper.map(obj,"Column1", "Test");
        String fullName = obj.getFullName();
        System.out.println(fullName);
    }
}
如何将MyPojo对象作为引用传递给map方法

它在下面的代码中工作

MyPojo obj2 = new MyPojo();
Class  clsMeta = obj2.getClass();
String fieldName = ""; 
for (Field f : clsMeta.getDeclaredFields())
{
  if (f.isAnnotationPresent(WsField.class))
  {
      f.setAccessible(true);
      fieldName = f.getAnnotation(WsField.class).fieldName();
      if (fieldName.contains("Column1"))
      {
          try 
          {
            f.set(obj, "Test");
          } catch (IllegalAccessException e) 
          {
            e.printStackTrace();
          } catch (IllegalArgumentException e) 
          {
            e.printStackTrace();
          }
      }   
  }
}

注意,在一个案例中,您是如何

for (Field field : clsMeta.getFields()) 
在另一个你有

for (Field f : clsMeta.getDeclaredFields())
您的
fullName
字段是
private
。不返回
private
字段。来自javadoc

返回一个数组,该数组包含反映所有 此字段表示的类或接口的可访问公共字段 类对象


您必须在
map
方法中使用
Class#getDeclaredFields()

我不明白。有什么问题吗?