Search in sources :

Example 21 with PsiAnnotation

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

the class RestSignatureInspectionTest method initializePsiClass.

private void initializePsiClass(String apiResource, String apiClassResource) {
    PsiAnnotationMemberValue mockAnnotationMemberValue1 = mock(PsiAnnotationMemberValue.class);
    when(mockAnnotationMemberValue1.getText()).thenReturn(apiResource);
    PsiAnnotationMemberValue mockAnnotationMemberValue2 = mock(PsiAnnotationMemberValue.class);
    when(mockAnnotationMemberValue2.getText()).thenReturn(apiClassResource);
    // Mock @Api(resource = "")
    PsiAnnotation mockAnnotation1 = mock(PsiAnnotation.class);
    when(mockAnnotation1.getQualifiedName()).thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API);
    when(mockAnnotation1.findAttributeValue("resource")).thenReturn(mockAnnotationMemberValue1);
    // Mock @ApiClass(resource = "")
    PsiAnnotation mockAnnotation2 = mock(PsiAnnotation.class);
    when(mockAnnotation2.getQualifiedName()).thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API_CLASS);
    when(mockAnnotation2.findAttributeValue("resource")).thenReturn(mockAnnotationMemberValue2);
    PsiAnnotation[] mockAnnotationsArray = { mockAnnotation1, mockAnnotation2 };
    PsiModifierList mockModifierList = mock(PsiModifierList.class);
    when(mockModifierList.getAnnotations()).thenReturn(mockAnnotationsArray);
    mockPsiClass = mock(PsiClass.class);
    when(mockPsiClass.getModifierList()).thenReturn(mockModifierList);
}
Also used : PsiClass(com.intellij.psi.PsiClass) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue) PsiModifierList(com.intellij.psi.PsiModifierList)

Example 22 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project component-runtime by Talend.

the class SuggestionServiceImpl method fromComponent.

private Stream<Suggestion> fromComponent(final PsiClass clazz, final String defaultFamily) {
    final PsiAnnotation componentAnnotation = AnnotationUtil.findAnnotation(clazz, PARTITION_MAPPER, PROCESSOR, EMITTER);
    final PsiAnnotationMemberValue name = componentAnnotation.findAttributeValue("name");
    if (name == null || "\"\"".equals(name.getText())) {
        return Stream.empty();
    }
    final PsiAnnotationMemberValue familyValue = componentAnnotation.findAttributeValue("family");
    final String componentFamily = (familyValue == null || removeQuotes(familyValue.getText()).isEmpty()) ? null : removeQuotes(familyValue.getText());
    final String family = ofNullable(componentFamily).orElseGet(() -> ofNullable(defaultFamily).orElse(null));
    if (family == null) {
        return Stream.empty();
    }
    return Stream.of(new Suggestion(family + "." + DISPLAY_NAME, Suggestion.Type.Family), new Suggestion(family + "." + removeQuotes(name.getText()) + "." + DISPLAY_NAME, Suggestion.Type.Component));
}
Also used : Suggestion(org.talend.sdk.component.intellij.completion.properties.Suggestion) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue)

Example 23 with PsiAnnotation

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

the class FunctionUtils method getUserDefinedBinding.

private static Binding getUserDefinedBinding(final Project project, PsiAnnotation annotation) throws AzureExecutionException {
    final PsiJavaCodeReferenceElement referenceElement = annotation.getNameReferenceElement();
    if (referenceElement == null) {
        return null;
    }
    final PsiAnnotation customBindingAnnotation = AnnotationUtil.findAnnotation((PsiModifierListOwner) referenceElement.resolve(), AZURE_FUNCTION_CUSTOM_BINDING_CLASS);
    if (customBindingAnnotation == null) {
        return null;
    }
    final Map<String, Object> annotationProperties = AnnotationHelper.evaluateAnnotationProperties(project, annotation, CUSTOM_BINDING_RESERVED_PROPERTIES);
    final Map<String, Object> customBindingProperties = AnnotationHelper.evaluateAnnotationProperties(project, customBindingAnnotation, null);
    final Map<String, Object> mergedMap = new HashMap<>(annotationProperties);
    customBindingProperties.forEach(mergedMap::putIfAbsent);
    final Binding extendBinding = new Binding(BindingEnum.CustomBinding) {

        public String getName() {
            return (String) mergedMap.get("name");
        }

        public String getDirection() {
            return (String) mergedMap.get("direction");
        }

        public String getType() {
            return (String) mergedMap.get("type");
        }
    };
    annotationProperties.forEach((name, value) -> {
        if (!CUSTOM_BINDING_RESERVED_PROPERTIES.contains(name)) {
            extendBinding.setAttribute(name, value);
        }
    });
    return extendBinding;
}
Also used : Binding(com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) JsonObject(com.google.gson.JsonObject) PsiAnnotation(com.intellij.psi.PsiAnnotation)

