use of com.intellij.psi.PsiJavaFile in project intellij-community by JetBrains.
the class GotoClassTest method testGotoClass.
public void testGotoClass() throws Exception {
PsiJavaFile file = (PsiJavaFile) myFixture.configureByText("Foo.java", "public class Foo {\n" + "}\n" + "\n" + "class Bar {}\n");
VirtualFile virtualFile = file.getVirtualFile();
assertNotNull(virtualFile);
myManager.openFile(virtualFile, true);
assertEquals(0, getOffset(virtualFile));
myManager.closeAllFiles();
PsiClass psiClass = file.getClasses()[1];
int identifierOffset = psiClass.getNameIdentifier().getTextOffset();
NavigationUtil.activateFileWithPsiElement(psiClass);
assertEquals(identifierOffset, getOffset(virtualFile));
// it's still inside the class, so keep it
getEditor(virtualFile).getCaretModel().moveToOffset(identifierOffset + 3);
myManager.closeAllFiles();
NavigationUtil.activateFileWithPsiElement(psiClass);
assertEquals(identifierOffset + 3, getOffset(virtualFile));
getEditor(virtualFile).getCaretModel().moveToOffset(0);
NavigationUtil.activateFileWithPsiElement(psiClass);
assertEquals(identifierOffset, getOffset(virtualFile));
myManager.closeAllFiles();
// GoTo file should keep offset
NavigationUtil.activateFileWithPsiElement(file);
assertEquals(identifierOffset, getOffset(virtualFile));
}
use of com.intellij.psi.PsiJavaFile in project intellij-community by JetBrains.
the class JavaImportOptimizer method processFile.
@Override
@NotNull
public Runnable processFile(final PsiFile file) {
if (!(file instanceof PsiJavaFile)) {
return EmptyRunnable.getInstance();
}
Project project = file.getProject();
final PsiImportList newImportList = JavaCodeStyleManager.getInstance(project).prepareOptimizeImportsResult((PsiJavaFile) file);
if (newImportList == null)
return EmptyRunnable.getInstance();
return new CollectingInfoRunnable() {
private int myImportListLengthDiff;
@Override
public void run() {
try {
final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject());
final Document document = manager.getDocument(file);
if (document != null) {
manager.commitDocument(document);
}
final PsiImportList oldImportList = ((PsiJavaFile) file).getImportList();
assert oldImportList != null;
int importsBefore = oldImportList.getAllImportStatements().length;
oldImportList.replace(newImportList);
myImportListLengthDiff = importsBefore - newImportList.getAllImportStatements().length;
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
@Nullable
@Override
public String getUserNotificationInfo() {
if (myImportListLengthDiff > 0) {
return "removed " + myImportListLengthDiff + " import" + (myImportListLengthDiff > 1 ? "s" : "");
}
return null;
}
};
}
use of com.intellij.psi.PsiJavaFile in project intellij-community by JetBrains.
the class ControlFlowTest method testMethodWithOnlyDoWhileStatementHasExitPoints.
public void testMethodWithOnlyDoWhileStatementHasExitPoints() throws Exception {
configureFromFileText("a.java", "public class Foo {\n" + " public void foo() {\n" + " boolean f;\n" + " do {\n" + " f = something();\n" + " } while (f);\n" + " }\n" + "}");
final PsiCodeBlock body = ((PsiJavaFile) getFile()).getClasses()[0].getMethods()[0].getBody();
ControlFlow flow = ControlFlowFactory.getInstance(getProject()).getControlFlow(body, new LocalsControlFlowPolicy(body), false);
IntArrayList exitPoints = new IntArrayList();
ControlFlowUtil.findExitPointsAndStatements(flow, 0, flow.getSize() - 1, exitPoints, ControlFlowUtil.DEFAULT_EXIT_STATEMENTS_CLASSES);
assertEquals(1, exitPoints.size());
}
use of com.intellij.psi.PsiJavaFile in project intellij-community by JetBrains.
the class ClassInheritorsTest method testStressInPresenceOfPCEs.
public void testStressInPresenceOfPCEs() throws IOException {
// no write action can go through while we test
ApplicationManager.getApplication().assertIsDispatchThread();
int N = 1000;
PsiJavaFile file0 = (PsiJavaFile) myFixture.addFileToProject("C0.java", "class C0 { }");
for (int i = 1; i < N; i++) {
// 10 inheritors
int extI = i - 1 - (i - 1) % 10;
myFixture.addClass("class C" + i + " extends C" + extI + " { }");
}
PsiClass class0 = file0.getClasses()[0];
int delayToCancel = 100;
for (int i = 0; i < 1000; i++) {
//System.out.println("i = " + i+ "; delayToCancel="+delayToCancel);
StandardProgressIndicatorBase progress = new StandardProgressIndicatorBase();
JobScheduler.getScheduler().schedule(progress::cancel, delayToCancel, TimeUnit.MILLISECONDS);
try {
Collections.nCopies(Runtime.getRuntime().availableProcessors(), "").stream().parallel().forEach(__ -> {
Collection<PsiClass> inheritors = Collections.synchronizedSet(new THashSet<>());
ProgressManager.getInstance().executeProcessUnderProgress(() -> {
boolean success = ClassInheritorsSearch.search(class0).forEach(new CommonProcessors.CollectProcessor<>(inheritors));
if (N - 1 != inheritors.size() || !success) {
assertEquals(N - 1, inheritors.size());
}
}, progress);
});
myFixture.getPsiManager().dropResolveCaches();
//System.out.println("Iterated all");
delayToCancel--;
} catch (ProcessCanceledException e) {
//System.out.println("e = " + e);
delayToCancel++;
}
}
}
use of com.intellij.psi.PsiJavaFile in project intellij-community by JetBrains.
the class ClassNameDiffersFromFileNameInspection method buildFix.
@Override
@Nullable
protected InspectionGadgetsFix buildFix(Object... infos) {
final PsiJavaFile file = (PsiJavaFile) infos[0];
final String fileName = file.getName();
final int prefixIndex = fileName.indexOf((int) '.');
final String filenameWithoutPrefix = fileName.substring(0, prefixIndex);
final PsiClass[] classes = file.getClasses();
for (PsiClass psiClass : classes) {
final String className = psiClass.getName();
if (filenameWithoutPrefix.equals(className)) {
return null;
}
}
return new RenameFix(filenameWithoutPrefix);
}
Aggregations