Search in sources :

Example 51 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project google-cloud-intellij by GoogleCloudPlatform.

the class NamedResourceInspectionTest method testQuickFix_noQueryNameSpecifiedWithoutParameter.

/**
 * 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());
}
Also used : Project(com.intellij.openapi.project.Project) MockProblemDescriptor(com.intellij.testFramework.MockProblemDescriptor) PsiAnnotation(com.intellij.psi.PsiAnnotation)

Example 52 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project google-cloud-intellij by GoogleCloudPlatform.

the class NamedResourceInspectionTest method testQuickFix_noQueryNameSpecifiedWithParameter.

/**
 * 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());
}
Also used : Project(com.intellij.openapi.project.Project) PsiParameter(com.intellij.psi.PsiParameter) MockProblemDescriptor(com.intellij.testFramework.MockProblemDescriptor) PsiAnnotation(com.intellij.psi.PsiAnnotation)

Example 53 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project google-cloud-intellij by GoogleCloudPlatform.

the class RestSignatureInspectionTest method initializePsiMethod.

private void initializePsiMethod(String methodName, String httpMethodValue, String pathValue) {
    PsiAnnotationMemberValue mockAnnotationMemberValue1 = mock(PsiAnnotationMemberValue.class);
    when(mockAnnotationMemberValue1.getText()).thenReturn(httpMethodValue);
    PsiAnnotationMemberValue mockAnnotationMemberValue2 = mock(PsiAnnotationMemberValue.class);
    when(mockAnnotationMemberValue2.getText()).thenReturn(pathValue);
    PsiAnnotation mockAnnotation = mock(PsiAnnotation.class);
    when(mockAnnotation.getQualifiedName()).thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API_METHOD);
    when(mockAnnotation.findAttributeValue("httpMethod")).thenReturn(mockAnnotationMemberValue1);
    when(mockAnnotation.findAttributeValue("path")).thenReturn(mockAnnotationMemberValue2);
    PsiAnnotation[] mockAnnotationsArray = { mockAnnotation };
    PsiModifierList mockModifierList = mock(PsiModifierList.class);
    when(mockModifierList.getAnnotations()).thenReturn(mockAnnotationsArray);
    mockPsiMethod = mock(PsiMethod.class);
    when(mockPsiMethod.getModifierList()).thenReturn(mockModifierList);
    when(mockPsiMethod.getName()).thenReturn(methodName);
    when(mockPsiMethod.getContainingClass()).thenReturn(mockPsiClass);
    PsiParameterList mockParameterList = mock(PsiParameterList.class);
    when(mockParameterList.getParameters()).thenReturn(new PsiParameter[0]);
    when(mockPsiMethod.getParameterList()).thenReturn(mockParameterList);
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiParameterList(com.intellij.psi.PsiParameterList) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue) PsiModifierList(com.intellij.psi.PsiModifierList)

Example 54 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project google-cloud-intellij by GoogleCloudPlatform.

the class FullMethodNameInspectionTest method testQuickFix_NonApiMethodAnnotation.

/**
 * 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());
}
Also used : Project(com.intellij.openapi.project.Project) MockProblemDescriptor(com.intellij.testFramework.MockProblemDescriptor) PsiAnnotation(com.intellij.psi.PsiAnnotation)

Example 55 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project azure-tools-for-java by Microsoft.

the class FunctionUtils method patchStorageBinding.

private static void patchStorageBinding(final PsiMethod method, final List<Binding> bindings) {
    final PsiAnnotation storageAccount = AnnotationUtil.findAnnotation(method, StorageAccount.class.getCanonicalName());
    if (storageAccount != null) {
        // todo: Remove System.out.println
        System.out.println(message("function.binding.storage.found"));
        final String connectionString = AnnotationUtil.getDeclaredStringAttributeValue(storageAccount, "value");
        // Replace empty connection string
        bindings.stream().filter(binding -> binding.getBindingEnum().isStorage()).filter(binding -> StringUtils.isEmpty((String) binding.getAttribute("connection"))).forEach(binding -> binding.setAttribute("connection", connectionString));
    } else {
        // todo: Remove System.out.println
        System.out.println(message("function.binding.storage.notFound"));
    }
}
Also used : JsonObject(com.google.gson.JsonObject) Azure(com.microsoft.azure.toolkit.lib.Azure) AzureExecutionException(com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException) Arrays(java.util.Arrays) AzureConfiguration(com.microsoft.azure.toolkit.lib.AzureConfiguration) AzureBundle.message(com.microsoft.intellij.ui.messages.AzureBundle.message) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) JsonUtils(com.microsoft.azuretools.utils.JsonUtils) VirtualFile(com.intellij.openapi.vfs.VirtualFile) StringUtils(org.apache.commons.lang3.StringUtils) Library(com.intellij.openapi.roots.libraries.Library) ClassUtils(org.apache.commons.lang3.ClassUtils) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) Map(java.util.Map) Module(com.intellij.openapi.module.Module) CompilerModuleExtension(com.intellij.openapi.roots.CompilerModuleExtension) Path(java.nio.file.Path) MavenProject(org.jetbrains.idea.maven.project.MavenProject) FunctionCliResolver(com.microsoft.azure.toolkit.lib.appservice.utils.FunctionCliResolver) OrderRootType(com.intellij.openapi.roots.OrderRootType) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable) MetaAnnotationUtil(com.intellij.codeInsight.MetaAnnotationUtil) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Collectors(java.util.stream.Collectors) MavenProjectsManager(org.jetbrains.idea.maven.project.MavenProjectsManager) List(java.util.List) AnnotatedElementsSearch(com.intellij.psi.search.searches.AnnotatedElementsSearch) AnnotationUtil(com.intellij.codeInsight.AnnotationUtil) PsiAnnotation(com.intellij.psi.PsiAnnotation) IntelliJSecureStore(com.microsoft.intellij.secure.IntelliJSecureStore) JvmParameter(com.intellij.lang.jvm.JvmParameter) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) ModuleManager(com.intellij.openapi.module.ModuleManager) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) ArrayUtils(org.apache.commons.lang3.ArrayUtils) HashMap(java.util.HashMap) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PsiClass(com.intellij.psi.PsiClass) Charset(java.nio.charset.Charset) Project(com.intellij.openapi.project.Project) FunctionConfiguration(com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration) Binding(com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner) PsiMethod(com.intellij.psi.PsiMethod) MapUtils(org.apache.commons.collections.MapUtils) Files(java.nio.file.Files) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) JvmAnnotation(com.intellij.lang.jvm.JvmAnnotation) File(java.io.File) BindingEnum(com.microsoft.azure.toolkit.lib.legacy.function.bindings.BindingEnum) Paths(java.nio.file.Paths) Log(com.microsoft.azure.toolkit.lib.common.logging.Log) StorageAccount(com.microsoft.azure.functions.annotation.StorageAccount) StorageAccount(com.microsoft.azure.functions.annotation.StorageAccount) PsiAnnotation(com.intellij.psi.PsiAnnotation)

Aggregations

PsiAnnotation (com.intellij.psi.PsiAnnotation)61 PsiModifierList (com.intellij.psi.PsiModifierList)18 PsiAnnotationMemberValue (com.intellij.psi.PsiAnnotationMemberValue)14 PsiMethod (com.intellij.psi.PsiMethod)14 NotNull (org.jetbrains.annotations.NotNull)13 Project (com.intellij.openapi.project.Project)12 PsiClass (com.intellij.psi.PsiClass)11 PsiElement (com.intellij.psi.PsiElement)9 MockProblemDescriptor (com.intellij.testFramework.MockProblemDescriptor)6 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)5 PsiParameter (com.intellij.psi.PsiParameter)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5 Nullable (org.jetbrains.annotations.Nullable)5 PsiParameterList (com.intellij.psi.PsiParameterList)4 TestSize (com.google.idea.blaze.base.dependencies.TestSize)3 PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)3 PsiNameValuePair (com.intellij.psi.PsiNameValuePair)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Nullable (javax.annotation.Nullable)3