private void injectAutoAnnotation(PsiElementFactory factory, PsiClass cls) { PsiModifierList modifierList = cls.getModifierList(); if (modifierList != null) { PsiElement firstChild = modifierList.getFirstChild(); Pattern pattern = Pattern.compile("@.*?AutoValue"); if (firstChild != null && !pattern.matcher(firstChild.getText()).find()) { PsiAnnotation annotationFromText = factory.createAnnotationFromText("@com.google.auto.value.AutoValue", cls); modifierList.addBefore(annotationFromText, firstChild); } if (!modifierList.hasModifierProperty(PsiModifier.ABSTRACT)) { modifierList.setModifierProperty(PsiModifier.ABSTRACT, true); } } }
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; }
private static boolean acceptsMethod(PsiMethod method, MethodType methodType) { PsiModifierList modifierList = method.getModifierList(); return (methodType == MethodType.STATIC) == modifierList.hasModifierProperty(PsiModifier.STATIC) && !modifierList.hasModifierProperty(PsiModifier.ABSTRACT) && !(methodType == MethodType.CONSTRUCTOR && modifierList.hasModifierProperty(PsiModifier.PROTECTED)); }
private boolean hasParameterName(PsiParameter psiParameter) { PsiModifierList modifierList = psiParameter.getModifierList(); if (modifierList == null) { return false; } PsiAnnotation annotation = modifierList.findAnnotation("javax.inject.Named"); if (annotation != null) { return true; } annotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED); if (annotation != null) { return true; } return false; }
? list.hasExplicitModifier(PsiModifier.PUBLIC) : list.hasModifierProperty(PsiModifier.PUBLIC)) { appendModifier(buffer, PsiModifier.PUBLIC); if (list.hasModifierProperty(PsiModifier.PROTECTED)){ appendModifier(buffer, PsiModifier.PROTECTED); if (list.hasModifierProperty(PsiModifier.PRIVATE)){ appendModifier(buffer, PsiModifier.PRIVATE); ? list.hasExplicitModifier(PsiModifier.PACKAGE_LOCAL) : list.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) { if (element instanceof PsiClass && element.getParent() instanceof PsiDeclarationStatement) {// local class appendModifier(buffer, PsiBundle.message("local.class.preposition")); ? list.hasExplicitModifier(PsiModifier.STATIC) : list.hasModifierProperty(PsiModifier.STATIC)) appendModifier(buffer, PsiModifier.STATIC); ? list.hasExplicitModifier(PsiModifier.ABSTRACT) : list.hasModifierProperty(PsiModifier.ABSTRACT))) appendModifier(buffer, PsiModifier.ABSTRACT); ? list.hasExplicitModifier(PsiModifier.FINAL) : list.hasModifierProperty(PsiModifier.FINAL)) appendModifier(buffer, PsiModifier.FINAL); if (list.hasModifierProperty(PsiModifier.NATIVE) && (options & JAVADOC_MODIFIERS_ONLY) == 0){ appendModifier(buffer, PsiModifier.NATIVE); if (list.hasModifierProperty(PsiModifier.SYNCHRONIZED) && (options & JAVADOC_MODIFIERS_ONLY) == 0){ appendModifier(buffer, PsiModifier.SYNCHRONIZED);
PsiElement parent = ((PsiModifierList) owner).getParent(); PsiClass cls = (PsiClass) resolved; if (cls.isAnnotationType() && cls.getModifierList() != null) { for (PsiAnnotation a : cls.getModifierList().getAnnotations()) { String name = a.getQualifiedName(); if (INT_DEF_ANNOTATION.equals(name)) {
/** * NOTE: requires readAction, if not on the dispatch thread Get the public class in a file that is * annotated with a certain annotation. */ public static PsiClass getPublicAnnotatedClass(PsiJavaFile psiJavaFile, String annotationFqn) { PsiClass[] classes = psiJavaFile.getClasses(); for (PsiClass cls : classes) { PsiModifierList modifierList = cls.getModifierList(); if (modifierList != null && modifierList.hasExplicitModifier(PsiModifier.PUBLIC)) { if (AnnotationUtil.isAnnotated(cls, annotationFqn, false)) { return cls; } } } return null; }
/** * The default generation uses @java.lang.Override for some reason; * Let's clean that up */ void cleanOverrideAnnotation(PsiMethodMember member) { final PsiMethod[] ms = targetClass.findMethodsBySignature( member.getElement(), false); for (PsiMethod m : ms) { PsiModifierList modifierList = m.getModifierList(); PsiAnnotation annotation = modifierList.findAnnotation(Override.class.getName()); if (null != annotation) { annotation.delete(); modifierList.addAnnotation("Override"); } } }
if (modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED) != null) { return; if (modifierList.findAnnotation("") != null) { return; JavaPsiFacade.getElementFactory(project) .createAnnotationFromText(annotationString, element); modifierList.add(annotation);
/** * Tests that the Quick fix for a parameter with the javax @Nullable and @DefaultValue returns the * parameter without @Nullable and @DefaultValue. */ public void testQuickFix_withJavaxNullableAndDefaultAnnotation() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameter("param", PsiType.BOOLEAN); parameter.getModifierList().addAnnotation("javax.annotation.Nullable"); parameter.getModifierList().addAnnotation(GctConstants.APP_ENGINE_ANNOTATION_DEFAULT_VALUE); runQuickFixTest(parameter, " boolean param"); }
public void addAnnotation(@NotNull PsiModifierListOwner parameter, @NotNull Annotation annotation) { PsiModifierList modifierList = parameter.getModifierList(); if (JavaUtils.isAnnotationPresent(parameter, annotation) || null == modifierList) { return; } JavaService.getInstance(parameter.getProject()).importClazz((PsiJavaFile) parameter.getContainingFile(), annotation.getQualifiedName()); PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); PsiAnnotation psiAnnotation = elementFactory.createAnnotationFromText(annotation.toString(), parameter); modifierList.add(psiAnnotation); JavaCodeStyleManager.getInstance(project).shortenClassReferences(psiAnnotation.getParent()); }
PsiClass psiClass = ...; PsiModifierList psiModifierList = psiClass.getModifierList(); PsiAnnotation[] annotations = psiModifierList.getAnnotations();
@AccessLevel public static int getAccessLevel(@NotNull PsiModifierList modifierList) { if (modifierList.hasModifierProperty(PsiModifier.PRIVATE)) { return ACCESS_LEVEL_PRIVATE; } if (modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) { return ACCESS_LEVEL_PACKAGE_LOCAL; } if (modifierList.hasModifierProperty(PsiModifier.PROTECTED)) { return ACCESS_LEVEL_PROTECTED; } return ACCESS_LEVEL_PUBLIC; }
public boolean accepts(@NotNull final T t, final ProcessingContext context) { final PsiModifierList modifierList = t.getModifierList(); return modifierList != null && modifierList.findAnnotation(qualifiedName) != null; } });
private static boolean calculateIfFinal(@NotNull PsiVariable element) { PsiModifierList modifiers = element.getModifierList(); if (modifiers != null) { boolean isFinal = modifiers.hasExplicitModifier(PsiModifier.FINAL); if (!isFinal) { PsiElement body = element.getParent() instanceof PsiDeclarationStatement
/** Test that the Quick fix for a parameter with @Named returns the same parameter unchanged. */ public void testQuickFix_withNamedAnnotation() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameter("param", PsiType.BOOLEAN); parameter.getModifierList().addAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED); runQuickFixTest(parameter, "@" + GctConstants.APP_ENGINE_ANNOTATION_NAMED + " boolean param"); }
@NotNull public static PsiAnnotation[] findAnnotations(@Nullable PsiModifierListOwner modifierListOwner, @NotNull Collection<String> annotationNames) { if (modifierListOwner == null) return PsiAnnotation.EMPTY_ARRAY; final PsiModifierList modifierList = modifierListOwner.getModifierList(); if (modifierList == null) return PsiAnnotation.EMPTY_ARRAY; final PsiAnnotation[] annotations = modifierList.getAnnotations(); ArrayList<PsiAnnotation> result = null; for (final PsiAnnotation psiAnnotation : annotations) { if (annotationNames.contains(psiAnnotation.getQualifiedName())) { if (result == null) result = new ArrayList<PsiAnnotation>(); result.add(psiAnnotation); } } return result == null ? PsiAnnotation.EMPTY_ARRAY : result.toArray(new PsiAnnotation[result.size()]); }
public static boolean isAbstractClass(@NotNull PsiClass clazz) { PsiModifierList modifierList = clazz.getModifierList(); return modifierList != null && modifierList.hasModifierProperty(PsiModifier.ABSTRACT); }
/** * Checks if the parameter is annotated with the provided {@code annotation}. * * @param psiParameter the parameter on which we need to check for the annotation * @param annotation the annotation that we need to find * * @return {@code true} if the {@code psiParameter} is annotated with the {@code annotation}, {@code false} * otherwise */ private static boolean hasAnnotation(PsiParameter psiParameter, String annotation) { PsiModifierList modifierList = psiParameter.getModifierList(); return modifierList != null && modifierList.findAnnotation( annotation ) != null; }
@Override public void visitMethod(PsiMethod method) { if (method.getModifierList().hasExplicitModifier(PsiModifier.DEFAULT)) { int api = 24; // minSdk for default methods int minSdk = getMinSdk(mContext);