コード例 #1
0
  private void createSetter(Property property, Field field) {
    ClassMethod setterMethod = MembersFactory.eINSTANCE.createClassMethod();
    setterMethod.setName("set" + PropjavaStringUtil.capitalize(property.getName()));
    setterMethod.setTypeReference(TypesFactory.eINSTANCE.createVoid());

    OrdinaryParameter parameter = ParametersFactory.eINSTANCE.createOrdinaryParameter();
    parameter.setName("newValue");
    TypeReference typeReference = field.getTypeReference();
    TypeReference tReferenceCopy = (TypeReference) EcoreUtil.copy(typeReference);
    parameter.setTypeReference(tReferenceCopy);
    setterMethod.getParameters().add(parameter);

    AssignmentExpression assignment = ExpressionsFactory.eINSTANCE.createAssignmentExpression();
    assignment.setAssignmentOperator(OperatorsFactory.eINSTANCE.createAssignment());

    IdentifierReference parameterRef = ReferencesFactory.eINSTANCE.createIdentifierReference();
    parameterRef.setTarget(parameter);
    assignment.setValue(parameterRef);
    IdentifierReference fieldRef = ReferencesFactory.eINSTANCE.createIdentifierReference();
    fieldRef.setTarget(field);
    assignment.setChild(fieldRef);

    ExpressionStatement assignmentStatement =
        StatementsFactory.eINSTANCE.createExpressionStatement();
    assignmentStatement.setExpression(assignment);
    setterMethod.getStatements().add(assignmentStatement);
    ((MemberContainer) property.eContainer()).getMembers().add(setterMethod);
  }
コード例 #2
0
  private void createGetter(Property property, Field field) {
    // create getter
    ClassMethod getterMethod = MembersFactory.eINSTANCE.createClassMethod();
    getterMethod.setName("get" + PropjavaStringUtil.capitalize(property.getName()));

    Return returnStatement = StatementsFactory.eINSTANCE.createReturn();
    IdentifierReference fieldRef = ReferencesFactory.eINSTANCE.createIdentifierReference();
    fieldRef.setTarget(field);
    returnStatement.setReturnValue(fieldRef);

    getterMethod.getStatements().add(returnStatement);

    TypeReference typeReference = field.getTypeReference();
    TypeReference tReferenceCopy = (TypeReference) EcoreUtil.copy(typeReference);
    getterMethod.setTypeReference(tReferenceCopy);

    ((MemberContainer) property.eContainer()).getMembers().add(getterMethod);
  }