Search in sources :

Example 1 with PsiIdentifier

use of com.intellij.psi.PsiIdentifier in project Main by SpartanRefactoring.

the class UtilsTest method testGetAllReferences.

public void testGetAllReferences() throws Exception {
    PsiMethod m = createTestMethodFromString("int foo() { int x = 0; x++; x--; return x;}");
    PsiIdentifier id = createTestIdentifierFromString("x");
    assertEquals(Utils.getAllReferences(m, id).size(), 4);
    id = createTestIdentifierFromString("id");
    PsiIdentifier nonExistent = createTestIdentifierFromString("banana");
    String m1String = "void foo(){ int k=5; }";
    PsiMethod m1 = createTestMethodFromString(m1String);
    String m2String = "int id() { int id=7; return 8;}";
    PsiMethod m2 = createTestMethodFromString(m2String);
    String m3String = "int id() { int id=7; return id;}";
    PsiMethod m3 = createTestMethodFromString(m3String);
    assert Utils.getAllReferences(null, null).isEmpty();
    assert Utils.getAllReferences(null, id).isEmpty();
    assert Utils.getAllReferences(m1, id).isEmpty();
    assert Utils.getAllReferences(m2, id).size() == 1;
    assert Utils.getAllReferences(m2, nonExistent).isEmpty();
    assert Utils.getAllReferences(m3, id).size() == 2;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiIdentifier(com.intellij.psi.PsiIdentifier)

Example 2 with PsiIdentifier

use of com.intellij.psi.PsiIdentifier in project intellij-community by JetBrains.

the class ClassIndependentOfModuleInspection method checkElement.

@Nullable
@Override
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext) {
    if (!(refEntity instanceof RefClass)) {
        return null;
    }
    final RefClass refClass = (RefClass) refEntity;
    final RefEntity owner = refClass.getOwner();
    if (!(owner instanceof RefPackage)) {
        return null;
    }
    final Set<RefClass> dependencies = DependencyUtils.calculateDependenciesForClass(refClass);
    for (RefClass dependency : dependencies) {
        if (inSameModule(refClass, dependency)) {
            return null;
        }
    }
    final Set<RefClass> dependents = DependencyUtils.calculateDependentsForClass(refClass);
    for (RefClass dependent : dependents) {
        if (inSameModule(refClass, dependent)) {
            return null;
        }
    }
    final PsiClass aClass = refClass.getElement();
    final PsiIdentifier identifier = aClass.getNameIdentifier();
    if (identifier == null) {
        return null;
    }
    return new CommonProblemDescriptor[] { manager.createProblemDescriptor(identifier, InspectionGadgetsBundle.message("class.independent.of.module.problem.descriptor"), true, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false) };
}
Also used : CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) PsiClass(com.intellij.psi.PsiClass) PsiIdentifier(com.intellij.psi.PsiIdentifier) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PsiIdentifier

use of com.intellij.psi.PsiIdentifier in project intellij-community by JetBrains.

the class ClassUnconnectedToPackageInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalInspectionContext) {
    if (!(refEntity instanceof RefClass)) {
        return null;
    }
    final RefClass refClass = (RefClass) refEntity;
    final RefEntity owner = refClass.getOwner();
    if (!(owner instanceof RefPackage)) {
        return null;
    }
    final Set<RefClass> dependencies = DependencyUtils.calculateDependenciesForClass(refClass);
    for (RefClass dependency : dependencies) {
        if (inSamePackage(refClass, dependency)) {
            return null;
        }
    }
    final Set<RefClass> dependents = DependencyUtils.calculateDependentsForClass(refClass);
    for (RefClass dependent : dependents) {
        if (inSamePackage(refClass, dependent)) {
            return null;
        }
    }
    final PsiClass aClass = refClass.getElement();
    final PsiIdentifier identifier = aClass.getNameIdentifier();
    if (identifier == null) {
        return null;
    }
    return new CommonProblemDescriptor[] { manager.createProblemDescriptor(identifier, InspectionGadgetsBundle.message("class.unconnected.to.package.problem.descriptor"), true, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false) };
}
Also used : RefClass(com.intellij.codeInspection.reference.RefClass) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefEntity(com.intellij.codeInspection.reference.RefEntity) RefPackage(com.intellij.codeInspection.reference.RefPackage) PsiClass(com.intellij.psi.PsiClass) PsiIdentifier(com.intellij.psi.PsiIdentifier) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PsiIdentifier

