@Nullable @Override @RequiredReadAction public PsiJavaPackage findPackage(@NotNull String qualifiedName) { if(qualifiedName.equals("cli")) { return new PsiPackageImpl(myPsiManager, myPsiPackageManager, JavaModuleExtension.class, qualifiedName); } if(qualifiedName.startsWith("cli.")) { PsiPackage aPackage = PsiPackageManager.getInstance(myProject).findPackage(qualifiedName.substring(4, qualifiedName.length()), JavaModuleExtension.class); if(aPackage != null) { return new PsiPackageImpl(myPsiManager, myPsiPackageManager, JavaModuleExtension.class, qualifiedName); } } return super.findPackage(qualifiedName); }
@NotNull protected PsiElement[] create(final String newName, final PsiDirectory directory) throws IncorrectOperationException { PsiFile sourceFile = PsiFileFactory.getInstance(directory.getProject()) .createFileFromText(newName + ".java", createClassBody(newName, myRecentGenerateOK, myRecentGenerateCancel, myRecentGenerateMain)); sourceFile = (PsiFile)directory.add(sourceFile); JavaCodeStyleManager.getInstance(directory.getProject()).shortenClassReferences(sourceFile); CodeStyleManager.getInstance(directory.getProject()).reformat(sourceFile); final PsiJavaPackage aPackage = JavaDirectoryService.getInstance().getPackage(directory); final String packageName = aPackage.getQualifiedName(); final String fqClassName = packageName.length() == 0 ? newName : packageName + "." + newName; final String formBody = createFormBody(fqClassName, "/com/intellij/uiDesigner/NewDialog.xml", GuiDesignerConfiguration.getInstance(directory.getProject()).DEFAULT_LAYOUT_MANAGER); final PsiFile formFile = PsiFileFactory.getInstance(directory.getProject()).createFileFromText(newName + ".form", formBody); PsiElement createdFile = directory.add(formFile); PsiClass[] classes = ((PsiJavaFile)sourceFile).getClasses(); return new PsiElement[]{createdFile, classes[0]}; }
private static boolean checkMovePackage(Project project, PsiJavaPackage aPackage) { final PsiDirectory[] directories = aPackage.getDirectories(); final VirtualFile[] virtualFiles = aPackage.occursInPackagePrefixes(); if (directories.length > 1 || virtualFiles.length > 0) { final StringBuffer message = new StringBuffer(); RenameUtil.buildPackagePrefixChangedMessage(virtualFiles, message, aPackage.getQualifiedName()); if (directories.length > 1) { DirectoryAsPackageRenameHandlerBase.buildMultipleDirectoriesInPackageMessage(message, aPackage.getQualifiedName(), directories); message.append("\n\n"); String report = RefactoringBundle .message("all.these.directories.will.be.moved.and.all.references.to.0.will.be.changed", aPackage.getQualifiedName()); message.append(report); } message.append("\n"); message.append(RefactoringBundle.message("do.you.wish.to.continue")); int ret = Messages.showYesNoDialog(project, message.toString(), RefactoringBundle.message("warning.title"), Messages.getWarningIcon()); if (ret != 0) { return false; } } return true; }
@Override public ItemPresentation getPresentation(final PsiJavaPackage aPackage) { return new ColoredItemPresentation() { @Override public TextAttributesKey getTextAttributesKey() { return null; } @Override public String getPresentableText() { return aPackage.getName(); } @Override public String getLocationString() { return aPackage.getQualifiedName(); } @Override public Icon getIcon(boolean open) { return PlatformIcons.PACKAGE_ICON; } }; }
public @Nullable PackageDependenciesNode getFileParentNode(VirtualFile vFile) { LOG.assertTrue(vFile != null); final VirtualFile containingDirectory = vFile.getParent(); LOG.assertTrue(containingDirectory != null); PsiJavaPackage aPackage = null; final String packageName = myFileIndex.getPackageNameByDirectory(containingDirectory); if (packageName != null) { aPackage = myJavaPsiFacade.findPackage(packageName); } if (aPackage != null) { if (myFileIndex.isInLibrarySource(vFile) || myFileIndex.isInLibraryClasses(vFile)) { return getLibraryDirNode(aPackage, getLibraryForFile(vFile)); } else { return getModuleDirNode(aPackage, myFileIndex.getModuleForFile(vFile), getFileScopeType(vFile)); } } return myFileIndex.isInLibrarySource(vFile) ? null : getModuleNode(myFileIndex.getModuleForFile(vFile), getFileScopeType(vFile)); }
private static PsiJavaPackage findLongestExistingPackage(Project project, String packageName) { PsiManager manager = PsiManager.getInstance(project); String nameToMatch = packageName; while(true) { PsiJavaPackage aPackage = JavaPsiFacade.getInstance(manager.getProject()).findPackage(nameToMatch); if(aPackage != null && isWritablePackage(aPackage)) { return aPackage; } int lastDotIndex = nameToMatch.lastIndexOf('.'); if(lastDotIndex >= 0) { nameToMatch = nameToMatch.substring(0, lastDotIndex); } else { return null; } } }
private static PsiJavaPackage findLongestExistingPackage(Module module, String packageName) { final PsiManager manager = PsiManager.getInstance(module.getProject()); String nameToMatch = packageName; while(true) { Query<VirtualFile> vFiles = DirectoryIndex.getInstance(module.getProject()).getDirectoriesByPackageName(nameToMatch, false); PsiDirectory directory = getWritableModuleDirectory(vFiles, module, manager); if(directory != null) { return JavaDirectoryService.getInstance().getPackage(directory); } int lastDotIndex = nameToMatch.lastIndexOf('.'); if(lastDotIndex >= 0) { nameToMatch = nameToMatch.substring(0, lastDotIndex); } else { return null; } } }
private PackageDependenciesNode getModuleDirNode(PsiJavaPackage aPackage, Module module, ScopeType scopeType) { if (aPackage == null) { return getModuleNode(module, scopeType); } Pair<Module, PsiJavaPackage> descriptor = new Pair<Module, PsiJavaPackage>(myShowModules ? module : null, aPackage); PackageNode node = getMap(myModulePackageNodes, scopeType).get(descriptor); if (node != null) return node; node = new PackageNode(aPackage, myFlattenPackages); getMap(myModulePackageNodes, scopeType).put(descriptor, node); if (myFlattenPackages) { getModuleNode(module, scopeType).add(node); } else { getModuleDirNode(aPackage.getParentPackage(), module, scopeType).add(node); } return node; }
public static RefactoringElementListener getListener(final PsiJavaPackage psiPackage, final Accessor<PsiJavaPackage> accessor) { final StringBuilder path = new StringBuilder(); for(PsiJavaPackage parent = accessor.getPsiElement(); parent != null; parent = parent.getParentPackage()) { if(parent.equals(psiPackage)) { return new RefactorPackage(accessor, path.toString()); } if(path.length() > 0) { path.insert(0, '.'); } path.insert(0, parent.getName()); } return null; }
public static RefactoringElementListener getClassOrPackageListener(final PsiElement element, final Accessor<PsiClass> accessor) { if(element instanceof PsiClass) { return getListeners((PsiClass) element, accessor); } if(element instanceof PsiJavaPackage) { final PsiClass aClass = accessor.getPsiElement(); if(aClass == null) { return null; } return getListener((PsiJavaPackage) element, new ClassPackageAccessor(accessor)); } return null; }
private void performAction(String[] packageNames, String newPackageName) throws Exception { final PsiJavaPackage[] packages = new PsiJavaPackage[packageNames.length]; for (int i = 0; i < packages.length; i++) { String packageName = packageNames[i]; packages[i] = JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage(packageName); assertNotNull("Package " + packageName + " not found", packages[i]); } PsiJavaPackage newParentPackage = JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage(newPackageName); assertNotNull(newParentPackage); final PsiDirectory[] dirs = newParentPackage.getDirectories(); assertEquals(dirs.length, 1); new MoveClassesOrPackagesProcessor(myProject, packages, new SingleSourceRootMoveDestination(PackageWrapper.create(newParentPackage), dirs[0]), true, false, null).run(); FileDocumentManager.getInstance().saveAllDocuments(); }
public void setPsiElement(final PsiJavaPackage psiPackage) { if(myInpackageName == null) { return; //we can do nothing } final String classQName = getClassQName(psiPackage.getQualifiedName()); final PsiClass newClass = JUnitUtil.findPsiClass(classQName, myModule, psiPackage.getProject()); if(newClass != null) { myAccessor.setPsiElement(newClass); } else { myAccessor.setName(classQName); } }
protected boolean spansMultipleModules(final String qualifiedName) { if(qualifiedName != null) { final Project project = getConfiguration().getProject(); final PsiJavaPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(qualifiedName); if(aPackage != null) { final TestSearchScope scope = getScope(); if(scope != null) { final SourceScope sourceScope = scope.getSourceScope(getConfiguration()); if(sourceScope != null) { final GlobalSearchScope configurationSearchScope = GlobalSearchScopesCore.projectTestScope(project).intersectWith(sourceScope.getGlobalSearchScope()); final PsiDirectory[] directories = aPackage.getDirectories(configurationSearchScope); return Arrays.stream(directories).map(dir -> ModuleUtilCore.findModuleForFile(dir.getVirtualFile(), project)).filter(Objects::nonNull).distinct().count() > 1; } } } } return false; }
@Override public boolean isEnabledOnElements(@NotNull PsiElement[] elements) { Project project = DataManager.getInstance().getDataContext().getData(CommonDataKeys.PROJECT); if(project == null) { return false; } for(int i = 0; i < elements.length; i++) { PsiElement element = elements[i]; if(!(element instanceof PsiClass || element instanceof PsiJavaFile || element instanceof PsiDirectory || element instanceof PsiJavaPackage)) { return false; } } return true; }
@Override public Collection<PsiJavaPackage> resolvePackageName(@Nullable final PsiJavaPackage context, final String packageName) { if (context == null) return Collections.emptySet(); if (packageName.contains("*")) { final Pattern pattern = PatternUtil.fromMask(packageName); final Set<PsiJavaPackage> packages = new HashSet<PsiJavaPackage>(); processSubPackages(context, new Processor<PsiJavaPackage>() { @Override public boolean process(PsiJavaPackage psiPackage) { String name = psiPackage.getName(); if (name != null && pattern.matcher(name).matches()) { packages.add(psiPackage); } return true; } }); return packages; } else { return super.resolvePackageName(context, packageName); } }
@Override public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception { final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(myProject); final Comparator<PsiDirectory> directoryComparator = new Comparator<PsiDirectory>() { @Override public int compare(PsiDirectory o1, PsiDirectory o2) { return o1.getVirtualFile().getPresentableUrl().compareTo(o2.getVirtualFile().getPresentableUrl()); } }; final PsiJavaPackage sourcePackage = psiFacade.findPackage(myPackageName); assertNotNull(sourcePackage); final PsiDirectory[] srcDirectories = sourcePackage.getDirectories(); assertEquals(srcDirectories.length, 2); Arrays.sort(srcDirectories, directoryComparator); final PsiJavaPackage targetPackage = psiFacade.findPackage(myTargetPackageName); assertNotNull(targetPackage); final PsiDirectory[] targetDirectories = targetPackage.getDirectories(); Arrays.sort(targetDirectories, directoryComparator); assertTrue(targetDirectories.length > 0); preprocessSrcDir(srcDirectories[0]); new MoveDirectoryWithClassesProcessor(getProject(), new PsiDirectory[]{srcDirectories[0]}, targetDirectories[0], false, false, true, null).run(); postProcessTargetDir(targetDirectories[0]); FileDocumentManager.getInstance().saveAllDocuments(); }
@Override public AnalysisScope getData(@NotNull final DataProvider dataProvider) { final Object psiFile = dataProvider.getDataUnchecked(LangDataKeys.PSI_FILE); if(psiFile instanceof PsiJavaFile) { return new JavaAnalysisScope((PsiJavaFile) psiFile); } Object psiTarget = dataProvider.getDataUnchecked(LangDataKeys.PSI_ELEMENT); if(psiTarget instanceof PsiJavaPackage) { PsiJavaPackage pack = (PsiJavaPackage) psiTarget; PsiManager manager = pack.getManager(); if(!manager.isInProject(pack)) { return null; } PsiDirectory[] dirs = pack.getDirectories(GlobalSearchScope.projectScope(manager.getProject())); if(dirs.length == 0) { return null; } return new JavaAnalysisScope(pack, dataProvider.getDataUnchecked(LangDataKeys.MODULE)); } return null; }
@NotNull private static PsiDirectory createParentDirectories(@NotNull PsiDirectory directoryRoot, @NotNull String className) throws IncorrectOperationException { final PsiJavaPackage currentPackage = JavaDirectoryService.getInstance().getPackage(directoryRoot); final String packagePrefix = currentPackage == null? null : currentPackage.getQualifiedName() + "."; final String packageName = extractPackage(packagePrefix != null && className.startsWith(packagePrefix)? className.substring(packagePrefix.length()) : className); final StringTokenizer tokenizer = new StringTokenizer(packageName, "."); while (tokenizer.hasMoreTokens()) { String packagePart = tokenizer.nextToken(); PsiDirectory subdirectory = directoryRoot.findSubdirectory(packagePart); if (subdirectory == null) { directoryRoot.checkCreateSubdirectory(packagePart); subdirectory = directoryRoot.createSubdirectory(packagePart); } directoryRoot = subdirectory; } return directoryRoot; }
@Override public PsiClass createClass(String qualifiedName) { assertValid(); ApplicationManager.getApplication().assertWriteAccessAllowed(); final MigrationClassImpl migrationClass = new MigrationClassImpl(this, qualifiedName); final MigrationClassImpl oldMigrationClass = myQNameToClassMap.put(qualifiedName, migrationClass); LOG.assertTrue(oldMigrationClass == null, qualifiedName); String packageName = parentPackageName(qualifiedName); final PsiJavaPackage aPackage = myFacade.findPackage(packageName); if (aPackage == null) { createPackage(packageName); } List<PsiClass> psiClasses = getClassesList(packageName); psiClasses.add(migrationClass); myMigrationManager.migrationModified(false); return migrationClass; }
@Override public PsiJavaPackage createPackage(String qualifiedName) { assertValid(); ApplicationManager.getApplication().assertWriteAccessAllowed(); final MigrationPackageImpl migrationPackage = new MigrationPackageImpl(this, qualifiedName); final MigrationPackageImpl oldMigrationPackage = myQNameToPackageMap.put(qualifiedName, migrationPackage); LOG.assertTrue(oldMigrationPackage == null, qualifiedName); final String parentName = parentPackageName(qualifiedName); final PsiJavaPackage aPackage = myFacade.findPackage(parentName); if (aPackage == null) { createPackage(parentName); } List<PsiJavaPackage> psiPackages = getSubpackagesList(parentName); psiPackages.add(migrationPackage); myMigrationManager.migrationModified(false); return migrationPackage; }
private PackageDependenciesNode getLibraryDirNode(PsiJavaPackage aPackage, OrderEntry libraryOrJdk) { if (aPackage == null || aPackage.getName() == null) { return getLibraryOrJDKNode(libraryOrJdk); } if (!myShowModules && !myGroupByScopeType) { return getModuleDirNode(aPackage, null, ScopeType.LIB); } Pair<OrderEntry, PsiJavaPackage> descriptor = new Pair<OrderEntry, PsiJavaPackage>(myShowIndividualLibs ? libraryOrJdk : null, aPackage); PackageNode node = getMap(myLibraryPackageNodes, ScopeType.LIB).get(descriptor); if (node != null) return node; node = new PackageNode(aPackage, myFlattenPackages); getMap(myLibraryPackageNodes, ScopeType.LIB).put(descriptor, node); if (myFlattenPackages) { getLibraryOrJDKNode(libraryOrJdk).add(node); } else { getLibraryDirNode(aPackage.getParentPackage(), libraryOrJdk).add(node); } return node; }
public Collection<PsiJavaPackage> resolvePackage() { final PsiPackageReference packageReference = getLastReference(); if(packageReference == null) { return Collections.emptyList(); } return ContainerUtil.map2List(packageReference.multiResolve(false), new NullableFunction<ResolveResult, PsiJavaPackage>() { @Override public PsiJavaPackage fun(final ResolveResult resolveResult) { return (PsiJavaPackage) resolveResult.getElement(); } }); }
@NotNull private Set<PsiJavaPackage> getContext() { if(myIndex == 0) { return myReferenceSet.getInitialContext(); } Set<PsiJavaPackage> psiPackages = new HashSet<PsiJavaPackage>(); for(ResolveResult resolveResult : myReferenceSet.getReference(myIndex - 1).doMultiResolve()) { PsiElement psiElement = resolveResult.getElement(); if(psiElement instanceof PsiJavaPackage) { psiPackages.add((PsiJavaPackage) psiElement); } } return psiPackages; }
public void testBackwardPackageScope(){ final PsiJavaPackage bPackage = JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage("com.a"); final DependenciesBuilder builder = new BackwardDependenciesBuilder(myProject, new JavaAnalysisScope(bPackage, null)); builder.analyze(); final Set<PsiFile> searchFor = new HashSet<PsiFile>(); searchFor.add(myJavaFacade.findClass("com.a.A", GlobalSearchScope.allScope(myProject)).getContainingFile()); final Set<PsiFile> searchIn = new HashSet<PsiFile>(); final PsiClass bClass = myJavaFacade.findClass("com.b.B", GlobalSearchScope.allScope(myProject)); searchIn.add(bClass.getContainingFile()); final PsiClass cClass = myJavaFacade.findClass("com.a.C", GlobalSearchScope.allScope(myProject)); searchFor.add(cClass.getContainingFile()); final UsageInfo[] usagesInfos = FindDependencyUtil.findBackwardDependencies(builder, searchIn, searchFor); final UsageInfo2UsageAdapter[] usages = UsageInfo2UsageAdapter.convert(usagesInfos); final String [] psiUsages = new String [usagesInfos.length]; for (int i = 0; i < usagesInfos.length; i++) { psiUsages[i] = toString(usages[i]); } checkResult(new String []{"(4: 3) A myA = new A();", "(4: 15) A myA = new A();", "(5: 3) C myC = new C();", "(5: 15) C myC = new C();", "(7: 9) myA.aa();", "(8: 9) myC.cc();"}, psiUsages); }
@NotNull @Override @RequiredReadAction public PsiJavaPackage[] getSubPackages(@NotNull PsiJavaPackage psiPackage, @NotNull GlobalSearchScope scope) { String qualifiedName = psiPackage.getQualifiedName(); if(qualifiedName.startsWith("cli")) { String substring = qualifiedName.substring(3, qualifiedName.length()); PsiPackage aPackage = PsiPackageManager.getInstance(myProject).findPackage(substring, DotNetModuleExtension.class); if(aPackage == null) { aPackage = PsiPackageManager.getInstance(myProject).findPackage(substring, JavaModuleExtension.class); } if(aPackage != null) { PsiPackage[] subPackages = aPackage.getSubPackages(scope); if(subPackages.length == 0) { return PsiJavaPackage.EMPTY_ARRAY; } PsiJavaPackage[] packages = new PsiJavaPackage[subPackages.length]; for(int i = 0; i < subPackages.length; i++) { PsiPackage subPackage = subPackages[i]; packages[i] = new PsiPackageImpl(myPsiManager, myPsiPackageManager, JavaModuleExtension.class, subPackage.getQualifiedName()); } return packages; } } return super.getSubPackages(psiPackage, scope); }
@NotNull @Override @RequiredReadAction public PsiClass[] getClasses(@NotNull PsiJavaPackage psiPackage, @NotNull GlobalSearchScope scope) { boolean cli = false; String qualifiedName = psiPackage.getQualifiedName(); if(StringUtil.startsWith(qualifiedName, "cli.")) { qualifiedName = qualifiedName.substring(4, qualifiedName.length()); cli = true; } DotNetNamespaceAsElement namespace = DotNetPsiSearcher.getInstance(myProject).findNamespace(qualifiedName, scope); if(namespace == null) { return PsiClass.EMPTY_ARRAY; } PsiElement[] children = namespace.getChildren(scope, DotNetNamespaceAsElement.ChildrenFilter.ONLY_ELEMENTS); if(children.length == 0) { return PsiClass.EMPTY_ARRAY; } return toClasses(children, cli); }
private void setEditorText(final String className) { final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(myProject); PsiJavaPackage defaultPackage = JavaPsiFacade.getInstance(myProject).findPackage(""); final PsiCodeFragment fragment = factory.createReferenceCodeFragment(className, defaultPackage, true, true); myDocument = PsiDocumentManager.getInstance(myProject).getDocument(fragment); myEditorTextField.setDocument(myDocument); updateOKAction(); }
public PsiDirectory[] getDirectories() { String qName = myQualifiedName; while (qName.endsWith(".")) { qName = StringUtil.trimEnd(qName, "."); } final PsiJavaPackage aPackage = JavaPsiFacade.getInstance(myManager.getProject()).findPackage(qName); if (aPackage != null) { return aPackage.getDirectories(); } else { return PsiDirectory.EMPTY_ARRAY; } }
@Override public boolean isReferenceTo(final PsiElement element) { if (!(element instanceof PsiJavaPackage)) { return false; } final String qName = ((PsiJavaPackage)element).getQualifiedName().replace('.', '/'); final String rangeText = getRangeText(); return qName.equals(rangeText); }
private void setEditorText(final String s) { final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(myProject); PsiJavaPackage defaultPackage = JavaPsiFacade.getInstance(myProject).findPackage(""); final PsiCodeFragment fragment = factory.createReferenceCodeFragment(s, defaultPackage, true, true); myDocument = PsiDocumentManager.getInstance(myProject).getDocument(fragment); myEditorTextField.setDocument(myDocument); }
private void createUIComponents() { myEditorTextField1 = new EditorTextField("", myProject, JavaFileType.INSTANCE); final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(myProject); PsiJavaPackage defaultPackage = JavaPsiFacade.getInstance(myProject).findPackage(""); final PsiCodeFragment fragment = factory.createReferenceCodeFragment("", defaultPackage, true, true); myEditorTextField1.setDocument(PsiDocumentManager.getInstance(myProject).getDocument(fragment)); }
public void testPackageScope2() { //com.b<->com.a - find //com.c<->com.d - not in scope final CyclicDependenciesBuilder builder = new CyclicDependenciesBuilder(myProject, new JavaAnalysisScope(JavaPsiFacade .getInstance(myPsiManager.getProject()).findPackage( "com.subscope1"), null)); builder.analyze(); final HashMap<PsiJavaPackage, Set<List<PsiJavaPackage>>> cyclicDependencies = builder.getCyclicDependencies(); HashMap<String, String[][]> expected = new HashMap<String, String[][]>(); expected.put("com.subscope1.b", new String[][]{{"com.subscope1.a", "com.subscope1.b"}}); expected.put("com.subscope1.a", new String[][]{{"com.subscope1.b", "com.subscope1.a"}}); checkResult(expected, cyclicDependencies); }
@Nullable private static String verifyDestinationForElement(final PsiElement element, final MoveDestination moveDestination) { final String message; if (element instanceof PsiDirectory) { message = moveDestination.verify((PsiDirectory)element); } else if (element instanceof PsiJavaPackage) { message = moveDestination.verify((PsiJavaPackage)element); } else { message = moveDestination.verify(element.getContainingFile()); } return message; }
@NotNull @Override public PsiReference[] getReferences() { final List<PsiReference> refs = new ArrayList<PsiReference>(); int idx = 0; int d; final String fileName = getName(); while ((d = fileName.indexOf(".", idx)) > -1) { final PsiJavaPackage aPackage = JavaPsiFacade.getInstance(getProject()).findPackage(fileName.substring(0, d)); if (aPackage != null) { refs.add(new SPIFileName2PackageReference(this, aPackage)); } idx = d + 1; } final PsiReference reference = getReference(); PsiElement resolve = reference.resolve(); while (resolve instanceof PsiClass) { resolve = ((PsiClass)resolve).getContainingClass(); if (resolve != null) { final String jvmClassName = ClassUtil.getJVMClassName((PsiClass)resolve); if (jvmClassName != null) { refs.add(new SPIFileName2PackageReference(this, resolve)); } } } refs.add(reference); return refs.toArray(new PsiReference[refs.size()]); }
private void setClassesToMove(final PsiClass[] classesToMove) { myClassesToMove = classesToMove; mySourcePackage = new PsiJavaPackage[classesToMove.length]; mySourceVisibility = new String[classesToMove.length]; for (int i = 0; i < classesToMove.length; i++) { PsiClass psiClass = classesToMove[i]; mySourceVisibility[i] = VisibilityUtil.getVisibilityModifier(psiClass.getModifierList()); mySourcePackage[i] = JavaDirectoryService.getInstance().getPackage(psiClass.getContainingFile().getContainingDirectory()); } }
@Nullable @RequiredReadAction public static Document createDocument(final String text, Project project, boolean isClassesAccepted) { final PsiJavaPackage defaultPackage = JavaPsiFacade.getInstance(project).findPackage(""); final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(project); final JavaCodeFragment fragment = factory.createReferenceCodeFragment(text, defaultPackage, true, isClassesAccepted); fragment.setVisibilityChecker(JavaCodeFragment.VisibilityChecker.EVERYTHING_VISIBLE); return PsiDocumentManager.getInstance(project).getDocument(fragment); }
public static ElementPresentation forElement(PsiElement psiElement) { if (psiElement == null || !psiElement.isValid()) return new InvalidPresentation(); if (psiElement instanceof PsiDirectory) return new ForDirectory((PsiDirectory)psiElement); if (psiElement instanceof PsiFile) return new ForFile((PsiFile)psiElement); if (psiElement instanceof PsiJavaPackage) return new ForPackage((PsiJavaPackage)psiElement); if (psiElement instanceof XmlTag) return new ForXmlTag((XmlTag)psiElement); if (psiElement instanceof PsiAnonymousClass) return new ForAnonymousClass((PsiAnonymousClass)psiElement); if (psiElement instanceof PsiClass) return new ForClass((PsiClass)psiElement); if (psiElement instanceof PsiMethod) return new ForMethod((PsiMethod)psiElement); if (psiElement instanceof PsiField) return new ForField((PsiField)psiElement); return new ForGeneralElement(psiElement); }
public void testT1() { // com.a<->com.b final CyclicDependenciesBuilder builder = new CyclicDependenciesBuilder(myProject, new AnalysisScope(myProject)); builder.analyze(); final HashMap<PsiJavaPackage, Set<List<PsiJavaPackage>>> cyclicDependencies = builder.getCyclicDependencies(); HashMap<String, String[][]> expected = new HashMap<String, String[][]>(); expected.put("com.b", new String[][]{{"com.a", "com.b"}}); expected.put("com.a", new String[][]{{"com.b", "com.a"}}); checkResult(expected, cyclicDependencies); }
public boolean isInPackage(PsiJavaPackage aPackage) { PsiElement myContext = getContext(); if(myContext != null) { return JavaPsiFacade.getInstance(myContext.getProject()).isInPackage(myContext, aPackage); } return aPackage == null || aPackage.getQualifiedName().isEmpty(); }
public void testForwardPackageScope(){ final PsiJavaPackage bPackage = JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage("com.b"); final DependenciesBuilder builder = new ForwardDependenciesBuilder(myProject, new JavaAnalysisScope(bPackage, null)); builder.analyze(); final Set<PsiFile> searchFor = new HashSet<PsiFile>(); searchFor.add(myJavaFacade.findClass("com.a.A", GlobalSearchScope.allScope(myProject)).getContainingFile()); final Set<PsiFile> searchIn = new HashSet<PsiFile>(); final PsiClass bClass = myJavaFacade.findClass("com.b.B", GlobalSearchScope.allScope(myProject)); searchIn.add(bClass.getContainingFile()); final PsiClass cClass = myJavaFacade.findClass("com.b.C", GlobalSearchScope.allScope(myProject)); searchIn.add(cClass.getContainingFile()); final UsageInfo[] usagesInfos = FindDependencyUtil.findDependencies(builder, searchIn, searchFor); final UsageInfo2UsageAdapter[] usages = UsageInfo2UsageAdapter.convert(usagesInfos); final String [] psiUsages = new String [usagesInfos.length]; for (int i = 0; i < usagesInfos.length; i++) { psiUsages[i] = toString(usages[i]); } checkResult(new String []{ "(2: 14) import com.a.A;", "(4: 3) A myA = new A();", "(4: 15) A myA = new A();", "(6: 9) myA.aa();", "(2: 14) import com.a.A;", "(4: 3) A myA = new A();", "(4: 15) A myA = new A();", "(6: 9) myA.aa();"}, psiUsages); }