private static PsiReferenceProcessor createReferenceProcessor(@NotNull final List<UsagesProcessor> processors, final GlobalInspectionContext context) { return new PsiReferenceProcessor() { @Override public boolean execute(PsiReference reference) { AnalysisScope scope = context.getRefManager().getScope(); if (scope.contains(reference.getElement()) && reference.getElement().getLanguage() == JavaLanguage.INSTANCE || PsiTreeUtil.getParentOfType(reference.getElement(), PsiDocComment.class) != null) { return true; } synchronized (processors) { UsagesProcessor[] processorsArrayed = processors.toArray(new UsagesProcessor[processors.size()]); for (UsagesProcessor processor : processorsArrayed) { if (!processor.process(reference)) { processors.remove(processor); } } } return !processors.isEmpty(); } }; }
private static void processPackageReferences(final PsiPlainTextFile file, final PsiReferenceProcessor processor, final XmlAttribute attribute) { final TextRange valueRange = getValueRange(attribute); final String value = attribute.getValue(); int pos=-1; while(true) { pos = value.indexOf('/', pos+1); if (pos < 0) { break; } processor.execute(new FormPackageReference(file, new TextRange(valueRange.getStartOffset(), valueRange.getStartOffset() + pos))); } }
private static void processNestedFormReference(final XmlTag tag, final PsiReferenceProcessor processor, final PsiPlainTextFile file) { final XmlAttribute formFileAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_FORM_FILE, null); if (formFileAttribute != null) { processPackageReferences(file, processor, formFileAttribute); processor.execute(new ResourceFileReference(file, getValueRange(formFileAttribute))); } }
private static void processButtonGroupReference(final XmlTag tag, final PsiReferenceProcessor processor, final PsiPlainTextFile file, final PsiReference classReference) { final XmlAttribute boundAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_BOUND, null); final XmlAttribute nameAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_NAME, null); if (boundAttribute != null && Boolean.parseBoolean(boundAttribute.getValue()) && nameAttribute != null) { processor.execute(new FieldFormReference(file, classReference, getValueRange(nameAttribute), null, null, false)); } }
private static CachedFormData getCachedData(final PsiPlainTextFile element) { CachedValue<CachedFormData> data = element.getUserData(CACHED_DATA); if(data == null) { data = CachedValuesManager.getManager(element.getProject()).createCachedValue(new CachedValueProvider<CachedFormData>() { final Map<String, Pair<PsiType, TextRange>> map = new HashMap<String, Pair<PsiType, TextRange>>(); public Result<CachedFormData> compute() { final PsiReferenceProcessor.CollectElements processor = new PsiReferenceProcessor.CollectElements() { public boolean execute(PsiReference ref) { if (ref instanceof FieldFormReference) { final FieldFormReference fieldRef = ((FieldFormReference)ref); final String componentClassName = fieldRef.getComponentClassName(); if (componentClassName != null) { final PsiClassType type = JavaPsiFacade.getInstance(element.getProject()).getElementFactory() .createTypeByFQClassName(componentClassName, element.getResolveScope()); map.put(fieldRef.getRangeText(), new Pair<PsiType, TextRange>(type, fieldRef.getComponentClassNameTextRange())); } } return super.execute(ref); } }; processReferences(element, processor); final PsiReference[] refs = processor.toArray(PsiReference.EMPTY_ARRAY); return new Result<CachedFormData>(new CachedFormData(refs, map), element); } }, false); element.putUserData(CACHED_DATA, data); } return data.getValue(); }
private static void processReferences(final PsiPlainTextFile file, final PsiReferenceProcessor processor) { final Project project = file.getProject(); final XmlTag rootTag = ApplicationManager.getApplication().runReadAction(new Computable<XmlTag>() { public XmlTag compute() { final XmlFile xmlFile = (XmlFile) PsiFileFactory.getInstance(project).createFileFromText("a.xml", XmlFileType.INSTANCE, file.getViewProvider().getContents()); return xmlFile.getRootTag(); } }); if (rootTag == null || !Utils.FORM_NAMESPACE.equals(rootTag.getNamespace())) { return; } @NonNls final String name = rootTag.getName(); if (!"form".equals(name)){ return; } PsiReference classReference = null; final XmlAttribute classToBind = rootTag.getAttribute("bind-to-class", null); if (classToBind != null) { // reference to class final XmlAttributeValue valueElement = classToBind.getValueElement(); if (valueElement == null) { return; } final String className = valueElement.getValue().replace('$','.'); final PsiReference[] referencesByString = new JavaClassReferenceProvider().getReferencesByString(className, file, valueElement.getTextRange().getStartOffset() + 1); if(referencesByString.length < 1){ // There are no references there return; } for (PsiReference aReferencesByString : referencesByString) { processor.execute(aReferencesByString); } classReference = referencesByString[referencesByString.length - 1]; } final PsiReference finalClassReference = classReference; ApplicationManager.getApplication().runReadAction(new Runnable() { public void run() { processReferences(rootTag, finalClassReference, file, processor); } }); }
private static void processReferences(final XmlTag tag, final PsiReference classReference, final PsiPlainTextFile file, final PsiReferenceProcessor processor) { final XmlAttribute clsAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_CLASS, null); final String classNameStr = clsAttribute != null? clsAttribute.getValue().replace('$','.') : null; // field { final XmlAttribute bindingAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_BINDING, null); if (bindingAttribute != null && classReference != null) { final XmlAttribute customCreateAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_CUSTOM_CREATE, null); boolean customCreate = (customCreateAttribute != null && Boolean.parseBoolean(customCreateAttribute.getValue())); final TextRange nameRange = clsAttribute != null ? getValueRange(clsAttribute) : null; processor.execute(new FieldFormReference(file, classReference, getValueRange(bindingAttribute), classNameStr, nameRange, customCreate)); } final XmlAttribute titleBundleAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_TITLE_RESOURCE_BUNDLE, null); final XmlAttribute titleKeyAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_TITLE_KEY, null); if (titleBundleAttribute != null && titleKeyAttribute != null) { processResourceBundleFileReferences(file, processor, titleBundleAttribute); processor.execute(new ResourceBundleKeyReference(file, titleBundleAttribute.getValue(), getValueRange(titleKeyAttribute))); } final XmlAttribute bundleAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_RESOURCE_BUNDLE, null); final XmlAttribute keyAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_KEY, null); if (bundleAttribute != null && keyAttribute != null) { processResourceBundleFileReferences(file, processor, bundleAttribute); processor.execute(new ResourceBundleKeyReference(file, bundleAttribute.getValue(), getValueRange(keyAttribute))); } processNestedFormReference(tag, processor, file); processButtonGroupReference(tag, processor, file, classReference); } // component class { if (clsAttribute != null) { final JavaClassReferenceProvider provider = new JavaClassReferenceProvider(); final PsiReference[] referencesByString = provider.getReferencesByString(classNameStr, file, clsAttribute.getValueElement().getTextRange().getStartOffset() + 1); if(referencesByString.length < 1){ // There are no references there return; } for (PsiReference aReferencesByString : referencesByString) { processor.execute(aReferencesByString); } } } // property references XmlTag parentTag = tag.getParentTag(); if (parentTag != null && parentTag.getName().equals(UIFormXmlConstants.ELEMENT_PROPERTIES)) { XmlTag componentTag = parentTag.getParentTag(); if (componentTag != null) { String className = componentTag.getAttributeValue(UIFormXmlConstants.ATTRIBUTE_CLASS, Utils.FORM_NAMESPACE); if (className != null) { processPropertyReference(tag, processor, file, className.replace('$', '.')); } } } final XmlTag[] subtags = tag.getSubTags(); for (XmlTag subtag : subtags) { processReferences(subtag, classReference, file, processor); } }
private static void processResourceBundleFileReferences(final PsiPlainTextFile file, final PsiReferenceProcessor processor, final XmlAttribute titleBundleAttribute) { processPackageReferences(file, processor, titleBundleAttribute); processor.execute(new ResourceBundleFileReference(file, getValueRange(titleBundleAttribute))); }
private static void processPropertyReference(final XmlTag tag, final PsiReferenceProcessor processor, final PsiPlainTextFile file, final String className) { final XmlAttribute valueAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_VALUE, null); if (valueAttribute != null) { PsiReference reference = ApplicationManager.getApplication().runReadAction(new Computable<PsiReference>() { @Nullable public PsiReference compute() { final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(file.getProject()); final Module module = ModuleUtil.findModuleForPsiElement(file); if (module == null) return null; final GlobalSearchScope scope = module.getModuleWithDependenciesAndLibrariesScope(false); PsiClass psiClass = psiFacade.findClass(className, scope); if (psiClass != null) { PsiMethod getter = PropertyUtil.findPropertyGetter(psiClass, tag.getName(), false, true); if (getter != null) { final PsiType returnType = getter.getReturnType(); if (returnType instanceof PsiClassType) { PsiClassType propClassType = (PsiClassType)returnType; PsiClass propClass = propClassType.resolve(); if (propClass != null) { if (propClass.isEnum()) { return new FormEnumConstantReference(file, getValueRange(valueAttribute), propClassType); } PsiClass iconClass = psiFacade.findClass("javax.swing.Icon", scope); if (iconClass != null && InheritanceUtil.isInheritorOrSelf(propClass, iconClass, true)) { return new ResourceFileReference(file, getValueRange(valueAttribute)); } } } } } return null; }}); if (reference != null) { if (reference instanceof ResourceFileReference) { processPackageReferences(file, processor, valueAttribute); } processor.execute(reference); } } }
private static void processReferences(final PsiPlainTextFile file, final PsiReferenceProcessor processor) { final Project project = file.getProject(); final XmlTag rootTag = ApplicationManager.getApplication().runReadAction(new Computable<XmlTag>() { public XmlTag compute() { final XmlFile xmlFile = (XmlFile) PsiFileFactory.getInstance(project).createFileFromText("a.xml", XmlFileType.INSTANCE, file.getText()); return xmlFile.getRootTag(); } }); if (rootTag == null || !Utils.FORM_NAMESPACE.equals(rootTag.getNamespace())) { return; } @NonNls final String name = rootTag.getName(); if (!"form".equals(name)){ return; } PsiReference classReference = null; final XmlAttribute classToBind = rootTag.getAttribute("bind-to-class", null); if (classToBind != null) { // reference to class final XmlAttributeValue valueElement = classToBind.getValueElement(); if (valueElement == null) { return; } final String className = valueElement.getValue().replace('$','.'); final PsiReference[] referencesByString = new JavaClassReferenceProvider().getReferencesByString(className, file, valueElement.getTextRange().getStartOffset() + 1); if(referencesByString.length < 1){ // There are no references there return; } for (PsiReference aReferencesByString : referencesByString) { processor.execute(aReferencesByString); } classReference = referencesByString[referencesByString.length - 1]; } final PsiReference finalClassReference = classReference; ApplicationManager.getApplication().runReadAction(new Runnable() { public void run() { processReferences(rootTag, finalClassReference, file, processor); } }); }