一、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); } }
相关推荐
单片机考试选择填空简答分析题总结.doc
体育教师招聘简答分析论述.docx
自整理财务分析简答题.doc
2020—2021学年下学期电子信息工程专业《电子技术基础》期末考试题试卷(简答分析题G).pdf
小学教育知识与能力第三章练习题(单选、简答、材料分析).doc
油气集输期末考试复习资料。参考书:《油气集输与矿场加工》,适用于石油院校储运专业期末考试复习,包含了简答、分析题
油气集输期末考试复习资料。参考书:《油气集输与矿场加工》,适用于石油院校储运专业期末考试复习,包含了简答、分析题。
油气集输期末考试复习资料。参考书:《油气集输与矿场加工》,适用于石油院校储运专业期末考试复习,包含了简答、分析题。
元明清文学复习大全(填空、简答、分析论述).doc
2020—2021学年下学期电气工程专业《电子技术基础》期末考试题试卷(简答分析题B).pdf
2020—2021学年下学期光电信息工程专业《电子技术基础》期末考试题试卷(简答分析题E).pdf
软件工程简答题软件工程简答题软件工程简答题软件工程简答题
python简答题复习题库1
外科名解简答病例分析.doc
历年试题简答题答案,很有用的考试资料,开卷必备
企业战略管理考试题目简答题论述题案例分析.pdf
化工原理简答题答案
BIM案例分析简答.docx
仪器分析简答题汇总
自动控制原理简答题。名词解释,简答题答案