Search in sources :

Example 6 with PsiJavaFile

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

the class CurrentPackageMacro method calculateResult.

@Override
public Result calculateResult(@NotNull Expression[] params, ExpressionContext context) {
    Project project = context.getProject();
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
    if (!(file instanceof PsiJavaFile))
        return new TextResult("");
    return new TextResult(((PsiJavaFile) file).getPackageName());
}
Also used : Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) PsiJavaFile(com.intellij.psi.PsiJavaFile)

Example 7 with PsiJavaFile

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

the class PackageNode method canSelectInLeftTree.

@Override
public boolean canSelectInLeftTree(final Map<PsiFile, Set<PsiFile>> deps) {
    Set<PsiFile> files = deps.keySet();
    String packageName = myPackageQName;
    for (PsiFile file : files) {
        if (file instanceof PsiJavaFile && Comparing.equal(packageName, ((PsiJavaFile) file).getPackageName())) {
            return true;
        }
    }
    return false;
}
Also used : PsiFile(com.intellij.psi.PsiFile) PsiJavaFile(com.intellij.psi.PsiJavaFile)

Example 8 with PsiJavaFile

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

the class AdvHighlightingTest method testSameClassesInSourceAndLib.

public void testSameClassesInSourceAndLib() throws Exception {
    String path = PathManagerEx.getTestDataPath() + BASE_PATH + "/" + getTestName(true);
    VirtualFile root = LocalFileSystem.getInstance().findFileByIoFile(new File(path));
    assert root != null : path;
    loadAllModulesUnder(root);
    configureByExistingFile(root.findFileByRelativePath("src/ppp/SomeClass.java"));
    PsiField field = ((PsiJavaFile) myFile).getClasses()[0].findFieldByName("f", false);
    assert field != null;
    PsiClass aClass = ((PsiClassType) field.getType()).resolve();
    assert aClass != null;
    assertEquals("ppp.BadClass", aClass.getQualifiedName());
    //lies in source
    VirtualFile vFile1 = myFile.getVirtualFile();
    VirtualFile vFile2 = aClass.getContainingFile().getVirtualFile();
    assert vFile1 != null;
    assert vFile2 != null;
    assertEquals(vFile1.getParent(), vFile2.getParent());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiClassType(com.intellij.psi.PsiClassType) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) PsiJavaFile(com.intellij.psi.PsiJavaFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiJavaFile(com.intellij.psi.PsiJavaFile) File(java.io.File)

Example 9 with PsiJavaFile

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

the class SaveJavaAsTemplateHandler method getTemplateText.

@Nullable
public String getTemplateText(final PsiFile psiFile, String fileText, String nameWithoutExtension) {
    if (psiFile instanceof PsiJavaFile) {
        PsiJavaFile javaFile = (PsiJavaFile) psiFile;
        String packageName = javaFile.getPackageName();
        if (packageName.length() > 0) {
            fileText = StringUtil.replace(fileText, packageName, "${PACKAGE_NAME}");
        }
        PsiClass[] classes = javaFile.getClasses();
        PsiClass psiClass = null;
        if ((classes.length > 0)) {
            for (PsiClass aClass : classes) {
                if (nameWithoutExtension.equals(aClass.getName())) {
                    psiClass = aClass;
                    break;
                }
            }
        }
        if (psiClass != null) {
            //todo[myakovlev] ? PsiIdentifier nameIdentifier = psiClass.getNameIdentifier();
            return StringUtil.replace(fileText, nameWithoutExtension, "${NAME}");
        }
    }
    return null;
}
Also used : PsiClass(com.intellij.psi.PsiClass) PsiJavaFile(com.intellij.psi.PsiJavaFile) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with PsiJavaFile

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

the class LombokPsiConverterTest method testJava7.

public void testJava7() {
    @Language("JAVA") String testClass = "package test.pkg;\n" + "\n" + "import java.io.BufferedReader;\n" + "import java.io.FileReader;\n" + "import java.io.IOException;\n" + "import java.lang.reflect.InvocationTargetException;\n" + "import java.util.List;\n" + "import java.util.Map;\n" + "import java.util.TreeMap;\n" + "\n" + "public class Java7LanguageFeatureTest {\n" + "    public void testDiamondOperator() {\n" + "        Map<String, List<Integer>> map = new TreeMap<>();\n" + "    }\n" + "\n" + "    public int testStringSwitches(String value) {\n" + "        final String first = \"first\";\n" + "        final String second = \"second\";\n" + "\n" + "        switch (value) {\n" + "            case first:\n" + "                return 41;\n" + "            case second:\n" + "                return 42;\n" + "            default:\n" + "                return 0;\n" + "        }\n" + "    }\n" + "\n" + "    public String testTryWithResources(String path) throws IOException {\n" + "        try (BufferedReader br = new BufferedReader(new FileReader(path))) {\n" + "            return br.readLine();\n" + "        }\n" + "    }\n" + "\n" + "    public void testNumericLiterals() {\n" + "        int thousand = 1_000;\n" + "        int million = 1_000_000;\n" + "        int binary = 0B01010101;\n" + "    }\n" + "\n" + "    public void testMultiCatch() {\n" + "\n" + "        try {\n" + "            Class.forName(\"java.lang.Integer\").getMethod(\"toString\").invoke(null);\n" + "        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {\n" + "            e.printStackTrace();\n" + "        } catch (ClassNotFoundException e) {\n" + "            // TODO: Logging here\n" + "        }\n" + "    }\n" + "}\n";
    PsiFile psiFile = myFixture.addFileToProject("src/test/pkg/R9.java", testClass);
    // Can't call check(psiFile, testClass) here; the source code won't parse
    // with Lombok's parser since it doesn't support Java 7, so we just manually
    // check that the LombokPsiConverter doesn't abort, and format it's view of
    // the Lombok AST and check that it looks like what we expect; an AST containing
    // fragments usable by lint, but not providing support for syntactic constructs
    // such as try with resources or containing type variables where the diamond operator
    // should have been etc.
    assertTrue(psiFile.getClass().getName(), psiFile instanceof PsiJavaFile);
    PsiJavaFile psiJavaFile = (PsiJavaFile) psiFile;
    CompilationUnit node = LombokPsiConverter.convert(psiJavaFile);
    assertNotNull(node);
    TextFormatter formatter = new TextFormatter();
    node.accept(new SourcePrinter(formatter));
    String actual = formatter.finish();
    assertEquals("package test.pkg;\n" + "\n" + "import java.io.BufferedReader;\n" + "import java.io.FileReader;\n" + "import java.io.IOException;\n" + "import java.lang.reflect.InvocationTargetException;\n" + "import java.util.List;\n" + "import java.util.Map;\n" + "import java.util.TreeMap;\n" + "\n" + "public class Java7LanguageFeatureTest {\n" + "    public void testDiamondOperator() {\n" + "        Map<String, List<Integer>> map = new TreeMap();\n" + "    }\n" + "    \n" + "    public int testStringSwitches(String value) {\n" + "        final String first = \"first\";\n" + "        final String second = \"second\";\n" + "        switch (value) {\n" + "        case first:\n" + "            return 41;\n" + "        case second:\n" + "            return 42;\n" + "        case :\n" + "            return 0;\n" + "        }\n" + "    }\n" + "    \n" + "    public String testTryWithResources(String path) throws IOException {\n" + "        try {\n" + "            return br.readLine();\n" + "        }\n" + "    }\n" + "    \n" + "    public void testNumericLiterals() {\n" + "        int thousand = 1_000;\n" + "        int million = 1_000_000;\n" + "        int binary = 0B01010101;\n" + "    }\n" + "    \n" + "    public void testMultiCatch() {\n" + "        try {\n" + "            Class.forName(\"java.lang.Integer\").getMethod(\"toString\").invoke(null);\n" + "        } catch (?!?INVALID_IDENTIFIER: IllegalAccessException | InvocationTargetException | NoSuchMethodException?!? e) {\n" + "            e.printStackTrace();\n" + "        } catch (ClassNotFoundException e) {\n" + "        }\n" + "    }\n" + "}", actual);
}
Also used : CompilationUnit(lombok.ast.CompilationUnit) SourcePrinter(lombok.ast.printer.SourcePrinter) TextFormatter(lombok.ast.printer.TextFormatter) Language(org.intellij.lang.annotations.Language) PsiFile(com.intellij.psi.PsiFile) PsiJavaFile(com.intellij.psi.PsiJavaFile)

Aggregations

PsiJavaFile (com.intellij.psi.PsiJavaFile)24 PsiClass (com.intellij.psi.PsiClass)10 PsiFile (com.intellij.psi.PsiFile)7 Nullable (org.jetbrains.annotations.Nullable)4 Document (com.intellij.openapi.editor.Document)3 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2 PsiElement (com.intellij.psi.PsiElement)2 PsiMethod (com.intellij.psi.PsiMethod)2 List (java.util.List)2 CompilationUnit (lombok.ast.CompilationUnit)2 SourcePrinter (lombok.ast.printer.SourcePrinter)2 TextFormatter (lombok.ast.printer.TextFormatter)2 NotNull (org.jetbrains.annotations.NotNull)2 SubLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.SubLookupElement)1 LighterASTNode (com.intellij.lang.LighterASTNode)1 GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)1 CaretModel (com.intellij.openapi.editor.CaretModel)1