/** * Checks of the {@code psiClass} is annotated with {@link Mapper}. * * @param psiClass the class that needs to be checked * * @return {@code true} if the {@code psiClass} is annotated with {@link Mapper}, {@code false} otherwise */ public static boolean isMapper(PsiClass psiClass) { return isAnnotated( psiClass, MAPPER_ANNOTATION_FQN, false ); }
/** * Checks of the {@code psiClass} is annotated with {@link MapperConfig}. * * @param psiClass the class that needs to be checked * * @return {@code true} if the {@code psiClass} is annotated with {@link MapperConfig}, {@code false} otherwise */ public static boolean isMapperConfig(PsiClass psiClass) { return isAnnotated( psiClass, MAPPER_CONFIG_ANNOTATION_FQN, false ); }
/** * Checks if the {@code psiMethod} is annotated with {@link InheritInverseConfiguration}. * * @param method to be checked * * @return {@code true} if the {@code method} is annotated with {@link InheritInverseConfiguration}, * {@code false} otherwise */ public static boolean isInheritInverseConfiguration(PsiMethod method) { return isAnnotated( method, INHERIT_INVERSE_CONFIGURATION, false ); }
/** * Checks if the {@code psiMethod} is a mapping method. A mapping method is a method that is annotated with one of: * <ul> * <li>{@link Mapping}</li> * <li>{@link Mappings}</li> * <li>{@link ValueMapping}</li> * <li>{@link ValueMappings}</li> * </ul> * * @param psiMethod * * @return */ public static boolean isMappingMethod(PsiMethod psiMethod) { return isAnnotated( psiMethod, MAPPING_ANNOTATION_FQN, false ) || isAnnotated( psiMethod, MAPPINGS_ANNOTATION_FQN, false ) || isAnnotated( psiMethod, VALUE_MAPPING_ANNOTATION_FQN, false ) || isAnnotated( psiMethod, VALUE_MAPPINGS_ANNOTATION_FQN, false ); }
public static boolean isAnnotated(@NotNull PsiModifierListOwner listOwner, @NotNull Collection<String> annotations, final boolean checkHierarchy, boolean skipExternal) { for (String annotation : annotations) { if (isAnnotated(listOwner, annotation, checkHierarchy, skipExternal)) return true; } return false; }
/** * Returns true if the class containing the psiElement has the @Api annotation * (com.google.api.server.spi.config.Api). Returns false otherwise. * * @return true if the class containing to the psiElement has the @Api * (com.google.api.server.spi.config.Api). Returns false otherwise. */ public static boolean isEndpointClass(PsiElement psiElement) { PsiClass psiClass = PsiUtils.findClass(psiElement); if (psiClass == null) { return false; } if (AnnotationUtil.isAnnotated(psiClass, GctConstants.APP_ENGINE_ANNOTATION_API, true) || AnnotationUtil.isAnnotated(psiClass, GctConstants.APP_ENGINE_ANNOTATION_API_CLASS, true) || AnnotationUtil.isAnnotated( psiClass, GctConstants.APP_ENGINE_ANNOTATION_API_REFERENCE, true)) { return true; } else { return false; } }
public static boolean isAnnotated(@NotNull PsiModifierListOwner listOwner, @NotNull Collection<String> annotations) { return isAnnotated(listOwner, annotations, false); }
/** * NOTE: requires readAction, if not on the dispatch thread Get an field annotated with a * particular annotation from a class. * * @return the first field found with the annotation */ public static PsiField getAnnotatedFieldFromClass(PsiClass entityClass, String annotationFqn) { for (PsiField field : entityClass.getAllFields()) { if (AnnotationUtil.isAnnotated(field, annotationFqn, false)) { return field; } } return null; }
public static boolean isAnnotated(@NotNull PsiModifierListOwner listOwner, @NonNls @NotNull String annotationFQN, boolean checkHierarchy, boolean skipExternal) { return isAnnotated(listOwner, annotationFQN, checkHierarchy, skipExternal, null); }
public static boolean isAnnotated(@NotNull PsiModifierListOwner listOwner, @NonNls @NotNull String annotationFQN, boolean checkHierarchy) { return isAnnotated(listOwner, annotationFQN, checkHierarchy, true, null); }
private static boolean isPolymorphicMethod(PsiMethodCallExpression expression) { final PsiElement method = expression.getMethodExpression().resolve(); return method instanceof PsiMethod && AnnotationUtil.isAnnotated((PsiMethod)method, CommonClassNames.JAVA_LANG_INVOKE_MH_POLYMORPHIC, false, true); } }
for (String fqn : annotations) { boolean isPattern = fqn.endsWith("*"); if (!isPattern && isAnnotated(owner, fqn, false)) { return true; } else if (isPattern) {
private static boolean isNullabilityDefault(@NotNull PsiAnnotation annotation, boolean nullable, PsiAnnotation.TargetType[] placeTargetTypes) { PsiJavaCodeReferenceElement element = annotation.getNameReferenceElement(); PsiElement declaration = element == null ? null : element.resolve(); if (!(declaration instanceof PsiClass)) return false; if (!AnnotationUtil.isAnnotated((PsiClass)declaration, nullable ? JAVAX_ANNOTATION_NULLABLE : JAVAX_ANNOTATION_NONNULL, false, true)) { return false; } PsiAnnotation tqDefault = AnnotationUtil.findAnnotation((PsiClass)declaration, true, "javax.annotation.meta.TypeQualifierDefault"); if (tqDefault == null) return false; Set<PsiAnnotation.TargetType> required = AnnotationTargetUtil.extractRequiredAnnotationTargets(tqDefault.findAttributeValue(null)); if (required == null) return false; return required.isEmpty() || ContainerUtil.intersects(required, Arrays.asList(placeTargetTypes)); }
/** * 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; }
/** * Returns "/{}" for every parameter with a valid @Named annotation in {@code method} that does * not have @Nullable/@Default. */ private String getPathParameter(PsiMethod method) { StringBuilder path = new StringBuilder(); EndpointPsiElementVisitor elementVisitor = new EndpointPsiElementVisitor(); List<String> annotions = Arrays.asList( GctConstants.APP_ENGINE_ANNOTATION_NULLABLE, "javax.annotation.Nullable", GctConstants.APP_ENGINE_ANNOTATION_DEFAULT_VALUE); for (PsiParameter param : method.getParameterList().getParameters()) { // Check for @Nullable/@Default PsiModifierList modifierList = param.getModifierList(); if (modifierList == null) { continue; } if (AnnotationUtil.isAnnotated(param, annotions)) { continue; } PsiAnnotationMemberValue namedValue = elementVisitor.getNamedAnnotationValue(param); if (namedValue != null) { path.append("/{}"); } } return path.toString(); }
@Nullable private PsiAnnotation findNullabilityAnnotationWithDefault(@NotNull PsiModifierListOwner owner, boolean checkBases, boolean nullable) { PsiAnnotation annotation = findPlainNullabilityAnnotation(owner, checkBases); if (annotation != null) { String qName = annotation.getQualifiedName(); if (qName == null) return null; List<String> contradictory = nullable ? getNotNulls() : getNullables(); if (contradictory.contains(qName)) return null; return annotation; } PsiType type = getOwnerType(owner); if (type == null || TypeConversionUtil.isPrimitiveAndNotNull(type)) return null; // even if javax.annotation.Nullable is not configured, it should still take precedence over ByDefault annotations if (AnnotationUtil.isAnnotated(owner, nullable ? Arrays.asList(DEFAULT_NOT_NULLS) : Arrays.asList(DEFAULT_NULLABLES), checkBases, false)) { return null; } if (!nullable && hasHardcodedContracts(owner)) { return null; } return findNullabilityDefaultInHierarchy(owner, nullable); }
final PsiMethod[] superMethods = method.findSuperMethods(); for (PsiMethod superMethod : superMethods) { if (isAnnotated(superMethod, annotationFQN, checkHierarchy, skipExternal, processed)) return true; final PsiClass[] superClasses = clazz.getSupers(); for (PsiClass superClass : superClasses) { if (isAnnotated(superClass, annotationFQN, checkHierarchy, skipExternal, processed)) return true;