private static boolean isJavaElementForType( PsiModifierListOwner modifierListOwner, PsiClass psiClass ) { PsiAnnotation annotation = modifierListOwner.getModifierList().findAnnotation( TypeReference.class.getName() ); if( annotation != null ) { PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes(); for( PsiNameValuePair pair : attributes ) { String fqn = pair.getLiteralValue(); if( psiClass.getQualifiedName().contains( fqn ) ) { return true; } } } return false; }
/** * Implementation of dynamicProxy.toString() */ private String toStringImpl() { StringBuilder result = new StringBuilder(128); result.append('@'); result.append(type.getName()); result.append('('); boolean firstMember = true; PsiNameValuePair[] attributes = myAnnotation.getParameterList().getAttributes(); for (PsiNameValuePair e : attributes) { if (firstMember) { firstMember = false; } else { result.append(", "); } result.append(e.getName()); result.append('='); PsiAnnotationMemberValue value = e.getValue(); result.append(value == null ? "null" : value.getText()); } result.append(')'); return result.toString(); }
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(); } } }
@NotNull public String getDescription(boolean full) { if ( !element.equals(resolve(element.getNameReferenceElement())) ) { return element.getText(); } StringBuilder buf = new StringBuilder(); buf.append('@'); buf.append(element.getQualifiedName()); if ( full && element.getParameterList().getAttributes().length > 0 ) { buf.append('('); boolean first = true; for( PsiNameValuePair value : element.getParameterList().getAttributes() ) { if ( first ) { first = false; } else { buf.append(", "); } buf.append(value.getName()).append("="); buf.append(value.getValue() == null ? "?" : value.getValue().getText()); } buf.append(')'); } return buf.toString(); }
@Override public boolean accepts(@Nullable Object o, ProcessingContext context) { if (o instanceof PsiLiteralExpression) { final PsiAnnotation parentOfType = PsiTreeUtil.getParentOfType((PsiLiteralExpression) o, PsiAnnotation.class); if (parentOfType != null && type.equals(parentOfType.getQualifiedName())) { if (attributeName != null) { final PsiNameValuePair nvp = PsiTreeUtil.getParentOfType((PsiLiteralExpression) o, PsiNameValuePair.class); return nvp != null && attributeName.equals(nvp.getName()); } else { return true; } } } return false; }
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 List<Block> buildBlocks() { final Wrap wrap = Wrap.createWrap(getWrapType(myJavaSettings.ANNOTATION_PARAMETER_WRAP), false); final Alignment alignment = myJavaSettings.ALIGN_MULTILINE_ANNOTATION_PARAMETERS ? Alignment.createAlignment() : null; ChildrenBlocksBuilder.Config config = new ChildrenBlocksBuilder.Config().setDefaultIndent(Indent.getContinuationWithoutFirstIndent()).setIndent(JavaTokenType.RPARENTH, Indent.getNoneIndent() ).setIndent(JavaTokenType.LPARENTH, Indent.getNoneIndent()) .setDefaultWrap(wrap).setNoWrap(JavaTokenType.COMMA).setNoWrap(JavaTokenType.RPARENTH).setNoWrap(JavaTokenType.LPARENTH) .setDefaultAlignment(alignment).setNoAlignment(JavaTokenType.COMMA).setNoAlignment(JavaTokenType.LPARENTH).setNoAlignmentIf(JavaTokenType.RPARENTH, node -> { PsiElement prev = PsiTreeUtil.skipSiblingsBackward(node.getPsi(), PsiWhiteSpace.class); if(prev == null) { return false; } return prev instanceof PsiNameValuePair && !PsiTreeUtil.hasErrorElements(prev); }); return config.createBuilder().buildNodeChildBlocks(myNode, myFactory); }
@Nullable PsiExpression getAnnotationValue(PsiAnnotation annotation, String annotationKey) { PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes(); return Arrays.stream(attributes) .filter(a -> annotationKey.equalsIgnoreCase(a.getName())) .map(PsiNameValuePair::getValue) .filter(v -> v instanceof PsiExpression) .map(v -> (PsiExpression) v) .findFirst().orElse(null); }
private static boolean isJavaElementFor( PsiModifierListOwner modifierListOwner, PsiElement element ) { PsiAnnotation annotation = modifierListOwner.getModifierList().findAnnotation( SourcePosition.class.getName() ); if( annotation != null ) { int textOffset = element.getTextOffset(); int textLength = element instanceof PsiNamedElement ? ((PsiNamedElement)element).getName().length() : element.getTextLength(); PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes(); int offset = -1; for( PsiNameValuePair pair : attributes ) { if( pair.getNameIdentifier().getText().equals( SourcePosition.OFFSET ) ) { String literalValue = pair.getLiteralValue(); if( literalValue == null ) { return false; } offset = Integer.parseInt( literalValue ); break; } } if( offset >= textOffset && offset <= textOffset + textLength ) { return true; } } return false; }
private void addAnnotations( SrcAnnotated<?> srcAnnotated, PsiModifierListOwner annotated ) { for( PsiAnnotation psiAnno : annotated.getModifierList().getAnnotations() ) { SrcAnnotationExpression annoExpr = new SrcAnnotationExpression( psiAnno.getQualifiedName() ); for( PsiNameValuePair value : psiAnno.getParameterList().getAttributes() ) { SrcArgument srcArg = new SrcArgument( new SrcRawExpression( value.getLiteralValue() ) ); annoExpr.addArgument( srcArg ).name( value.getName() ); } srcAnnotated.addAnnotation( annoExpr ); } }
public PsiNameValuePairPattern withName(@NotNull @NonNls final String requiredName) { return with(new PatternCondition<PsiNameValuePair>("withName") { public boolean accepts(@NotNull final PsiNameValuePair psiNameValuePair, final ProcessingContext context) { String actualName = psiNameValuePair.getName(); return requiredName.equals(actualName) || actualName == null && "value".equals(requiredName); } }); }
private void annotateMethod(@NotNull PsiMethod method) { try { AddAnnotationPsiFix fix = new AddAnnotationPsiFix(myAnnotation, method, PsiNameValuePair.EMPTY_ARRAY, myAnnotationsToRemove); fix.invoke(method.getProject(), method.getContainingFile(), method, method); } catch (IncorrectOperationException e) { LOG.error(e); } }
public List<Block> buildBlocks() { final Wrap wrap = Wrap.createWrap(getWrapType(myJavaSettings.ANNOTATION_PARAMETER_WRAP), false); final Alignment alignment = myJavaSettings.ALIGN_MULTILINE_ANNOTATION_PARAMETERS ? Alignment.createAlignment() : null; ChildrenBlocksBuilder.Config config = new ChildrenBlocksBuilder.Config() .setDefaultIndent(Indent.getContinuationWithoutFirstIndent()) .setIndent(JavaTokenType.RPARENTH, Indent.getNoneIndent()) .setIndent(JavaTokenType.LPARENTH, Indent.getNoneIndent()) .setDefaultWrap(wrap) .setNoWrap(JavaTokenType.COMMA) .setNoWrap(JavaTokenType.RPARENTH) .setNoWrap(JavaTokenType.LPARENTH) .setDefaultAlignment(alignment) .setNoAlignment(JavaTokenType.COMMA) .setNoAlignment(JavaTokenType.LPARENTH) .setNoAlignmentIf(JavaTokenType.RPARENTH, new Condition<ASTNode>() { @Override public boolean value(ASTNode node) { PsiElement prev = PsiTreeUtil.skipSiblingsBackward(node.getPsi(), PsiWhiteSpace.class); if (prev == null) return false; return prev instanceof PsiNameValuePair && !PsiTreeUtil.hasErrorElements(prev); } }); return config.createBuilder().buildNodeChildBlocks(myNode, myFactory); }
@Override public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) { if (first.getElementType() == GroovyElementTypes.ANNOTATION_MEMBER_VALUE_PAIR && last.getElementType() == GroovyElementTypes.ANNOTATION_MEMBER_VALUE_PAIR) { ASTNode lparenth = getNode().getFirstChildNode(); ASTNode rparenth = getNode().getLastChildNode(); if (lparenth == null) { getNode().addLeaf(GroovyTokenTypes.mLPAREN, "(", null); } if (rparenth == null) { getNode().addLeaf(GroovyTokenTypes.mRPAREN, ")", null); } final PsiNameValuePair[] nodes = getAttributes(); if (nodes.length == 1) { final PsiNameValuePair pair = nodes[0]; if (pair.getName() == null) { final String text = pair.getValue().getText(); try { final PsiAnnotation annotation = GroovyPsiElementFactory.getInstance(getProject()).createAnnotationFromText("@AAA(value = " + text + ")"); getNode().replaceChild(pair.getNode(), annotation.getParameterList().getAttributes()[0].getNode()); } catch (IncorrectOperationException e) { LOG.error(e); } } } if (anchor == null && before != null) { anchor = before.booleanValue() ? getNode().getLastChildNode() : getNode().getFirstChildNode(); } } return super.addInternal(first, last, anchor, before); }
/** * Returns the value for @Named if it exists for <code>psiParameter</code> or null if it does not * exist. * * @param psiParameter The parameter whose @Named value is to be returned. * @return The @Named value if it exists for <code>psiParameter</code> or null if it does not * exist. */ @Nullable public PsiAnnotationMemberValue getNamedAnnotationValue(PsiParameter psiParameter) { PsiModifierList modifierList = psiParameter.getModifierList(); if (modifierList == null) { return null; } PsiAnnotation annotation = modifierList.findAnnotation("javax.inject.Named"); if (annotation == null) { annotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED); if (annotation == null) { return null; } } PsiNameValuePair[] nameValuePairs = annotation.getParameterList().getAttributes(); if (nameValuePairs.length != 1) { return null; } if (nameValuePairs[0] == null) { return null; } return nameValuePairs[0].getValue(); }
@Override public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) { if (first.getElementType() == ANNOTATION_MEMBER_VALUE_PAIR && last.getElementType() == ANNOTATION_MEMBER_VALUE_PAIR) { ASTNode lparenth = getNode().getFirstChildNode(); ASTNode rparenth = getNode().getLastChildNode(); if (lparenth == null) { getNode().addLeaf(mLPAREN, "(", null); } if (rparenth == null) { getNode().addLeaf(mRPAREN, ")", null); } final PsiNameValuePair[] nodes = getAttributes(); if (nodes.length == 1) { final PsiNameValuePair pair = nodes[0]; if (pair.getName() == null) { final String text = pair.getValue().getText(); try { final PsiAnnotation annotation = GroovyPsiElementFactory.getInstance(getProject()).createAnnotationFromText("@AAA(value = " + text + ")"); getNode().replaceChild(pair.getNode(), annotation.getParameterList().getAttributes()[0].getNode()); } catch (IncorrectOperationException e) { LOG.error(e); } } } if (anchor == null && before != null) { anchor = before.booleanValue() ? getNode().getLastChildNode() : getNode().getFirstChildNode(); } } return super.addInternal(first, last, anchor, before); }
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()); }
private static PsiNameValuePair findContainingNameValuePair(PsiElement highlightedElement) { PsiElement nameValuePair = highlightedElement; while (!(nameValuePair == null || nameValuePair instanceof PsiNameValuePair)) { nameValuePair = nameValuePair.getContext(); } return (PsiNameValuePair) nameValuePair; }
public static boolean isEqualsAndHashCodeCallSuperDefault(HighlightInfo highlightInfo, PsiFile file) { PsiElement element = file.findElementAt(highlightInfo.getStartOffset()); PsiNameValuePair psiNameValuePair = PsiTreeUtil.getParentOfType(element, PsiNameValuePair.class); if (psiNameValuePair == null) { return false; } PsiAnnotation psiAnnotation = PsiTreeUtil.getParentOfType(psiNameValuePair, PsiAnnotation.class); if (psiAnnotation == null) { return false; } return "callSuper".equals(psiNameValuePair.getName()) && "EqualsAndHashCode".equals(PsiAnnotationSearchUtil.getSimpleNameOf(psiAnnotation)); }
public static LocalQuickFix createAddAnnotationQuickFix(@NotNull PsiClass psiClass, @NotNull String annotationFQN, @Nullable String annotationParam) { PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiClass.getProject()); PsiAnnotation newAnnotation = elementFactory.createAnnotationFromText("@" + annotationFQN + "(" + StringUtil.notNullize(annotationParam) + ")", psiClass); final PsiNameValuePair[] attributes = newAnnotation.getParameterList().getAttributes(); return new AddAnnotationFix(annotationFQN, psiClass, attributes); }
private void applyFixInner(final Project project) { final PsiFile file = myAnnotation.getContainingFile(); final Editor editor = CodeInsightUtil.positionCursor(project, file, myAnnotation); if (editor != null) { new WriteCommandAction(project, file) { protected void run(@NotNull Result result) throws Throwable { final PsiNameValuePair valuePair = selectAnnotationAttribute(); if (null != valuePair) { // delete this parameter valuePair.delete(); } if (null != myNewValue) { //add new parameter final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(myAnnotation.getProject()); PsiAnnotation newAnnotation = elementFactory.createAnnotationFromText("@" + myAnnotation.getQualifiedName() + "(" + myName + "=" + myNewValue + ")", myAnnotation.getContext()); final PsiNameValuePair[] attributes = newAnnotation.getParameterList().getAttributes(); myAnnotation.setDeclaredAttributeValue(attributes[0].getName(), attributes[0].getValue()); } UndoUtil.markPsiFileForUndo(file); } @Override protected boolean isGlobalUndoAction() { return true; } }.execute(); } }
private PsiNameValuePair selectAnnotationAttribute() { PsiNameValuePair result = null; PsiNameValuePair[] attributes = myAnnotation.getParameterList().getAttributes(); for (PsiNameValuePair attribute : attributes) { @NonNls final String attributeName = attribute.getName(); if (equals(myName, attributeName) || attributeName == null && myName.equals(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME)) { result = attribute; break; } } return result; }
public TemplateMetaData(TemplateExpression templateExpression, boolean defaultReference, PsiNameValuePair attribute, PsiClass templateClass, VirtualFile templateFile, XmlTag rootTag, Project project) { this.templateExpression = templateExpression; this.defaultReference = defaultReference; this.attribute = attribute; this.templateClass = templateClass; this.templateFile = templateFile; this.rootTag = rootTag; this.project = project; }
private static void ensureTemplateExists(ProblemsHolder holder, PsiAnnotation annotation) { final TemplateMetaData metaData = TemplateUtil.getTemplateMetaData(annotation); if (metaData == null) { return; } final VirtualFile templateVF = metaData.getTemplateFile(); final PsiNameValuePair attribute = metaData.getAttribute(); if (templateVF == null) { if (annotation != null && metaData.isDefaultReference()) { holder.registerProblem(annotation, "Could not find companion Errai UI template: " + metaData.getTemplateExpression().getFileName()); } else if (attribute != null) { holder.registerProblem(attribute, "Errai UI template file cannot be resolved: " + metaData.getTemplateExpression().getFileName()); } } else if (attribute != null && !metaData.getTemplateExpression().getRootNode().equals("")) { final Multimap<String, TemplateDataField> allDataFieldTags = metaData.getAllDataFieldsInTemplate(true); if (!allDataFieldTags.containsKey(metaData.getTemplateExpression().getRootNode())) { holder.registerProblem(attribute, "The data-field element specified for the root " + "note does not exist: " + metaData.getTemplateExpression().getRootNode()); } } }
public static PsiAnnotationMemberValue getAnnotationMemberValue(PsiAnnotation annotation, String attributeName) { final PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes(); for (PsiNameValuePair attribute : attributes) { if (attributeName.equals(attribute.getName())) { final PsiAnnotationMemberValue value = attribute.getValue(); if (value != null) { return value; } break; } } return null; }
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); } }; }
@Override public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException { setMirrorCheckingType(element, null); PsiNameValuePair mirror = SourceTreeToPsiMap.treeToPsiNotNull(element); setMirrorIfPresent(getNameIdentifier(), mirror.getNameIdentifier()); setMirrorIfPresent(getValue(), mirror.getValue()); }
/** * Implementation of dynamicProxy.toString() */ private String toStringImpl() { StringBuilder result = new StringBuilder(128); result.append('@'); result.append(type.getName()); result.append('('); boolean firstMember = true; PsiNameValuePair[] attributes = myAnnotation.getParameterList().getAttributes(); for(PsiNameValuePair e : attributes) { if(firstMember) { firstMember = false; } else { result.append(", "); } result.append(e.getName()); result.append('='); PsiAnnotationMemberValue value = e.getValue(); result.append(value == null ? "null" : value.getText()); } result.append(')'); return result.toString(); }
@Override public void visitNameValuePair(PsiNameValuePair pair) { mVisitor.report("PsiNameValuePair", pair.getText(), pair); super.visitElement(pair); }
@Override public PsiNameValuePair createPsi(@NotNull ASTNode node) { return new PsiNameValuePairImpl(node); }
@Override public PsiNameValuePair createPsi(@NotNull PsiNameValuePairStub stub) { return getPsiFactory(stub).createNameValuePair(stub); }
@NotNull @Override public PsiNameValuePair[] getAttributes() { return getStubOrPsiChildren(JavaStubElementTypes.NAME_VALUE_PAIR, PsiNameValuePair.ARRAY_FACTORY); }
protected PsiNameValuePairPattern() { super(PsiNameValuePair.class); }
public AddAnnotationFix(@NotNull String fqn, @NotNull PsiModifierListOwner modifierListOwner, @NotNull String... annotationsToRemove) { this(fqn, modifierListOwner, PsiNameValuePair.EMPTY_ARRAY, annotationsToRemove); }
public AddAnnotationFix(@NotNull String fqn, @NotNull PsiModifierListOwner modifierListOwner, @NotNull PsiNameValuePair[] values, @NotNull String... annotationsToRemove) { super(fqn, modifierListOwner, values, annotationsToRemove); }