@Test public void createOnEventMethod_whenEventClassWithReturnTypeAndParameters_createMethodWithReturnTypeAndAdditionalParameters() { ApplicationManager.getApplication() .invokeAndWait( () -> { final PsiClass testClassWithoutOnEventMethod = LithoPluginUtils.getFirstClass( psiFile, cls -> "TestClassWithoutOnEventMethod".equals(cls.getName())) .get(); final PsiClass testEventWithReturnTypeAndParam = LithoPluginUtils.getFirstClass( psiFile, cls -> "TestEventWithReturnTypeAndParam".equals(cls.getName())) .get(); final PsiMethod onEventMethod = OnEventGenerateUtils.createOnEventMethod( testClassWithoutOnEventMethod, testEventWithReturnTypeAndParam, Collections.emptyList()); final PsiParameter[] parameterList = onEventMethod.getParameterList().getParameters(); assertThat(parameterList).hasSize(2); assertThat(parameterList[0].getType().getCanonicalText()) .isEqualTo(LithoClassNames.COMPONENT_CONTEXT_CLASS_NAME); assertThat(parameterList[0].getName()).isEqualTo("c"); assertThat(parameterList[1].getType().getCanonicalText()).isEqualTo("int"); assertThat(parameterList[1].getName()).isEqualTo("number"); assertThat(onEventMethod.getReturnType().getCanonicalText()).isEqualTo("boolean"); assertThat(onEventMethod.getName()).isEqualTo("onTestEventWithReturnTypeAndParam"); }); }
public static PsiMethod createOnEventMethod( PsiClass context, PsiClass eventClass, Collection<PsiParameter> additionalParameters) { final Project project = context.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiType methodReturnType = PsiEventDeclarationsExtractor.getReturnPsiType(eventClass); String methodName = "on" + eventClass.getName(); int postfix = 1; while (context.findMethodsByName(methodName).length > 0) { methodName = "on" + eventClass.getName() + postfix; postfix++; } final PsiMethod method = factory.createMethod(methodName, methodReturnType, context); final PsiParameterList parameterList = method.getParameterList(); final PsiParameter contextParameter = factory.createParameter( CONTEXT_PARAMETER_NAME, PsiType.getTypeByName( getContextClassName(context), project, GlobalSearchScope.allScope(project)), context); parameterList.add(contextParameter); final PsiField[] eventFields = eventClass.getFields(); for (PsiField field : eventFields) { final String fieldName = field.getName(); if (fieldName == null) { continue; } final PsiParameter parameter = factory.createParameter(fieldName, field.getType()); final PsiModifierList parameterModifierList = parameter.getModifierList(); if (parameterModifierList == null) { continue; } parameterModifierList.addAnnotation(FromEvent.class.getName()); parameterList.add(parameter); } for (PsiParameter parameter : additionalParameters) { parameterList.add(parameter); } final PsiModifierList methodModifierList = method.getModifierList(); methodModifierList.addAnnotation( OnEvent.class.getName() + "(" + eventClass.getQualifiedName() + ".class)"); methodModifierList.setModifierProperty(PsiModifier.PACKAGE_LOCAL, true); methodModifierList.setModifierProperty(PsiModifier.STATIC, true); return method; }
OnEventGenerateUtils { public static PsiMethod createOnEventMethod( PsiClass context, PsiClass eventClass, Collection<PsiParameter> additionalParameters) { final Project project = context.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiType methodReturnType = PsiEventDeclarationsExtractor.getReturnPsiType(eventClass); String methodName = "on" + eventClass.getName(); int postfix = 1; while (context.findMethodsByName(methodName).length > 0) { methodName = "on" + eventClass.getName() + postfix; postfix++; } final PsiMethod method = factory.createMethod(methodName, methodReturnType, context); final PsiParameterList parameterList = method.getParameterList(); final PsiParameter contextParameter = factory.createParameter( CONTEXT_PARAMETER_NAME, PsiType.getTypeByName( getContextClassName(context), project, GlobalSearchScope.allScope(project)), context); parameterList.add(contextParameter); final PsiField[] eventFields = eventClass.getFields(); for (PsiField field : eventFields) { final String fieldName = field.getName(); if (fieldName == null) { continue; } final PsiParameter parameter = factory.createParameter(fieldName, field.getType()); final PsiModifierList parameterModifierList = parameter.getModifierList(); if (parameterModifierList == null) { continue; } parameterModifierList.addAnnotation(FromEvent.class.getName()); parameterList.add(parameter); } for (PsiParameter parameter : additionalParameters) { parameterList.add(parameter); } final PsiModifierList methodModifierList = method.getModifierList(); methodModifierList.addAnnotation( OnEvent.class.getName() + "(" + eventClass.getQualifiedName() + ".class)"); methodModifierList.setModifierProperty(PsiModifier.PACKAGE_LOCAL, true); methodModifierList.setModifierProperty(PsiModifier.STATIC, true); return method; } }
OnEventGenerateUtils { public static PsiMethod createOnEventMethod( PsiClass context, PsiClass eventClass, Collection<PsiParameter> additionalParameters) { final Project project = context.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiType methodReturnType = PsiEventDeclarationsExtractor.getReturnPsiType(eventClass); String methodName = "on" + eventClass.getName(); int postfix = 1; while (context.findMethodsByName(methodName).length > 0) { methodName = "on" + eventClass.getName() + postfix; postfix++; } final PsiMethod method = factory.createMethod(methodName, methodReturnType, context); final PsiParameterList parameterList = method.getParameterList(); final PsiParameter contextParameter = factory.createParameter( CONTEXT_PARAMETER_NAME, PsiType.getTypeByName( getContextClassName(context), project, GlobalSearchScope.allScope(project)), context); parameterList.add(contextParameter); final PsiField[] eventFields = eventClass.getFields(); for (PsiField field : eventFields) { final String fieldName = field.getName(); if (fieldName == null) { continue; } final PsiParameter parameter = factory.createParameter(fieldName, field.getType()); final PsiModifierList parameterModifierList = parameter.getModifierList(); if (parameterModifierList == null) { continue; } parameterModifierList.addAnnotation(FromEvent.class.getName()); parameterList.add(parameter); } for (PsiParameter parameter : additionalParameters) { parameterList.add(parameter); } final PsiModifierList methodModifierList = method.getModifierList(); methodModifierList.addAnnotation( OnEvent.class.getName() + "(" + eventClass.getQualifiedName() + ".class)"); methodModifierList.setModifierProperty(PsiModifier.PACKAGE_LOCAL, true); methodModifierList.setModifierProperty(PsiModifier.STATIC, true); return method; } private  OnEventGenerateUtils(); }
OnEventGenerateUtils { public static PsiMethod createOnEventMethod( PsiClass context, PsiClass eventClass, Collection<PsiParameter> additionalParameters) { final Project project = context.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiType methodReturnType = PsiEventDeclarationsExtractor.getReturnPsiType(eventClass); String methodName = "on" + eventClass.getName(); int postfix = 1; while (context.findMethodsByName(methodName).length > 0) { methodName = "on" + eventClass.getName() + postfix; postfix++; } final PsiMethod method = factory.createMethod(methodName, methodReturnType, context); final PsiParameterList parameterList = method.getParameterList(); final PsiParameter contextParameter = factory.createParameter( CONTEXT_PARAMETER_NAME, PsiType.getTypeByName( getContextClassName(context), project, GlobalSearchScope.allScope(project)), context); parameterList.add(contextParameter); final PsiField[] eventFields = eventClass.getFields(); for (PsiField field : eventFields) { final String fieldName = field.getName(); if (fieldName == null) { continue; } final PsiParameter parameter = factory.createParameter(fieldName, field.getType()); final PsiModifierList parameterModifierList = parameter.getModifierList(); if (parameterModifierList == null) { continue; } parameterModifierList.addAnnotation(FromEvent.class.getName()); parameterList.add(parameter); } for (PsiParameter parameter : additionalParameters) { parameterList.add(parameter); } final PsiModifierList methodModifierList = method.getModifierList(); methodModifierList.addAnnotation( OnEvent.class.getName() + "(" + eventClass.getQualifiedName() + ".class)"); methodModifierList.setModifierProperty(PsiModifier.PACKAGE_LOCAL, true); methodModifierList.setModifierProperty(PsiModifier.STATIC, true); return method; } private  OnEventGenerateUtils(); static PsiMethod createOnEventMethod(
      PsiClass context, PsiClass eventClass, Collection<PsiParameter> additionalParameters); static void addComment(PsiClass contextClass, PsiMethod method); }
