private void errrantThisOrExtension( PsiElement element, AnnotationHolder holder ) { if( element instanceof PsiModifierList ) { PsiModifierList mods = (PsiModifierList)element; PsiAnnotation annotation; if( (annotation = mods.findAnnotation( Extension.class.getName() )) != null || (annotation = mods.findAnnotation( This.class.getName() )) != null) { TextRange range = new TextRange( annotation.getTextRange().getStartOffset(), annotation.getTextRange().getEndOffset() ); //noinspection ConstantConditions holder.createErrorAnnotation( range, ExtIssueMsg.MSG_NOT_IN_EXTENSION_CLASS.get( ClassUtil.extractClassName( annotation.getQualifiedName() ) ) ); } } }
private void findPsiClasses( @NotNull @NonNls String name, @NotNull GlobalSearchScope scope, Set<PsiClass> psiClasses, ManModule start, ManModule module ) { for( ITypeManifold tm: module.getTypeManifolds() ) { for( String fqn: tm.getAllTypeNames() ) { String simpleName = ClassUtil.extractClassName( fqn ); if( simpleName.equals( name ) ) { PsiClass psiClass = ManifoldPsiClassCache.instance().getPsiClass( scope, module, fqn ); if( psiClass == null ) { return; } psiClasses.add( psiClass ); } } } for( Dependency d : module.getDependencies() ) { if( module == start || d.isExported() ) { findPsiClasses( name, scope, psiClasses, start, (ManModule)d.getModule() ); } } }
private void findClassFqns( @NotNull HashSet<String> dest, ManModule start, ManModule module ) { for( ITypeManifold tm: module.getTypeManifolds() ) { if( tm.getProducerKind() == ITypeManifold.ProducerKind.Supplemental ) { continue; } dest.addAll( tm.getAllTypeNames().stream().map( ClassUtil::extractClassName ).collect( Collectors.toList() ) ); } for( Dependency d : module.getDependencies() ) { if( module == start || d.isExported() ) { findClassFqns( dest, start, (ManModule)d.getModule() ); } } }
@Override public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { final VirtualFile file = PsiUtilCore.getVirtualFile(element); if (file != null && file.getFileType() instanceof SPIFileType) { final String serviceProviderName = file.getName(); final PsiClass psiClass = ClassUtil.findPsiClass(element.getManager(), serviceProviderName, null, true, element.getContainingFile().getResolveScope()); if (element instanceof PsiFile) { if (psiClass == null) { holder.createErrorAnnotation(element, "No service provider \"" + serviceProviderName + "\' found").setFileLevelAnnotation(true); } } else if (element instanceof SPIClassProviderReferenceElement) { final PsiElement resolve = ((SPIClassProviderReferenceElement)element).resolve(); if (resolve == null) { holder.createErrorAnnotation(element, "Cannot resolve symbol " + element.getText()); } else if (resolve instanceof PsiClass && psiClass != null) { if (!((PsiClass)resolve).isInheritor(psiClass, true)) { holder.createErrorAnnotation(element, "Registered extension should implement " + serviceProviderName); } } } } }
@NotNull @Override public Object[] getVariants() { final String name = getContainingFile().getName(); final PsiClass superProvider = JavaPsiFacade.getInstance(getProject()).findClass(name, getResolveScope()); if (superProvider != null) { final List<Object> result = new ArrayList<Object>(); ClassInheritorsSearch.search(superProvider).forEach(new Processor<PsiClass>() { @Override public boolean process(PsiClass psiClass) { if (!psiClass.hasModifierProperty(PsiModifier.ABSTRACT)) { final String jvmClassName = ClassUtil.getJVMClassName(psiClass); if (jvmClassName != null) { result.add(LookupElementBuilder.create(psiClass, jvmClassName)); } } return true; } }); return ArrayUtil.toObjectArray(result); } return ArrayUtil.EMPTY_OBJECT_ARRAY; }
/** * Converts a (possibly ambiguous) class name like A.B.C.D into an Android-style class name * like A.B$C$D where package names are separated by dots and inner classes are separated by dollar * signs (similar to how the internal JVM format uses dollar signs for inner classes but slashes * as package separators) */ @NotNull private static String getFragmentClass(@NotNull final Module module, @NotNull final String fqcn) { return ApplicationManager.getApplication().runReadAction(new Computable<String>() { @Override @NotNull public String compute() { Project project = module.getProject(); JavaPsiFacade finder = JavaPsiFacade.getInstance(project); PsiClass psiClass = finder.findClass(fqcn, module.getModuleScope()); if (psiClass == null) { psiClass = finder.findClass(fqcn, GlobalSearchScope.allScope(project)); } if (psiClass != null) { String jvmClassName = ClassUtil.getJVMClassName(psiClass); if (jvmClassName != null) { return jvmClassName.replace('/', '.'); } } return fqcn; } }); }
@Override public void fillTestObjects(Map<PsiClass, Map<PsiMethod, List<String>>> classes) throws CantRunException { final TestData data = myConfig.getPersistantData(); //it's a class final PsiClass psiClass = ApplicationManager.getApplication().runReadAction( new Computable<PsiClass>() { @Nullable public PsiClass compute() { return ClassUtil.findPsiClass(PsiManager.getInstance(myConfig.getProject()), data.getMainClassName().replace('/', '.'), null, true, getSearchScope()); } } ); if (psiClass == null) { throw new CantRunException("No tests found in the class \"" + data.getMainClassName() + '\"'); } if (null == ApplicationManager.getApplication().runReadAction(new Computable<String>() { @Nullable public String compute() { return psiClass.getQualifiedName(); } })) { throw new CantRunException("Cannot test anonymous or local class \"" + data.getMainClassName() + '\"'); } calculateDependencies(null, classes, getSearchScope(), psiClass); }
private TestProxy getPackageClassNodeFor(final TestResultMessage result) { TestProxy owner = treeBuilder.getRoot(); final String packageName1 = StringUtil.getPackageName(result.getTestClass()); String packageName = packageName1.length() == 0 ? NO_PACKAGE : packageName1; owner = getChildNodeNamed(owner, packageName); if (owner.getPsiElement() == null) { owner.setPsiElement(JavaPsiFacade.getInstance(project).findPackage(packageName)); } owner = getChildNodeNamed(owner, StringUtil.getShortName(result.getTestClass())); //look up the psiclass now if (owner.getPsiElement() == null) { final TestProxy finalOwner = owner; ApplicationManager.getApplication().runReadAction(new Runnable() { public void run() { finalOwner.setPsiElement(ClassUtil.findPsiClass(PsiManager.getInstance(project), result.getTestClass())); } }); } return owner; }
public ChangeFormComponentTypeFix(PsiPlainTextFile formFile, String fieldName, PsiType componentTypeToSet) { myFormFile = formFile; myFieldName = fieldName; if (componentTypeToSet instanceof PsiClassType) { PsiClass psiClass = ((PsiClassType)componentTypeToSet).resolve(); if (psiClass != null) { myComponentTypeToSet = ClassUtil.getJVMClassName(psiClass); } else { myComponentTypeToSet = ((PsiClassType) componentTypeToSet).rawType().getCanonicalText(); } } else { myComponentTypeToSet = componentTypeToSet.getCanonicalText(); } }
@Override public void actionPerformed(AnActionEvent e) { final PsiClass psiClass = getPsiClass(e); if (psiClass == null) return; final VirtualFile virtualFile = psiClass.getContainingFile().getVirtualFile(); final Module module = ModuleUtilCore.findModuleForPsiElement(psiClass); if (module == null || virtualFile == null) return; final String className = ClassUtil.getJVMClassName(psiClass); final Project project = getEventProject(e); CompilerManager.getInstance(project).make(new FileSetCompileScope(Collections.singletonList(virtualFile), new Module[]{module}), new CompileStatusNotification() { @Override public void finished(boolean aborted, int errors, int warnings, CompileContext compileContext) { if (aborted || errors > 0) return; generateAndShowXml(module, className); } }); }
@Override public void visitReferenceElement( PsiJavaCodeReferenceElement reference) { super.visitReferenceElement(reference); if (reference.isQualified()) { return; } final PsiElement element = reference.resolve(); if (!(element instanceof PsiClass)) { return; } final PsiClass aClass = (PsiClass)element; final String qualifiedName = aClass.getQualifiedName(); final String packageName = ClassUtil.extractPackageName(qualifiedName); if (!importedPackageName.equals(packageName)) { return; } importedClasses.add(aClass); }
@SuppressWarnings("rawtypes") private List<Location> findTestCase(Project project, String path) { String[] parts = path.split(SmRunnerUtils.TEST_NAME_PARTS_SPLITTER, 2); if (parts.length < 2) { return ImmutableList.of(); } String className = parts[0]; String testName = parts[1]; PsiClass testClass = ClassUtil.findPsiClass(PsiManager.getInstance(project), className); for (ScInfixExpr testCase : PsiTreeUtil.findChildrenOfType(testClass, ScInfixExpr.class)) { if (TestNodeProvider.isSpecs2TestExpr(testCase) && Objects.equal(Specs2Utils.getSpecs2ScopedTestName(testCase), testName)) { return ImmutableList.of(new PsiLocation<>(testCase)); } } return ImmutableList.of(); }
private String extractSourceRoot(PsiPackage selectedPackage) { PsiDirectory target; PsiDirectory[] directories = selectedPackage.getDirectories(); if (directories.length == 1) { target = directories[0]; } else { String[] arr = new String[directories.length]; for (int i = 0; i < directories.length; i++) { arr[i] = directories[i].getVirtualFile().getPath(); } int selectedIndex = ComboChooser.show( "Package found in multiple directories", "Select package source root:", arr); if (selectedIndex == -1) { return null; } target = directories[selectedIndex]; } return ClassUtil.sourceRoot(target).getVirtualFile().getPath(); }
@NotNull @Override public Object[] getVariants() { final String name = getContainingFile().getName(); final PsiClass superProvider = JavaPsiFacade.getInstance(getProject()).findClass(name, getResolveScope()); if (superProvider != null) { final List<Object> result = new ArrayList<Object>(); ClassInheritorsSearch.search(superProvider).forEach(new Processor<PsiClass>() { @Override public boolean process(PsiClass psiClass) { final String jvmClassName = ClassUtil.getJVMClassName(psiClass); if (jvmClassName != null) { result.add(LookupElementBuilder.create(psiClass, jvmClassName)); } return false; } }); return ArrayUtil.toObjectArray(result); } return ArrayUtil.EMPTY_OBJECT_ARRAY; }
@Override public void actionPerformed(AnActionEvent e) { final PsiClass psiClass = getPsiClass(e); if (psiClass == null) return; final VirtualFile virtualFile = psiClass.getContainingFile().getVirtualFile(); final Module module = ModuleUtilCore.findModuleForPsiElement(psiClass); if (module == null || virtualFile == null) return; final String className = ClassUtil.getJVMClassName(psiClass); final Project project = getEventProject(e); CompilerManager.getInstance(project).make(new FileSetCompileScope(Arrays.asList(virtualFile), new Module[]{module}), new CompileStatusNotification() { @Override public void finished(boolean aborted, int errors, int warnings, CompileContext compileContext) { if (aborted || errors > 0) return; generateAndShowXml(module, className); } }); }
@Override protected String getTextForElement(PsiElement element) { final PsiAnnotation annotation = (PsiAnnotation)element; final String qualifiedName = annotation.getQualifiedName(); if (qualifiedName == null) { return null; } final String annotationName = ClassUtil.extractClassName(qualifiedName); final PsiElement grandParent = element.getParent().getParent(); if (grandParent instanceof PsiMethod) { return IntentionPowerPackBundle.message( "annotate.overridden.methods.intention.method.name", annotationName); } else { return IntentionPowerPackBundle.message( "annotate.overridden.methods.intention.parameters.name", annotationName); } }
public static String chooseClass(Project project, String initialValue) { PsiManager psiManager = PsiManager.getInstance(project); TreeClassChooser chooser = new TreeJavaClassChooserDialog( "Select a Java class", project, GlobalSearchScope.allScope(project), null, ClassUtil.findPsiClass(psiManager, initialValue)); chooser.showDialog(); PsiClass psiClass = chooser.getSelected(); return psiClass != null ? psiClass.getQualifiedName() : (initialValue.isEmpty() ? null : initialValue); }
@Nullable public static PsiMethod findPsiMethod(PsiManager manager, String externalName) { final int spaceIdx = externalName.indexOf(' '); final String className = externalName.substring(0, spaceIdx); final PsiClass psiClass = ClassUtil.findPsiClass(manager, className); if(psiClass == null) { return null; } try { PsiElementFactory factory = JavaPsiFacade.getInstance(psiClass.getProject()).getElementFactory(); String methodSignature = externalName.substring(spaceIdx + 1); PsiMethod patternMethod = factory.createMethodFromText(methodSignature, psiClass); return psiClass.findMethodBySignature(patternMethod, false); } catch(IncorrectOperationException e) { // Do nothing. Returning null is acceptable in this case. return null; } }
@Override public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { final VirtualFile file = PsiUtilCore.getVirtualFile(element); if (file != null) { final String serviceProviderName = file.getName(); final PsiClass psiClass = ClassUtil.findPsiClass(element.getManager(), serviceProviderName, null, true, element.getContainingFile().getResolveScope()); if (element instanceof SPIFile) { if (psiClass == null) { holder.createErrorAnnotation(element, "No service provider \"" + serviceProviderName + "\' found").setFileLevelAnnotation(true); } } else if (element instanceof SPIClassProviderReferenceElement) { final PsiElement resolve = ((SPIClassProviderReferenceElement)element).resolve(); if (resolve == null) { holder.createErrorAnnotation(element, "Cannot resolve symbol " + element.getText()); } else if (resolve instanceof PsiClass && psiClass != null) { if (!((PsiClass)resolve).isInheritor(psiClass, true)) { holder.createErrorAnnotation(element, "Registered extension should implement " + serviceProviderName); } } } } }
public static boolean hasDefaultImportConflict(String fqName, PsiJavaFile file) { final String shortName = ClassUtil.extractClassName(fqName); final String packageName = ClassUtil.extractPackageName(fqName); final String filePackageName = file.getPackageName(); if (filePackageName.equals(packageName)) { return false; } final Project project = file.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiJavaPackage filePackage = psiFacade.findPackage(filePackageName); if (filePackage == null) { return false; } final PsiClass[] classes = filePackage.getClasses(); for (PsiClass aClass : classes) { final String className = aClass.getName(); if (shortName.equals(className)) { return true; } } return false; }
public static boolean hasJavaLangImportConflict(String fqName, PsiJavaFile file) { final String shortName = ClassUtil.extractClassName(fqName); @NonNls final String packageName = ClassUtil.extractPackageName(fqName); if ("java.lang".equals(packageName)) { return false; } final Project project = file.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiJavaPackage javaLangPackage = psiFacade.findPackage("java.lang"); if (javaLangPackage == null) { return false; } final PsiClass[] classes = javaLangPackage.getClasses(); for (final PsiClass aClass : classes) { final String className = aClass.getName(); if (shortName.equals(className)) { return true; } } return false; }
public static boolean hasJavaLangImportConflict(String fqName, PsiJavaFile file) { final String shortName = ClassUtil.extractClassName(fqName); final String packageName = ClassUtil.extractPackageName(fqName); if (HardcodedMethodConstants.JAVA_LANG.equals(packageName)) { return false; } final Project project = file.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiJavaPackage javaLangPackage = psiFacade.findPackage(HardcodedMethodConstants.JAVA_LANG); if (javaLangPackage == null) { return false; } final PsiClass[] classes = javaLangPackage.getClasses(); for (final PsiClass aClass : classes) { final String className = aClass.getName(); if (shortName.equals(className)) { return true; } } return false; }
@Nullable public static String getClassVMName(@Nullable PsiClass containingClass) { // no support for local classes for now if(containingClass == null || PsiUtil.isLocalClass(containingClass)) { return null; } if(containingClass instanceof PsiAnonymousClass) { String parentName = getClassVMName(PsiTreeUtil.getParentOfType(containingClass, PsiClass.class)); if(parentName == null) { return null; } else { return parentName + JavaAnonymousClassesHelper.getName((PsiAnonymousClass) containingClass); } } return ClassUtil.getJVMClassName(containingClass); }
private static PsiClass getBoundingType( PsiTypeParameter tp ) { PsiReferenceList extendsList = tp.getExtendsList(); PsiClassType[] referencedTypes = extendsList.getReferencedTypes(); if( referencedTypes.length > 0 ) { return referencedTypes[0].resolve(); } return ClassUtil.findPsiClass( tp.getManager(), Object.class.getName() ); }
@Nullable public static PsiClass findClass(@NotNull final String className, @NotNull Project project, final GlobalSearchScope scope) { ApplicationManager.getApplication().assertReadAccessAllowed(); try { if (getArrayClass(className) != null) { return JavaPsiFacade.getInstance(project).getElementFactory() .getArrayClass(LanguageLevelProjectExtension.getInstance(project).getLanguageLevel()); } if (project.isDefault()) { return null; } PsiManager psiManager = PsiManager.getInstance(project); PsiClass psiClass = ClassUtil.findPsiClass(psiManager, className, null, true, scope); if (psiClass == null) { GlobalSearchScope globalScope = GlobalSearchScope.allScope(project); if (!globalScope.equals(scope)) { psiClass = ClassUtil.findPsiClass(psiManager, className, null, true, globalScope); } } return psiClass; } catch (IndexNotReadyException ignored) { return null; } }
@Nullable public static String getClassVMName(@Nullable PsiClass containingClass) { // no support for local classes for now if (containingClass == null || PsiUtil.isLocalClass(containingClass)) return null; if (containingClass instanceof PsiAnonymousClass) { String parentName = getClassVMName(PsiTreeUtil.getParentOfType(containingClass, PsiClass.class)); if (parentName == null) { return null; } else { return parentName + JavaAnonymousClassesHelper.getName((PsiAnonymousClass)containingClass); } } return ClassUtil.getJVMClassName(containingClass); }
@Nullable public static PsiField findPsiField(PsiManager manager, String externalName) { int classNameDelimeter = externalName.lastIndexOf(' '); if (classNameDelimeter > 0 && classNameDelimeter < externalName.length() - 1) { final String className = externalName.substring(0, classNameDelimeter); final String fieldName = externalName.substring(classNameDelimeter + 1); final PsiClass psiClass = ClassUtil.findPsiClass(manager, className); if (psiClass != null) { return psiClass.findFieldByName(fieldName, false); } } return null; }
@NotNull @Override public PsiReference getReference() { return new SPIFileName2ClassReference(this, ApplicationManager.getApplication().runReadAction(new Computable<PsiClass>() { @Override public PsiClass compute() { return ClassUtil.findPsiClass(getManager(), getName(), null, true, getResolveScope()); } })); }
@NotNull @Override public PsiReference[] getReferences() { return ApplicationManager.getApplication().runReadAction(new Computable<PsiReference[]>() { @Override public PsiReference[] compute() { final List<PsiReference> refs = new ArrayList<PsiReference>(); int idx = 0; int d; final String fileName = getName(); while ((d = fileName.indexOf(".", idx)) > -1) { final PsiPackage aPackage = JavaPsiFacade.getInstance(getProject()).findPackage(fileName.substring(0, d)); if (aPackage != null) { refs.add(new SPIFileName2PackageReference(SPIFile.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(SPIFile.this, resolve)); } } } refs.add(reference); return refs.toArray(new PsiReference[refs.size()]); } }); }
@Override public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { if (myClass != null) { final String className = ClassUtil.getJVMClassName(myClass); if (className != null) { final String newFileName = className.substring(0, className.lastIndexOf(myClass.getName())) + newElementName; return getElement().setName(newFileName); } } return getElement(); }
@Override public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException { if (element instanceof PsiClass) { final String className = ClassUtil.getJVMClassName((PsiClass)element); if (className != null) { return getElement().setName(className); } } return getElement(); }