@Test public void annotate_added() { testHelper.getPsiClass( classes -> { PsiClass cls = classes.get(0); ComponentGenerateService.getInstance().updateLayoutComponentAsync(cls); PsiMethodCallExpression call = PsiTreeUtil.findChildOfType(cls, PsiMethodCallExpression.class); assertThat(call.getText()).isEqualTo("Component.create(c).testEventHandler()"); EventHandlerAnnotator eventHandlerAnnotator = new EventHandlerAnnotator(); TestHolder holder1 = new TestHolder(); eventHandlerAnnotator.annotate(call, holder1); assertThat(holder1.errorElements).hasSize(1); assertThat(holder1.errorElements.get(0).getText()).isEqualTo("()"); assertThat(holder1.errorMessages).hasSize(1); assertThat(holder1.errorMessages.get(0)).isEqualTo("Add TestEventHandler"); assertThat(holder1.createdAnnotations).hasSize(1); List<Annotation.QuickFixInfo> fixes = holder1.createdAnnotations.get(0).getQuickFixes(); assertThat(fixes).hasSize(3); Project project = cls.getProject(); PsiFile file = cls.getContainingFile(); Editor editor = mock(Editor.class); when(editor.getCaretModel()).thenReturn(mock(CaretModel.class)); Annotation.QuickFixInfo fix1 = fixes.get(0); assertThat(fix1.toString()).isEqualTo("Add .handler1() TestEventHandler"); fix1.quickFix.invoke(project, editor, file); assertThat(call.getArgumentList().getExpressions()[0].getText()) .isEqualTo("EventHandlerAnnotator.handler1()"); Annotation.QuickFixInfo fix2 = fixes.get(1); assertThat(fix2.toString()).isEqualTo("Add .handlerTwo() TestEventHandler"); fix2.quickFix.invoke(project, editor, file); assertThat(call.getArgumentList().getExpressions()[0].getText()) .isEqualTo("EventHandlerAnnotator.handlerTwo()"); Annotation.QuickFixInfo fix3 = fixes.get(2); assertThat(fix3.toString()).isEqualTo("Add .thirdHandler() TestEventHandler"); fix3.quickFix.invoke(project, editor, file); assertThat(call.getArgumentList().getExpressions()[0].getText()) .isEqualTo("EventHandlerAnnotator.thirdHandler()"); TestHolder holder2 = new TestHolder(); eventHandlerAnnotator.annotate(call, holder2); assertThat(holder2.createdAnnotations).hasSize(0); return true; }, "EventHandlerAnnotatorSpec.java"); }
@Override public void annotate(PsiElement element, AnnotationHolder holder) { DEBUG_LOGGER.logStep("start " + element); if (!(element instanceof PsiMethodCallExpression)) { return; } final PsiMethodCallExpression eventHandlerSetter = (PsiMethodCallExpression) element; final PsiExpressionList list = eventHandlerSetter.getArgumentList(); if (!list.isEmpty()) { return; } final String eventQualifiedName = resolveEventName(eventHandlerSetter); if (eventQualifiedName == null) { return; } final PsiClass parentCls = (PsiClass) PsiTreeUtil.findFirstParent(eventHandlerSetter, PsiClass.class::isInstance); if (parentCls == null) { return; } final SpecModel parentModel = ComponentGenerateService.getInstance().getSpecModel(parentCls); if (parentModel == null) { return; } final ImmutableList<SpecMethodModel<EventMethod, EventDeclarationModel>> implementedEventHandlers = parentModel.getEventMethods(); final String componentQualifiedName = parentModel.getComponentTypeName().toString(); final Project project = eventHandlerSetter.getProject(); final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); final String message = "Add " + AddArgumentFix.getCapitalizedMethoName(eventHandlerSetter); final SpecModelValidationError error = new SpecModelValidationError(list, message); final List<IntentionAction> fixes = implementedEventHandlers.stream() .filter(handler -> eventQualifiedName.equals(handler.typeModel.name.reflectionName())) .map(handler -> handler.name.toString()) .distinct() .map( methodName -> AddArgumentFix.createAddMethodCallFix( eventHandlerSetter, componentQualifiedName, methodName, elementFactory)) .collect(Collectors.toList()); final PsiClass event = PsiSearchUtils.findClass(project, eventQualifiedName); if (event != null) { fixes.add( AddArgumentFix.createNewMethodCallFix( eventHandlerSetter, componentQualifiedName, event, parentCls)); } AnnotatorUtils.addError(holder, error, fixes); DEBUG_LOGGER.logStep("end " + element); }
EventHandlerAnnotator implements Annotator { @Override public void annotate(PsiElement element, AnnotationHolder holder) { DEBUG_LOGGER.logStep("start " + element); if (!(element instanceof PsiMethodCallExpression)) { return; } final PsiMethodCallExpression eventHandlerSetter = (PsiMethodCallExpression) element; final PsiExpressionList list = eventHandlerSetter.getArgumentList(); if (!list.isEmpty()) { return; } final String eventQualifiedName = resolveEventName(eventHandlerSetter); if (eventQualifiedName == null) { return; } final PsiClass parentCls = (PsiClass) PsiTreeUtil.findFirstParent(eventHandlerSetter, PsiClass.class::isInstance); if (parentCls == null) { return; } final SpecModel parentModel = ComponentGenerateService.getInstance().getSpecModel(parentCls); if (parentModel == null) { return; } final ImmutableList<SpecMethodModel<EventMethod, EventDeclarationModel>> implementedEventHandlers = parentModel.getEventMethods(); final String componentQualifiedName = parentModel.getComponentTypeName().toString(); final Project project = eventHandlerSetter.getProject(); final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); final String message = "Add " + AddArgumentFix.getCapitalizedMethoName(eventHandlerSetter); final SpecModelValidationError error = new SpecModelValidationError(list, message); final List<IntentionAction> fixes = implementedEventHandlers.stream() .filter(handler -> eventQualifiedName.equals(handler.typeModel.name.reflectionName())) .map(handler -> handler.name.toString()) .distinct() .map( methodName -> AddArgumentFix.createAddMethodCallFix( eventHandlerSetter, componentQualifiedName, methodName, elementFactory)) .collect(Collectors.toList()); final PsiClass event = PsiSearchUtils.findClass(project, eventQualifiedName); if (event != null) { fixes.add( AddArgumentFix.createNewMethodCallFix( eventHandlerSetter, componentQualifiedName, event, parentCls)); } AnnotatorUtils.addError(holder, error, fixes); DEBUG_LOGGER.logStep("end " + element); } }
EventHandlerAnnotator implements Annotator { @Override public void annotate(PsiElement element, AnnotationHolder holder) { DEBUG_LOGGER.logStep("start " + element); if (!(element instanceof PsiMethodCallExpression)) { return; } final PsiMethodCallExpression eventHandlerSetter = (PsiMethodCallExpression) element; final PsiExpressionList list = eventHandlerSetter.getArgumentList(); if (!list.isEmpty()) { return; } final String eventQualifiedName = resolveEventName(eventHandlerSetter); if (eventQualifiedName == null) { return; } final PsiClass parentCls = (PsiClass) PsiTreeUtil.findFirstParent(eventHandlerSetter, PsiClass.class::isInstance); if (parentCls == null) { return; } final SpecModel parentModel = ComponentGenerateService.getInstance().getSpecModel(parentCls); if (parentModel == null) { return; } final ImmutableList<SpecMethodModel<EventMethod, EventDeclarationModel>> implementedEventHandlers = parentModel.getEventMethods(); final String componentQualifiedName = parentModel.getComponentTypeName().toString(); final Project project = eventHandlerSetter.getProject(); final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); final String message = "Add " + AddArgumentFix.getCapitalizedMethoName(eventHandlerSetter); final SpecModelValidationError error = new SpecModelValidationError(list, message); final List<IntentionAction> fixes = implementedEventHandlers.stream() .filter(handler -> eventQualifiedName.equals(handler.typeModel.name.reflectionName())) .map(handler -> handler.name.toString()) .distinct() .map( methodName -> AddArgumentFix.createAddMethodCallFix( eventHandlerSetter, componentQualifiedName, methodName, elementFactory)) .collect(Collectors.toList()); final PsiClass event = PsiSearchUtils.findClass(project, eventQualifiedName); if (event != null) { fixes.add( AddArgumentFix.createNewMethodCallFix( eventHandlerSetter, componentQualifiedName, event, parentCls)); } AnnotatorUtils.addError(holder, error, fixes); DEBUG_LOGGER.logStep("end " + element); }  }
EventHandlerAnnotator implements Annotator { @Override public void annotate(PsiElement element, AnnotationHolder holder) { DEBUG_LOGGER.logStep("start " + element); if (!(element instanceof PsiMethodCallExpression)) { return; } final PsiMethodCallExpression eventHandlerSetter = (PsiMethodCallExpression) element; final PsiExpressionList list = eventHandlerSetter.getArgumentList(); if (!list.isEmpty()) { return; } final String eventQualifiedName = resolveEventName(eventHandlerSetter); if (eventQualifiedName == null) { return; } final PsiClass parentCls = (PsiClass) PsiTreeUtil.findFirstParent(eventHandlerSetter, PsiClass.class::isInstance); if (parentCls == null) { return; } final SpecModel parentModel = ComponentGenerateService.getInstance().getSpecModel(parentCls); if (parentModel == null) { return; } final ImmutableList<SpecMethodModel<EventMethod, EventDeclarationModel>> implementedEventHandlers = parentModel.getEventMethods(); final String componentQualifiedName = parentModel.getComponentTypeName().toString(); final Project project = eventHandlerSetter.getProject(); final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); final String message = "Add " + AddArgumentFix.getCapitalizedMethoName(eventHandlerSetter); final SpecModelValidationError error = new SpecModelValidationError(list, message); final List<IntentionAction> fixes = implementedEventHandlers.stream() .filter(handler -> eventQualifiedName.equals(handler.typeModel.name.reflectionName())) .map(handler -> handler.name.toString()) .distinct() .map( methodName -> AddArgumentFix.createAddMethodCallFix( eventHandlerSetter, componentQualifiedName, methodName, elementFactory)) .collect(Collectors.toList()); final PsiClass event = PsiSearchUtils.findClass(project, eventQualifiedName); if (event != null) { fixes.add( AddArgumentFix.createNewMethodCallFix( eventHandlerSetter, componentQualifiedName, event, parentCls)); } AnnotatorUtils.addError(holder, error, fixes); DEBUG_LOGGER.logStep("end " + element); }  @Override void annotate(PsiElement element, AnnotationHolder holder); }
EventHandlerAnnotator implements Annotator { @Override public void annotate(PsiElement element, AnnotationHolder holder) { DEBUG_LOGGER.logStep("start " + element); if (!(element instanceof PsiMethodCallExpression)) { return; } final PsiMethodCallExpression eventHandlerSetter = (PsiMethodCallExpression) element; final PsiExpressionList list = eventHandlerSetter.getArgumentList(); if (!list.isEmpty()) { return; } final String eventQualifiedName = resolveEventName(eventHandlerSetter); if (eventQualifiedName == null) { return; } final PsiClass parentCls = (PsiClass) PsiTreeUtil.findFirstParent(eventHandlerSetter, PsiClass.class::isInstance); if (parentCls == null) { return; } final SpecModel parentModel = ComponentGenerateService.getInstance().getSpecModel(parentCls); if (parentModel == null) { return; } final ImmutableList<SpecMethodModel<EventMethod, EventDeclarationModel>> implementedEventHandlers = parentModel.getEventMethods(); final String componentQualifiedName = parentModel.getComponentTypeName().toString(); final Project project = eventHandlerSetter.getProject(); final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); final String message = "Add " + AddArgumentFix.getCapitalizedMethoName(eventHandlerSetter); final SpecModelValidationError error = new SpecModelValidationError(list, message); final List<IntentionAction> fixes = implementedEventHandlers.stream() .filter(handler -> eventQualifiedName.equals(handler.typeModel.name.reflectionName())) .map(handler -> handler.name.toString()) .distinct() .map( methodName -> AddArgumentFix.createAddMethodCallFix( eventHandlerSetter, componentQualifiedName, methodName, elementFactory)) .collect(Collectors.toList()); final PsiClass event = PsiSearchUtils.findClass(project, eventQualifiedName); if (event != null) { fixes.add( AddArgumentFix.createNewMethodCallFix( eventHandlerSetter, componentQualifiedName, event, parentCls)); } AnnotatorUtils.addError(holder, error, fixes); DEBUG_LOGGER.logStep("end " + element); }  @Override void annotate(PsiElement element, AnnotationHolder holder);  }