Tabnine Logo
FilePredicates.hasRelativePath
Code IndexAdd Tabnine to your IDE (free)

How to use
hasRelativePath
method
in
org.sonar.api.batch.fs.FilePredicates

Best Java code snippets using org.sonar.api.batch.fs.FilePredicates.hasRelativePath (Showing top 20 results out of 315)

origin: SonarSource/sonarqube

@Test
public void has_relative_path() {
 assertThat(predicates.hasRelativePath("src/main/java/struts/Action.java").apply(javaFile)).isTrue();
 assertThat(predicates.hasRelativePath("src/main/java/struts/Other.java").apply(javaFile)).isFalse();
 // path is normalized
 assertThat(predicates.hasRelativePath("src/main/java/../java/struts/Action.java").apply(javaFile)).isTrue();
 assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\Action.java").apply(javaFile)).isTrue();
 assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\Other.java").apply(javaFile)).isFalse();
 assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\..\\struts\\Action.java").apply(javaFile)).isTrue();
}
origin: SonarSource/sonarqube

@Test
public void input_file_returns_null_if_file_not_found() {
 assertThat(fs.inputFile(fs.predicates().hasRelativePath("src/Bar.java"))).isNull();
 assertThat(fs.inputFile(fs.predicates().hasLanguage("cobol"))).isNull();
}
origin: SonarSource/sonarqube

@Test
public void files() {
 assertThat(fs.inputFiles(fs.predicates().all())).isEmpty();
 fs.add(new TestInputFileBuilder("foo", "src/Foo.php").setLanguage("php").build());
 fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build());
 fs.add(new TestInputFileBuilder("foo", "src/Baz.java").setLanguage("java").build());
 // no language
 fs.add(new TestInputFileBuilder("foo", "src/readme.txt").build());
 assertThat(fs.inputFile(fs.predicates().hasRelativePath("src/Bar.java"))).isNotNull();
 assertThat(fs.inputFile(fs.predicates().hasRelativePath("does/not/exist"))).isNull();
 assertThat(fs.inputFile(fs.predicates().hasAbsolutePath(new File(basedir, "src/Bar.java").getAbsolutePath()))).isNotNull();
 assertThat(fs.inputFile(fs.predicates().hasAbsolutePath(new File(basedir, "does/not/exist").getAbsolutePath()))).isNull();
 assertThat(fs.inputFile(fs.predicates().hasAbsolutePath(new File(basedir, "../src/Bar.java").getAbsolutePath()))).isNull();
 assertThat(fs.inputFile(fs.predicates().hasURI(new File(basedir, "src/Bar.java").toURI()))).isNotNull();
 assertThat(fs.inputFile(fs.predicates().hasURI(new File(basedir, "does/not/exist").toURI()))).isNull();
 assertThat(fs.inputFile(fs.predicates().hasURI(new File(basedir, "../src/Bar.java").toURI()))).isNull();
 assertThat(fs.files(fs.predicates().all())).hasSize(4);
 assertThat(fs.files(fs.predicates().hasLanguage("java"))).hasSize(2);
 assertThat(fs.files(fs.predicates().hasLanguage("cobol"))).isEmpty();
 assertThat(fs.hasFiles(fs.predicates().all())).isTrue();
 assertThat(fs.hasFiles(fs.predicates().hasLanguage("java"))).isTrue();
 assertThat(fs.hasFiles(fs.predicates().hasLanguage("cobol"))).isFalse();
 assertThat(fs.inputFiles(fs.predicates().all())).hasSize(4);
 assertThat(fs.inputFiles(fs.predicates().hasLanguage("php"))).hasSize(1);
 assertThat(fs.inputFiles(fs.predicates().hasLanguage("java"))).hasSize(2);
 assertThat(fs.inputFiles(fs.predicates().hasLanguage("cobol"))).isEmpty();
 assertThat(fs.languages()).containsOnly("java", "php");
}
origin: org.sonarsource.sonarqube/sonar-batch

@CheckForNull
private InputFile getInputFile(String filePath) {
 return fs.inputFile(fs.predicates().hasRelativePath(filePath));
}
origin: fr.sii.sonar/sonar-report-core

private static FilePredicate searchFilePredicate(FileSystem fileSystem, String path) {
  FilePredicates predicatesFactory = fileSystem.predicates();
  Collection<FilePredicate> searchPredicates = new ArrayList<FilePredicate>();
  // try to find the file directly using the provided path (absolute or
  // relative)
  searchPredicates.add(predicatesFactory.hasPath(path));
  // if not found, maybe the path starts with '/'
  // in this case, Sonar thinks it's an absolute path => manually try
  // relative
  if (path.startsWith("/")) {
    searchPredicates.add(predicatesFactory.hasRelativePath(path.substring(1)));
  }
  // if not found, try to search it everywhere
  searchPredicates.add(predicatesFactory.matchesPathPattern("**" + path));
  return predicatesFactory.or(searchPredicates);
}
origin: org.codehaus.sonar.plugins/sonar-xoo-plugin

private void processLine(File coverPerTest, int lineNumber, SensorContext context, String line, InputFile file) {
 try {
  Iterator<String> split = Splitter.on(":").split(line).iterator();
  String otherFileRelativePath = split.next();
  FileSystem fs = context.fileSystem();
  InputFile otherFile = fs.inputFile(fs.predicates().hasRelativePath(otherFileRelativePath));
  if (otherFile == null) {
   throw new IllegalStateException("Unable to find file " + otherFileRelativePath);
  }
  int weight = Integer.parseInt(split.next());
  context.newDependency()
   .from(file)
   .to(otherFile)
   .weight(weight)
   .save();
 } catch (Exception e) {
  throw new IllegalStateException("Error processing line " + lineNumber + " of file " + coverPerTest.getAbsolutePath(), e);
 }
}
origin: SonarSource/sonar-go

/**
 *  It is possible that absolutePath references a file that does not exist in the file system.
 *  It happens when go tests where executed on a different computer.
 *  Even when absolute path does not match a file of the project, this method try to find a valid
 *  mach using a shorter relative path.
 *  @see <a href="https://github.com/SonarSource/sonar-go/issues/218">sonar-go/issues/218</a>
 */
private static InputFile findInputFile(String absolutePath, FileSystem fileSystem) {
 FilePredicates predicates = fileSystem.predicates();
 InputFile inputFile = fileSystem.inputFile(predicates.hasAbsolutePath(absolutePath));
 if (inputFile != null) {
  return inputFile;
 }
 LOG.debug("Resolving file {} using relative path", absolutePath);
 Path path = Paths.get(absolutePath);
 inputFile = fileSystem.inputFile(predicates.hasRelativePath(path.toString()));
 while (inputFile == null && path.getNameCount() > 1) {
  path = path.subpath(1, path.getNameCount());
  inputFile = fileSystem.inputFile(predicates.hasRelativePath(path.toString()));
 }
 return inputFile;
}
origin: eu.rssw.openedge.checks/openedge-checks

 private InputFile getSourceFile(InputFile file, Element refElement) {
  Element parentNode = (Element) refElement.getParentNode();
  String fileNum = getChildNodeValue(refElement, "File-num");
  if ("1".equals(fileNum)) {
   return file;
  } else {
   return getContext().fileSystem().inputFile(
     getContext().fileSystem().predicates().hasRelativePath(parentNode.getAttribute("File-name")));
  }
 }
}
origin: Riverside-Software/sonar-openedge

 private InputFile getSourceFile(InputFile file, Element refElement) {
  Element parentNode = (Element) refElement.getParentNode();
  String fileNum = getChildNodeValue(refElement, "File-num");
  if ("1".equals(fileNum)) {
   return file;
  } else {
   return getContext().fileSystem().inputFile(
     getContext().fileSystem().predicates().hasRelativePath(parentNode.getAttribute("File-name")));
  }
 }
}
origin: spotbugs/sonar-findbugs

/**
 * Look in the potential source directories to find the given source file
 * @param fileName Path to the source file (/package/MyClass.java)
 * @param fs File system
 * @return The InputFile instance to the source file
 */
private InputFile buildInputFile(String fileName,FileSystem fs) {
  for(String sourceDir : SOURCE_DIRECTORIES) { //Quick lookup to skip manual iteration (see next loop)
    //System.out.println("Source file tested : "+sourceDir+"/"+fileName);
    Iterable<InputFile> files = fs.inputFiles(fs.predicates().hasRelativePath(sourceDir+"/"+fileName));
    for (InputFile f : files) {
      return f;
    }
  }
  //Search for path _ending_ with the filename (https://github.com/SonarQubeCommunity/sonar-findbugs/issues/51)
  for (InputFile f : fs.inputFiles(fs.predicates().hasType(InputFile.Type.MAIN))) {
    if (f.relativePath().endsWith(fileName)) {
      return f;
    }
  }
  return null;
}
origin: Backelite/sonar-swift

private void save(Collection<SourceCode> squidSourceFiles) {
  for (SourceCode squidSourceFile : squidSourceFiles) {
    SourceFile squidFile = (SourceFile) squidSourceFile;
    String relativePath = pathResolver.relativePath(context.fileSystem().baseDir(), new File(squidFile.getKey()));
    InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasRelativePath(relativePath));
    saveMeasures(inputFile, squidFile);
    saveIssues(inputFile, squidFile);
  }
}
origin: SonarSource/SonarJS

private static InputFile getInputFile(SensorContext context, String fileName) {
 FilePredicates predicates = context.fileSystem().predicates();
 InputFile inputFile = context.fileSystem().inputFile(predicates.or(predicates.hasRelativePath(fileName), predicates.hasAbsolutePath(fileName)));
 if (inputFile == null) {
  LOG.warn("No input file found for {}. No {} issues will be imported on this file.", fileName, LINTER_NAME);
  return null;
 }
 return inputFile;
}
origin: Backelite/sonar-swift

private void save(Collection<SourceCode> squidSourceFiles) {
  for (SourceCode squidSourceFile : squidSourceFiles) {
    SourceFile squidFile = (SourceFile) squidSourceFile;
    String relativePath = pathResolver.relativePath(context.fileSystem().baseDir(), new java.io.File(squidFile.getKey()));
    InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasRelativePath(relativePath));
    saveMeasures(inputFile, squidFile);
    saveIssues(inputFile, squidFile);
  }
}
origin: org.codehaus.sonar/sonar-batch

private long getDevelopmentCost(DecoratorContext context) {
 InputFile file = fs.inputFile(fs.predicates().hasRelativePath(context.getResource().getKey()));
 if (file != null) {
  String language = file.language();
  return getMeasureValue(context, sqaleRatingSettings.getSizeMetric(language, metrics)) * sqaleRatingSettings.getDevCost(language);
 } else {
  Collection<Measure> childrenMeasures = context.getChildrenMeasures(CoreMetrics.DEVELOPMENT_COST);
  Double sum = sum(childrenMeasures);
  return sum.longValue();
 }
}
origin: org.sonarsource.javascript/sonar-javascript-plugin

private static InputFile getInputFile(SensorContext context, String fileName) {
 FilePredicates predicates = context.fileSystem().predicates();
 InputFile inputFile = context.fileSystem().inputFile(predicates.or(predicates.hasRelativePath(fileName), predicates.hasAbsolutePath(fileName)));
 if (inputFile == null) {
  LOG.warn("No input file found for {}. No {} issues will be imported on this file.", fileName, LINTER_NAME);
  return null;
 }
 return inputFile;
}
origin: Riverside-Software/sonar-openedge

protected InputFile getInputFile(InputFile file, JPNode node) {
 if (node.getFileIndex() == 0) {
  return file;
 } else {
  return getContext().fileSystem().inputFile(
    getContext().fileSystem().predicates().hasRelativePath(node.getFilename()));
 }
}
origin: eu.rssw.openedge.checks/openedge-checks

protected InputFile getInputFile(InputFile file, JPNode node) {
 if (node.getFileIndex() == 0) {
  return file;
 } else {
  return getContext().fileSystem().inputFile(
    getContext().fileSystem().predicates().hasRelativePath(node.getFilename()));
 }
}
origin: SonarSource/sonar-go

InputFile getInputFile(SensorContext context, String filePath) {
 FilePredicates predicates = context.fileSystem().predicates();
 InputFile inputFile = context.fileSystem().inputFile(predicates.or(predicates.hasRelativePath(filePath), predicates.hasAbsolutePath(filePath)));
 if (inputFile == null) {
  LOG.warn(logPrefix() + "No input file found for {}. No {} issues will be imported on this file.", filePath, linterName());
  return null;
 }
 return inputFile;
}
origin: sonar-perl/sonar-perl

void getResourceAndSaveIssue(PerlCriticViolation violation) {
  log.debug(violation.toString());
  InputFile inputFile = fileSystem
      .inputFile(fileSystem.predicates().hasRelativePath(violation.getFilePath()));
  if (inputFile != null) {
    saveIssue(inputFile, violation.getLine(), violation.getType(), violation.getDescription());
  } else {
    log.warn("Not able to find an input file with path '{}'", violation.getFilePath());
  }
}
origin: SonarSource/sonar-custom-plugin-example

private void getResourceAndSaveIssue(final ErrorDataFromExternalLinter error) {
 LOGGER.debug(error.toString());
 InputFile inputFile = fileSystem.inputFile(
  fileSystem.predicates().and(
   fileSystem.predicates().hasRelativePath(error.getFilePath()),
   fileSystem.predicates().hasType(InputFile.Type.MAIN)));
 LOGGER.debug("inputFile null ? " + (inputFile == null));
 if (inputFile != null) {
  saveIssue(inputFile, error.getLine(), error.getType(), error.getDescription());
 } else {
  LOGGER.error("Not able to find a InputFile with " + error.getFilePath());
 }
}
org.sonar.api.batch.fsFilePredicateshasRelativePath

Javadoc

Predicate that gets a file by its relative path. The parameter accepts forward/back slashes as separator and non-normalized values (foo/../bar.txt is same as bar.txt). It must not be null.

Warning - may not be supported in SonarLint

Popular methods of FilePredicates

  • hasLanguage
  • and
  • hasType
  • hasPath
    if the parameter represents an absolute path for the running environment, then returns #hasAbsoluteP
  • hasAbsolutePath
    Predicate that find file by its absolute path. The parameter accepts forward/back slashes as separat
  • all
    Predicate that always evaluates to true
  • matchesPathPattern
    Predicate that gets the files which "path" matches a wildcard pattern. The path is the path part of
  • or
  • hasLanguages
  • is
    Warning - may not be supported in SonarLint
  • hasStatus
    Look for InputFile having a specific InputFile#status()
  • doesNotMatchPathPatterns
    Predicate that gets the files that do not match any of the given wildcard patterns. No filter is app
  • hasStatus,
  • doesNotMatchPathPatterns,
  • not,
  • hasAnyStatus,
  • matchesPathPatterns,
  • doesNotMatchPathPattern,
  • hasExtension,
  • hasFilename,
  • hasURI

Popular in Java

  • Parsing JSON documents to java classes using gson
  • onRequestPermissionsResult (Fragment)
  • setContentView (Activity)
  • getResourceAsStream (ClassLoader)
  • FileNotFoundException (java.io)
    Thrown when a file specified by a program cannot be found.
  • Date (java.sql)
    A class which can consume and produce dates in SQL Date format. Dates are represented in SQL as yyyy
  • HashMap (java.util)
    HashMap is an implementation of Map. All optional operations are supported.All elements are permitte
  • Scanner (java.util)
    A parser that parses a text string of primitive types and strings with the help of regular expressio
  • TreeSet (java.util)
    TreeSet is an implementation of SortedSet. All optional operations (adding and removing) are support
  • IsNull (org.hamcrest.core)
    Is the value null?
  • Best IntelliJ plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now