/** * Checks if the parameter is annotated with the provided {@code annotation}. * * @param psiParameter the parameter on which we need to check for the annotation * @param annotation the annotation that we need to find * * @return {@code true} if the {@code psiParameter} is annotated with the {@code annotation}, {@code false} * otherwise */ private static boolean hasAnnotation(PsiParameter psiParameter, String annotation) { PsiModifierList modifierList = psiParameter.getModifierList(); return modifierList != null && modifierList.findAnnotation( annotation ) != null; }
private boolean hasParameterName(PsiParameter psiParameter) { PsiModifierList modifierList = psiParameter.getModifierList(); if (modifierList == null) { return false; } PsiAnnotation annotation = modifierList.findAnnotation("javax.inject.Named"); if (annotation != null) { return true; } annotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED); if (annotation != null) { return true; } return false; }
/** * Returns "/{}" for every parameter with a valid @Named annotation in {@code method} that does * not have @Nullable/@Default. */ private String getPathParameter(PsiMethod method) { StringBuilder path = new StringBuilder(); EndpointPsiElementVisitor elementVisitor = new EndpointPsiElementVisitor(); List<String> annotions = Arrays.asList( GctConstants.APP_ENGINE_ANNOTATION_NULLABLE, "javax.annotation.Nullable", GctConstants.APP_ENGINE_ANNOTATION_DEFAULT_VALUE); for (PsiParameter param : method.getParameterList().getParameters()) { // Check for @Nullable/@Default PsiModifierList modifierList = param.getModifierList(); if (modifierList == null) { continue; } if (AnnotationUtil.isAnnotated(param, annotions)) { continue; } PsiAnnotationMemberValue namedValue = elementVisitor.getNamedAnnotationValue(param); if (namedValue != null) { path.append("/{}"); } } return path.toString(); }
/** * 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(); }
/** Remove @Default and @Nullable from the method parameter if they exist. */ @Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element == null) { return; } if (!(element instanceof PsiParameter)) { return; } PsiModifierList modifierList = ((PsiParameter) element).getModifierList(); PsiAnnotation gaeNullableAnnotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NULLABLE); if (gaeNullableAnnotation != null) { gaeNullableAnnotation.delete(); } PsiAnnotation javaxNullableAnnotation = modifierList.findAnnotation("javax.annotation.Nullable"); if (javaxNullableAnnotation != null) { javaxNullableAnnotation.delete(); } PsiAnnotation defaultValueAnnotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_DEFAULT_VALUE); if (defaultValueAnnotation != null) { defaultValueAnnotation.delete(); } } }
/** * Tests that the Quick fix for a parameter with the javax @Nullable and @DefaultValue returns the * parameter without @Nullable and @DefaultValue. */ public void testQuickFix_withJavaxNullableAndDefaultAnnotation() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameter("param", PsiType.BOOLEAN); parameter.getModifierList().addAnnotation("javax.annotation.Nullable"); parameter.getModifierList().addAnnotation(GctConstants.APP_ENGINE_ANNOTATION_DEFAULT_VALUE); runQuickFixTest(parameter, " boolean param"); }
PsiModifierList modifierList = parameter.getModifierList(); if (modifierList == null) { return;
.getModifierList() .findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NULLABLE) != null) || (psiParameter.getModifierList().findAnnotation("javax.annotation.Nullable") != null) || (psiParameter .getModifierList() .findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_DEFAULT_VALUE) != null)) {
private void validateMethodNameUnique( PsiParameter psiParameter, Map<String, PsiParameter> methodNames) { PsiModifierList modifierList = psiParameter.getModifierList(); if (modifierList == null) { return;
PsiModifierList modifierList = psiParameter.getModifierList(); PsiAnnotation annotation = modifierList.findAnnotation("javax.inject.Named"); if (annotation == null) {
/** Test that the Quick fix for a parameter with @Named returns the same parameter unchanged. */ public void testQuickFix_withNamedAnnotation() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameter("param", PsiType.BOOLEAN); parameter.getModifierList().addAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED); runQuickFixTest(parameter, "@" + GctConstants.APP_ENGINE_ANNOTATION_NAMED + " boolean param"); }
/** * Tests that the Quick fix for a parameter with the javax @Nullable returns the parameter * without @Nullable. */ public void testQuickFix_withJavaxNullableAnnotation() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameter("param", PsiType.BOOLEAN); parameter.getModifierList().addAnnotation("javax.annotation.Nullable"); runQuickFixTest(parameter, " boolean param"); }
/** * Tests that the Quick fix for a parameter with @DefaultValue returns the parameter * without @DefaultValue. */ public void testQuickFix_withDefaultAnnotation() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameter("param", PsiType.BOOLEAN); parameter.getModifierList().addAnnotation(GctConstants.APP_ENGINE_ANNOTATION_DEFAULT_VALUE); runQuickFixTest(parameter, " boolean param"); }
/** * Tests that the Quick fix for a parameter with the App Engine @Nullable returns the parameter * without @Nullable. */ public void testQuickFix_withGaeNullableAnnotation() { Project myProject = myFixture.getProject(); PsiParameter parameter = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createParameter("param", PsiType.BOOLEAN); parameter.getModifierList().addAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NULLABLE); runQuickFixTest(parameter, " boolean param"); }
PsiParameter[] parameters = methodElement.getParameterList().getParameters(); if (index < parameters.length) { PsiModifierList parameterModifiers = parameters[index].getModifierList(); if (parameterModifiers != null) { Collections.addAll(all, parameterModifiers.getAnnotations());
/** * 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()); }