MethodSpec.methodBuilder("create") .addModifiers(Modifier.PUBLIC) .addParameter(String.class, "id") .returns(TypeName.get(superClassName.asType())); // For Each Found @Factory method.beginControlFlow("if ($S.equals(id))", item.getId()) .addStatement("return new $L()”, item.getTypeElement().getQualifiedName().toString()) .endControlFlow(); TypeSpec typeSpec = TypeSpec.classBuilder(factoryClassName) .addMethod(method.build()).build(); JavaFile.builder(packageName, typeSpec).build().writeTo(filer); } https://github.com/square/javapoet JavaPoet