use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.
the class TestDataLineMarkerProvider method getTestDataBasePath.
@Nullable
public static String getTestDataBasePath(@Nullable PsiClass psiClass) {
if (psiClass == null)
return null;
final PsiAnnotation annotation = AnnotationUtil.findAnnotationInHierarchy(psiClass, Collections.singleton(TEST_DATA_PATH_ANNOTATION_QUALIFIED_NAME));
if (annotation != null) {
final PsiAnnotationMemberValue value = annotation.findAttributeValue(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME);
if (value instanceof PsiExpression) {
final Project project = value.getProject();
final PsiConstantEvaluationHelper evaluationHelper = JavaPsiFacade.getInstance(project).getConstantEvaluationHelper();
final Object constantValue = evaluationHelper.computeConstantExpression(value, false);
if (constantValue instanceof String) {
String path = (String) constantValue;
if (path.contains(CONTENT_ROOT_VARIABLE)) {
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final VirtualFile file = psiClass.getContainingFile().getVirtualFile();
if (file == null) {
return null;
}
final VirtualFile contentRoot = fileIndex.getContentRootForFile(file);
if (contentRoot == null)
return null;
path = path.replace(CONTENT_ROOT_VARIABLE, contentRoot.getPath());
}
if (path.contains(PROJECT_ROOT_VARIABLE)) {
final VirtualFile baseDir = project.getBaseDir();
if (baseDir == null) {
return null;
}
path = path.replace(PROJECT_ROOT_VARIABLE, baseDir.getPath());
}
return path;
}
}
}
return null;
}
use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.
the class TestLocationDataRule method collectRelativeLocations.
@NotNull
protected static List<Location> collectRelativeLocations(Project project, VirtualFile file) {
if (DumbService.isDumb(project))
return Collections.emptyList();
final List<Location> locations = new ArrayList<>();
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
if (fileIndex.isInContent(file) && !fileIndex.isInSource(file) && !fileIndex.isInLibraryClasses(file)) {
final VirtualFile parent = file.getParent();
final VirtualFile contentRoot = fileIndex.getContentRootForFile(file);
if (contentRoot != null && parent != null) {
final String relativePath = VfsUtilCore.getRelativePath(parent, contentRoot, '/');
if (relativePath != null) {
final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(project);
final List<String> words = StringUtil.getWordsIn(relativePath);
// put longer strings first
Collections.sort(words, (o1, o2) -> o2.length() - o1.length());
final GlobalSearchScope testScope = GlobalSearchScopesCore.projectTestScope(project);
Set<PsiFile> resultFiles = null;
for (String word : words) {
if (word.length() < 5) {
continue;
}
final Set<PsiFile> files = new THashSet<>();
searchHelper.processAllFilesWithWordInLiterals(word, testScope, new CommonProcessors.CollectProcessor<>(files));
if (resultFiles == null) {
resultFiles = files;
} else {
resultFiles.retainAll(files);
}
if (resultFiles.isEmpty())
break;
}
if (resultFiles != null) {
for (Iterator<PsiFile> iterator = resultFiles.iterator(); iterator.hasNext(); ) {
if (!VfsUtilCore.isAncestor(contentRoot, iterator.next().getVirtualFile(), true)) {
iterator.remove();
}
}
final String fileName = file.getName();
final String nameWithoutExtension = file.getNameWithoutExtension();
for (PsiFile resultFile : resultFiles) {
if (resultFile instanceof PsiClassOwner) {
final PsiClass[] classes = ((PsiClassOwner) resultFile).getClasses();
if (classes.length > 0) {
ContainerUtil.addIfNotNull(locations, getLocation(project, fileName, nameWithoutExtension, classes[0]));
}
}
}
}
}
}
}
return locations;
}
use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.
the class AppEngineForbiddenCodeInspection method checkFile.
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
final Project project = manager.getProject();
Module module = ModuleUtilCore.findModuleForPsiElement(file);
final AppEngineFacet appEngineFacet = AppEngineFacet.getAppEngineFacetByModule(module);
if (appEngineFacet == null) {
return null;
}
final AppEngineSdk appEngineSdk = appEngineFacet.getSdk();
if (!appEngineSdk.isValid()) {
return null;
}
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final List<ProblemDescriptor> problems = new ArrayList<>();
file.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitDocComment(PsiDocComment comment) {
}
@Override
public void visitMethod(PsiMethod method) {
final PsiModifierList modifierList = method.getModifierList();
if (modifierList.hasModifierProperty(PsiModifier.NATIVE)) {
if (!isNativeMethodAllowed(method)) {
problems.add(manager.createProblemDescriptor(modifierList, "Native methods aren't allowed in App Engine application", isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
}
}
super.visitMethod(method);
}
@Override
public void visitNewExpression(PsiNewExpression expression) {
final PsiJavaCodeReferenceElement classReference = expression.getClassReference();
if (classReference != null) {
final PsiElement resolved = classReference.resolve();
if (resolved instanceof PsiClass) {
final String qualifiedName = ((PsiClass) resolved).getQualifiedName();
if (qualifiedName != null && appEngineSdk.isMethodInBlacklist(qualifiedName, "new")) {
final String message = "App Engine application should not create new instances of '" + qualifiedName + "' class";
problems.add(manager.createProblemDescriptor(classReference, message, isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
}
}
}
super.visitNewExpression(expression);
}
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final PsiElement element = methodExpression.resolve();
if (element instanceof PsiMethod) {
final PsiMethod method = (PsiMethod) element;
final PsiClass psiClass = method.getContainingClass();
if (psiClass != null) {
final String qualifiedName = psiClass.getQualifiedName();
final String methodName = method.getName();
if (qualifiedName != null && appEngineSdk.isMethodInBlacklist(qualifiedName, methodName)) {
final String message = "AppEngine application should not call '" + StringUtil.getShortName(qualifiedName) + "." + methodName + "' method";
problems.add(manager.createProblemDescriptor(methodExpression, message, isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
}
}
}
super.visitMethodCallExpression(expression);
}
@Override
public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
final PsiElement resolved = reference.resolve();
if (resolved instanceof PsiClass) {
final PsiFile psiFile = resolved.getContainingFile();
if (psiFile != null) {
final VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile != null && !fileIndex.isInSource(virtualFile)) {
final List<OrderEntry> list = fileIndex.getOrderEntriesForFile(virtualFile);
for (OrderEntry entry : list) {
if (entry instanceof JdkOrderEntry) {
final String className = ClassUtil.getJVMClassName((PsiClass) resolved);
if (className != null && !appEngineSdk.isClassInWhiteList(className)) {
problems.add(manager.createProblemDescriptor(reference, "Class '" + className + "' is not included in App Engine JRE White List", isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
}
}
}
}
}
}
super.visitReferenceElement(reference);
}
});
return problems.toArray(new ProblemDescriptor[problems.size()]);
}
use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.
the class GradleTreeStructureProvider method getProjectNodeChildren.
@NotNull
private static Collection<AbstractTreeNode> getProjectNodeChildren(@NotNull Project project, @NotNull Collection<AbstractTreeNode> children) {
Collection<AbstractTreeNode> modifiedChildren = ContainerUtil.newSmartList();
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
for (AbstractTreeNode child : children) {
Pair<VirtualFile, PsiDirectoryNode> parentNodePair = null;
if (child instanceof ProjectViewModuleGroupNode) {
final ProjectViewModuleGroupNode groupNode = (ProjectViewModuleGroupNode) child;
final Collection<AbstractTreeNode> groupNodeChildren = groupNode.getChildren();
for (final AbstractTreeNode node : groupNodeChildren) {
if (node instanceof PsiDirectoryNode) {
final PsiDirectoryNode psiDirectoryNode = (PsiDirectoryNode) node;
final PsiDirectory psiDirectory = psiDirectoryNode.getValue();
if (psiDirectory == null) {
parentNodePair = null;
break;
}
final VirtualFile virtualFile = psiDirectory.getVirtualFile();
final Module module = fileIndex.getModuleForFile(virtualFile);
if (!ExternalSystemApiUtil.isExternalSystemAwareModule(GradleConstants.SYSTEM_ID, module)) {
parentNodePair = null;
break;
}
if (parentNodePair == null || VfsUtilCore.isAncestor(virtualFile, parentNodePair.first, false)) {
parentNodePair = Pair.pair(virtualFile, psiDirectoryNode);
} else if (!VfsUtilCore.isAncestor(parentNodePair.first, virtualFile, false)) {
parentNodePair = null;
break;
}
} else {
parentNodePair = null;
break;
}
}
}
modifiedChildren.add(parentNodePair != null ? parentNodePair.second : child);
}
return modifiedChildren;
}
use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.
the class GroovyDslFileIndex method getProjectGdslFiles.
private static List<VirtualFile> getProjectGdslFiles(Project project) {
final List<VirtualFile> result = ContainerUtil.newArrayList();
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
for (VirtualFile vfile : FileBasedIndex.getInstance().getContainingFiles(NAME, OUR_KEY, scope)) {
if (!vfile.isValid()) {
continue;
}
if (!GdslUtil.GDSL_FILTER.value(vfile)) {
LOG.error("Index returned non-gdsl file: " + vfile);
continue;
}
if (fileIndex.isInLibrarySource(vfile)) {
continue;
}
if (!fileIndex.isInLibraryClasses(vfile)) {
if (!fileIndex.isInSourceContent(vfile) || !isActivated(vfile)) {
continue;
}
}
result.add(vfile);
}
return result;
}
Aggregations