Exemplo n.º 1
0
  private ClassWriter generatePushCommandX(final Pushmethod method, final String type)
      throws Exception {
    final String className =
        method.getName().substring(0, 1).toUpperCase() + method.getName().substring(1) + "Command";
    final ClassWriter classWriter =
        new ClassWriter(
            this, getSrcGeneratedDirectory(), GeneratorHelper.getCommandPackage(domain), className);

    final Parameter pushListener = new Parameter();
    pushListener.setName("listener");
    pushListener.setClazz("com.ponysdk.core.command.PushListener<" + type + ">");

    final List<Parameter> parameters = method.getParameter();

    final List<Parameter> clonedParameters = new ArrayList<Parameter>(parameters);
    clonedParameters.add(pushListener);

    final Constructor constructor = new Constructor();
    constructor.setConstructorParameters(clonedParameters);
    constructor.setSuperConstructorParameters(Arrays.asList(pushListener));

    classWriter.addConstructor(constructor);

    for (final Parameter param : parameters) {
      classWriter.addClassMembers(param);
    }
    classWriter.addConstants(
        "private static " + GeneratorHelper.getServiceFullClassName(domain) + " service;");

    final StringBuilder template = new StringBuilder();
    template.append("@Override\n");
    template.append("public com.ponysdk.core.event.HandlerRegistration execute(){\n");
    template.append("   if (service == null) {\n");
    template.append(
        "       service = com.ponysdk.core.service.PonyServiceRegistry.getPonyService("
            + GeneratorHelper.getServiceFullClassName(domain)
            + ".class);");
    template.append("   }\n");
    if (method.getParameter().isEmpty())
      template.append("   return service.%1$sRegistration(this);\n");
    else template.append("   return service.%1$sRegistration(%2$s,this);\n");
    template.append("}\n");

    classWriter.addMethod(
        template.toString(),
        method.getName(),
        GeneratorHelper.getParameterNamesToString(method.getParameter()));

    return classWriter;
  }
Exemplo n.º 2
0
  private ClassWriter generateCommandX(final Method method, final String resultClass)
      throws Exception {
    final String className =
        method.getName().substring(0, 1).toUpperCase() + method.getName().substring(1) + "Command";

    final ClassWriter classWriter =
        new ClassWriter(
            this, getSrcGeneratedDirectory(), GeneratorHelper.getCommandPackage(domain), className);

    final Constructor constructor = new Constructor();
    final Parameter eventBusParameter = new Parameter();
    eventBusParameter.setName("eventBus");
    eventBusParameter.setClazz("com.ponysdk.core.event.EventBus");

    final List<Parameter> parameters = method.getParameter();
    final List<Parameter> clonedParameters = new ArrayList<Parameter>();
    for (final Parameter parameter : parameters) {
      final Parameter clonedParameter = new Parameter();
      clonedParameter.setClazz(parameter.getClazz());
      clonedParameter.setName(parameter.getName());
      clonedParameter.setCollection(parameter.getCollection());
      clonedParameters.add(clonedParameter);
    }

    final Constructor constructor2 = new Constructor();
    constructor2.setConstructorParameters(new ArrayList<Parameter>(clonedParameters));

    clonedParameters.add(0, eventBusParameter);
    constructor.setConstructorParameters(clonedParameters);
    constructor.setSuperConstructorParameters(Arrays.asList(eventBusParameter));

    classWriter.addConstructor(constructor);
    classWriter.addConstructor(constructor2);

    for (final Parameter param : parameters) {
      classWriter.addClassMembers(param);
    }
    classWriter.addConstants(
        "private static " + GeneratorHelper.getServiceFullClassName(domain) + " service;");

    final StringBuilder template = new StringBuilder();
    template.append("@Override\n");
    if (resultClass.equals("void") && method.getReturn().getValue() == null) {
      template.append("protected java.lang.Void  execute0() throws Exception {\n");
    } else {
      template.append("protected " + resultClass + " execute0() throws Exception {\n");
    }
    template.append("   if (service == null) {\n");
    template.append(
        "       service = com.ponysdk.core.service.PonyServiceRegistry.getPonyService("
            + GeneratorHelper.getServiceFullClassName(domain)
            + ".class);");
    template.append("   }\n");
    if (resultClass.equals("void") && method.getReturn().getValue() == null) {
      template.append("	service.%1$s(%2$s);\n");
      template.append("	return null;\n");
    } else {
      template.append("	return service.%1$s(%2$s);\n");
    }
    template.append("}\n");

    classWriter.addMethod(
        template.toString(),
        method.getName(),
        GeneratorHelper.getParameterNamesToString(method.getParameter()));

    return classWriter;
  }