Search in sources :

Example 1 with PsiClass

use of com.intellij.psi.PsiClass in project buck by facebook.

the class BuckAutoDepsContributor method getVirtualFileFromClassname.

@Nullable
public VirtualFile getVirtualFileFromClassname(String classname) {
    GlobalSearchScope scope = GlobalSearchScope.allScope(mProject);
    PsiClass psiClass = JavaPsiFacade.getInstance(mProject).findClass(classname, scope);
    if (psiClass == null) {
        return null;
    }
    PsiFile psiFile = psiClass.getContainingFile();
    if (psiFile == null) {
        return null;
    }
    return psiFile.getVirtualFile();
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiClass(com.intellij.psi.PsiClass) PsiFile(com.intellij.psi.PsiFile) Nullable(javax.annotation.Nullable)

Example 2 with PsiClass

use of com.intellij.psi.PsiClass in project buck by facebook.

the class BuckCopyPasteProcessor method referenceNameToBuckFile.

private VirtualFile referenceNameToBuckFile(Project project, String reference) {
    // First test if it is a absolute path of a file.
    File tryFile = new File(reference);
    if (tryFile != null) {
        VirtualFile file = VfsUtil.findFileByIoFile(tryFile, true);
        if (file != null) {
            return BuckBuildUtil.getBuckFileFromDirectory(file.getParent());
        }
    }
    // Try class firstly.
    PsiClass classElement = JavaPsiFacade.getInstance(project).findClass(reference, GlobalSearchScope.allScope(project));
    if (classElement != null) {
        VirtualFile file = PsiUtilCore.getVirtualFile(classElement);
        return BuckBuildUtil.getBuckFileFromDirectory(file.getParent());
    }
    // Then try package.
    PsiPackage packageElement = JavaPsiFacade.getInstance(project).findPackage(reference);
    if (packageElement != null) {
        PsiDirectory directory = packageElement.getDirectories()[0];
        return BuckBuildUtil.getBuckFileFromDirectory(directory.getVirtualFile());
    }
    // Extract the package from the reference.
    int index = reference.lastIndexOf(".");
    if (index == -1) {
        return null;
    }
    reference = reference.substring(0, index);
    // Try to find the package again.
    packageElement = JavaPsiFacade.getInstance(project).findPackage(reference);
    if (packageElement != null) {
        PsiDirectory directory = packageElement.getDirectories()[0];
        return BuckBuildUtil.getBuckFileFromDirectory(directory.getVirtualFile());
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiClass(com.intellij.psi.PsiClass) PsiPackage(com.intellij.psi.PsiPackage) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) BuckFile(com.facebook.buck.intellij.ideabuck.lang.BuckFile) File(java.io.File)

Example 3 with PsiClass

use of com.intellij.psi.PsiClass in project qi4j-sdk by Qi4j.

the class CreateConcernOfInPackageAction method isAvailable.

@Override
protected final boolean isAvailable(DataContext dataContext) {
    boolean isAvailable = super.isAvailable(dataContext);
    if (!isAvailable) {
        return false;
    }
    PsiElement psiElement = PSI_ELEMENT.getData(dataContext);
    if (psiElement == null) {
        return false;
    }
    GlobalSearchScope searchScope = determineSearchScope(psiElement);
    if (searchScope == null) {
        return false;
    }
    Project project = PROJECT.getData(dataContext);
    PsiClass psiClass = getConcernOfClass(project, searchScope);
    return psiClass != null;
}
Also used : Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiClass(com.intellij.psi.PsiClass) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PsiClass

use of com.intellij.psi.PsiClass in project qi4j-sdk by Qi4j.

the class ConcernsAnnotationDeclaredCorrectlyInspection method checkClass.

@Override
public final ProblemDescriptor[] checkClass(@NotNull PsiClass psiClass, @NotNull InspectionManager manager, boolean isOnTheFly) {
    // If class does not have @Concerns, ignore
    PsiAnnotation concernsAnnotation = getConcernsAnnotation(psiClass);
    if (concernsAnnotation == null) {
        return null;
    }
    // If @Concerns declared in class, suggest remove @Concerns annotation
    if (!psiClass.isInterface()) {
        String message = message("concerns.annotation.declared.correctly.error.annotation.declared.in.class");
        RemoveConcernsAnnotationFix fix = new RemoveConcernsAnnotationFix(concernsAnnotation);
        ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(concernsAnnotation, message, fix, GENERIC_ERROR_OR_WARNING);
        return new ProblemDescriptor[] { problemDescriptor };
    }
    // If @Concerns annotation is empty, ignore
    List<PsiAnnotationMemberValue> concernsAnnotationValue = getConcernsAnnotationValue(concernsAnnotation);
    if (concernsAnnotationValue.isEmpty()) {
        return null;
    }
    // If ConcernOfClass is not resolved, ignore
    Project project = psiClass.getProject();
    GlobalSearchScope searchScope = determineSearchScope(psiClass);
    PsiClass concernOfClass = getConcernOfClass(project, searchScope);
    if (concernOfClass == null) {
        return null;
    }
    List<ProblemDescriptor> problems = new LinkedList<ProblemDescriptor>();
    for (PsiAnnotationMemberValue concernClassAnnotationValue : concernsAnnotationValue) {
        PsiJavaCodeReferenceElement concernClassReference = getConcernClassReference(concernClassAnnotationValue);
        // If it's not a class reference, ignore
        if (concernClassReference == null) {
            continue;
        }
        // If class reference can't be resolved, ignore
        PsiClass concernClass = (PsiClass) concernClassReference.resolve();
        if (concernClass == null) {
            continue;
        }
        // If concern class does not inherit concern class, suggest remove that reference.
        if (!concernClass.isInheritor(concernOfClass, true)) {
            String message = Qi4jResourceBundle.message("concerns.annotation.declared.correctly.error.concern.class.does.not.extend.ConcernOf", concernClass.getQualifiedName());
            RemoveInvalidConcernClassReferenceFix fix = new RemoveInvalidConcernClassReferenceFix(concernClassAnnotationValue, concernClassReference);
            ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(concernClassAnnotationValue, message, fix, GENERIC_ERROR_OR_WARNING);
            problems.add(problemDescriptor);
        } else {
        // TODO: Test whether it is a generic concern
        // TODO: Test whether it is a specific concern
        }
    }
    return problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiClass(com.intellij.psi.PsiClass) LinkedList(java.util.LinkedList) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue)

Example 5 with PsiClass

use of com.intellij.psi.PsiClass in project qi4j-sdk by Qi4j.

the class SideEffectsAnnotationDeclaredCorrectlyInspection method checkClass.

@Override
public final ProblemDescriptor[] checkClass(@NotNull PsiClass psiClass, @NotNull InspectionManager manager, boolean isOnTheFly) {
    // If class does not have @SideEffects, ignore
    PsiAnnotation sideEffectsAnnotation = getSideEffectsAnnotation(psiClass);
    if (sideEffectsAnnotation == null) {
        return null;
    }
    // If @SideEffects declared in class, suggest remove @SideEffects annotation
    if (!psiClass.isInterface()) {
        String message = message("side.effects.annotation.declared.correctly.error.annotation.declared.in.class");
        RemoveSideEffectsAnnotationFix fix = new RemoveSideEffectsAnnotationFix(sideEffectsAnnotation);
        ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(sideEffectsAnnotation, message, fix, GENERIC_ERROR_OR_WARNING);
        return new ProblemDescriptor[] { problemDescriptor };
    }
    // If @SideEffects annotation is empty, ignore
    List<PsiAnnotationMemberValue> sideEffectsAnnotationValue = getSideEffectsAnnotationValue(sideEffectsAnnotation);
    if (sideEffectsAnnotationValue.isEmpty()) {
        return null;
    }
    // If SideEffectOf is not resolved, ignore
    Project project = psiClass.getProject();
    GlobalSearchScope searchScope = determineSearchScope(psiClass);
    PsiClass sideEffectOfClass = Qi4jSideEffectUtil.getGenericSideEffectClass(project, searchScope);
    if (sideEffectOfClass == null) {
        return null;
    }
    List<ProblemDescriptor> problems = new LinkedList<ProblemDescriptor>();
    for (PsiAnnotationMemberValue sideEffectClassReferenceWrapper : sideEffectsAnnotationValue) {
        PsiJavaCodeReferenceElement sideEffectClassReference = getSideEffectClassReference(sideEffectClassReferenceWrapper);
        // If it's not a class reference, ignore
        if (sideEffectClassReference == null) {
            continue;
        }
        // If class reference can't be resolved, ignore
        PsiClass sideEffectClass = (PsiClass) sideEffectClassReference.resolve();
        if (sideEffectClass == null) {
            continue;
        }
        // If side effect class does not inherit SideEffectOf class, suggest remove that reference.
        if (!sideEffectClass.isInheritor(sideEffectOfClass, true)) {
            String message = Qi4jResourceBundle.message("side.effects.annotation.declared.correctly.error.side.effect.does.not.extend.side.effect.of", sideEffectClass.getQualifiedName());
            RemoveAnnotationValueFix fix = new RemoveAnnotationValueFix(sideEffectClassReferenceWrapper, sideEffectClassReference);
            ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(sideEffectClassReferenceWrapper, message, fix, GENERIC_ERROR_OR_WARNING);
            problems.add(problemDescriptor);
        } else {
        // TODO: Test whether it is a generic side effect
        // TODO: Test whether it is a specific side effect
        }
    }
    return problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiClass(com.intellij.psi.PsiClass) LinkedList(java.util.LinkedList) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue)

Aggregations

PsiClass (com.intellij.psi.PsiClass)594 PsiElement (com.intellij.psi.PsiElement)113 PsiMethod (com.intellij.psi.PsiMethod)97 Nullable (org.jetbrains.annotations.Nullable)75 NotNull (org.jetbrains.annotations.NotNull)60 Project (com.intellij.openapi.project.Project)59 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)57 Module (com.intellij.openapi.module.Module)55 PsiFile (com.intellij.psi.PsiFile)49 VirtualFile (com.intellij.openapi.vfs.VirtualFile)47 ArrayList (java.util.ArrayList)37 PsiField (com.intellij.psi.PsiField)36 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)25 Location (com.intellij.execution.Location)20 File (java.io.File)16 HashSet (java.util.HashSet)16 PsiClassType (com.intellij.psi.PsiClassType)15 PsiPackage (com.intellij.psi.PsiPackage)15 List (java.util.List)15 PsiType (com.intellij.psi.PsiType)13