首页

基于aether的依赖关系dependency定义相关节点及Exclusion包含、不包含关系的源码分析说明

标签:dependency,依赖关系,系统设计,解耦,代码优化,重构,sonatype,exclusion     发布时间:2017-12-06   

一、前言

基于aether-api包中定义的org.sonatype.aether.graph.Dependency依赖关系的实现原理 - 基于基础构件artifact的实现设计,将范围、包含Exclusion及不包含NO_EXCLUSIONS等依赖关系定义清楚,当然在aether-util包中包含了具体实现(org.sonatype.aether.util.graph.DefaultDependencyNode、org.sonatype.aether.util.graph、org.sonatype.aether.util.graph.FilteringDependencyVisitor/CloningDependencyVisitor、org.sonatype.aether.util.graph.DefaultDependencyNode等)

二、标准定义源码

1.标准依赖类org.sonatype.aether.graph.Dependency

package org.sonatype.aether.graph;@b@@b@import java.util.Arrays;@b@import java.util.Collection;@b@import java.util.Collections;@b@import java.util.HashSet;@b@import org.sonatype.aether.artifact.Artifact;@b@@b@public final class Dependency@b@{@b@  private static final Exclusion[] NO_EXCLUSIONS = new Exclusion[0];@b@  private final Artifact artifact;@b@  private final String scope;@b@  private final boolean optional;@b@  private final Exclusion[] exclusions;@b@@b@  public Dependency(Artifact artifact, String scope)@b@  {@b@    this(artifact, scope, false);@b@  }@b@@b@  public Dependency(Artifact artifact, String scope, boolean optional)@b@  {@b@    this(artifact, scope, optional, NO_EXCLUSIONS);@b@  }@b@@b@  public Dependency(Artifact artifact, String scope, boolean optional, Collection<Exclusion> exclusions)@b@  {@b@    this(artifact, scope, optional, toArray(exclusions));@b@  }@b@@b@  private static Exclusion[] toArray(Collection<Exclusion> exclusions)@b@  {@b@    if ((exclusions != null) && (!(exclusions.isEmpty())))@b@    {@b@      return ((Exclusion[])exclusions.toArray(new Exclusion[exclusions.size()]));@b@    }@b@    return NO_EXCLUSIONS;@b@  }@b@@b@  private Dependency(Artifact artifact, String scope, boolean optional, Exclusion[] exclusions)@b@  {@b@    if (artifact == null)@b@    {@b@      throw new IllegalArgumentException("no artifact specified for dependency");@b@    }@b@    this.artifact = artifact;@b@    this.scope = ((scope != null) ? scope : "");@b@    this.optional = optional;@b@    this.exclusions = exclusions;@b@  }@b@@b@  public Artifact getArtifact()@b@  {@b@    return this.artifact;@b@  }@b@@b@  public Dependency setArtifact(Artifact artifact)@b@  {@b@    if (this.artifact.equals(artifact))@b@    {@b@      return this;@b@    }@b@    return new Dependency(artifact, this.scope, this.optional, this.exclusions);@b@  }@b@@b@  public String getScope()@b@  {@b@    return this.scope;@b@  }@b@@b@  public Dependency setScope(String scope)@b@  {@b@    if ((this.scope.equals(scope)) || ((scope == null) && (this.scope.length() <= 0)))@b@    {@b@      return this;@b@    }@b@    return new Dependency(this.artifact, scope, this.optional, this.exclusions);@b@  }@b@@b@  public boolean isOptional()@b@  {@b@    return this.optional;@b@  }@b@@b@  public Dependency setOptional(boolean optional)@b@  {@b@    if (this.optional == optional)@b@    {@b@      return this;@b@    }@b@    return new Dependency(this.artifact, this.scope, optional, this.exclusions);@b@  }@b@@b@  public Collection<Exclusion> getExclusions()@b@  {@b@    return Collections.unmodifiableCollection(Arrays.asList(this.exclusions));@b@  }@b@@b@  public Dependency setExclusions(Collection<Exclusion> exclusions)@b@  {@b@    if ((getExclusions().equals(exclusions)) || ((exclusions == null) && (this.exclusions.length <= 0)))@b@    {@b@      return this;@b@    }@b@    return new Dependency(this.artifact, this.scope, this.optional, exclusions);@b@  }@b@@b@  public String toString()@b@  {@b@    return String.valueOf(getArtifact()) + " (" + getScope() + ((isOptional()) ? "?" : "") + ")";@b@  }@b@@b@  public boolean equals(Object obj)@b@  {@b@    if (obj == this)@b@    {@b@      return true;@b@    }@b@    if ((obj == null) || (!(super.getClass().equals(obj.getClass()))))@b@    {@b@      return false;@b@    }@b@@b@    Dependency that = (Dependency)obj;@b@@b@    return ((this.artifact.equals(that.artifact)) && (this.scope.equals(that.scope)) && (this.optional == that.optional) && (new HashSet(Arrays.asList(this.exclusions)).equals(new HashSet(Arrays.asList(that.exclusions)))));@b@  }@b@@b@  public int hashCode()@b@  {@b@    int hash = 17;@b@    hash = hash * 31 + this.artifact.hashCode();@b@    hash = hash * 31 + this.scope.hashCode();@b@    hash = hash * 31 + ((this.optional) ? 1 : 0);@b@    hash = hash * 31 + this.exclusions.length;@b@    return hash;@b@  }@b@}

2.org.sonatype.aether.graph.Exclusion包含不包含关系

package org.sonatype.aether.graph;@b@@b@public final class Exclusion@b@{@b@  private final String groupId;@b@  private final String artifactId;@b@  private final String classifier;@b@  private final String extension;@b@@b@  public Exclusion(String groupId, String artifactId, String classifier, String extension)@b@  {@b@    this.groupId = ((groupId != null) ? groupId : "");@b@    this.artifactId = ((artifactId != null) ? artifactId : "");@b@    this.classifier = ((classifier != null) ? classifier : "");@b@    this.extension = ((extension != null) ? extension : "");@b@  }@b@@b@  public String getGroupId()@b@  {@b@    return this.groupId;@b@  }@b@@b@  public String getArtifactId()@b@  {@b@    return this.artifactId;@b@  }@b@@b@  public String getClassifier()@b@  {@b@    return this.classifier;@b@  }@b@@b@  public String getExtension()@b@  {@b@    return this.extension;@b@  }@b@@b@  public String toString()@b@  {@b@    return getGroupId() + ':' + getArtifactId() + ':' + getExtension() + ((getClassifier().length() > 0) ? ':' + getClassifier() : "");@b@  }@b@@b@  public boolean equals(Object obj)@b@  {@b@    if (obj == this)@b@    {@b@      return true;@b@    }@b@    if ((obj == null) || (!(super.getClass().equals(obj.getClass()))))@b@    {@b@      return false;@b@    }@b@@b@    Exclusion that = (Exclusion)obj;@b@@b@    return ((this.artifactId.equals(that.artifactId)) && (this.groupId.equals(that.groupId)) && (this.extension.equals(that.extension)) && (this.classifier.equals(that.classifier)));@b@  }@b@@b@  public int hashCode()@b@  {@b@    int hash = 17;@b@    hash = hash * 31 + this.artifactId.hashCode();@b@    hash = hash * 31 + this.groupId.hashCode();@b@    hash = hash * 31 + this.classifier.hashCode();@b@    hash = hash * 31 + this.extension.hashCode();@b@    return hash;@b@  }@b@}