Search in sources :

Example 41 with PsiClass

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

the class FormEnumUsageTest method testEnumUsage.

public void testEnumUsage() throws IncorrectOperationException {
    LanguageLevelProjectExtension.getInstance(myJavaFacade.getProject()).setLanguageLevel(LanguageLevel.JDK_1_5);
    CommandProcessor.getInstance().executeCommand(myProject, () -> {
        try {
            createFile(myModule, myTestProjectRoot, "PropEnum.java", "public enum PropEnum { valueA, valueB }");
            createFile(myModule, myTestProjectRoot, "CustomComponent.java", "public class CustomComponent extends JLabel { private PropEnum e; public PropEnum getE() { return e; } public void setE(E newE) { e = newE; } }");
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }, "", null);
    PsiClass enumClass = myJavaFacade.findClass("PropEnum", ProjectScope.getAllScope(myProject));
    PsiField valueBField = enumClass.findFieldByName("valueB", false);
    assertNotNull(valueBField);
    assertTrue(valueBField instanceof PsiEnumConstant);
    final PsiClass componentClass = myJavaFacade.findClass("CustomComponent", ProjectScope.getAllScope(myProject));
    assertNotNull(componentClass);
    assertEquals(1, ReferencesSearch.search(componentClass).findAll().size());
    assertEquals(1, ReferencesSearch.search(valueBField).findAll().size());
}
Also used : PsiEnumConstant(com.intellij.psi.PsiEnumConstant) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 42 with PsiClass

use of com.intellij.psi.PsiClass in project kotlin by JetBrains.

the class LightClassAnnotationsTest method doTest.

private void doTest(@NotNull String fqName) {
    PsiClass psiClass = finder.findClass(fqName, GlobalSearchScope.allScope(getProject()));
    if (!(psiClass instanceof KtLightClass)) {
        throw new IllegalStateException("Not a light class: " + psiClass + " (" + fqName + ")");
    }
    PsiModifierList modifierList = psiClass.getModifierList();
    assert modifierList != null : "No modifier list for " + psiClass.getText();
    StringBuilder sb = new StringBuilder();
    for (PsiAnnotation annotation : modifierList.getAnnotations()) {
        sb.append(annotation.getText()).append("\n");
    }
    KotlinTestUtils.assertEqualsToFile(new File(testDir, getTestName(false) + ".annotations.txt"), sb.toString());
}
Also used : PsiClass(com.intellij.psi.PsiClass) KtLightClass(org.jetbrains.kotlin.asJava.classes.KtLightClass) PsiAnnotation(com.intellij.psi.PsiAnnotation) File(java.io.File) PsiModifierList(com.intellij.psi.PsiModifierList)

Example 43 with PsiClass

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

the class ConvertSimpleGetterToPropertyIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    GrMethod method = (GrMethod) element.getParent();
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return;
    GrStatement statement = block.getStatements()[0];
    GrExpression value;
    if (statement instanceof GrReturnStatement) {
        value = ((GrReturnStatement) statement).getReturnValue();
    } else {
        value = (GrExpression) statement;
    }
    String fieldName = GroovyPropertyUtils.getPropertyNameByGetter(method);
    if (fieldName == null)
        return;
    PsiClass aClass = method.getContainingClass();
    if (aClass == null)
        return;
    List<String> modifiers = ContainerUtil.newArrayList();
    for (String modifier : MODIFIERS_TO_CHECK) {
        if (method.hasModifierProperty(modifier))
            modifiers.add(modifier);
    }
    modifiers.add(PsiModifier.FINAL);
    GrTypeElement returnTypeElement = method.getReturnTypeElementGroovy();
    PsiType returnType = returnTypeElement == null ? null : returnTypeElement.getType();
    GrVariableDeclaration declaration = GroovyPsiElementFactory.getInstance(project).createFieldDeclaration(ArrayUtil.toStringArray(modifiers), fieldName, value, returnType);
    PsiElement replaced = method.replace(declaration);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiClass(com.intellij.psi.PsiClass) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) PsiType(com.intellij.psi.PsiType)

Example 44 with PsiClass

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

the class ConvertJunitAssertionToAssertStatementIntention method getReplacementStatement.

@Nullable
private static String getReplacementStatement(@NotNull PsiMethod method, @NotNull GrMethodCall methodCall) {
    PsiClass containingClass = method.getContainingClass();
    if (containingClass == null)
        return null;
    String qualifiedName = containingClass.getQualifiedName();
    if (!"junit.framework.Assert".equals(qualifiedName) && !"groovy.util.GroovyTestCase".equals(qualifiedName))
        return null;
    String[] replacementStatements = ourStatementMap.get(method.getName());
    if (replacementStatements == null)
        return null;
    GrArgumentList argumentList = methodCall.getArgumentList();
    if (argumentList.getNamedArguments().length > 0)
        return null;
    GrExpression[] arguments = argumentList.getExpressionArguments();
    if (arguments.length >= replacementStatements.length)
        return null;
    return replacementStatements[arguments.length];
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) PsiClass(com.intellij.psi.PsiClass) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with PsiClass

use of com.intellij.psi.PsiClass in project kotlin by JetBrains.

the class KtEnumEntry method isEquivalentTo.

@Override
public boolean isEquivalentTo(@Nullable PsiElement another) {
    if (another instanceof PsiEnumConstant) {
        PsiEnumConstant enumConstant = (PsiEnumConstant) another;
        PsiClass containingClass = enumConstant.getContainingClass();
        if (containingClass != null) {
            String containingClassQName = containingClass.getQualifiedName();
            if (containingClassQName != null && enumConstant.getName() != null) {
                String theirFQName = containingClassQName + "." + enumConstant.getName();
                if (theirFQName.equals(getQualifiedName())) {
                    return true;
                }
            }
        }
    }
    return super.isEquivalentTo(another);
}
Also used : PsiEnumConstant(com.intellij.psi.PsiEnumConstant) PsiClass(com.intellij.psi.PsiClass)

Aggregations

PsiClass (com.intellij.psi.PsiClass)598 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