use of com.intellij.psi.PsiField in project intellij-community by JetBrains.
the class StaticFieldCanBeMovedToUseInspection method checkElement.
@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager inspectionManager, @NotNull GlobalInspectionContext globalInspectionContext) {
if (!(refEntity instanceof RefField)) {
return null;
}
final RefField refField = (RefField) refEntity;
final PsiField field = refField.getElement();
if (field == null) {
return null;
}
final PsiType type = field.getType();
if (!type.equals(PsiType.BOOLEAN)) {
return null;
}
final RefClass fieldClass = refField.getOwnerClass();
final Collection<RefElement> inReferences = refField.getInReferences();
final RefJavaUtil refUtil = RefJavaUtil.getInstance();
final Set<RefClass> classesUsed = new HashSet<>();
for (RefElement inReference : inReferences) {
final RefClass referringClass = refUtil.getOwnerClass(inReference);
if (referringClass == null) {
return null;
}
if (referringClass.equals(fieldClass)) {
return null;
}
classesUsed.add(referringClass);
if (classesUsed.size() > 1) {
return null;
}
}
if (classesUsed.size() != 1) {
return null;
}
final RefClass referencingClass = classesUsed.iterator().next();
final String errorString = "Static field " + refEntity.getName() + " is only accessed in subclass " + referencingClass.getName();
return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
use of com.intellij.psi.PsiField in project intellij-community by JetBrains.
the class GebContentDeclarationSearcher method findDeclarationsAt.
@Override
public void findDeclarationsAt(@NotNull PsiElement element, int offsetInElement, Consumer<PomTarget> consumer) {
PsiElement grCall = element.getParent();
if (!(grCall instanceof GrMethodCall))
return;
PsiElement grClosure = grCall.getParent();
if (!(grClosure instanceof GrClosableBlock))
return;
PsiElement contentField = grClosure.getParent();
if (!(contentField instanceof GrField))
return;
GrField field = (GrField) contentField;
if (!"content".equals(field.getName()) || !field.hasModifierProperty(PsiModifier.STATIC))
return;
PsiClass containingClass = field.getContainingClass();
if (!InheritanceUtil.isInheritor(containingClass, "geb.Page") && !InheritanceUtil.isInheritor(containingClass, "geb.Module"))
return;
Map<String, PsiField> elements = GebUtil.getContentElements(containingClass);
for (PsiField f : elements.values()) {
if (f.getNavigationElement() == element) {
consumer.consume(f);
return;
}
}
}
use of com.intellij.psi.PsiField in project intellij-community by JetBrains.
the class ConvertMethodToClosureIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
final GrMethod method;
if (element.getParent() instanceof GrMethod) {
method = (GrMethod) element.getParent();
} else {
final PsiReference ref = element.getReference();
LOG.assertTrue(ref != null);
final PsiElement resolved = ref.resolve();
LOG.assertTrue(resolved instanceof GrMethod);
method = (GrMethod) resolved;
}
final PsiClass containingClass = method.getContainingClass();
final String methodName = method.getName();
final PsiField field = containingClass.findFieldByName(methodName, true);
if (field != null) {
conflicts.putValue(field, GroovyIntentionsBundle.message("field.already.exists", methodName));
}
final Collection<PsiReference> references = MethodReferencesSearch.search(method).findAll();
final Collection<GrReferenceExpression> usagesToConvert = new HashSet<>(references.size());
for (PsiReference ref : references) {
final PsiElement psiElement = ref.getElement();
if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("method.is.used.outside.of.groovy"));
} else if (!PsiUtil.isMethodUsage(psiElement)) {
if (psiElement instanceof GrReferenceExpression) {
if (((GrReferenceExpression) psiElement).hasMemberPointer()) {
usagesToConvert.add((GrReferenceExpression) psiElement);
}
}
}
}
if (!conflicts.isEmpty()) {
ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(method, usagesToConvert));
conflictsDialog.show();
if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
}
execute(method, usagesToConvert);
}
use of com.intellij.psi.PsiField in project intellij-community by JetBrains.
the class ComponentTree method getData.
/**
* Provides {@link PlatformDataKeys#NAVIGATABLE} to navigate to
* binding of currently selected component (if any)
*/
public Object getData(final String dataId) {
if (GuiEditor.DATA_KEY.is(dataId)) {
return myEditor;
}
if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) {
return myDeleteProvider;
}
if (PlatformDataKeys.COPY_PROVIDER.is(dataId) || PlatformDataKeys.CUT_PROVIDER.is(dataId) || PlatformDataKeys.PASTE_PROVIDER.is(dataId)) {
return myEditor == null ? null : myEditor.getData(dataId);
}
if (LW_INSPECTION_SUPPRESSION_ARRAY_DATA_KEY.is(dataId)) {
Collection<LwInspectionSuppression> elements = getSelectedElements(LwInspectionSuppression.class);
return elements.size() == 0 ? null : elements.toArray(new LwInspectionSuppression[elements.size()]);
}
if (PlatformDataKeys.HELP_ID.is(dataId)) {
return ourHelpID;
}
if (PlatformDataKeys.FILE_EDITOR.is(dataId)) {
return myFormEditor;
}
if (!CommonDataKeys.NAVIGATABLE.is(dataId)) {
return null;
}
final RadComponent selectedComponent = getSelectedComponent();
if (selectedComponent == null) {
return null;
}
final String classToBind = myEditor.getRootContainer().getClassToBind();
if (classToBind == null) {
return null;
}
final PsiClass aClass = FormEditingUtil.findClassToBind(myEditor.getModule(), classToBind);
if (aClass == null) {
return null;
}
if (selectedComponent instanceof RadRootContainer) {
return EditSourceUtil.getDescriptor(aClass);
}
final String binding = selectedComponent.getBinding();
if (binding == null) {
return null;
}
final PsiField[] fields = aClass.getFields();
for (final PsiField field : fields) {
if (binding.equals(field.getName())) {
return EditSourceUtil.getDescriptor(field);
}
}
return null;
}
use of com.intellij.psi.PsiField in project intellij-community by JetBrains.
the class EncapsulateFieldsAction method isAvailableOnElementInEditorAndFile.
@Override
protected boolean isAvailableOnElementInEditorAndFile(@NotNull PsiElement element, @NotNull Editor editor, @NotNull PsiFile file, @NotNull DataContext context) {
final PsiElement psiElement = file.findElementAt(editor.getCaretModel().getOffset());
final PsiClass containingClass = PsiTreeUtil.getParentOfType(psiElement, PsiClass.class, false);
if (containingClass != null) {
final PsiField[] fields = containingClass.getFields();
for (PsiField field : fields) {
if (isAcceptedField(field))
return true;
}
}
return false;
}
Aggregations