@Override public void visitField(@NotNull PsiField field) { super.visitField(field); if (field instanceof PsiEnumConstant) { return; } if (!field.hasModifierProperty(PsiModifier.STATIC) || !field.hasModifierProperty(PsiModifier.FINAL)) { return; } final String name = field.getName(); if (name == null) { return; } final PsiType type = field.getType(); if (onlyCheckImmutables && !ClassUtils.isImmutable(type)) { return; } if (isValid(name)) { return; } registerFieldError(field, name); }
@Override public TextWithImports getEditorText(PsiElement elementAtCaret) { String result = ""; PsiElement element = findExpressionInner(elementAtCaret, true); if (element != null) { if (element instanceof GrReferenceExpression) { final GrReferenceExpression reference = (GrReferenceExpression)element; if (reference.getQualifier() == null) { final PsiElement resolved = reference.resolve(); if (resolved instanceof PsiEnumConstant) { final PsiEnumConstant enumConstant = (PsiEnumConstant)resolved; final PsiClass enumClass = enumConstant.getContainingClass(); if (enumClass != null) { result = enumClass.getName() + "." + enumConstant.getName(); } } } } if (result.isEmpty()) { result = element.getText(); } } return new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, result); }
@Override public TextWithImports getEditorText(PsiElement elementAtCaret) { String result = ""; PsiElement element = findExpressionInner(elementAtCaret, true); if (element != null) { if (element instanceof GrReferenceExpression) { final GrReferenceExpression reference = (GrReferenceExpression)element; if (reference.getQualifier() == null) { final PsiElement resolved = reference.resolve(); if (resolved instanceof PsiEnumConstant) { final PsiEnumConstant enumConstant = (PsiEnumConstant)resolved; final PsiClass enumClass = enumConstant.getContainingClass(); if (enumClass != null) { result = enumClass.getName() + "." + enumConstant.getName(); } } } } if (result.length() == 0) { result = element.getText(); } } return new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, result); }
@Override @NotNull public Collection<EncapsulatableClassMember> fun(PsiClass s) { if(s.getLanguage() != JavaLanguage.INSTANCE) { return Collections.emptyList(); } final List<EncapsulatableClassMember> result = new ArrayList<EncapsulatableClassMember>(); for(PsiField field : s.getFields()) { if(!(field instanceof PsiEnumConstant)) { result.add(new PsiFieldMember(field)); } } return result; }
public void testEnumConstructor() throws Exception { PsiClass enumClass = myJavaFacade.findClass("pack.OurEnum", GlobalSearchScope.moduleScope(myModule)); assertNotNull(enumClass); assertTrue(enumClass.isEnum()); PsiMethod[] constructors = enumClass.getConstructors(); assertEquals(2, constructors.length); PsiReference[] references0 = ReferencesSearch.search(constructors[0], GlobalSearchScope.moduleScope(myModule), false).toArray(new PsiReference[0]); assertEquals(2, references0.length); assertTrue(references0[0].getElement() instanceof PsiEnumConstant); assertTrue(references0[1].getElement() instanceof PsiEnumConstant); PsiReference[] references1 = ReferencesSearch.search(constructors[1], GlobalSearchScope.moduleScope(myModule), false).toArray(new PsiReference[0]); assertEquals(1, references1.length); assertTrue(references1[0].getElement() instanceof PsiEnumConstant); }
private boolean isFieldMissingNullAnnotation(PsiField field, PsiType type) { return reportFields && field.isPhysical() && !(field instanceof PsiEnumConstant) && !TypeConversionUtil.isPrimitiveAndNotNull(type) && shouldCheckField(field) && !hasAnnotation(field); }
@Override public Stream<LookupElementBuilder> resolveCompletions(String propertyName, PsiType psiType) { PsiClass resolved = ((PsiClassReferenceType) psiType).resolve(); if (resolved != null) { return Arrays.stream(resolved.getChildren()) .filter(child -> child instanceof PsiEnumConstant) .map(child -> buildEnumLookup((PsiEnumConstant) child)) .filter(Optional::isPresent) .map(Optional::get); } return Stream.empty(); }
@Override @NotNull public Collection<EncapsulatableClassMember> fun(PsiClass s) { if (s.getLanguage() != StdLanguages.JAVA) return Collections.emptyList(); final List<EncapsulatableClassMember> result = new ArrayList<EncapsulatableClassMember>(); for (PsiField field : s.getFields()) { if (!(field instanceof PsiEnumConstant)) { result.add(new PsiFieldMember(field)); } } return result; }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") final Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { final PsiElement myPsiElement = startElement; if (editor == null || !FileModificationService.getInstance().prepareFileForWrite(myPsiElement.getContainingFile())) return; if (myPsiElement instanceof PsiEnumConstant) { final boolean hasClassInitializer = ((PsiEnumConstant)myPsiElement).getInitializingClass() != null; final MemberChooser<PsiMethodMember> chooser = chooseMethodsToImplement(editor, startElement, ((PsiEnumConstant)myPsiElement).getContainingClass(), hasClassInitializer); if (chooser == null) return; final List<PsiMethodMember> selectedElements = chooser.getSelectedElements(); if (selectedElements == null || selectedElements.isEmpty()) return; new WriteCommandAction(project, file) { @Override protected void run(@NotNull final Result result) throws Throwable { final PsiClass psiClass = ((PsiEnumConstant)myPsiElement).getOrCreateInitializingClass(); OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor, psiClass, selectedElements, chooser.isCopyJavadoc(), chooser.isInsertOverrideAnnotation()); } }.execute(); } else { OverrideImplementUtil.chooseAndImplementMethods(project, editor, (PsiClass)myPsiElement); } }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if (psiElement instanceof PsiEnumConstant) { int insertionOffset = psiElement.getTextRange().getEndOffset(); Document doc = editor.getDocument(); final CharSequence text = doc.getCharsSequence(); final int probableCommaOffset = CharArrayUtil.shiftForward(text, insertionOffset, " \t"); if (probableCommaOffset >= text.length() || text.charAt(probableCommaOffset) != ',') { doc.insertString(insertionOffset, ","); } } }
public void testEnumUsage() throws IncorrectOperationException { LanguageLevelProjectExtension.getInstance(myJavaFacade.getProject()).setLanguageLevel(LanguageLevel.JDK_1_5); CommandProcessor.getInstance().executeCommand(myProject, new Runnable() { @Override public void run() { try { createFile(myModule, myTestProjectRoot, "PropEnum.java", "public enum PropEnum { valueA, valueB }"); createFile(myModule, myTestProjectRoot, "CustomComponent.java", "public class CustomComponent extends JLabel { private PropEnum e; public PropEnum getE() { return e; } public void setE(E newE) { e = newE; } }"); } catch (Exception e) { fail(e.getMessage()); } } }, "", null); PsiClass enumClass = myJavaFacade.findClass("PropEnum", ProjectScope.getAllScope(myProject)); PsiField valueBField = enumClass.findFieldByName("valueB", false); assertNotNull(valueBField); assertTrue(valueBField instanceof PsiEnumConstant); final PsiClass componentClass = myJavaFacade.findClass("CustomComponent", ProjectScope.getAllScope(myProject)); assertNotNull(componentClass); assertEquals(1, ReferencesSearch.search(componentClass).findAll().size()); assertEquals(1, ReferencesSearch.search(valueBField).findAll().size()); }
@Override public void visitEnumConstant(PsiEnumConstant constant) { super.visitEnumConstant(constant); final String name = constant.getName(); if (name == null) { return; } if (isValid(name)) { return; } registerFieldError(constant, name); }
private int countFields(PsiClass aClass) { int totalFields = 0; final PsiField[] fields = aClass.getFields(); for (final PsiField field : fields) { if (field instanceof PsiEnumConstant) { if (myCountEnumConstants) { totalFields++; } } else if (m_countConstantFields || !fieldIsConstant(field)) { totalFields++; } } return totalFields; }
@Override @NotNull public Collection<EncapsulatableClassMember> fun(PsiClass s) { if (!(s instanceof GrTypeDefinition)) return Collections.emptyList(); final List<EncapsulatableClassMember> result = new ArrayList<EncapsulatableClassMember>(); for (PsiField field : s.getFields()) { if (!(field instanceof PsiEnumConstant) && field instanceof GrField) { result.add(new GrFieldMember((GrField)field)); } } return result; }
@NotNull public Collection<EncapsulatableClassMember> fun(PsiClass s) { if (!(s instanceof GrTypeDefinition)) return Collections.emptyList(); final List<EncapsulatableClassMember> result = new ArrayList<EncapsulatableClassMember>(); for (PsiField field : s.getFields()) { if (!(field instanceof PsiEnumConstant) && field instanceof GrField) { result.add(new GrFieldMember((GrField)field)); } } return result; }
@Override public boolean isSupported(@NotNull PsiModifierList modifierList) { // FieldDefaults only change modifiers of class fields // but nor for enum constants or lombok generated fields final PsiElement psiElement = modifierList.getParent(); if (!(psiElement instanceof PsiField) || psiElement instanceof PsiEnumConstant || psiElement instanceof LombokLightFieldBuilder) { return false; } PsiClass searchableClass = PsiTreeUtil.getParentOfType(modifierList, PsiClass.class, true); return null != searchableClass && PsiAnnotationSearchUtil.isAnnotatedWith(searchableClass, lombok.experimental.FieldDefaults.class); }
private static boolean processReferencesInUIForms(Processor<PsiReference> processor, final PsiEnumConstant enumConstant, GlobalSearchScope scope, final LocalSearchScope filterScope) { PsiManagerImpl manager = (PsiManagerImpl) enumConstant.getManager(); String className = ApplicationManager.getApplication().runReadAction(new Computable<String>() { @Override public String compute() { return enumConstant.getName(); } }); return className == null || processReferencesInUIFormsInner(className, enumConstant, processor, scope, manager, filterScope); }
public void testEnumUsage() throws IncorrectOperationException { //LanguageLevelProjectExtension.getInstance(myJavaFacade.getProject()).setLanguageLevel(LanguageLevel.JDK_1_5); CommandProcessor.getInstance().executeCommand(myProject, new Runnable() { @Override public void run() { try { createFile(myModule, myTestProjectRoot, "PropEnum.java", "public enum PropEnum { valueA, valueB }"); createFile(myModule, myTestProjectRoot, "CustomComponent.java", "public class CustomComponent extends JLabel { private PropEnum e; public PropEnum getE() { return e; } public void setE(E newE) { e = newE; } }"); } catch (Exception e) { fail(e.getMessage()); } } }, "", null); PsiClass enumClass = myJavaFacade.findClass("PropEnum", ProjectScope.getAllScope(myProject)); PsiField valueBField = enumClass.findFieldByName("valueB", false); assertNotNull(valueBField); assertTrue(valueBField instanceof PsiEnumConstant); final PsiClass componentClass = myJavaFacade.findClass("CustomComponent", ProjectScope.getAllScope(myProject)); assertNotNull(componentClass); assertEquals(1, ReferencesSearch.search(componentClass).findAll().size()); assertEquals(1, ReferencesSearch.search(valueBField).findAll().size()); }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if(psiElement instanceof PsiEnumConstant) { int insertionOffset = psiElement.getTextRange().getEndOffset(); Document doc = editor.getDocument(); final CharSequence text = doc.getCharsSequence(); final int probableCommaOffset = CharArrayUtil.shiftForward(text, insertionOffset, " \t"); if(probableCommaOffset >= text.length() || text.charAt(probableCommaOffset) != ',') { doc.insertString(insertionOffset, ","); } } }
@RequiredReadAction @NotNull @Override public PsiElement[] getMovableSubElements(@NotNull PsiElement element) { if(element instanceof PsiParameterList) { return ((PsiParameterList) element).getParameters(); } else if(element instanceof PsiExpressionList) { return ((PsiExpressionList) element).getExpressions(); } else if(element instanceof PsiAnnotationParameterList) { return ((PsiAnnotationParameterList) element).getAttributes(); } else if(element instanceof PsiArrayInitializerExpression) { return ((PsiArrayInitializerExpression) element).getInitializers(); } else if(element instanceof PsiClass && ((PsiClass) element).isEnum()) { PsiEnumConstant[] enumConstants = PsiTreeUtil.getChildrenOfType(element, PsiEnumConstant.class); if(enumConstants != null) { return enumConstants; } } return PsiElement.EMPTY_ARRAY; }
private Optional<LookupElementBuilder> buildEnumLookup(PsiEnumConstant psiEnumConstant) { return Optional.of(LookupElementBuilder.create(psiEnumConstant).withIcon(psiEnumConstant.getIcon(Iconable.ICON_FLAG_VISIBILITY))); }
@Override public void visitEnumConstant(PsiEnumConstant enumConstant) { mVisitor.report("PsiEnumConstant", enumConstant.getText(), enumConstant); super.visitField(enumConstant); }
private static boolean preserveConstantDistinction(final Object c1, final Object c2) { return c1 == null && c2 instanceof PsiEnumConstant || c2 == null && c1 instanceof PsiEnumConstant; }
public boolean execute(@NotNull final ReferencesSearch.SearchParameters p, @NotNull final Processor<PsiReference> consumer) { if(!scopeCanContainForms(p.getScope())) { return true; } final PsiElement refElement = p.getElementToSearch(); final PsiFile psiFile = ApplicationManager.getApplication().runReadAction(new Computable<PsiFile>() { public PsiFile compute() { if(!refElement.isValid()) { return null; } return refElement.getContainingFile(); } }); if(psiFile == null) { return true; } final VirtualFile virtualFile = psiFile.getVirtualFile(); if(virtualFile == null) { return true; } Module module = ProjectRootManager.getInstance(refElement.getProject()).getFileIndex().getModuleForFile(virtualFile); if(module == null) { return true; } final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesScope(module); final LocalSearchScope filterScope = p.getScope() instanceof LocalSearchScope ? (LocalSearchScope) p.getScope() : null; if(refElement instanceof PsiJavaPackage) { //no need to do anything //if (!UIFormUtil.processReferencesInUIForms(consumer, (PsiPackage)refElement, scope)) return false; } else if(refElement instanceof PsiClass) { if(!processReferencesInUIForms(consumer, (PsiClass) refElement, scope, filterScope)) { return false; } } else if(refElement instanceof PsiEnumConstant) { if(!processReferencesInUIForms(consumer, (PsiEnumConstant) refElement, scope, filterScope)) { return false; } } else if(refElement instanceof PsiField) { if(!processReferencesInUIForms(consumer, (PsiField) refElement, scope, filterScope)) { return false; } } else if(refElement instanceof IProperty) { if(!processReferencesInUIForms(consumer, (Property) refElement, scope, filterScope)) { return false; } } else if(refElement instanceof PropertiesFile) { if(!processReferencesInUIForms(consumer, (PropertiesFile) refElement, scope, filterScope)) { return false; } } return true; }