OnEventGenerateUtils { public static PsiMethod createOnEventMethod( PsiClass context, PsiClass eventClass, Collection<PsiParameter> additionalParameters) { final Project project = context.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiType methodReturnType = PsiEventDeclarationsExtractor.getReturnPsiType(eventClass); String methodName = "on" + eventClass.getName(); int postfix = 1; while (context.findMethodsByName(methodName).length > 0) { methodName = "on" + eventClass.getName() + postfix; postfix++; } final PsiMethod method = factory.createMethod(methodName, methodReturnType, context); final PsiParameterList parameterList = method.getParameterList(); final PsiParameter contextParameter = factory.createParameter( CONTEXT_PARAMETER_NAME, PsiType.getTypeByName( getContextClassName(context), project, GlobalSearchScope.allScope(project)), context); parameterList.add(contextParameter); final PsiField[] eventFields = eventClass.getFields(); for (PsiField field : eventFields) { final String fieldName = field.getName(); if (fieldName == null) { continue; } final PsiParameter parameter = factory.createParameter(fieldName, field.getType()); final PsiModifierList parameterModifierList = parameter.getModifierList(); if (parameterModifierList == null) { continue; } parameterModifierList.addAnnotation(FromEvent.class.getName()); parameterList.add(parameter); } for (PsiParameter parameter : additionalParameters) { parameterList.add(parameter); } final PsiModifierList methodModifierList = method.getModifierList(); methodModifierList.addAnnotation( OnEvent.class.getName() + "(" + eventClass.getQualifiedName() + ".class)"); methodModifierList.setModifierProperty(PsiModifier.PACKAGE_LOCAL, true); methodModifierList.setModifierProperty(PsiModifier.STATIC, true); return method; } private  OnEventGenerateUtils(); static PsiMethod createOnEventMethod(
      PsiClass context, PsiClass eventClass, Collection<PsiParameter> additionalParameters); static void addComment(PsiClass contextClass, PsiMethod method);  }