Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

Talk about ObjectWrapper of Reflection in Mybatis

The class MetaClass that gets the class attributes

MetaClass is primarily used to get class attributes, and its constructor encapsulates the class as Reflector through the ReflectorFactory factory

private MetaClass(Class<? > type, ReflectorFactory reflectorFactory) { this.reflectorFactory = reflectorFactory; this.reflector = reflectorFactory.findForClass(type); }Copy the code

The MetaClass findProperty() method is the method that obtains the property, and it also determines whether the property has a getter setter method:

public boolean hasSetter(String name) { PropertyTokenizer prop = new PropertyTokenizer(name); if (prop.hasNext()) { if (reflector.hasSetter(prop.getName())) { MetaClass metaProp = metaClassForProperty(prop.getName()); return metaProp.hasSetter(prop.getChildren()); } else { return false; } } else { return reflector.hasSetter(prop.getName()); } } public boolean hasGetter(String name) { PropertyTokenizer prop = new PropertyTokenizer(name); if (prop.hasNext()) { if (reflector.hasGetter(prop.getName())) { MetaClass metaProp = metaClassForProperty(prop); return metaProp.hasGetter(prop.getChildren()); } else { return false; } } else { return reflector.hasGetter(prop.getName()); }}Copy the code

We know from the source that these two methods rely primarily on PropertyTokenizer for parsing.

ObjectWrapper

ObjectWrapper is an interface that encapsulates object meta-information. CollectionWrapper implements the ObjectWrapper interface and implements the Add (),addAll() and isCollection() methods.

  private final Collection<Object> object;
  @Override
  public boolean isCollection() {
    return true;
  }

  @Override
  public void add(Object element) {
    object.add(element);
  }

  @Override
  public <E> void addAll(List<E> element) {
    object.addAll(element);
  }
Copy the code

The BaseWrapper abstract class implements the ObjectWrapper interface and implements the resolveCollection method: the function is to return the specified property as an object

protected Object resolveCollection(PropertyTokenizer prop, Object object) { if ("".equals(prop.getName())) { return object; } else {// return metaObject.getValue(prop.getName()); }}Copy the code

GetCollectionValue () and setCollectionValue() methods are also implemented

MetaObject is used here, and MetaObject is used to record metadata

BeanWrapper inherits BaseWrapper and implements the get set method of ObjectWrapper interface. Its main function is to get and set the corresponding attribute values.

@Override public Object get(PropertyTokenizer prop) { if (prop.getIndex() ! Object collection = resolveCollection(prop, Object); return getCollectionValue(prop, collection); } else { return getBeanProperty(prop, object); } } @Override public void set(PropertyTokenizer prop, Object value) { if (prop.getIndex() ! = null) { Object collection = resolveCollection(prop, object); setCollectionValue(prop, collection, value); } else { setBeanProperty(prop, object, value); }}Copy the code

MapWrapper also inherits BaseWrapper and differs from BeanWrapper in that it uses a map to store operations

conclusion

MetaClass, whose methods rely on PropertyTokenizer for parsing, and ObjectWrapper, an interface that encapsulates object meta-information. Its implementation classes include CollectionWrapper, BaseWrapper abstract class, BaseWrapper subclass BeanWrapper, MapWrapper, etc. BeanWrapper implements the get set method to get property values or set property values. MapWrapper mainly uses map structure to store object meta information.