简体   繁体   中英

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

I have a WsField named an annotation.

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();
}

I use this WsField annotation in MyPojo class.

MyPojo.java

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


    public String getFullName()
    {
        return fullName;
    }

    public void setFullName(String fullName)
    {
        this.fullName = fullName;
    }
}

I want to set the value of the fields that have WsField annotations in the map method.

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);
    }
}

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

It works in the codes below.

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();
          }
      }   
  }
}

Notice how in one case you have

for (Field field : clsMeta.getFields()) 

and in the other you have

for (Field f : clsMeta.getDeclaredFields())

Your fullName field is private . Class#getFields() does not return private fields. From the javadoc

Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.

You'll have to use Class#getDeclaredFields() in your map method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM