@Test public void matches_inclusion_pattern() { assertThat(predicates.matchesPathPattern("src/main/**/Action.java").apply(javaFile)).isTrue(); assertThat(predicates.matchesPathPattern("Action.java").apply(javaFile)).isFalse(); assertThat(predicates.matchesPathPattern("src/**/*.php").apply(javaFile)).isFalse(); }
@Override public void execute(SensorContext context) { RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY); FilePredicate unknownFilesPredicate = context.fileSystem().predicates().matchesPathPattern("**/*.unknown"); Iterable<InputFile> unknownFiles = context.fileSystem().inputFiles(unknownFilesPredicate); unknownFiles.forEach(inputFile -> { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(inputFile) .message("This issue is generated on each file with extension 'unknown'")) .save(); }); }
private static FilePredicate getFileNamePredicateFromSuffixes(FilePredicates p, String fileName, String[] suffixes) { List<FilePredicate> fileNamePredicates = new ArrayList<>(suffixes.length); for (String suffix : suffixes) { fileNamePredicates.add(p.matchesPathPattern("**/" + fileName + suffix)); } return p.or(fileNamePredicates); }
protected InputFile getUnitTestInputFile(String classKey, FileSystem myFS) { String filename = classKey.replace('.', '/') + ".scala"; InputFile sonarFile = myFS.inputFile(myFS.predicates().matchesPathPattern("**/" + filename)); return sonarFile; } }
@CheckForNull public InputFile sourceInputFileFromRelativePath(String relativePath) { return fileSystem.inputFile(predicates.and(predicates.matchesPathPattern("**/" + relativePath), isSwiftLanguage, isMainTypeFile)); }
@CheckForNull public InputFile sourceInputFileFromRelativePath(String relativePath) { return fileSystem.inputFile(predicates.and(predicates.matchesPathPattern("**/" + relativePath), isGroovyLanguage, isMainTypeFile)); }
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); }
@CheckForNull private static InputFile findInputFile(FileSystem fileSystem, FilePredicates predicates, String fileName) { String key = fileName.startsWith(File.separator) ? fileName : (File.separator + fileName); return fileSystem.inputFile(predicates.and( predicates.matchesPathPattern("**" + key.replace(File.separator, "/")), predicates.hasType(InputFile.Type.MAIN), predicates.hasLanguage(Flex.KEY))); }
private FilePredicate mainFilePredicate(FileSystem fileSystem) { String[] suffixes = getSettings().getStringArray(Plugin.EMBEDDED_CSS_FILE_SUFFIXES_KEY); if (suffixes == null || suffixes.length == 0) { suffixes = StringUtils.split(Plugin.CSS_FILE_SUFFIXES_DEFAULT_VALUE, ","); } List<FilePredicate> filePredicates = new ArrayList<>(); for (String suffix : suffixes) { filePredicates.add(fileSystem.predicates().matchesPathPattern("**/*." + suffix)); } return fileSystem.predicates().or(filePredicates); }
/** * Determine if the project has Java source files. This is used to determine if the project has no compiled classes on * purpose or because the compilation was omit from the process. * @return If at least one Java file is present */ private boolean hasSourceFiles() { FilePredicates pred = fileSystem.predicates(); return fileSystem.hasFiles( pred.and( pred.hasType(Type.MAIN), pred.or(FindbugsPlugin.getSupportedLanguagesFilePredicate(pred)), //package-info.java will not generate any class files. //See: https://github.com/SonarQubeCommunity/sonar-findbugs/issues/36 pred.not(pred.matchesPathPattern("**/package-info.java")), pred.not(pred.matchesPathPattern("**/module-info.java")), pred.not(pred.matchesPathPattern("**/*.jsp")) ) ); }
@Override public void execute(SensorContext context) { FileSystem fs = context.fileSystem(); FilePredicate xmlFilesPredicate = fs.predicates().matchesPathPattern("**/*.xml");
@Override public InputFile getUnitTestResource(FileSystem fileSystem, String classname) { String fileName = classname.replace('.', '/') + ".m"; FilePredicate fp = fileSystem.predicates().hasPath(fileName); if(fileSystem.hasFiles(fp)){ return fileSystem.inputFile(fp); } /* * Most xcodebuild JUnit parsers don't include the path to the class in the class field, so search for it if it * wasn't found in the root. */ fp = fileSystem.predicates().and( fileSystem.predicates().hasType(InputFile.Type.TEST), fileSystem.predicates().matchesPathPattern("**/" + fileName.replace("_", "+"))); if(fileSystem.hasFiles(fp)){ /* * Lazily get the first file, since we wouldn't be able to determine the correct one from just the * test class name in the event that there are multiple matches. */ return fileSystem.inputFiles(fp).iterator().next(); } LOGGER.info("Unable to locate test source file {}", fileName); return null; } }
InputFile scalaFile = fileSystem.inputFile(fileSystem.predicates().matchesPathPattern(path));
@Override public InputFile getUnitTestResource(FileSystem fileSystem, String classname) { String fileName = classname.replace('.', '/') + ".swift"; String wildcardFileName = classname.replace(".", "/**/") + ".swift"; FilePredicate fp = fileSystem.predicates().hasPath(fileName); if(fileSystem.hasFiles(fp)){ return fileSystem.inputFile(fp); } /* * Most xcodebuild JUnit parsers don't include the path to the class in the class field, so search for it if it * wasn't found in the root. */ fp = fileSystem.predicates().and( fileSystem.predicates().hasType(InputFile.Type.TEST), fileSystem.predicates().matchesPathPattern("**/" + wildcardFileName)); if(fileSystem.hasFiles(fp)){ /* * Lazily get the first file, since we wouldn't be able to determine the correct one from just the * test class name in the event that there are multiple matches. */ return fileSystem.inputFiles(fp).iterator().next(); } LOGGER.info("Unable to locate test source file {}", wildcardFileName); return null; } }
@Override public void execute(SensorContext context) { RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY); FilePredicate unknownFilesPredicate = context.fileSystem().predicates().matchesPathPattern("**/*.unknown"); Iterable<InputFile> unknownFiles = context.fileSystem().inputFiles(unknownFilesPredicate); unknownFiles.forEach(inputFile -> { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(inputFile) .message("This issue is generated on each file with extension 'unknown'")) .save(); }); }
@VisibleForTesting protected void execute(SensorContext context, Checks<PhpIniCheck> checks) { PhpIniParser parser = new PhpIniParser(); FileSystem fs = context.fileSystem(); Iterable<InputFile> inputFiles = fs.inputFiles(fs.predicates().matchesPathPattern("**/php.ini")); for (InputFile inputFile : inputFiles) { PhpIniFile phpIni; try { phpIni = parser.parse(new PhpFileImpl(inputFile)); } catch (RecognitionException e) { LOG.error("Unable to parse file: " + inputFile.absolutePath()); LOG.error(e.getMessage()); continue; } for (PhpIniCheck check : checks.all()) { List<PhpIniIssue> issues = check.analyze(phpIni); saveIssues(context, inputFile, checks.ruleKey(check), issues); } } }
@VisibleForTesting protected void execute(SensorContext context, Checks<PhpIniCheck> checks) { PhpIniParser parser = new PhpIniParser(); FileSystem fs = context.fileSystem(); Iterable<InputFile> inputFiles = fs.inputFiles(fs.predicates().matchesPathPattern("**/php.ini")); for (InputFile inputFile : inputFiles) { PhpIniFile phpIni; try { phpIni = parser.parse(new PhpFileImpl(inputFile)); } catch (RecognitionException e) { LOG.error("Unable to parse file: " + inputFile.absolutePath()); LOG.error(e.getMessage()); continue; } for (PhpIniCheck check : checks.all()) { List<PhpIniIssue> issues = check.analyze(phpIni); saveIssues(context, inputFile, checks.ruleKey(check), issues); } } }