/** * Returns the Sonar file representation ({@link File}) of the given file, if that file exists in the given project. * * @param file * the real file * @param project * the project * @return the Sonar resource if it exists in this project, or null if not. */ public File fromIOFile(java.io.File file, Project project) { return File.fromIOFile(file, project); }
/** * Internal use only. * @deprecated since 5.1 use {@link FileSystem#inputFile(org.sonar.api.batch.fs.FilePredicate)} */ @Deprecated public static File create(String relativePathFromBasedir, Language language, boolean unitTest) { File file = create(relativePathFromBasedir); file.setLanguage(language); if (unitTest) { file.setQualifier(Qualifiers.UNIT_TEST_FILE); } return file; }
public void addResource(File resource, String fileKey) { directories.put(resource.getParent(), resource); fileKeyByResource.put(resource, fileKey); }
public Resource getUnitTestResource(String filename) { org.sonar.api.resources.File sonarFile = new org.sonar.api.resources.File(filename); sonarFile.setQualifier(Qualifiers.UNIT_TEST_FILE); return sonarFile; } }
if (reference instanceof File) { File referenceFile = (File) reference; isTest = Qualifiers.UNIT_TEST_FILE.equals(referenceFile.getQualifier()); relativePathFromSourceDir = referenceFile.relativePathFromSourceDir(); } else if (reference instanceof Directory) { isDir = true; Bucket b = getBucket(isDir ? Directory.fromIOFile(abs, getProject()) : File.fromIOFile(abs, getProject())); if (b != null) { return b;
private File getMainResource(InputFile mainFile) { File mainRes = File.create(mainFile.relativePath()); // Reload mainRes = sonarIndex.getResource(mainRes); if (mainRes == null) { throw new IllegalArgumentException("Provided input file is not indexed or not a main file: " + mainRes); } return mainRes; }
@Override public void scanFile(JavaFileScannerContext context) { Preconditions.checkNotNull(sensorContext); JavaFilesCache javaFilesCache = new JavaFilesCache(); javaFilesCache.scanFile(context); org.sonar.api.resources.File currentResource = org.sonar.api.resources.File.fromIOFile(context.getFile(), project); if (currentResource == null) { throw new IllegalStateException("resource not found : " + context.getFileKey()); } resourceMapping.addResource(currentResource, context.getFileKey()); for (Map.Entry<String, File> classIOFileEntry : javaFilesCache.getResourcesCache().entrySet()) { resourcesByClass.put(classIOFileEntry.getKey(), currentResource); if (context.getFileKey() != null) { sourceFileByClass.put(classIOFileEntry.getKey(), context.getFileKey()); } } methodStartLines.putAll(javaFilesCache.getMethodStartLines()); org.sonar.api.resources.File indexedResource = sensorContext.getResource(currentResource); if (indexedResource != null && javaFilesCache.hasSuppressWarningLines()) { suppressWarningsFilter.addComponent(indexedResource.getEffectiveKey(), javaFilesCache.getSuppressWarningLines()); } } }
private void saveMeasures(Collection<SourceCode> sourceFiles) { for (SourceCode squidFileCode : sourceFiles) { SourceFile squidFile = (SourceFile) squidFileCode; /* Create the sonar file */ File sonarFile = File.fromIOFile(new java.io.File(squidFile.getKey()), project); sonarFile.setLanguage(cSharp); /* Fill the resource bridge API that can be used by other C# plugins to map logical resources to physical ones */ cSharpResourcesBridge.indexFile(squidFile, sonarFile); /* No Sonar */ noSonarFilter.addResource(sonarFile, squidFile.getNoSonarTagLines()); /* Classes complexity distribution */ saveClassesComplexityDistribution(sonarFile, squidFile); /* Methods complexity distribution */ saveMethodsComplexityDistribution(sonarFile, squidFile); /* Check messages */ saveViolations(squidFile, sonarFile); /* Metrics at the file level */ saveMeasures(sonarFile, squidFile); } // and lock everything to prevent future modifications LOG.debug("Locking the C# Resource Bridge and the Sonar Index: future modifications won't be possible."); cSharpResourcesBridge.lock(); resourceCreationLock.lock(); }
private File getTestResource(InputFile testFile) { File testRes = File.create(testFile.relativePath()); testRes.setQualifier(Qualifiers.UNIT_TEST_FILE); // Reload testRes = sonarIndex.getResource(testRes); if (testRes == null) { throw new IllegalArgumentException("Provided input file is not indexed or not a test file: " + testFile); } return testRes; }
try { CoverageMeasuresBuilder fileCoverage = coveredFiles.get(inputFile); org.sonar.api.resources.File resource = org.sonar.api.resources.File.create(inputFile.relativePath()); LOG.debug("Default value of zero will be saved for file: {}", resource.getPath()); LOG.debug("Because: either was not present in LCOV report either was not able to retrieve associated SonarQube resource"); saveZeroValueForResource(resource, context);
@Override public void analyse(Project module, org.sonar.api.batch.SensorContext context) { createIssueOnDir(new Directory("")); File src = module.getFileSystem().getSourceDirs().get(0); for (File f : fileSystem.files(FileQuery.onMain().onLanguage(Xoo.KEY))) { String relativePathFromSourceDir = new PathResolver().relativePath(src, f); org.sonar.api.resources.File sonarFile = new org.sonar.api.resources.File(relativePathFromSourceDir); Issuable issuable = perspectives.as(Issuable.class, sonarFile); issuable.addIssue(issuable.newIssueBuilder() .ruleKey(RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY)) .message("Issue created using deprecated API") .line(1) .build()); sonarFile = context.getResource(sonarFile); Directory parent = sonarFile.getParent(); createIssueOnDir(parent); } }
/** * Internal use only. * @deprecated since 5.1 use {@link FileSystem#inputFile(org.sonar.api.batch.fs.FilePredicate)} */ @Deprecated public static File create(String relativePathFromBasedir) { File file = new File(); String normalizedPath = normalize(relativePathFromBasedir); file.setKey(normalizedPath); file.setPath(normalizedPath); String directoryPath; if (normalizedPath != null && normalizedPath.contains(Directory.SEPARATOR)) { directoryPath = StringUtils.substringBeforeLast(normalizedPath, Directory.SEPARATOR); file.filename = StringUtils.substringAfterLast(normalizedPath, Directory.SEPARATOR); } else { directoryPath = Directory.SEPARATOR; file.filename = normalizedPath; } file.parent = Directory.create(directoryPath); return file; }
@Override public String getSource(Resource reference) { Resource resource = getResource(reference); if (resource instanceof File) { File file = (File) resource; Project module = currentProject; ProjectDefinition def = projectTree.getProjectDefinition(module); try { return FileUtils.readFileToString(new java.io.File(def.getBaseDir(), file.getPath())); } catch (IOException e) { throw new IllegalStateException("Unable to read file content " + reference, e); } } return null; }
/** * Creates a File from an io.file and a list of sources directories * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)} */ @Deprecated @CheckForNull public static File fromIOFile(java.io.File file, List<java.io.File> sourceDirs) { PathResolver.RelativePath relativePath = new PathResolver().relativePath(sourceDirs, file); if (relativePath != null) { return new File(relativePath.path()); } return null; }
if (reference instanceof File) { File referenceFile = (File) reference; isTest = Qualifiers.UNIT_TEST_FILE.equals(referenceFile.getQualifier()); relativePathFromSourceDir = referenceFile.relativePathFromSourceDir(); } else if (reference instanceof Directory) { isDir = true; java.io.File dirOrFile = pathResolver.relativeFile(projectDef.getBaseDir(), src); java.io.File abs = new java.io.File(dirOrFile, relativePathFromSourceDir); Bucket b = getBucket(isDir ? Directory.fromIOFile(abs, getProject()) : File.fromIOFile(abs, getProject())); if (b != null) { return b;
@Override public Resource toResource(File file) { if (file == null || !file.exists()) { return null; } String relativePath = pathResolver.relativePath(getBasedir(), file); if (relativePath == null) { return null; } return file.isFile() ? org.sonar.api.resources.File.create(relativePath) : org.sonar.api.resources.Directory.create(relativePath); }
@Override public String getSource(Resource reference) { Resource resource = getResource(reference); if (resource instanceof File) { File file = (File) resource; Project module = currentProject; ProjectDefinition def = projectTree.getProjectDefinition(module); try { return FileUtils.readFileToString(new java.io.File(def.getBaseDir(), file.getPath())); } catch (IOException e) { throw new IllegalStateException("Unable to read file content " + reference, e); } } return null; }
public Resource resourceFromIOFile(File file) { return org.sonar.api.resources.File.fromIOFile(file, project); }