public static boolean isOnXParameterAnnotation(HighlightInfo highlightInfo, PsiFile file) { if (!(ANNOTATION_TYPE_EXPECTED.equals(highlightInfo.getDescription()) || CANNOT_RESOLVE_UNDERSCORES_MESSAGE.matcher(StringUtil.notNullize(highlightInfo.getDescription())).matches())) { return false; } PsiElement highlightedElement = file.findElementAt(highlightInfo.getStartOffset()); PsiNameValuePair nameValuePair = findContainingNameValuePair(highlightedElement); if (nameValuePair == null || !(nameValuePair.getContext() instanceof PsiAnnotationParameterList)) { return false; } String parameterName = nameValuePair.getName(); if (!ONX_PARAMETERS.contains(parameterName)) { return false; } PsiElement containingAnnotation = nameValuePair.getContext().getContext(); return containingAnnotation instanceof PsiAnnotation && ONXABLE_ANNOTATIONS.contains(((PsiAnnotation) containingAnnotation).getQualifiedName()); }
protected void removeDefaultAnnotation(@NotNull PsiModifierListOwner targetElement, @NotNull Class<? extends Annotation> annotationClass) { final PsiAnnotation psiAnnotation = PsiAnnotationSearchUtil.findAnnotation(targetElement, annotationClass); if (null != psiAnnotation) { boolean hasOnlyDefaultValues = true; final PsiAnnotationParameterList psiAnnotationParameterList = psiAnnotation.getParameterList(); for (PsiNameValuePair nameValuePair : psiAnnotationParameterList.getAttributes()) { if (null != psiAnnotation.findDeclaredAttributeValue(nameValuePair.getName())) { hasOnlyDefaultValues = false; break; } } if (hasOnlyDefaultValues) { psiAnnotation.delete(); } } }
public static AnnotationValueElement getValueStringFromAnnotationWithDefault(PsiAnnotation annotation) { final PsiAnnotationParameterList parameterList = annotation.getParameterList(); final PsiNameValuePair[] attributes = parameterList.getAttributes(); final PsiElement logicalElement = getImmediateOwnerElement(annotation); if (logicalElement == null) { return null; } final String value; final PsiElement errorElement; if (attributes.length == 0) { value = getNameOfElement(logicalElement); errorElement = annotation; } else { final String text = attributes[0].getText(); value = text.substring(1, text.length() - 1); errorElement = attributes[0]; } return new AnnotationValueElement(value, errorElement); }
public static boolean isOnXParameterValue(HighlightInfo highlightInfo, PsiFile file) { if (!CANNOT_FIND_METHOD_VALUE_MESSAGE.equals(highlightInfo.getDescription())) { return false; } PsiElement highlightedElement = file.findElementAt(highlightInfo.getStartOffset()); PsiNameValuePair nameValuePair = findContainingNameValuePair(highlightedElement); if (nameValuePair == null || !(nameValuePair.getContext() instanceof PsiAnnotationParameterList)) { return false; } PsiElement leftSibling = nameValuePair.getContext().getPrevSibling(); return (leftSibling != null && UNDERSCORES.matcher(StringUtil.notNullize(leftSibling.getText())).matches()); }
@NotNull public static Collection<String> getOnX(@NotNull PsiAnnotation psiAnnotation, @NotNull String parameterName) { PsiAnnotationMemberValue onXValue = psiAnnotation.findAttributeValue(parameterName); if (!(onXValue instanceof PsiAnnotation)) { return Collections.emptyList(); } Collection<PsiAnnotation> annotations = PsiAnnotationUtil.getAnnotationValues((PsiAnnotation) onXValue, "value", PsiAnnotation.class); Collection<String> annotationStrings = new ArrayList<String>(); for (PsiAnnotation annotation : annotations) { PsiAnnotationParameterList params = annotation.getParameterList(); annotationStrings.add(PsiAnnotationSearchUtil.getSimpleNameOf(annotation) + params.getText()); } return annotationStrings; }
public static PsiElement findDefaultValue(PsiAnnotation annotation){ final PsiAnnotationParameterList parameters = annotation.getParameterList(); final PsiNameValuePair[] pairs = parameters.getAttributes(); for(PsiNameValuePair pair : pairs){ final String name = pair.getName(); if("value".equals(name) || name == null){ return pair.getValue(); } } return null; }
public ClsAnnotationImpl(final PsiAnnotationStub stub) { super(stub); myReferenceElement = new AtomicNotNullLazyValue<ClsJavaCodeReferenceElementImpl>() { @NotNull @Override protected ClsJavaCodeReferenceElementImpl compute() { String annotationText = getStub().getText(); int index = annotationText.indexOf('('); String refText = index > 0 ? annotationText.substring(1, index) : annotationText.substring(1); return new ClsJavaCodeReferenceElementImpl(ClsAnnotationImpl.this, refText); } }; myParameterList = new AtomicNotNullLazyValue<ClsAnnotationParameterListImpl>() { @NotNull @Override protected ClsAnnotationParameterListImpl compute() { PsiNameValuePair[] attrs = getStub().getText().indexOf('(') > 0 ? PsiTreeUtil.getRequiredChildOfType(getStub().getPsiElement(), PsiAnnotationParameterList.class).getAttributes() : PsiNameValuePair.EMPTY_ARRAY; return new ClsAnnotationParameterListImpl(ClsAnnotationImpl.this, attrs); } }; }
@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; }
@Override public void visitAnnotationParameterList(PsiAnnotationParameterList list) { mVisitor.report("PsiAnnotationParameterList", list.getText(), list); super.visitElement(list); }
@Override public PsiAnnotationParameterList createPsi(@NotNull ASTNode node) { return new PsiAnnotationParamListImpl(node); }
@Override public PsiAnnotationParameterList createPsi(@NotNull PsiAnnotationParameterListStub stub) { return getPsiFactory(stub).createAnnotationParameterList(stub); }
@NotNull @Override public PsiElementVisitor buildVisitor( @NotNull final com.intellij.codeInspection.ProblemsHolder holder, boolean isOnTheFly) { return new EndpointPsiElementVisitor() { @Override public void visitAnnotation(PsiAnnotation annotation) { if (annotation == null) { return; } if (!GctConstants.APP_ENGINE_ANNOTATION_API.equals(annotation.getQualifiedName())) { return; } // Need to check for user added attributes because default values are used when not // specified by user and we are only interested in the user specified values PsiAnnotationParameterList parameterList = annotation.getParameterList(); if (parameterList.getAttributes().length == 0) { return; } PsiAnnotationMemberValue annotationMemberValue = annotation.findAttributeValue(API_NAME_ATTRIBUTE); if (annotationMemberValue == null) { return; } String nameValueWithQuotes = annotationMemberValue.getText(); String nameValue = EndpointUtilities.removeBeginningAndEndingQuotes(nameValueWithQuotes); // Empty API name is valid if (nameValue.isEmpty()) { return; } if (!API_NAME_PATTERN.matcher(nameValue).matches()) { holder.registerProblem( annotationMemberValue, "Invalid api name: it must start with a lower case letter and consists only of " + "letter and digits", new MyQuickFix()); } } }; }
@Override @NotNull public PsiAnnotationParameterList getParameterList() { return myParameterList.getValue(); }
@Override @NotNull public PsiAnnotationParameterList getParameterList() { return myParameterList; }