NavigatorCodeGenerator { MethodSpec handleParamActivity(String group, NavigableAnnotatedClass value) { TypeName activityTypeName = AndroidSpecificClassProvider.getActivityTypeName(); TypeName intentTypeName = AndroidSpecificClassProvider.getIntentTypeName(); String activityQualifiedName = value.getTypeElement().getQualifiedName().toString(); ArrayTypeName integerArray = ArrayTypeName.of(ClassName.get(Integer.class)); int forResultCounter = 0; for (VariableElement param : value.getParamAnnotatedFields().get(group)) { if (param.getAnnotation(Param.class).forResult()) { forResultCounter++; } } String methodName = value.getAnnotatedClassName() + CodeGeneratorHelper.firstLetterToUpperCase(group); if (forResultCounter > 0) { methodName += "ForResult"; } String annotatedClassName = CodeGeneratorHelper.buildMethodName(methodName, value.getNavigableAnnotation()); MethodSpec.Builder builder = MethodSpec.methodBuilder(annotatedClassName); builder.addParameter(activityTypeName, "activity", Modifier.FINAL); TypeName paramTypeName; System.out.println(value.getParamAnnotatedFields() + " group: " + group); for (VariableElement param : value.getParamAnnotatedFields().get(group)) { paramTypeName = getTypeName(param); builder.addParameter(paramTypeName, param.getSimpleName().toString(), Modifier.FINAL); } if (forResultCounter > 0 && (forResultCounter != value.getParamAnnotatedFields().get(group).size())) { throw new NotAllFieldsAreAnnotatedForResultException("When at least one param is marked as \"forResult\" each element of this group should be marked as \"forResult\""); } if (forResultCounter > 0) { builder.addParameter(TypeName.INT, "requestCode", Modifier.FINAL); } builder.addParameter(integerArray, "flags", Modifier.FINAL) .varargs() .addModifiers(Modifier.STATIC, Modifier.PUBLIC) .addStatement("$T intent = new $T(activity, " + activityQualifiedName + ".class)", intentTypeName, intentTypeName) .beginControlFlow("for (Integer flag : flags)") .addStatement("intent.addFlags(flag)") .endControlFlow(); for (VariableElement param : value.getParamAnnotatedFields().get(group)) { builder.addStatement("intent.putExtra(\"" + param.getSimpleName().toString() + "\"," + param.getSimpleName().toString() + ")"); } if (forResultCounter > 0) { builder.addStatement("activity.startActivityForResult(intent, requestCode)"); } else { builder.addStatement("activity.startActivity(intent)"); } return builder.build(); } NavigatorCodeGenerator(ProcessingEnvironment processingEnv); void generateClass(List<NavigableAnnotatedClass> navigableAnnotatedClassList);  }
@Test public void testHandleParamActivity() { String className = TestConstants.testPackageName + ".AA"; NavigableAnnotatedClass navigableAnnotatedClass = Mockito.mock(NavigableAnnotatedClass.class); TypeElement typeElement = Mockito.mock(TypeElement.class); Name name = Mockito.mock(Name.class); when(typeElement.getQualifiedName()).thenReturn(name); when(name.toString()).thenReturn(className); when(navigableAnnotatedClass.getTypeElement()).thenReturn(typeElement); when(navigableAnnotatedClass.getTypeElement().getQualifiedName()).thenReturn(name); when(navigableAnnotatedClass.getAnnotatedClassName()).thenReturn(className); MethodSpec methodSpec = navigatorCodeGenerator.handleParamActivity("bbb", navigableAnnotatedClass); TestUtils.testMethodSpec(methodSpec, className, TypeName.VOID, Modifier.PUBLIC, Modifier.STATIC); assertEquals(2, methodSpec.parameters.size()); TestUtils.testMethodParams(methodSpec.parameters.get(0), "activity", "android.app.Activity", Modifier.FINAL); TestUtils.testMethodParams(methodSpec.parameters.get(1), "flags", "java.lang.Integer[]", Modifier.FINAL); assertEquals(0, methodSpec.annotations.size()); }