簡體   English   中英

如何傳遞MyPojo對象作為對map方法的引用?

[英]How can I pass the MyPojo object as reference to the map method?

我有一個名為注解的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 Class#getFields()不返回private字段。 從javadoc

返回一個包含Field對象的數組,該對象反映此Class對象表示的類或接口的所有可訪問公共字段

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM