private void injectAnnotation(PsiElementFactory factory, PsiClass generateClass) { if (factory == null || generateClass == null) { return; } PsiModifierList modifierList = generateClass.getModifierList(); PsiElement firstChild = modifierList.getFirstChild(); Pattern pattern = Pattern.compile("@.*?JsonIgnoreProperties"); if (firstChild != null && !pattern.matcher(firstChild.getText()).find()) { PsiAnnotation annotationFromText = factory.createAnnotationFromText("@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)", generateClass); modifierList.addBefore(annotationFromText, firstChild); } } }
private void injectAnnotation(PsiElementFactory factory, PsiClass generateClass) { if (factory == null || generateClass == null) { return; } PsiModifierList modifierList = generateClass.getModifierList(); if (modifierList != null) { PsiElement firstChild = modifierList.getFirstChild(); Pattern pattern = Pattern.compile("@.*?JsonObject"); if (firstChild != null && !pattern.matcher(firstChild.getText()).find()) { PsiAnnotation annotationFromText = factory.createAnnotationFromText("@com.bluelinelabs.logansquare.annotation.JsonObject", generateClass); modifierList.addBefore(annotationFromText, firstChild); } } } }
private void injectAnnotation(PsiElementFactory factory, PsiClass generateClass) { if (factory == null || generateClass == null) { return; } PsiModifierList modifierList = generateClass.getModifierList(); if (modifierList != null) { PsiElement firstChild = modifierList.getFirstChild(); Pattern pattern = Pattern.compile("@.*?NoArgsConstructor"); if (firstChild != null && !pattern.matcher(firstChild.getText()).find()) { PsiAnnotation annotationFromText = factory.createAnnotationFromText("@lombok.NoArgsConstructor", generateClass); modifierList.addBefore(annotationFromText, firstChild); } Pattern pattern2 = Pattern.compile("@.*?Data"); if (firstChild != null && !pattern2.matcher(firstChild.getText()).find()) { PsiAnnotation annotationFromText = factory.createAnnotationFromText("@lombok.Data", generateClass); modifierList.addBefore(annotationFromText, firstChild); } } }
private void injectAutoAnnotation(PsiElementFactory factory, PsiClass cls) { PsiModifierList modifierList = cls.getModifierList(); if (modifierList != null) { PsiElement firstChild = modifierList.getFirstChild(); Pattern pattern = Pattern.compile("@.*?AutoValue"); if (firstChild != null && !pattern.matcher(firstChild.getText()).find()) { PsiAnnotation annotationFromText = factory.createAnnotationFromText("@com.google.auto.value.AutoValue", cls); modifierList.addBefore(annotationFromText, firstChild); } if (!modifierList.hasModifierProperty(PsiModifier.ABSTRACT)) { modifierList.setModifierProperty(PsiModifier.ABSTRACT, true); } } }
private PsiAnnotation copyAnnotation(PsiModifierListOwner owner, PsiAnnotation annotation) { final String notNull = checkContainer(annotation, false); if (notNull != null) { return JavaPsiFacade.getElementFactory(owner.getProject()).createAnnotationFromText("@" + notNull, owner); } return null; }
@Override protected void run(final Result result) throws Throwable { // @A(ownerDomain = "your-company.com") PsiAnnotationMemberValue newMemberValue = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText( "@A(" + API_NAMESPACE_DOMAIN_ATTRIBUTE + " = \"" + SUGGESTED_DOMAIN_ATTRIBUTE + "\")", null) .findDeclaredAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE); annotation.setDeclaredAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE, newMemberValue); } }.execute();
@Override protected void run(final Result result) throws Throwable { // @A(ownerName = "YourCo") PsiAnnotationMemberValue newMemberValue = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText( "@A(" + API_NAMESPACE_NAME_ATTRIBUTE + " = \"" + SUGGESTED_OWNER_ATTRIBUTE + "\")", null) .findDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE); annotation.setDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE, newMemberValue); } }.execute();
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (!(element instanceof PsiAnnotation)) { return; } PsiAnnotation annotation = (PsiAnnotation) element; if (!GctConstants.APP_ENGINE_ANNOTATION_API_METHOD.equals(annotation.getQualifiedName())) { return; } PsiAnnotationMemberValue apiMethodNameAttribute = annotation.findAttributeValue(API_METHOD_NAME_ATTRIBUTE); if (apiMethodNameAttribute == null) { return; } // Get name String nameValueWithQuotes = apiMethodNameAttribute.getText(); String nameValue = EndpointUtilities.removeBeginningAndEndingQuotes(nameValueWithQuotes); // @A(name = "someName") PsiAnnotationMemberValue newMemberValue = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText( "@A(" + API_METHOD_NAME_ATTRIBUTE + " = \"" + nameValue + "_1\")", null) .findDeclaredAttributeValue(API_METHOD_NAME_ATTRIBUTE); apiMethodNameAttribute.replace(newMemberValue); } }
/** * Add unmapped property fix. Property fix that adds a {@link org.mapstruct.Mapping} annotation with the * given {@code target} * * @param method the method to which the property needs to be added * @param target the name of the target property * * @return the Local Quick fix */ private static UnmappedTargetPropertyFix createAddUnmappedTargetPropertyFix(PsiMethod method, String target) { String fqn = MapstructUtil.MAPPING_ANNOTATION_FQN; Supplier<PsiAnnotation> annotationSupplier = () -> JavaPsiFacade.getElementFactory( method.getProject() ) .createAnnotationFromText( "@" + fqn + "(target = \"" + target + "\", source=\"\")", null ); String message = MapStructBundle.message( "inspection.add.unmapped.target.property", target ); return new UnmappedTargetPropertyFix( method, message, MapStructBundle.message( "intention.add.unmapped.target.property" ), annotationSupplier ); }
public void addAnnotation(@NotNull PsiModifierListOwner parameter, @NotNull Annotation annotation) { PsiModifierList modifierList = parameter.getModifierList(); if (JavaUtils.isAnnotationPresent(parameter, annotation) || null == modifierList) { return; } JavaService.getInstance(parameter.getProject()).importClazz((PsiJavaFile) parameter.getContainingFile(), annotation.getQualifiedName()); PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); PsiAnnotation psiAnnotation = elementFactory.createAnnotationFromText(annotation.toString(), parameter); modifierList.add(psiAnnotation); JavaCodeStyleManager.getInstance(project).shortenClassReferences(psiAnnotation.getParent()); }
/** * Add ignore unmapped property fix. Property fix that adds a {@link org.mapstruct.Mapping} annotation that ignores * the given {@code target} * * @param method the method to which the property needs to be added * @param target the name of the target property * * @return the Local Quick fix */ private static UnmappedTargetPropertyFix createAddIgnoreUnmappedTargetPropertyFix(PsiMethod method, String target) { String fqn = MapstructUtil.MAPPING_ANNOTATION_FQN; Supplier<PsiAnnotation> annotationSupplier = () -> JavaPsiFacade.getElementFactory( method.getProject() ) .createAnnotationFromText( "@" + fqn + "(target = \"" + target + "\", ignore= true)", null ); String message = MapStructBundle.message( "inspection.add.ignore.unmapped.target.property", target ); return new UnmappedTargetPropertyFix( method, message, MapStructBundle.message( "intention.add.ignore.unmapped.target.property" ), annotationSupplier ); } }
JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText(newNamedValue, null); assert (newAnnotation.getParameterList().getAttributes().length == 1);
@Override protected void run(final Result result) throws Throwable { // @A(ownerName = "YourCo", ownerDomain = "your-company.com") String annotationString = "@A(" + API_NAMESPACE_NAME_ATTRIBUTE + " = \"" + SUGGESTED_OWNER_ATTRIBUTE + "\", " + API_NAMESPACE_DOMAIN_ATTRIBUTE + " = \"" + "your-company.com" + "\")"; PsiAnnotation newAnnotation = JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText(annotationString, null); PsiAnnotationMemberValue newDomainMemberValue = newAnnotation.findDeclaredAttributeValue(API_NAMESPACE_DOMAIN_ATTRIBUTE); PsiAnnotationMemberValue newNameMemberValue = newAnnotation.findDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE); annotation.setDeclaredAttributeValue(API_NAMESPACE_NAME_ATTRIBUTE, newNameMemberValue); annotation.setDeclaredAttributeValue( API_NAMESPACE_DOMAIN_ATTRIBUTE, newDomainMemberValue); } }.execute();
JavaPsiFacade.getInstance(project) .getElementFactory() .createAnnotationFromText(newNamedValue, null); assert (newAnnotation.getParameterList().getAttributes().length == 1);
/** * 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()); }
PsiAnnotation psiGetterAnnotation = elementFactory.createAnnotationFromText(Annotation.GETTER.toString(), clazz); PsiAnnotation psiGetterAnnotation = elementFactory.createAnnotationFromText(Annotation.SETTER.toString(), clazz); clazz.addBefore(psiGetterAnnotation,JavaUtils.findNealModifierElement(clazz.getFirstChild()).getFirstChild()); JavaCodeStyleManager.getInstance(project).shortenClassReferences(psiGetterAnnotation);
/** * 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 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()); }