list of dots Digital Research Alliance of Canada logo  NSERC logo  University of Ottawa logo / UniversitĂ© d'Ottawa

User Manual    [Previous]   [Next]   

JavaScript

The JavaScript generator generates basic JavaScript classes from Umple class models, and lets you embed JavaScript methods in Umple. It is chosen with generate JavaScript; in a model, -g JavaScript on the command line, or JavaScript Code in UmpleOnline. One module is generated for each class. JavaScript is generated by the Umple compiler itself, so nothing else needs to be installed.

The generated code uses CommonJS modules: each class is exported with module.exports, a subclass obtains its superclass with require, and so does a class whose methods use another generated class. The generated files run under Node.js without any other library.

 

Supported features:

  • Classes, including abstract classes. An abstract class throws a TypeError if a new expression names it, as only a subclass is meant to be created.
  • Attributes of type String, Integer, Double and Boolean, including lazy, defaulted and immutable attributes, with the generated constructor, getters and setters. A Boolean attribute also gets its is method.
  • Inheritance with isA, over any number of levels, within one namespace.
  • Associations whose ends are optional (0..1 or *), including directed associations, compositions and reflexive associations, with the generated get, set, add, remove, index and delete methods.
  • Methods written in the model with a JavaScript body, and abstract methods.
  • Comments on classes, attributes, associations and methods, which become JSDoc comments.
 

Embedding JavaScript methods: To insert a JavaScript method in Umple, write the method signature followed by the keyword JavaScript, an open curly bracket, any number of lines of JavaScript, and a closing curly bracket, as in String greet() JavaScript { return "Hello, " + this.getName(); }. The body is used as written, indented to fit the generated method. A method can have a Java body and a JavaScript body side by side; each generator uses its own. A body with no language tag is used by every generator, so it must be valid JavaScript when JavaScript is generated. A method with bodies only for other languages is left out of the generated class with warning W9222.

Inside a body, use this to reach the generated getters, setters and association methods of the object, as in this.getName() or this.addLine(aLine). The return type of the method is only documentation in JavaScript, and static generates a static method. Another generated class of the same folder can be used by name, as in new Line(aName, aQuantity): the generated module requires it after exporting its own class, so two classes that use each other both load. An abstract method is generated with a body that throws a TypeError, so a call that is not answered by a subclass is reported rather than silently doing nothing.

Umple finds the closing curly bracket with Java's comment rules, so keep curly brackets inside // and /* */ comments balanced, and avoid them in regular expression and template literals.

Method modifiers: static generates a static method. public, protected and private all generate an ordinary method, because JavaScript enforces no access control on a method declared this way, as Python does not either. synchronized also generates an ordinary method: a synchronous JavaScript body runs to its end before anything else does, so there is nothing for it to lock. A queued method needs a thread of its own and is reported with error E9220.

 

The generated JavaScript:

  • Each class is a module ClassName.js, in the folder given by its namespace, exporting the class.
  • Attribute values and links are held in properties named after the attribute or role with a leading underscore, such as this._name. They are not part of the API: use the generated methods.
  • Attribute setters and the methods that set, add or remove links return true, or false when the change is refused.
  • An association end that holds one object starts as null, as it reads after being unset, rather than undefined. An end that holds many objects starts as an empty array, and its getter returns a copy of that array.
  • The delete method unlinks the object from every association and, for a composition, deletes the parts, as in the generated Java.
  • The initial value of an attribute is copied to the generated code as it is written, so it must be a literal, such as 3, -1.5, true, "text" or null for a String. Expressions are not translated from Java, so even a constant expression such as 2*3 is unsupported: write its value instead. Numbers must also be valid in Java: an Integer value must fit in an int, and a number cannot have a leading zero (as in 010) or a suffix (as in 10L).
 

Command line use: java -jar umple.jar -g JavaScript model.ump writes the modules beside the model. Run them with Node.js, for example node -e "const Team = require('./Team.js'); console.log(new Team('Reds').getName());".

JavaScript in UmpleOnline: Select JavaScript Code from the Generate menu. The generated modules are shown one tab per class and can be downloaded as a zip file.



Currently unsupported features, reported with error E9220 (the class concerned is then not generated, while the other classes still are):

  • State machines.
  • Interfaces, singletons, keys, constraints, constants, enumerations and tracing.
  • Code injection (before, after and around).
  • Constructors and queued methods written in the model, and two methods of one name (JavaScript has no overloading).
  • Associations with a mandatory end (such as 1 or 1..*) or a bounded multiplicity (such as 0..3), sorted associations, specialized associations, and immutable associations with a many end.
  • Two methods a class would generate under one name, since JavaScript has no overloading: an association role name whose singular is the role name itself, such as series, gives the method taking an index and the method returning the list one name; so does an attribute named like the singular of a many role, as item is of items. A method written in the model that repeats a generated name, such as a second delete(), is reported for the same reason.
  • Inner classes, and the classes that contain them.
  • A superclass in another namespace, an external superclass (an external class is never generated, so the module could not be loaded), and a subclass of a class that is not generated.
  • Attributes of other types, such as Date, Time or lists, fixml attributes, unique and autounique attributes, and derived attributes.
  • Initial values that are not literals, such as 7/2, start + 1 or a method call.
  • Code in a class that Umple could not interpret (extra code).
  • Distributed classes.
  • The generated toString method.
A class or parameter whose name JavaScript reserves, or that is one of Node's own module variables, is reported with error E9221.

If any of the features that are supported appear to function incorrectly, please submit a new issue here.

Attributes, an association and inheritance

// Attributes, an association and inheritance generated as JavaScript.
// Each class becomes a CommonJS module: Team.js, Player.js and Goalie.js
generate JavaScript;

class Team {
  name;
  0..1 -- * Player;
}

class Player {
  name;
  Integer number;
}

class Goalie {
  isA Player;
  Integer saves = 0;
}

      

Load the above code into classic UmpleOnline
Load the above code into UmpleOnline V2

 

Methods written in JavaScript

// Methods written in JavaScript. A method can call the generated
// getters and the generated association methods, and it can use
// another generated class
generate JavaScript;

class Order {
  reference;
  0..1 -- * Line;

  String label() JavaScript {
    return "order " + this.getReference();
  }

  Boolean addItem(String aName, Integer aQuantity) JavaScript {
    return this.addLine(new Line(aName, aQuantity));
  }

  Integer total() JavaScript {
    let sum = 0;
    for (const line of this.getLines()) {
      sum += line.getQuantity();
    }
    return sum;
  }

  static String currency() JavaScript {
    return "CAD";
  }
}

class Line {
  name;
  Integer quantity;
}

      

Load the above code into classic UmpleOnline
Load the above code into UmpleOnline V2