public MissingAttributeException(PsiAnnotation annotation, String attribute) { super( "Attribute \"" + attribute + "\" does not exist in annotation \"" + annotation.getText() + "\""); } }
public InvalidAnnotationException(PsiAnnotation annotation, String annotationType) { super("Annotation \"" + annotation.getText() + "\" is not of type \"" + annotationType + "\""); } }
return mappingAnnotation; if ( !containerAnnotation.getText().contains( "{" ) ) { return JavaPsiFacade.getInstance( project ).getElementFactory().createAnnotationFromText( "@" + MapstructUtil.MAPPINGS_ANNOTATION_FQN + "({\n" + currentMappings + ",\n " + mappingAnnotation.getText() + "\n})", container ); final int curlyBraceIndex = containerAnnotation.getText().lastIndexOf( '}' ); if ( curlyBraceIndex > 0 ) { final String textBeforeCurlyBrace = containerAnnotation.getText().substring( 0, curlyBraceIndex ); int braceIndex = textBeforeCurlyBrace.lastIndexOf( ')' ); final String textToPreserve = braceIndex < 0 ? textBeforeCurlyBrace : textBeforeCurlyBrace.substring( 0, braceIndex ) + "),\n"; return JavaPsiFacade.getInstance( project ).getElementFactory().createAnnotationFromText( textToPreserve + " " + mappingAnnotation.getText() + "\n})", container ); throw new IncorrectOperationException( containerAnnotation.getText() );
private void checkAnnotationTarget(@NonNull PsiAnnotationMemberValue element, PsiModifierList modifierList) { if (element instanceof PsiReferenceExpression) { PsiReferenceExpression ref = (PsiReferenceExpression) element; if ("TYPE_PARAMETER".equals(ref.getReferenceName()) || "TYPE_USE".equals(ref.getReferenceName())) { PsiAnnotation retention = modifierList .findAnnotation("java.lang.annotation.Retention"); if (retention == null || retention.getText().contains("RUNTIME")) { Location location = mContext.getLocation(element); String message = String.format("Type annotations are not " + "supported in Android: %1$s", ref.getReferenceName()); mContext.report(UNSUPPORTED, element, location, message); } } } }
@Override public Object invoke(Object proxy, Method method, Object[] args) { Class<?>[] paramTypes = method.getParameterTypes(); assert paramTypes.length == 0: Arrays.toString(paramTypes); String member = method.getName(); if (member.equals("toString")) { return toStringImpl(); } if (member.equals("annotationType")) { return type; } // Handle annotation member accessors PsiAnnotationMemberValue value = myAnnotation.findAttributeValue(member); if (value == null) { throw new IncompleteAnnotationException(type, member+". (Unable to find attribute in '"+myAnnotation.getText()+"')"); } Object result = JavaPsiFacade.getInstance(myAnnotation.getProject()).getConstantEvaluationHelper().computeConstantExpression(value); if (result == null) { throw new IncompleteAnnotationException(type, member+". (Unable to evaluate annotation value '"+value+"')"); } // todo arrays return result; }
MapstructUtil.MAPPING_ANNOTATION_FQN ); String otherMappings = oldMappingAnnotation == null ? "" : "\n" + oldMappingAnnotation.getText();
/** * Tests that the FullMethodNameInspection's quick fix does not update an annotation that is not * {@link GctConstants.APP_ENGINE_ANNOTATION_API_METHOD} */ public void testQuickFix_NonApiMethodAnnotation() { Project myProject = myFixture.getProject(); String annotationString = "@" + GctConstants.APP_ENGINE_ANNOTATION_NAMED + "(name = \"someName\")"; PsiAnnotation annotation = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createAnnotationFromText(annotationString, null); FullMethodNameInspection.MyQuickFix myQuickFix = new FullMethodNameInspection().new MyQuickFix(); MockProblemDescriptor problemDescriptor = new MockProblemDescriptor(annotation, "", ProblemHighlightType.ERROR); myQuickFix.applyFix(myProject, problemDescriptor); Assert.assertEquals(annotationString, annotation.getText()); }
/** * Tests that the FullMethodNameInspection's quick fix updates the name attribute of {@link * GctConstants.APP_ENGINE_ANNOTATION_API_METHOD} by adding "_1" as a suffix. */ public void testQuickFix_ApiMethodAnnotation() { Project myProject = myFixture.getProject(); String annotationString = "@" + GctConstants.APP_ENGINE_ANNOTATION_API_METHOD + "(name = \"someName\")"; PsiAnnotation annotation = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createAnnotationFromText(annotationString, null); FullMethodNameInspection.MyQuickFix myQuickFix = new FullMethodNameInspection().new MyQuickFix(); MockProblemDescriptor problemDescriptor = new MockProblemDescriptor(annotation, "", ProblemHighlightType.ERROR); myQuickFix.applyFix(myProject, problemDescriptor); Assert.assertEquals( "@" + GctConstants.APP_ENGINE_ANNOTATION_API_METHOD + "(name = \"someName_1\")", annotation.getText()); }
private void runQuickFixTest(String annotationString, String expectedString) { final Project myProject = myFixture.getProject(); PsiAnnotation annotation = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createAnnotationFromText(annotationString, null); MockProblemDescriptor problemDescriptor = new MockProblemDescriptor(annotation, "", ProblemHighlightType.ERROR); ApiNamespaceInspection.MyQuickFix myQuickFix = new ApiNamespaceInspection().new MyQuickFix(); myQuickFix.applyFix(myProject, problemDescriptor); assertEquals(expectedString, annotation.getText()); }
/** * Tests that the NamedResourceInspection's quick fix flagged with {@link * NamedResourceError#MISSING_NAME} for an @Named annotation with no parent updates the query name * to "myName". */ public void testQuickFix_noQueryNameSpecifiedWithoutParameter() { Project myProject = myFixture.getProject(); String annotationString = "@" + GctConstants.APP_ENGINE_ANNOTATION_NAMED + "()"; PsiAnnotation annotation = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createAnnotationFromText(annotationString, null); NamedResourceInspection.MissingNameQuickFix myQuickFix = new NamedResourceInspection().new MissingNameQuickFix(); MockProblemDescriptor problemDescriptor = new MockProblemDescriptor(annotation, "", ProblemHighlightType.ERROR); myQuickFix.applyFix(myProject, problemDescriptor); assertEquals( "@" + GctConstants.APP_ENGINE_ANNOTATION_NAMED + "(\"myName\")", annotation.getText()); }
/** * Tests that the NamedResourceInspection's quick fix flagged with {@link * NamedResourceError#DUPLICATE_PARAMETER} for an @Named annotation updates the query name by * adding "_1" as a suffix. */ public void testQuickFix_duplicateParameter() { Project myProject = myFixture.getProject(); String annotationString = "@" + GctConstants.APP_ENGINE_ANNOTATION_NAMED + "(\"someName\")"; PsiAnnotation annotation = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createAnnotationFromText(annotationString, null); NamedResourceInspection.DuplicateNameQuickFix myQuickFix = new NamedResourceInspection().new DuplicateNameQuickFix(); MockProblemDescriptor problemDescriptor = new MockProblemDescriptor(annotation, "", ProblemHighlightType.ERROR); myQuickFix.applyFix(myProject, problemDescriptor); assertEquals( "@" + GctConstants.APP_ENGINE_ANNOTATION_NAMED + "(\"someName_1\")", annotation.getText()); }
/** * Tests that the NamedResourceInspection's quick fix flagged with {@link * NamedResourceError#MISSING_NAME} for an @Named annotation with a {@link PsiParameter} parent * updates the query name to to the name of the {@link PsiParameter}. */ public void testQuickFix_noQueryNameSpecifiedWithParameter() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameterFromText("@javax.inject.Named() String foobar", null); PsiAnnotation[] annotationsList = parameter.getModifierList().getAnnotations(); assert (annotationsList.length == 1); NamedResourceInspection.MissingNameQuickFix myQuickFix = new NamedResourceInspection().new MissingNameQuickFix(); MockProblemDescriptor problemDescriptor = new MockProblemDescriptor(annotationsList[0], "", ProblemHighlightType.ERROR); myQuickFix.applyFix(myProject, problemDescriptor); assertEquals("@javax.inject.Named(\"foobar\")", annotationsList[0].getText()); }