com.intellij.psi.PsiModifierList Java Examples
The following examples show how to use
com.intellij.psi.PsiModifierList.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestSizeFinder.java From intellij with Apache License 2.0 | 7 votes |
@Nullable public static TestSize getTestSize(PsiClass psiClass) { PsiModifierList psiModifierList = psiClass.getModifierList(); if (psiModifierList == null) { return null; } PsiAnnotation[] annotations = psiModifierList.getAnnotations(); TestSize testSize = getTestSize(annotations); if (testSize != null) { return testSize; } String fullText = psiModifierList.getText(); return CATEGORY_ANNOTATION_HEURISTIC .entrySet() .stream() .filter(e -> fullText.contains(e.getKey())) .map(Map.Entry::getValue) .findFirst() .orElse(null); }
Example #2
Source File: AnnotationDetector.java From here-be-dragons with MIT License | 7 votes |
static PsiAnnotation findAnnotation(PsiElement element, String annotationName) { if (element instanceof PsiModifierListOwner) { PsiModifierListOwner listOwner = (PsiModifierListOwner) element; PsiModifierList modifierList = listOwner.getModifierList(); if (modifierList != null) { for (PsiAnnotation psiAnnotation : modifierList.getAnnotations()) { if (annotationName.equals(psiAnnotation.getQualifiedName())) { return psiAnnotation; } } } } return null; }
Example #3
Source File: UtilityClassModifierProcessor.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 6 votes |
public static boolean isModifierListSupported(@NotNull PsiModifierList modifierList) { PsiElement modifierListParent = modifierList.getParent(); if (modifierListParent instanceof PsiClass) { PsiClass parentClass = (PsiClass) modifierListParent; if (PsiAnnotationSearchUtil.isAnnotatedWith(parentClass, UtilityClass.class)) { return UtilityClassProcessor.validateOnRightType(parentClass, new ProblemNewBuilder()); } } if (!isElementFieldOrMethodOrInnerClass(modifierListParent)) { return false; } PsiClass searchableClass = PsiTreeUtil.getParentOfType(modifierListParent, PsiClass.class, true); return null != searchableClass && PsiAnnotationSearchUtil.isAnnotatedWith(searchableClass, UtilityClass.class) && UtilityClassProcessor.validateOnRightType(searchableClass, new ProblemNewBuilder()); }
Example #4
Source File: GetterFieldProcessor.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 6 votes |
@NotNull public PsiMethod createGetterMethod(@NotNull PsiField psiField, @NotNull PsiClass psiClass, @NotNull String methodModifier) { final String methodName = LombokUtils.getGetterName(psiField); LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiField.getManager(), methodName) .withMethodReturnType(psiField.getType()) .withContainingClass(psiClass) .withNavigationElement(psiField); if (StringUtil.isNotEmpty(methodModifier)) { methodBuilder.withModifier(methodModifier); } boolean isStatic = psiField.hasModifierProperty(PsiModifier.STATIC); if (isStatic) { methodBuilder.withModifier(PsiModifier.STATIC); } final String blockText = String.format("return %s.%s;", isStatic ? psiClass.getName() : "this", psiField.getName()); methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(blockText, methodBuilder)); PsiModifierList modifierList = methodBuilder.getModifierList(); copyAnnotations(psiField, modifierList, LombokUtils.NON_NULL_PATTERN, LombokUtils.NULLABLE_PATTERN, LombokUtils.DEPRECATED_PATTERN); addOnXAnnotations(PsiAnnotationSearchUtil.findAnnotation(psiField, Getter.class), modifierList, "onMethod"); return methodBuilder; }
Example #5
Source File: RefactorSetterHandler.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override protected void process(List<ClassMember> classMembers) { for (ClassMember classMember : classMembers) { final PsiElementClassMember elementClassMember = (PsiElementClassMember) classMember; PsiField psiField = (PsiField) elementClassMember.getPsiElement(); PsiMethod psiMethod = PropertyUtil.findPropertySetter(psiField.getContainingClass(), psiField.getName(), false, false); if (null != psiMethod) { PsiModifierList modifierList = psiField.getModifierList(); if (null != modifierList) { PsiAnnotation psiAnnotation = modifierList.addAnnotation(Setter.class.getName()); psiMethod.delete(); } } } }
Example #6
Source File: RefactorGetterHandler.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override protected void process(List<ClassMember> classMembers) { for (ClassMember classMember : classMembers) { final PsiElementClassMember elementClassMember = (PsiElementClassMember) classMember; PsiField psiField = (PsiField) elementClassMember.getPsiElement(); PsiMethod psiMethod = PropertyUtil.findPropertyGetter(psiField.getContainingClass(), psiField.getName(), false, false); if (null != psiMethod) { PsiModifierList modifierList = psiField.getModifierList(); if (null != modifierList) { PsiAnnotation psiAnnotation = modifierList.addAnnotation(Getter.class.getName()); // psiAnnotation.setDeclaredAttributeValue("value", ) psiMethod.delete(); } } } }
Example #7
Source File: ProducerUtils.java From intellij with Apache License 2.0 | 6 votes |
private static boolean isJUnit4Class(PsiClass psiClass) { String qualifiedName = JUnitUtil.RUN_WITH; if (AnnotationUtil.isAnnotated(psiClass, qualifiedName, true)) { return true; } // handle the case where RunWith and/or the current class isn't indexed PsiModifierList modifierList = psiClass.getModifierList(); if (modifierList == null) { return false; } if (modifierList.hasAnnotation(qualifiedName)) { return true; } String shortName = StringUtil.getShortName(qualifiedName); return modifierList.hasAnnotation(shortName) && hasImport(psiClass, qualifiedName); }
Example #8
Source File: UtilityClassModifierProcessor.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void transformModifiers(@NotNull PsiModifierList modifierList, @NotNull final Set<String> modifiers) { final PsiElement parent = modifierList.getParent(); // FINAL if (parent instanceof PsiClass) { PsiClass psiClass = (PsiClass) parent; if (PsiAnnotationSearchUtil.isAnnotatedWith(psiClass, UtilityClass.class)) { modifiers.add(PsiModifier.FINAL); } } // STATIC if (isElementFieldOrMethodOrInnerClass(parent)) { modifiers.add(PsiModifier.STATIC); } }
Example #9
Source File: PropertyDetector.java From data-mediator with Apache License 2.0 | 6 votes |
@Override public void visitClass(UClass uClass) { //only check interface if(!uClass.isInterface()){ return; } Set<PropInfo> infos = getPropInfoWithSupers(uClass); if(infos.isEmpty()){ return; } //check method is relative of any field for(UMethod method: uClass.getMethods()){ PsiModifierList list = method.getModifierList(); PsiAnnotation pa_keep = list.findAnnotation(NAME_KEEP); PsiAnnotation pa_impl = list.findAnnotation(NAME_IMPL_METHOD); if (pa_keep == null && pa_impl == null) { if(!hasPropInfo(infos, method.getName())){ report(method); } } } }
Example #10
Source File: PsiConsultantImpl.java From dagger-intellij-plugin with Apache License 2.0 | 6 votes |
public static Set<String> getQualifierAnnotations(PsiElement element) { Set<String> qualifiedClasses = new HashSet<String>(); if (element instanceof PsiModifierListOwner) { PsiModifierListOwner listOwner = (PsiModifierListOwner) element; PsiModifierList modifierList = listOwner.getModifierList(); if (modifierList != null) { for (PsiAnnotation psiAnnotation : modifierList.getAnnotations()) { if (psiAnnotation != null && psiAnnotation.getQualifiedName() != null) { JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(element.getProject()); PsiClass psiClass = psiFacade.findClass(psiAnnotation.getQualifiedName(), GlobalSearchScope.projectScope(element.getProject())); if (hasAnnotation(psiClass, CLASS_QUALIFIER)) { qualifiedClasses.add(psiAnnotation.getQualifiedName()); } } } } } return qualifiedClasses; }
Example #11
Source File: InjectedFieldInJobNotTransientDetector.java From cathode with Apache License 2.0 | 6 votes |
@Override public boolean visitField(UField field) { PsiModifierList modifierList = field.getModifierList(); if (modifierList == null || modifierList.hasModifierProperty(PsiModifier.TRANSIENT)) { return false; } if (!isInJob(field)) { return false; } if (hasAnnotation(modifierList, JAVAX_INJECT)) { context.report(ISSUE, context.getLocation(field), LINT_ERROR_MESSAGE); } return super.visitField(field); }
Example #12
Source File: PropertyDetector.java From data-mediator with Apache License 2.0 | 6 votes |
@Override public void visitClass(UClass uClass) { //only check interface if(!uClass.isInterface()){ return; } Set<PropInfo> infos = getPropInfoWithSupers(uClass); if(infos.isEmpty()){ return; } //check method is relative of any field for(UMethod method: uClass.getMethods()){ PsiModifierList list = method.getModifierList(); PsiAnnotation pa_keep = list.findAnnotation(NAME_KEEP); PsiAnnotation pa_impl = list.findAnnotation(NAME_IMPL_METHOD); if (pa_keep == null && pa_impl == null) { if(!hasPropInfo(infos, method.getName())){ report(method); } } } }
Example #13
Source File: FieldDefaultsModifierTest.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@NotNull private PsiModifierList getFieldModifierListAtCaret() { PsiFile file = loadToPsiFile(getTestName(false) + ".java"); PsiField field = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiField.class); assertNotNull(field); PsiModifierList modifierList = field.getModifierList(); assertNotNull(modifierList); return modifierList; }
Example #14
Source File: InjectedFieldInJobNotTransientDetector.java From cathode with Apache License 2.0 | 5 votes |
private boolean hasAnnotation(PsiModifierList modifierList, String annotationName) { PsiAnnotation[] annotations = modifierList.getAnnotations(); if (annotations != null && annotations.length > 0) { for (PsiAnnotation annotation : annotations) { if (annotationName.equals(annotation.getQualifiedName())) { return true; } } } return false; }
Example #15
Source File: FieldDefaultsModifierTest.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void testFieldDefaultsPublicWithPackagePrivate() { //TODO disable assertions for the moment RecursionManager.disableMissedCacheAssertions(myFixture.getProjectDisposable()); PsiModifierList modifierList = getFieldModifierListAtCaret(); assertFalse("@FieldDefaults(level = AccessLevel.PUBLIC) should not make @PackagePrivate fields public", modifierList.hasModifierProperty(PsiModifier.PUBLIC)); assertTrue("@FieldDefaults should keep @PackagePrivate fields package-private", modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)); }
Example #16
Source File: FieldDefaultsModifierTest.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void testFieldDefaultsPrivate() { PsiModifierList modifierList = getFieldModifierListAtCaret(); assertFalse("@FieldDefaults(level = AccessLevel.PRIVATE) should not make fields public", modifierList.hasModifierProperty(PsiModifier.PUBLIC)); assertFalse("@FieldDefaults(level = AccessLevel.PRIVATE) should not make fields protected", modifierList.hasModifierProperty(PsiModifier.PROTECTED)); assertFalse("@FieldDefaults(level = AccessLevel.PRIVATE) should not make fields package-private", modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)); assertTrue("@FieldDefaults(level = AccessLevel.PRIVATE) should make non-@PackagePrivate fields private", modifierList.hasModifierProperty(PsiModifier.PRIVATE)); }
Example #17
Source File: FieldDefaultsModifierTest.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void testFieldDefaultsProtected() { PsiModifierList modifierList = getFieldModifierListAtCaret(); assertFalse("@FieldDefaults(level = AccessLevel.PROTECTED) should not make fields public", modifierList.hasModifierProperty(PsiModifier.PUBLIC)); assertFalse("@FieldDefaults(level = AccessLevel.PROTECTED) should not make fields package-private", modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)); assertTrue("@FieldDefaults(level = AccessLevel.PROTECTED) should non-@PackagePrivate make fields protected", modifierList.hasModifierProperty(PsiModifier.PROTECTED)); assertFalse("@FieldDefaults(level = AccessLevel.PROTECTED) should not make fields private", modifierList.hasModifierProperty(PsiModifier.PRIVATE)); }
Example #18
Source File: FieldDefaultsModifierTest.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void testFieldDefaultsNone() { PsiModifierList modifierList = getFieldModifierListAtCaret(); assertFalse("@FieldDefaults should not make fields public", modifierList.hasModifierProperty(PsiModifier.PUBLIC)); assertFalse("@FieldDefaults should not make fields protected", modifierList.hasModifierProperty(PsiModifier.PROTECTED)); assertFalse("@FieldDefaults should not make fields private", modifierList.hasModifierProperty(PsiModifier.PRIVATE)); assertTrue("@FieldDefaults should keep fields as package-private", modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)); }
Example #19
Source File: CreateFieldQuickFix.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { final PsiClass myClass = (PsiClass) startElement; final Editor editor = CodeInsightUtil.positionCursor(project, psiFile, myClass.getLBrace()); if (editor != null) { WriteCommandAction.writeCommandAction(project, psiFile).run(() -> { final PsiElementFactory psiElementFactory = JavaPsiFacade.getElementFactory(project); final PsiField psiField = psiElementFactory.createField(myName, myType); final PsiModifierList modifierList = psiField.getModifierList(); if (null != modifierList) { for (String modifier : myModifiers) { modifierList.setModifierProperty(modifier, true); } } if (null != myInitializerText) { PsiExpression psiInitializer = psiElementFactory.createExpressionFromText(myInitializerText, psiField); psiField.setInitializer(psiInitializer); } final List<PsiGenerationInfo<PsiField>> generationInfos = GenerateMembersUtil.insertMembersAtOffset(myClass.getContainingFile(), editor.getCaretModel().getOffset(), Collections.singletonList(new PsiGenerationInfo<>(psiField))); if (!generationInfos.isEmpty()) { PsiField psiMember = generationInfos.iterator().next().getPsiMember(); editor.getCaretModel().moveToOffset(psiMember.getTextRange().getEndOffset()); } UndoUtil.markPsiFileForUndo(psiFile); } ); } }
Example #20
Source File: FieldDefaultsModifierTest.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void testFieldDefaultsWithNonFinal() { //TODO disable assertions for the moment RecursionManager.disableMissedCacheAssertions(myFixture.getProjectDisposable()); PsiModifierList modifierList = getFieldModifierListAtCaret(); assertFalse("@FieldDefaults(makeFinal = true) should not make @NonFinal fields final", modifierList.hasModifierProperty(PsiModifier.FINAL)); }
Example #21
Source File: PsiConsultantImpl.java From dagger-intellij-plugin with Apache License 2.0 | 5 votes |
static PsiAnnotation findAnnotation(PsiElement element, String annotationName) { if (element instanceof PsiModifierListOwner) { PsiModifierListOwner listOwner = (PsiModifierListOwner) element; PsiModifierList modifierList = listOwner.getModifierList(); if (modifierList != null) { for (PsiAnnotation psiAnnotation : modifierList.getAnnotations()) { if (annotationName.equals(psiAnnotation.getQualifiedName())) { return psiAnnotation; } } } } return null; }
Example #22
Source File: FieldDefaultsModifierTest.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void testFieldDefaultsPublic() { PsiModifierList modifierList = getFieldModifierListAtCaret(); assertTrue("@FieldDefaults(level = AccessLevel.PUBLIC) should make non-@PackagePrivate fields public", modifierList.hasModifierProperty(PsiModifier.PUBLIC)); assertFalse("@FieldDefaults(level = AccessLevel.PUBLIC) should not make fields package-private", modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)); assertFalse("@FieldDefaults(level = AccessLevel.PUBLIC) should not make fields protected", modifierList.hasModifierProperty(PsiModifier.PROTECTED)); assertFalse("@FieldDefaults(level = AccessLevel.PUBLIC) should not make fields private", modifierList.hasModifierProperty(PsiModifier.PRIVATE)); }
Example #23
Source File: LombokProcessorUtil.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@NotNull public static PsiAnnotation createAnnotationWithAccessLevel(@NotNull Class<? extends Annotation> annotationClass, @NotNull PsiModifierListOwner psiModifierListOwner) { String value = ""; final PsiModifierList modifierList = psiModifierListOwner.getModifierList(); if (null != modifierList) { final int accessLevelCode = PsiUtil.getAccessLevel(modifierList); final AccessLevel accessLevel = ACCESS_LEVEL_MAP.get(accessLevelCode); if (null != accessLevel && !AccessLevel.PUBLIC.equals(accessLevel)) { value = AccessLevel.class.getName() + "." + accessLevel; } } return PsiAnnotationUtil.createPsiAnnotation(psiModifierListOwner, annotationClass, value); }
Example #24
Source File: PsiAnnotationSearchUtil.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static boolean checkAnnotationsSimpleNameExistsIn(@NotNull PsiModifierListOwner modifierListOwner, @NotNull Collection<String> annotationNames) { final PsiModifierList modifierList = modifierListOwner.getModifierList(); if (null != modifierList) { for (PsiAnnotation psiAnnotation : modifierList.getAnnotations()) { final String simpleName = getSimpleNameOf(psiAnnotation); if (annotationNames.contains(simpleName)) { return true; } } } return false; }
Example #25
Source File: PsiAnnotationSearchUtil.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static boolean isAnnotatedWith(@NotNull PsiModifierListOwner psiModifierListOwner, @NotNull final Pattern annotationPattern) { final PsiModifierList psiModifierList = psiModifierListOwner.getModifierList(); if (psiModifierList != null) { for (PsiAnnotation psiAnnotation : psiModifierList.getAnnotations()) { final String suspect = getSimpleNameOf(psiAnnotation); if (annotationPattern.matcher(suspect).matches()) { return true; } } } return false; }
Example #26
Source File: LombokAugmentProvider.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@NotNull @Override protected Set<String> transformModifiers(@NotNull PsiModifierList modifierList, @NotNull final Set<String> modifiers) { // make copy of original modifiers Set<String> result = new HashSet<>(modifiers); // Loop through all available processors and give all of them a chance to respond for (ModifierProcessor processor : modifierProcessors) { if (processor.isSupported(modifierList)) { processor.transformModifiers(modifierList, result); } } return result; }
Example #27
Source File: LombokProcessorProvider.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void addApplicableProcessors(@NotNull PsiMember psiMember, @NotNull Collection<LombokProcessorData> target) { final PsiModifierList psiModifierList = psiMember.getModifierList(); if (null != psiModifierList) { for (PsiAnnotation psiAnnotation : psiModifierList.getAnnotations()) { for (Processor processor : getProcessors(psiAnnotation)) { target.add(new LombokProcessorData(processor, psiAnnotation)); } } } }
Example #28
Source File: AbstractProcessor.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected static void addOnXAnnotations(@Nullable PsiAnnotation processedAnnotation, @NotNull PsiModifierList modifierList, @NotNull String onXParameterName) { if (processedAnnotation == null) { return; } Collection<String> annotationsToAdd = LombokProcessorUtil.getOnX(processedAnnotation, onXParameterName); for (String annotation : annotationsToAdd) { modifierList.addAnnotation(annotation); } }
Example #29
Source File: PsiConsultantImpl.java From otto-intellij-plugin with Apache License 2.0 | 5 votes |
static PsiAnnotation findAnnotationOnMethod(PsiMethod psiMethod, String annotationName) { PsiModifierList modifierList = psiMethod.getModifierList(); for (PsiAnnotation psiAnnotation : modifierList.getAnnotations()) { if (annotationName.equals(psiAnnotation.getQualifiedName())) { return psiAnnotation; } } return null; }
Example #30
Source File: AbstractFieldNameConstantsProcessor.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@NotNull Collection<PsiField> filterFields(@NotNull PsiClass psiClass, PsiAnnotation psiAnnotation) { final Collection<PsiField> psiFields = new ArrayList<>(); final boolean onlyExplicitlyIncluded = PsiAnnotationUtil.getBooleanAnnotationValue(psiAnnotation, "onlyExplicitlyIncluded", false); for (PsiField psiField : PsiClassUtil.collectClassFieldsIntern(psiClass)) { boolean useField = true; PsiModifierList modifierList = psiField.getModifierList(); if (null != modifierList) { //Skip static fields. useField = !modifierList.hasModifierProperty(PsiModifier.STATIC); //Skip transient fields useField &= !modifierList.hasModifierProperty(PsiModifier.TRANSIENT); } //Skip fields that start with $ useField &= !psiField.getName().startsWith(LombokUtils.LOMBOK_INTERN_FIELD_MARKER); //Skip fields annotated with @FieldNameConstants.Exclude useField &= !PsiAnnotationSearchUtil.isAnnotatedWith(psiField, FIELD_NAME_CONSTANTS_EXCLUDE); if (onlyExplicitlyIncluded) { //Only use fields annotated with @FieldNameConstants.Include, Include annotation overrides other rules useField = PsiAnnotationSearchUtil.isAnnotatedWith(psiField, FIELD_NAME_CONSTANTS_INCLUDE); } if (useField) { psiFields.add(psiField); } } return psiFields; }