public static boolean fieldOrMethodIsAnnotated(PsiElement element, String annotationType) { final PsiElement e = findFieldOrMethod(element); if (e instanceof PsiField) { final PsiModifierList modifierList = ((PsiField) e).getModifierList(); if (modifierList != null) { for (PsiAnnotation psiAnnotation : modifierList.getAnnotations()) { final String qualifiedName = psiAnnotation.getQualifiedName(); if (qualifiedName != null && qualifiedName.equals(annotationType)) { return true; } } } } else if (e instanceof PsiMethod) { for (PsiAnnotation psiAnnotation : ((PsiMethod) e).getModifierList().getAnnotations()) { final String qualifiedName = psiAnnotation.getQualifiedName(); if (qualifiedName != null && qualifiedName.equals(annotationType)) { return true; } } } return false; }
public static String cleanRefIdForPsiElement(PsiElement element) { final Language language = element.getLanguage(); if (language instanceof JavaLanguage) { if (element instanceof PsiModifierList) { PsiAnnotation[] annotations = ((PsiAnnotationOwner) element).getAnnotations(); for (PsiAnnotation annotation : annotations) { if (AnnoRefConfigSettings.getInstance(element.getProject()) .getAnnoRefState() .ANNOREF_ANNOTATION_FQN .equals(annotation.getQualifiedName())) { String refKey = SQLRefNamingUtil.cleanAnnoRefForName(annotation.getContainingFile(), annotation); if (refKey != null) { return refKey; } } } } if (element instanceof PsiAnnotation) { final PsiAnnotation psiAnnotation = (PsiAnnotation) element; if (AnnoRefConfigSettings.getInstance(element.getProject()) .getAnnoRefState() .ANNOREF_ANNOTATION_FQN .equals(psiAnnotation.getQualifiedName())) { String cleanedAnnoRef = SQLRefNamingUtil.cleanAnnoRefForName( psiAnnotation.getContainingFile(), psiAnnotation); if (cleanedAnnoRef != null) { return cleanedAnnoRef; } } if (element instanceof PsiJavaToken && ((PsiJavaToken) element).getTokenType() == JavaTokenType.STRING_LITERAL) { final String refKey = StringUtils.cleanQuote(element.getText()); if (refKey != null) { return refKey; } } } } if (language instanceof XMLLanguage) { if (SQLRefNamingUtil.isPropitiousXmlFile(element.getContainingFile())) { final boolean isXmlTokenValid = new FilterElementProcessor( new XmlTokenTypeFilter(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)) .execute(element); if (isXmlTokenValid) { final String refKey = getSqlRefKey(element); if (refKey != null) { return refKey; } } } } return null; }
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; }
@Override public void visitAnnotation(PsiAnnotation annotation) { super.visitAnnotation(annotation); @NonNls final String qualifiedName = annotation.getQualifiedName(); if (qualifiedName == null || !qualifiedName.startsWith("org.junit.")) { return; } myJUnit4AnnotationFound = true; }
public static PsiAnnotation findAnnotation( @NotNull PsiAnnotationOwner modifierList, @NotNull String qualifiedName) { final String shortName = StringUtil.getShortName(qualifiedName); PsiAnnotation[] annotations = modifierList.getAnnotations(); for (PsiAnnotation annotation : annotations) { final PsiJavaCodeReferenceElement referenceElement = annotation.getNameReferenceElement(); if (referenceElement != null && shortName.equals(referenceElement.getReferenceName())) { if (qualifiedName.equals(annotation.getQualifiedName())) return annotation; } } return null; }
public static boolean typeIsAnnotated(PsiClass psiClass, String annotationType) { final PsiModifierList modifierList = psiClass.getModifierList(); if (modifierList != null) { for (PsiAnnotation psiAnnotation : modifierList.getAnnotations()) { final String qualifiedName = psiAnnotation.getQualifiedName(); if (qualifiedName != null && qualifiedName.equals(annotationType)) { return true; } } } return false; }
public static PsiAnnotation getAnnotationFromElement(PsiElement element, String annotationType) { if (element instanceof PsiModifierListOwner) { final PsiModifierList modifierList = ((PsiModifierListOwner) element).getModifierList(); if (modifierList != null) { for (PsiAnnotation annotation : modifierList.getAnnotations()) { if (annotationType.equals(annotation.getQualifiedName())) { return annotation; } } } } return null; }
/** * @param annotation annotation to process. * @return {@code @SideEffects} annotation value. Returns {@link Collections#emptyList()} if * {@code annotation} is {@code null} or annotation is not a {@code @SideEffects} annotation. * @since 0.1 */ @NotNull public static List<PsiAnnotationMemberValue> getSideEffectsAnnotationValue( @Nullable PsiAnnotation annotation) { if (annotation == null) { return emptyList(); } String concernsQualifiedName = annotation.getQualifiedName(); if (!QUALIFIED_NAME_SIDE_EFFECTS.equals(concernsQualifiedName)) { return emptyList(); } return getAnnotationDefaultParameterValue(annotation); }
private static boolean hasAnnotation( PsiModifierListOwner modifierListOwner, String qualifiedName) { PsiModifierList modifierList = modifierListOwner.getModifierList(); if (modifierList != null) { for (PsiAnnotation annotation : modifierList.getAnnotations()) { if (annotation instanceof ClsAnnotationImpl) { if (qualifiedName.equals(annotation.getQualifiedName())) { return true; } } } } return false; }
public static boolean isTestNGAnnotation(PsiAnnotation annotation) { String qName = annotation.getQualifiedName(); if (qName != null) { if (qName.equals(TEST_ANNOTATION_FQN)) return true; for (String qn : CONFIG_ANNOTATIONS_FQN) { if (qName.equals(qn)) return true; } if (qName.equals(TEST_ANNOTATION_FQN)) return true; for (String qn : CONFIG_ANNOTATIONS_FQN) { if (qName.equals(qn)) return true; } } return false; }
public SNode resolveAnnotation(PsiAnnotation a) { String fqName = a.getQualifiedName(); // TODO q: handle this case? create dynamic reference? if (fqName == null) { return null; } System.out.println("Anno resolved: " + fqName); SNode anno = SConceptOperations.createNewNode( "jetbrains.mps.baseLanguage.structure.AnnotationInstance", null); anno.setReference("annotation", new DynamicReference("annotation", anno, null, fqName)); return anno; }
@Nullable public static HighlightInfo checkRepeatableAnnotation(PsiAnnotation annotation) { String qualifiedName = annotation.getQualifiedName(); if (!CommonClassNames.JAVA_LANG_ANNOTATION_REPEATABLE.equals(qualifiedName)) return null; String description = doCheckRepeatableAnnotation(annotation); if (description != null) { PsiAnnotationMemberValue containerRef = PsiImplUtil.findAttributeValue(annotation, null); if (containerRef != null) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(containerRef) .descriptionAndTooltip(description) .create(); } } return null; }
private static SQLRefReference getValidAnnoReference( final PsiAnnotation annotation, final Project project) { if (AnnoRefConfigSettings.getInstance(project) .getAnnoRefState() .ANNOREF_ANNOTATION_FQN .equals(annotation.getQualifiedName())) { String cleanedAnnoRef = SQLRefNamingUtil.cleanAnnoRefForName(annotation.getContainingFile(), annotation); if (cleanedAnnoRef != null) { SQLRefReference sqlRefReferenceForID = ServiceManager.getService(project, SQLRefRepository.class) .getSQLRefReferenceForID(cleanedAnnoRef); if (!sqlRefReferenceForID.getXmlQueryElements().isEmpty()) { return sqlRefReferenceForID; } } } return null; }
@Nullable public static PsiAnnotation findAnnotation( @Nullable PsiAnnotationOwner annotationOwner, @NotNull String qualifiedName) { if (annotationOwner == null) return null; PsiAnnotation[] annotations = annotationOwner.getAnnotations(); if (annotations.length == 0) return null; String shortName = StringUtil.getShortName(qualifiedName); for (PsiAnnotation annotation : annotations) { PsiJavaCodeReferenceElement referenceElement = annotation.getNameReferenceElement(); if (referenceElement != null && shortName.equals(referenceElement.getReferenceName())) { if (qualifiedName.equals(annotation.getQualifiedName())) { return annotation; } } } return null; }
static AllowedValues getAllowedValues( @NotNull PsiModifierListOwner element, PsiType type, Set<PsiClass> visited) { PsiAnnotation[] annotations = AnnotationUtil.getAllAnnotations(element, true, null); for (PsiAnnotation annotation : annotations) { AllowedValues values; if (type != null && MagicConstant.class.getName().equals(annotation.getQualifiedName())) { // PsiAnnotation magic = AnnotationUtil.findAnnotationInHierarchy(element, // Collections.singleton(MagicConstant.class.getName())); values = getAllowedValuesFromMagic(element, type, annotation); if (values != null) return values; } PsiJavaCodeReferenceElement ref = annotation.getNameReferenceElement(); PsiElement resolved = ref == null ? null : ref.resolve(); if (!(resolved instanceof PsiClass) || !((PsiClass) resolved).isAnnotationType()) continue; PsiClass aClass = (PsiClass) resolved; if (visited == null) visited = new THashSet<PsiClass>(); if (!visited.add(aClass)) continue; values = getAllowedValues(aClass, type, visited); if (values != null) return values; } return parseBeanInfo(element); }
public static boolean modifierListsAreEquivalent( @Nullable PsiModifierList list1, @Nullable PsiModifierList list2) { if (list1 == null) { return list2 == null; } else if (list2 == null) { return false; } final PsiAnnotation[] annotations = list1.getAnnotations(); for (PsiAnnotation annotation : annotations) { final String qualifiedName = annotation.getQualifiedName(); if (qualifiedName == null) { return false; } if (list2.findAnnotation(qualifiedName) == null) { return false; } } if (list1.hasModifierProperty(PsiModifier.ABSTRACT) && !list2.hasModifierProperty(PsiModifier.ABSTRACT)) { return false; } if (list1.hasModifierProperty(PsiModifier.FINAL) && !list2.hasModifierProperty(PsiModifier.FINAL)) { return false; } if (list1.hasModifierProperty(PsiModifier.NATIVE) && !list2.hasModifierProperty(PsiModifier.NATIVE)) { return false; } if (list1.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) && !list2.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) { return false; } if (list1.hasModifierProperty(PsiModifier.PRIVATE) && !list2.hasModifierProperty(PsiModifier.PRIVATE)) { return false; } if (list1.hasModifierProperty(PsiModifier.PROTECTED) && !list2.hasModifierProperty(PsiModifier.PROTECTED)) { return false; } if (list1.hasModifierProperty(PsiModifier.PUBLIC) && !list2.hasModifierProperty(PsiModifier.PUBLIC)) { return false; } if (list1.hasModifierProperty(PsiModifier.STATIC) && !list2.hasModifierProperty(PsiModifier.STATIC)) { return false; } if (list1.hasModifierProperty(PsiModifier.STRICTFP) && !list2.hasModifierProperty(PsiModifier.STRICTFP)) { return false; } if (list1.hasModifierProperty(PsiModifier.SYNCHRONIZED) && !list2.hasModifierProperty(PsiModifier.SYNCHRONIZED)) { return false; } if (list1.hasModifierProperty(PsiModifier.TRANSIENT) && !list2.hasModifierProperty(PsiModifier.TRANSIENT)) { return false; } return !(list1.hasModifierProperty(PsiModifier.VOLATILE) && !list2.hasModifierProperty(PsiModifier.VOLATILE)); }
/** * spring bean reference annotator * * @param psiElement psi element * @param holder annotation holder */ public void annotate(PsiElement psiElement, AnnotationHolder holder) { if (!(psiElement instanceof PsiJavaFile)) { return; } // Get java classes for (PsiClass javaClass : ((PsiJavaFile) psiElement).getClasses()) { PsiClass superClass = javaClass.getSuperClass(); PsiField[] superClassFields = superClass != null ? superClass.getAllFields() : new PsiField[] {}; for (PsiField field : javaClass.getAllFields()) { // We don't need scan super class fields if (isContain(superClassFields, field)) { continue; } PsiModifierList modifierList = field.getModifierList(); if (modifierList == null) { continue; } for (PsiAnnotation psiAnnotation : modifierList.getAnnotations()) { String qcName = psiAnnotation.getQualifiedName(); if (qcName == null || !qcName.startsWith("org.unitils.spring.annotation.")) { continue; } SpringManager springManager = SpringManager.getInstance(psiElement.getProject()); SpringModel springModel = springManager.getCombinedModel(ModuleUtil.findModuleForPsiElement(psiElement)); if (springModel == null) { continue; } // by type if (qcName.endsWith("SpringBeanByType")) { final PsiType type = field.getType(); if (!(type instanceof PsiClassType)) { continue; } final PsiClass typeClass = ((PsiClassType) type).resolve(); if (typeClass == null) { continue; } List<SpringBaseBeanPointer> springBaseBeanPointerList = springModel.findBeansByPsiClass(typeClass); if (springBaseBeanPointerList.size() > 0) { List<PsiElement> elements = new ArrayList<PsiElement>(); for (SpringBaseBeanPointer springBaseBeanPointer : springBaseBeanPointerList) { elements.add(springBaseBeanPointer.getPsiElement()); } NavigationGutterIconBuilder.create(SpringIcons.SPRING_BEAN_ICON) .setTooltipText("Navigate to the spring bean declaration(s)") .setTargets(elements) .install(holder, field.getNameIdentifier()); } } // by name else { String beanName = field.getName(); if (qcName.endsWith("SpringBean")) { PsiAnnotationMemberValue attributeValue = psiAnnotation.findAttributeValue("value"); if (attributeValue != null && attributeValue.getText() != null) { beanName = attributeValue.getText().replace("\"", ""); } } if (beanName != null && StringUtil.isNotEmpty(beanName)) { SpringBeanPointer springBeanPointer = springModel.findBeanByName(beanName); if (springBeanPointer != null) { NavigationGutterIconBuilder.create(SpringIcons.SPRING_BEAN_ICON) .setTooltipText("Navigate to the spring bean declaration(s)") .setTarget(springBeanPointer.getPsiElement()) .install(holder, field.getNameIdentifier()); } } } } } } }
@Nullable public String getNullable(PsiModifierListOwner owner) { PsiAnnotation annotation = findNullabilityAnnotation(owner, false, true); return annotation == null ? null : annotation.getQualifiedName(); }