1 源码解析
1.1 jdk解析
1.2 guava解析
1.3 spring解析
1.4 mybaties解析
1 源码解析
1.1 jdk解析
String
public StringBuilder append(boolean b) { super.append(b); return this; } public StringBuilder append(char c) { super.append(c); return this; } public StringBuilder append(int i) { super.append(i); return this; } public StringBuilder append(long lng) { super.append(lng); return this; } public StringBuilder append(float f) { super.append(f); return this; } public StringBuilder append(double d) { super.append(d); return this; }
StringBuilder
public synchronized StringBuffer append(Object obj) {
super.append(String.valueOf(obj));
return this; } public synchronized StringBuffer append(String str) { super.append(str); return this; }
1.2 guava解析(ImmutableSet)
copyOf方法
public staticImmutableSet copyOf(Collectionextends E> elements) { if (elements instanceof ImmutableSet && !(elements instanceof ImmutableSortedSet)) { ImmutableSet set = (ImmutableSet)elements; if (!set.isPartialView()) { return set; } } else if (elements instanceof EnumSet) { return copyOfEnumSet((EnumSet)elements); } Object[] array = elements.toArray(); return construct(array.length, array); }
Of方法
public staticImmutableSet of() { return RegularImmutableSet.EMPTY; } public static ImmutableSet of(E element) { return new SingletonImmutableSet(element); } public static ImmutableSet of(E e1, E e2) { return construct(2, e1, e2); } public static ImmutableSet of(E e1, E e2, E e3) { return construct(3, e1, e2, e3); } public static ImmutableSet of(E e1, E e2, E e3, E e4) { return construct(4, e1, e2, e3, e4); } public static ImmutableSet of(E e1, E e2, E e3, E e4, E e5) { return construct(5, e1, e2, e3, e4, e5); }
add方法(注意静态内部类)
@CanIgnoreReturnValue public ImmutableSet.Builderadd(E element) { super.add(element); return this; } public static class Builder extends ArrayBasedBuilder { public Builder() { this(4); } Builder(int capacity) { super(capacity); } @CanIgnoreReturnValue public ImmutableSet.Builderadd(E element) { super.add(element); return this; } @CanIgnoreReturnValue public ImmutableSet.Builder add(E... elements) { super.add(elements); return this; } @CanIgnoreReturnValue public ImmutableSet.Builder addAll(Iterableextends E> elements) { super.addAll(elements); return this; } @CanIgnoreReturnValue public ImmutableSet.Builder addAll(Iteratorextends E> elements) { super.addAll(elements); return this; } public ImmutableSet build() { ImmutableSet result = ImmutableSet.construct(this.size, this.contents); this.size = result.size(); return result; }
1.3 spring解析
BeanDefinitionBuilder类
public static BeanDefinitionBuilder genericBeanDefinition() { BeanDefinitionBuilder builder = new BeanDefinitionBuilder(); builder.beanDefinition = new GenericBeanDefinition(); return builder; }
1.4 mybaties解析
SqlSessionFactoryBuilder类(建造者模式中再使用建造者)
解析xml过程
public SqlSessionFactory build(Reader reader, String environment, Properties properties) { SqlSessionFactory var5; try { XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties); var5 = this.build(parser.parse()); } catch (Exception var14) { throw ExceptionFactory.wrapException("Error building SqlSession.", var14); } finally { ErrorContext.instance().reset(); try { reader.close(); } catch (IOException var13) { ; } } return var5; }
XMLConfigBuilder类
public Configuration parse() { if (parsed) { throw new BuilderException("Each XMLConfigBuilder can only be used once."); } parsed = true; parseConfiguration(parser.evalNode("/configuration")); return configuration; } private void parseConfiguration(XNode root) { try { Properties settings = settingsAsPropertiess(root.evalNode("settings")); //issue #117 read properties first propertiesElement(root.evalNode("properties")); loadCustomVfs(settings); typeAliasesElement(root.evalNode("typeAliases")); pluginElement(root.evalNode("plugins")); objectFactoryElement(root.evalNode("objectFactory")); objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); reflectorFactoryElement(root.evalNode("reflectorFactory")); settingsElement(settings); // read it after objectFactory and objectWrapperFactory issue #631 environmentsElement(root.evalNode("environments")); databaseIdProviderElement(root.evalNode("databaseIdProvider")); typeHandlerElement(root.evalNode("typeHandlers")); mapperElement(root.evalNode("mappers")); } catch (Exception e) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); } }