protected void addBaseViewFieldsAndMethodImpls(
      SourceGenerationContext context, JavaClassSource viewClass) {
    StringBuffer inputNames = new StringBuffer();
    StringBuffer readOnlyMethodSrc = new StringBuffer();

    for (FieldDefinition fieldDefinition : context.getFormDefinition().getFields()) {

      if ((fieldDefinition.isAnnotatedId() && !displaysId()) || isBanned(fieldDefinition)) continue;

      InputCreatorHelper helper = creatorHelpers.get(fieldDefinition.getCode());
      if (helper == null) continue;

      PropertySource<JavaClassSource> property =
          viewClass.addProperty(getWidgetFromHelper(helper), fieldDefinition.getName());

      FieldSource<JavaClassSource> field = property.getField();
      field.setPrivate();

      initializeProperty(helper, context, fieldDefinition, field);

      if (helper instanceof RequiresCustomCode)
        ((RequiresCustomCode) helper).addCustomCode(fieldDefinition, context, viewClass);

      if (!(fieldDefinition instanceof SubFormFieldDefinition)) {
        field
            .addAnnotation(ERRAI_BOUND)
            .setStringValue("property", fieldDefinition.getBindingExpression());
      }

      field.addAnnotation(ERRAI_DATAFIELD);

      property.removeAccessor();
      property.removeMutator();

      inputNames.append("inputNames.add(\"").append(fieldDefinition.getName()).append("\");");
      readOnlyMethodSrc.append(helper.getReadonlyMethod(fieldDefinition.getName(), READONLY_PARAM));
    }

    viewClass
        .addMethod()
        .setName("initInputNames")
        .setBody(inputNames.toString())
        .setPublic()
        .setReturnTypeVoid()
        .addAnnotation(JAVA_LANG_OVERRIDE);

    if (isEditable()) {
      MethodSource<JavaClassSource> readonlyMethod =
          viewClass
              .addMethod()
              .setName("setReadOnly")
              .setBody(readOnlyMethodSrc.toString())
              .setPublic()
              .setReturnTypeVoid();
      readonlyMethod.addParameter(boolean.class, SourceGenerationUtil.READONLY_PARAM);
      readonlyMethod.addAnnotation(JAVA_LANG_OVERRIDE);
    }
  }
  public static void createJavaProperty(
      JavaClassSource javaClass, EntityMemberVariableVO memberVO) {
    PropertySource<JavaClassSource> propSource =
        javaClass.addProperty(memberVO.getType(), memberVO.getName());
    FieldSource<JavaClassSource> fieldSource = propSource.getField();

    List<AnnotationVO> annotationList = memberVO.getAnnotationList();
    if (Utils.notEmptyList(annotationList)) {
      AnnotationGenerator.createAnnotationForField(fieldSource, annotationList);
    }
  }
Пример #3
0
  @Override
  public Result execute(UIExecutionContext context) throws Exception {
    JavaResource javaResource = targetClass.getValue();
    JavaClassSource targetClass = javaResource.getJavaType();
    GetSetMethodGenerator generator;
    if (builderPattern.getValue()) {
      generator = new BuilderGetSetMethodGenerator();
    } else {
      generator = new DefaultGetSetMethodGenerator();
    }
    List<PropertySource<JavaClassSource>> selectedProperties = new ArrayList<>();
    if (properties == null || properties.getValue() == null) {
      return Results.fail("No properties were selected");
    }
    for (String selectedProperty : properties.getValue()) {
      selectedProperties.add(targetClass.getProperty(selectedProperty));
    }

    for (PropertySource<JavaClassSource> property : selectedProperties) {
      MethodSource<JavaClassSource> accessor =
          targetClass.getMethod("get" + Strings.capitalize(property.getName()));
      if (accessor == null) {
        generator.createAccessor(property);
      } else {
        if (!generator.isCorrectAccessor(accessor, property)) {
          if (promptToFixMethod(context, accessor.getName(), property.getName())) {
            targetClass.removeMethod(accessor);
            generator.createMutator(property);
          }
        }
      }
      String mutatorMethodName = "set" + Strings.capitalize(property.getName());
      String mutatorMethodParameter = property.getType().getName();
      MethodSource<JavaClassSource> mutator =
          targetClass.getMethod(mutatorMethodName, mutatorMethodParameter);
      if (mutator == null) {
        generator.createMutator(property);
      } else {
        if (!generator.isCorrectMutator(mutator, property)) {
          if (promptToFixMethod(context, mutator.getName(), property.getName())) {
            targetClass.removeMethod(mutator);
            generator.createMutator(property);
          }
        }
      }
    }
    setCurrentWorkingResource(context, targetClass);
    return Results.success("Mutators and accessors were generated successfully");
  }