Example #1
0
  /**
   * Answer the canonical name that should be used in place of the fully-qualified {@linkplain
   * ModuleName module name}.
   *
   * @param qualifiedName A fully-qualified {@linkplain ModuleName module name}.
   * @return The canonical name that should be used in place of the fully-qualified {@linkplain
   *     ModuleName module name}.
   */
  ModuleName canonicalNameFor(final ModuleName qualifiedName) {
    final String substitute = renames.get(qualifiedName.qualifiedName());
    if (substitute != null) {
      return new ModuleName(substitute);
    }

    return qualifiedName;
  }
  public static void main(String[] args) throws ParseException, IOException {
    Options options = createOptions();
    CommandLineParser parser = new GnuParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption("?")) {
      new HelpFormatter().printHelp("java " + ShowMoves.class.getName(), options);
      System.exit(1);
    }
    File rootFile = new File(cmd.getOptionValue("root"));
    File movesFile = new File(cmd.getOptionValue("moves"));
    Predicate<ClassName> packageFilter = new PackagePredicate(cmd.getOptionValue("package", ""));
    String groupPrefix = cmd.getOptionValue("group", "");
    File refsFile = cmd.hasOption("refs") ? new File(cmd.getOptionValue("refs")) : null;

    // scan all the pom.xml files
    Modules modules = new Modules();
    modules.scan(rootFile, groupPrefix);

    // load the moves and refs files
    ClassLocations moves = ClassLocations.parseFile(movesFile, groupPrefix);
    ClassLocations refs =
        (refsFile != null) ? ClassLocations.parseFile(refsFile, groupPrefix) : null;

    // scan the compiled classes of all the maven targets
    ClassScanner classScanner = new ClassScanner(packageFilter);
    classScanner.scan(modules.getAllModules());
    ClassLocations locations = classScanner.getLocations();
    ClassDependencies dependencies = classScanner.getDependencies();

    // apply the moves file
    locations.moveAll(moves);

    // apply the refs file, if one was specified
    if (refs != null) {
      for (ModuleName moduleName : refs.getAllModules()) {
        ClassName refsName = new ClassName(moduleName + ":" + refsFile);
        locations.add(refsName, moduleName);
        dependencies.add(refsName, refs.getClasses(moduleName));
      }
    }

    // find modules that reference classes they don't have access to
    Map<ModuleName, ListMultimap<ClassName, ClassName>> brokenMap = Maps.newHashMap();
    for (Map.Entry<ClassName, ModuleName> entry : locations.getLocations()) {
      ClassName className = entry.getKey();
      ModuleName moduleName = entry.getValue();
      Set<ClassName> referencedClasses = dependencies.getReferencedClasses(className);

      ListMultimap<ClassName, ClassName> moduleBrokenMap = null;
      for (ClassName referencedClass : referencedClasses) {
        ModuleName referencedModule = locations.getModule(referencedClass);
        if (referencedModule != null && !modules.isDependentOf(moduleName, referencedModule)) {
          if (moduleBrokenMap == null) {
            moduleBrokenMap = brokenMap.get(moduleName);
            if (moduleBrokenMap == null) {
              brokenMap.put(moduleName, moduleBrokenMap = ArrayListMultimap.create());
            }
          }
          moduleBrokenMap.put(className, referencedClass);
        }
      }
    }

    // report broken dependencies
    System.out.println();
    for (ModuleName moduleName : Utils.sorted(brokenMap.keySet())) {
      ListMultimap<ClassName, ClassName> missingMap = brokenMap.get(moduleName);

      System.out.println();
      System.out.println(moduleName.toString(groupPrefix));

      for (ClassName className : Utils.sorted(missingMap.keySet())) {
        System.out.println("  " + className);
        for (ClassName referencedClass : Utils.sorted(missingMap.get(className))) {
          ModuleName referencedModule = locations.getModule(referencedClass);
          System.out.println(
              "    " + referencedClass + " (" + referencedModule.toString(groupPrefix) + ")");
        }
      }
    }
  }
Example #3
0
 public Object getDynamic(String token, StaplerRequest req, StaplerResponse rsp) {
   if (ModuleName.isValid(token)) return getModule(token);
   return super.getDynamic(token, req, rsp);
 }
Example #4
0
 public MavenModule getItem(String name) {
   return modules.get(ModuleName.fromString(name));
 }
