`

AttributeSource内容简答分析

 
阅读更多

一、AttributeImpl通过AttributeSource得到自己的属性和对应的值

//静态的final方法得到一个WeakIdentityMap<k,v> k=impl v=list<attribute>
private static final WeakIdentityMap<Class<? extends AttributeImpl>,LinkedList<WeakReference<Class<? extends Attribute>>>> knownImplClasses =
    WeakIdentityMap.newConcurrentHashMap(false);
  //得到Impl继承的接口list<attribute>
  static LinkedList<WeakReference<Class<? extends Attribute>>> getAttributeInterfaces(final Class<? extends AttributeImpl> clazz) {
//首先从WeakIdentityMap中得到
//如果是空值自己主动处理
//但还为什么明明第一次就是空值,是为了防止多次加载吗?
    LinkedList<WeakReference<Class<? extends Attribute>>> foundInterfaces = knownImplClasses.get(clazz);
    if (foundInterfaces == null) {
    	System.out.println("时空的自己加载的");
   //先实例化一个
      foundInterfaces = new LinkedList<WeakReference<Class<? extends Attribute>>>();
      //得到impl的所有接口遍历
   //如果接口满足不是Attribute 但是又是Attribute的子接口list就添加进去
   //然后impl=impl的一个超类,接着重复遍历
   //最后还是添加到了WeakIdentityMap中
      Class<?> actClazz = clazz;
      do {
        for (Class<?> curInterface : actClazz.getInterfaces()) {
          if (curInterface != Attribute.class && Attribute.class.isAssignableFrom(curInterface)) {
            foundInterfaces.add(new WeakReference<Class<? extends Attribute>>(curInterface.asSubclass(Attribute.class)));
          }
        }
        actClazz = actClazz.getSuperclass();
      } while (actClazz != null);
      knownImplClasses.put(clazz, foundInterfaces);
    }
    return foundInterfaces;
  } 

 二、AttributeSource的工厂是做什么的?

//Attribute的工厂 只有一个方法通过attriubte创建一个Impl的实例
//Attribute工厂的作用是什么? 通过Attribute得到AttributeImpl
  public static abstract class AttributeFactory {
//工厂的抽象方法 放回一个AttributeImpl参数是Attribute
    public abstract AttributeImpl createAttributeInstance(Class<? extends Attribute> attClass);
    //默认里面实现了一个工厂 我把他放到下面了单独看下
    public static final AttributeFactory DEFAULT_ATTRIBUTE_FACTORY = new DefaultAttributeFactory();
     }
//===============================================
 //默认工厂的实现
    private static final class DefaultAttributeFactory extends AttributeFactory {
//此处的map
      private static final WeakIdentityMap<Class<? extends Attribute>, WeakReference<Class<? extends AttributeImpl>>> attClassImplMap =
        WeakIdentityMap.newConcurrentHashMap(false);
//空的构造方法
      DefaultAttributeFactory() {} 
//要实现的方法
      @Override
      public AttributeImpl createAttributeInstance(Class<? extends Attribute> attClass) {
        try {
      //通过这个方法返回它的impl实例
          return getClassForInterface(attClass).newInstance();
        } catch (InstantiationException e) {
          throw new IllegalArgumentException("Could not instantiate implementing class for " + attClass.getName());
        } catch (IllegalAccessException e) {
          throw new IllegalArgumentException("Could not instantiate implementing class for " + attClass.getName());
        }
      }
      
//如何通过attribute得到impl
      private static Class<? extends AttributeImpl> getClassForInterface(Class<? extends Attribute> attClass) {
//attClassImplMap是上面从WeakIdentityMap实例化的一个
        final WeakReference<Class<? extends AttributeImpl>> ref = attClassImplMap.get(attClass);
        Class<? extends AttributeImpl> clazz = (ref == null) ? null : ref.get();
//如果是空值 自行得到
        if (clazz == null) {
          // we have the slight chance that another thread may do the same, but who cares?
          try {
            attClassImplMap.put(attClass,
              new WeakReference<Class<? extends AttributeImpl>>(
//试图得到impl
                clazz = Class.forName(attClass.getName() + "Impl", true, attClass.getClassLoader())
                .asSubclass(AttributeImpl.class)
              )
            );
          } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("Could not find implementing class for " + attClass.getName());
          }
        }
        return clazz;
      }
    }

 

 

三、Attribute的四个个内部属性

 //AttributeSource的四个属性
//Attribute----------Impl
private final Map<Class<? extends Attribute>, AttributeImpl> attributes;

//Impl---------------impl
 private final Map<Class<? extends AttributeImpl>, AttributeImpl> attributeImpls;

//???
 private final State[] currentState;

//内部的impl工厂
 private final AttributeFactory factory;

 

四、AttributeSource的AddAttriubte和AddAttributeImp两个方法

 public final void addAttributeImpl(final AttributeImpl att) {
    final Class<? extends AttributeImpl> clazz = att.getClass();
//属性包含直接返回了
    if (attributeImpls.containsKey(clazz)){          
    	return;
    }
//得到所有接口
    final LinkedList<WeakReference<Class<? extends Attribute>>> foundInterfaces =
      getAttributeInterfaces(clazz);
    
    // add all interfaces of this AttributeImpl to the maps
//遍历接口
    for (WeakReference<Class<? extends Attribute>> curInterfaceRef : foundInterfaces) {
//这个get是WeakReference的
      final Class<? extends Attribute> curInterface = curInterfaceRef.get();
      assert (curInterface != null) :
        "We have a strong reference on the class holding the interfaces, so they should never get evicted";
      // Attribute is a superclass of this interface
      if (!attributes.containsKey(curInterface)) {
        // invalidate state to force recomputation in captureState()
//填充属性了
        this.currentState[0] = null;
        attributes.put(curInterface, att);
        attributeImpls.put(clazz, att);
      }
    }
  }
  
//添加Attribute还是最终添加的是AttributeImpl因为impl是实现
  public final <A extends Attribute> A addAttribute(Class<A> attClass) {
                                       //attributes是source的一个属性
    AttributeImpl attImpl = attributes.get(attClass);
    if (attImpl == null) {
      if (!(attClass.isInterface() && Attribute.class.isAssignableFrom(attClass))) {
        throw new IllegalArgumentException(
          "addAttribute() only accepts an interface that extends Attribute, but " +
          attClass.getName() + " does not fulfil this contract."
        );
      }
    //通过工厂得到impl
      addAttributeImpl(attImpl = this.factory.createAttributeInstance(attClass));
    }
    return attClass.cast(attImpl);
  }

 五、State 是干嘛的?

public static final class State implements Cloneable {
    AttributeImpl attribute;
    State next;
    
    @Override
    public State clone() {
      State clone = new State();
      clone.attribute = attribute.clone();
      
      if (next != null) {
        clone.next = next.clone();
      }
      return clone;
    }
  }
//========================================
 public final String reflectAsString(final boolean prependAttClass) {
    final StringBuilder buffer = new StringBuilder();
    reflectWith(new AttributeReflector() {
      @Override
      public void reflect(Class<? extends Attribute> attClass, String key, Object value) {
        if (buffer.length() > 0) {
          buffer.append(',');
        }
        if (prependAttClass) {
          buffer.append(attClass.getName()).append('#');
        }
        buffer.append(key).append('=').append((value == null) ? "null" : value);
      }
    });
    return buffer.toString();
  }
  
  
  public final void reflectWith(AttributeReflector reflector) {
    for (State state = getCurrentState(); state != null; state = state.next) {
      state.attribute.reflectWith(reflector);
    }
  }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics