Ejemplo n.º 1
0
  private ViolationDTO createViolationDTO(Violation violation)
      throws RuleInstantionException, LanguageNotFoundException, RuleTypeNotFoundException {
    try {
      RuleTypeDTO rule = createRuleTypeDTO(violation);
      ViolationTypeDTO violationtype = rule.getViolationTypes()[0];

      final String classPathFrom = violation.getClassPathFrom();
      final String classPathTo = violation.getClassPathTo();
      final String logicalModuleFromPath =
          violation.getLogicalModules().getLogicalModuleFrom().getLogicalModulePath();
      final String logicalModuleToPath =
          violation.getLogicalModules().getLogicalModuleTo().getLogicalModulePath();
      final String message = messagebuilder.createMessage(violation.getMessage());
      final int linenumber = violation.getLinenumber();

      if (violation.getSeverity() != null) {
        final Severity severity = violation.getSeverity();
        final Color color = severity.getColor();
        final String userDefinedName = severity.getUserName();
        final String systemDefinedName = severity.getDefaultName();
        final int severityValue = configuration.getSeverityValue(violation.getSeverity());

        return new ViolationDTO(
            classPathFrom,
            classPathTo,
            logicalModuleFromPath,
            logicalModuleToPath,
            violationtype,
            rule,
            message,
            linenumber,
            color,
            userDefinedName,
            systemDefinedName,
            severityValue);
      } else {
        return new ViolationDTO(
            classPathFrom,
            classPathTo,
            logicalModuleFromPath,
            logicalModuleToPath,
            violationtype,
            rule,
            message,
            linenumber,
            Color.BLACK,
            "",
            "",
            0);
      }
    } catch (ViolationTypeNotFoundException e) {
      throw new ViolationTypeNotFoundException();
    }
  }
  @Override
  public List<Violation> check(
      ConfigurationServiceImpl configuration, RuleDTO rootRule, RuleDTO currentRule) {
    violations.clear();
    fromMappings = getAllClasspathsOfModule(currentRule.moduleFrom, currentRule.violationTypeKeys);
    toMappings = getAllClasspathsOfModule(currentRule.moduleTo, currentRule.violationTypeKeys);

    // Create HashMap with all allowed to-classes (including the from-classes)
    HashMap<String, Mapping> fromMap = new HashMap<String, Mapping>();
    for (Mapping from : fromMappings) {
      fromMap.put(from.getPhysicalPath(), from);
    }
    for (Mapping to : toMappings) {
      fromMap.put(to.getPhysicalPath(), to);
    }

    // Create a HashMap with all allowed from-to combinations, based on the exception rules.
    HashSet<String> allExceptionFromTos = getAllExceptionFromTos(currentRule);

    for (Mapping classPathTo : toMappings) {
      // Get all dependencies with matching dependency.classPathTo
      DependencyDTO[] dependenciesTo =
          analyseService.getDependenciesFromTo("", classPathTo.getPhysicalPath());
      for (DependencyDTO dependency : dependenciesTo) {
        String fromToCombi = dependency.from + "|" + classPathTo.getPhysicalPath();
        if (fromMap.containsKey(dependency.from) || allExceptionFromTos.contains(fromToCombi)) {
          // Do nothing
        } else {
          Mapping classPathFrom = new Mapping(dependency.from, classPathTo.getViolationTypes());
          Violation violation =
              createViolation(rootRule, classPathFrom, classPathTo, dependency, configuration);

          // Get logicalModuleFrom based on dependency.from and add it to the violation
          ModuleDTO moduleFrom =
              ServiceProvider.getInstance()
                  .getDefineService()
                  .getModule_BasedOnSoftwareUnitName(dependency.from);
          if (moduleFrom != null) {
            // Add moduleFrom to violation.logicalModules, so that graphics can include these
            // violations in architecture diagrams
            LogicalModules logicalModulesOld = violation.getLogicalModules();
            LogicalModule logicalModuleTo = logicalModulesOld.getLogicalModuleTo();
            LogicalModule logicalModuleFrom =
                new LogicalModule(moduleFrom.logicalPath, moduleFrom.type);
            LogicalModules logicalModules = new LogicalModules(logicalModuleFrom, logicalModuleTo);
            violation.setLogicalModules(logicalModules);
          }

          violations.add(violation);
        }
      }
    }
    return violations;
  }