Example #5
0
  /**
   * Actually resolve the qualified module name. This is @{@link InnerAccess} to ensure clients
   * always go through the cache.
   *
   * @param qualifiedName The qualified name of the module.
   * @return A {@link ModuleNameResolutionResult} indicating the result of the attempted resolution.
   */
  @InnerAccess
  ModuleNameResolutionResult privateResolve(final ModuleName qualifiedName) {
    IndexedRepositoryManager repository = null;
    File sourceFile = null;

    // Attempt to look up the fully-qualified name in the map of renaming
    // rules. Apply the rule if it exists.
    ModuleName canonicalName = canonicalNameFor(qualifiedName);

    // If the root cannot be resolved, then neither can the
    // module.
    final String enclosingRoot = canonicalName.rootName();
    ModuleRoot root = moduleRoots.moduleRootFor(enclosingRoot);
    if (root == null) {
      return new ModuleNameResolutionResult(
          new UnresolvedRootException(null, qualifiedName.localName(), enclosingRoot));
    }

    // Splitting the module group into its components.
    final ArrayList<ModuleName> checkedPaths = new ArrayList<ModuleName>();

    final String[] components = canonicalName.packageName().split("/");
    assert components.length > 1;
    assert components[0].isEmpty();

    final Deque<String> nameStack = new LinkedList<>();
    nameStack.addLast("/" + enclosingRoot);
    Deque<File> pathStack = null;

    // If the source directory is available, then build a search stack of
    // trials at ascending tiers of enclosing packages.
    File sourceDirectory = root.sourceDirectory();
    if (sourceDirectory != null) {
      pathStack = new LinkedList<File>();
      pathStack.addLast(sourceDirectory);
    }
    for (int index = 2; index < components.length; index++) {
      assert !components[index].isEmpty();
      nameStack.addLast(String.format("%s/%s", nameStack.peekLast(), components[index]));
      if (sourceDirectory != null) {
        assert pathStack != null;
        pathStack.addLast(new File(pathStack.peekLast(), components[index] + availExtension));
      }
    }

    // If the source directory is available, then search the file system.
    if (sourceDirectory != null) {
      assert pathStack != null;
      assert !pathStack.isEmpty();
      // Explore the search stack from most enclosing package to least
      // enclosing.
      while (!pathStack.isEmpty()) {
        canonicalName = new ModuleName(nameStack.removeLast(), canonicalName.localName());
        checkedPaths.add(canonicalName);
        final File trial =
            new File(filenameFor(pathStack.removeLast().getPath(), canonicalName.localName()));
        if (trial.exists()) {
          repository = root.repository();
          sourceFile = trial;
          break;
        }
      }
    }

    // If resolution failed, then one final option is available: search the
    // other roots.
    if (repository == null) {
      for (final String rootName : moduleRoots.rootNames()) {
        if (!rootName.equals(enclosingRoot)) {
          canonicalName =
              new ModuleName(String.format("/%s/%s", rootName, canonicalName.localName()));
          checkedPaths.add(canonicalName);
          root = moduleRoots.moduleRootFor(rootName);
          assert root != null;
          sourceDirectory = root.sourceDirectory();
          if (sourceDirectory != null) {
            final File trial =
                new File(sourceDirectory, canonicalName.localName() + availExtension);
            if (trial.exists()) {
              repository = root.repository();
              sourceFile = trial;
              break;
            }
          }
        }
      }
    }

    // We found a candidate.
    if (repository != null) {
      assert repository != null;
      if (sourceFile != null) {
        // If the candidate is a package, then substitute
        // the package representative.
        if (sourceFile.isDirectory()) {
          sourceFile = new File(sourceFile, canonicalName.localName() + availExtension);
          canonicalName = new ModuleName(canonicalName.qualifiedName(), canonicalName.localName());
          if (!sourceFile.isFile()) {
            // Alas, the package representative did not exist.
            return new ModuleNameResolutionResult(
                new UnresolvedModuleException(null, qualifiedName.localName(), checkedPaths));
          }
        }
      }
      return new ModuleNameResolutionResult(new ResolvedModuleName(canonicalName, root));
    }

    // Resolution failed.
    return new ModuleNameResolutionResult(
        new UnresolvedModuleException(null, qualifiedName.localName(), checkedPaths));
  }