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());
}
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;
}
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());
}
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;
}
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);
}
Aggregations