Example 24 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project qi4j-sdk by Qi4j.

the class Qi4jServiceAnnotationUtil method isValidServiceAnnotationDeclaration.

/**
 * Validates whether the variable has {@code @Service} annotation declared correctly.
 *
 * @param variable variable to check.
 * @return Look at {@link ServiceAnnotationDeclarationValidationResult}.
 * @since 0.1
 */
@NotNull
public static ServiceAnnotationDeclarationValidationResult isValidServiceAnnotationDeclaration(@NotNull PsiVariable variable) {
    PsiAnnotation serviceAnnotation = getServiceAnnotation(variable);
    if (serviceAnnotation == null) {
        return invalidServiceAnnotationNotDeclared;
    }
    PsiModifierList modifierList = variable.getModifierList();
    if (modifierList != null) {
        if (modifierList.hasModifierProperty(STATIC)) {
            return invalidDeclaredOnStaticVariable;
        }
    }
    // Can't be type that is injected by @Structure
    if (isInjecteableByStructureAnnotation(variable)) {
        return invalidTypeIsInjectedViaStructureAnnotation;
    }
    return valid;
}
Also used : PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiModifierList(com.intellij.psi.PsiModifierList) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project qi4j-sdk by Qi4j.

the class SideEffectsAnnotationDeclaredCorrectlyInspection method checkClass.

@Override
public final ProblemDescriptor[] checkClass(@NotNull PsiClass psiClass, @NotNull InspectionManager manager, boolean isOnTheFly) {
    // If class does not have @SideEffects, ignore
    PsiAnnotation sideEffectsAnnotation = getSideEffectsAnnotation(psiClass);
    if (sideEffectsAnnotation == null) {
        return null;
    }
    // If @SideEffects declared in class, suggest remove @SideEffects annotation
    if (!psiClass.isInterface()) {
        String message = message("side.effects.annotation.declared.correctly.error.annotation.declared.in.class");
        RemoveSideEffectsAnnotationFix fix = new RemoveSideEffectsAnnotationFix(sideEffectsAnnotation);
        ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(sideEffectsAnnotation, message, fix, GENERIC_ERROR_OR_WARNING);
        return new ProblemDescriptor[] { problemDescriptor };
    }
    // If @SideEffects annotation is empty, ignore
    List<PsiAnnotationMemberValue> sideEffectsAnnotationValue = getSideEffectsAnnotationValue(sideEffectsAnnotation);
    if (sideEffectsAnnotationValue.isEmpty()) {
        return null;
    }
    // If SideEffectOf is not resolved, ignore
    Project project = psiClass.getProject();
    GlobalSearchScope searchScope = determineSearchScope(psiClass);
    PsiClass sideEffectOfClass = Qi4jSideEffectUtil.getGenericSideEffectClass(project, searchScope);
    if (sideEffectOfClass == null) {
        return null;
    }
    List<ProblemDescriptor> problems = new LinkedList<ProblemDescriptor>();
    for (PsiAnnotationMemberValue sideEffectClassReferenceWrapper : sideEffectsAnnotationValue) {
        PsiJavaCodeReferenceElement sideEffectClassReference = getSideEffectClassReference(sideEffectClassReferenceWrapper);
        // If it's not a class reference, ignore
        if (sideEffectClassReference == null) {
            continue;
        }
        // If class reference can't be resolved, ignore
        PsiClass sideEffectClass = (PsiClass) sideEffectClassReference.resolve();
        if (sideEffectClass == null) {
            continue;
        }
        // If side effect class does not inherit SideEffectOf class, suggest remove that reference.
        if (!sideEffectClass.isInheritor(sideEffectOfClass, true)) {
            String message = Qi4jResourceBundle.message("side.effects.annotation.declared.correctly.error.side.effect.does.not.extend.side.effect.of", sideEffectClass.getQualifiedName());
            RemoveAnnotationValueFix fix = new RemoveAnnotationValueFix(sideEffectClassReferenceWrapper, sideEffectClassReference);
            ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(sideEffectClassReferenceWrapper, message, fix, GENERIC_ERROR_OR_WARNING);
            problems.add(problemDescriptor);
        } else {
        // TODO: Test whether it is a generic side effect
        // TODO: Test whether it is a specific side effect
        }
    }
    return problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiClass(com.intellij.psi.PsiClass) LinkedList(java.util.LinkedList) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue)

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