use of com.intellij.psi.PsiIdentifier in project intellij-community by JetBrains.

the class RegisterExtensionFixProvider method getQuickFixes.

@NotNull
@Override
public IntentionAction[] getQuickFixes(@NotNull PsiElement element) {
    if (!(element instanceof PsiIdentifier))
        return IntentionAction.EMPTY_ARRAY;
    PsiElement parent = element.getParent();
    if (!(parent instanceof PsiClass))
        return IntentionAction.EMPTY_ARRAY;
    PsiClass psiClass = (PsiClass) parent;
    if (InheritanceUtil.isInheritor(psiClass, LocalInspectionTool.class.getName())) {
        return new IntentionAction[] { new RegisterInspectionFix(psiClass, LocalInspectionEP.LOCAL_INSPECTION) };
    }
    if (InheritanceUtil.isInheritor(psiClass, GlobalInspectionTool.class.getName())) {
        return new IntentionAction[] { new RegisterInspectionFix(psiClass, InspectionEP.GLOBAL_INSPECTION) };
    }
    ExtensionPointLocator extensionPointLocator = new ExtensionPointLocator(psiClass);
    List<ExtensionPointCandidate> candidateList = extensionPointLocator.findSuperCandidates();
    if (!candidateList.isEmpty()) {
        return new IntentionAction[] { new RegisterExtensionFix(psiClass, candidateList) };
    }
    return IntentionAction.EMPTY_ARRAY;
}
Also used : GlobalInspectionTool(com.intellij.codeInspection.GlobalInspectionTool) ExtensionPointLocator(org.jetbrains.idea.devkit.util.ExtensionPointLocator) ExtensionPointCandidate(org.jetbrains.idea.devkit.util.ExtensionPointCandidate) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) PsiIdentifier(com.intellij.psi.PsiIdentifier) PsiClass(com.intellij.psi.PsiClass) LocalInspectionTool(com.intellij.codeInspection.LocalInspectionTool) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PsiIdentifier

use of com.intellij.psi.PsiIdentifier in project android by JetBrains.

the class ParcelableQuickFixTest method testIsApplicable.

public void testIsApplicable() throws Exception {
    ParcelableQuickFix fix = new ParcelableQuickFix("Fix Parcelable", IMPLEMENT);
    PsiFile file = myFixture.addFileToProject("src/com/example/Simple.java", SIMPLE_SOURCE);
    PsiIdentifier identifier = findClassIdentifier(file, "Simple");
    assertTrue(fix.isApplicable(identifier, identifier, AndroidQuickfixContexts.DesignerContext.TYPE));
    file = myFixture.addFileToProject("src/com/expected/Simple.java", SIMPLE_EXPECTED);
    identifier = findClassIdentifier(file, "Simple");
    assertFalse(fix.isApplicable(identifier, identifier, AndroidQuickfixContexts.DesignerContext.TYPE));
}
Also used : PsiIdentifier(com.intellij.psi.PsiIdentifier) PsiFile(com.intellij.psi.PsiFile)

Aggregations

PsiIdentifier (com.intellij.psi.PsiIdentifier)12 PsiClass (com.intellij.psi.PsiClass)6 Nullable (org.jetbrains.annotations.Nullable)5 CommonProblemDescriptor (com.intellij.codeInspection.CommonProblemDescriptor)4 LocalInspectionTool (com.intellij.codeInspection.LocalInspectionTool)2 RefClass (com.intellij.codeInspection.reference.RefClass)2 RefEntity (com.intellij.codeInspection.reference.RefEntity)2 RefPackage (com.intellij.codeInspection.reference.RefPackage)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 NotNull (org.jetbrains.annotations.NotNull)2 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 GlobalInspectionTool (com.intellij.codeInspection.GlobalInspectionTool)1 LocalInspectionToolSession (com.intellij.codeInspection.LocalInspectionToolSession)1 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 RefModule (com.intellij.codeInspection.reference.RefModule)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Document (com.intellij.openapi.editor.Document)1