All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.undefinedlabs.scope.utils.sourcecode.SourceCodeUtilsResolver Maven / Gradle / Ivy

package com.undefinedlabs.scope.utils.sourcecode;

import com.undefinedlabs.scope.logger.ScopeLogger;
import com.undefinedlabs.scope.logger.ScopeLoggerResolver;
import com.undefinedlabs.scope.settings.ScopeSettings;
import com.undefinedlabs.scope.settings.ScopeSettingsResolver;
import com.undefinedlabs.scope.statistics.Statistics;
import com.undefinedlabs.scope.utils.ClassNameUtils;
import com.undefinedlabs.scope.utils.StringUtils;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

public enum SourceCodeUtilsResolver {
  INSTANCE;

  private static final ScopeLogger LOGGER = ScopeLoggerResolver.INSTANCE.get();
  private static final String JAVA_EXTENSION = "java";
  private static final String KOTLIN_EXTENSION = "kt";
  private static final String SCALA_EXTENSION = "scala";
  private static final String GROOVY_EXTENSION = "groovy";
  private static final ExtensionContainer EXTENSION_CONTAINER =
      new ExtensionContainer(JAVA_EXTENSION, KOTLIN_EXTENSION, SCALA_EXTENSION, GROOVY_EXTENSION);
  private static final SourceCodeClassResolver CLASS_RESOLVER = new SourceCodeClassResolver();
  private static final SourceCodeUtils SOURCE_CODE_UTILS;

  static {
    final String rootPath =
        (String) ScopeSettingsResolver.INSTANCE.get().getSetting(ScopeSettings.SCOPE_SOURCE_ROOT);
    SOURCE_CODE_UTILS =
        new SourceCodeUtils(populateMap(rootPath, EXTENSION_CONTAINER, CLASS_RESOLVER));
  }

  public SourceCodeUtils get() {
    return SOURCE_CODE_UTILS;
  }

  protected static Map populateMap(
      final String rootPath,
      final ExtensionContainer extensionContainer,
      final SourceCodeClassResolver classResolver) {
    final Map sourceCodeByClassName = new HashMap<>();
    final Map> conflictFilepathsByClassName = new HashMap<>();

    if (StringUtils.isEmpty(rootPath)) {
      LOGGER.debug("Could not be obtained SCOPE_SOURCE_ROOT.");
      return sourceCodeByClassName;
    }

    try {
      LOGGER.trace(">> SourceCodeUtilsResolver: populate Map");
      LOGGER.trace(">>> RootPath: " + rootPath);
      final File rootDir = new File(rootPath);
      final List files =
          (List) FileUtils.listFiles(rootDir, extensionContainer.getExtensions(), true);

      LOGGER.trace(">>> Found " + files.size() + " files.");
      for (final File file : files) {
        final String className = ClassNameUtils.INSTANCE.extractClassNameFromSource(file);
        LOGGER.trace(">>> >>> ClassName: " + className);

        try {
          if (className.endsWith("package-info") || className.endsWith("module-info")) {
            continue;
          }

          final String sourceFileName =
              ClassNameUtils.INSTANCE.extractSourceFileName(
                  className, FilenameUtils.getExtension(file.getName()));
          LOGGER.trace(">>> >>> SourceFileName: " + sourceFileName);
          final String sourceFilePath = file.getCanonicalPath();
          LOGGER.trace(">>> >>> SourceFilePath: " + sourceFilePath);

          final SourceCode.Builder sourceCodeBuilder =
              SourceCode.newBuilder(sourceCodeByClassName.get(className));
          sourceCodeBuilder.withClassName(className);
          sourceCodeBuilder.withSourceFileName(sourceFileName);
          sourceCodeBuilder.withSourceFilePath(sourceFilePath);

          final SourceCode sourceCode = sourceCodeBuilder.build();
          if (sourceCode.hasSourceCodeFilePath()
              && (sourceCode.getSourceFilePath().contains("target/checkout")
                  || sourceCode.getSourceFilePath().contains("target/munged"))) {
            continue;
          }

          final SourceCode storedSourceCode = sourceCodeByClassName.get(className);
          if (storedSourceCode == null) {
            sourceCodeByClassName.put(className, sourceCode);
          } else {
            sourceCodeByClassName.put(
                className, SourceCode.newBuilder(storedSourceCode).withHasConflicts(true).build());

            final Set conflicts =
                conflictFilepathsByClassName.get(className) != null
                    ? conflictFilepathsByClassName.get(className)
                    : new HashSet();
            conflicts.add(sourceCode.getSourceFilePath());
            conflicts.add(storedSourceCode.getSourceFilePath());
            conflictFilepathsByClassName.put(className, conflicts);
          }

        } catch (final Throwable e) {
          LOGGER.error("Error loading class " + className + ". Error: " + e);
        }
      }

    } catch (final Exception e) {
      LOGGER.error("Could not be populated SourceCodes map. Error: " + e);
    }

    Statistics.INSTANCE.registerSourceCodeConflict(conflictFilepathsByClassName);
    return sourceCodeByClassName;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy