Example usage for org.eclipse.jdt.core.jdom DOMFactory createMethod

List of usage examples for org.eclipse.jdt.core.jdom DOMFactory createMethod

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.jdom DOMFactory createMethod.

Prototype

@Override
    public IDOMMethod createMethod() 

Source Link

Usage

From source file:ac.at.tuwien.dsg.uml.stereotype.export.transformation.rules.DataTypeTransformationRule.java

License:Open Source License

public Object createTarget(ITransformContext context) {
    DOMFactory domFactory = new DOMFactory();

    //create output content
    IDOMType target = domFactory.createClass();

    /**/*from  ww  w.  ja  v  a 2 s  . c  o m*/
     * Get the transformation UML Class
     */
    DataType dataType = (DataType) context.getSource();
    target.setName(dataType.getName());

    //get all properties
    for (Property attribute : dataType.getAllAttributes()) {

        String name = attribute.getName();
        Type type = attribute.getType();
        String typeName = type.getName();

        org.eclipse.uml2.uml.Package packageType = type.getPackage();
        /**
         * Create Java field/class variable for each attribute 
         */
        Namespace namespace = attribute.getNamespace();

        IDOMField field = domFactory.createField();
        field.setName(name);
        field.setFlags(Flags.AccPrivate);
        /**
         * In case the model is incomplete, we add the field with Object as type
         */
        if (typeName == null) {
            field.setType("Object");
            //add in the generated code a comment explaining why field type is Object
            field.setComment(
                    "/*Type for attribute \"" + name + "\" on stereotype \"" + dataType + "\" is null */");
            System.err
                    .println("Type for attribute \"" + name + "\" on stereotype \"" + dataType + "\" is null");
        } else {
            field.setType(typeName);
        }

        target.addChild(field);

        /**
         * Add setter/getter for the added field
         */
        IDOMMethod setter = domFactory.createMethod();
        /**
         * Capitalize the first letter of the variable name so we have nice camel-case 
         */
        setter.setName("set" + name.substring(0, 1).toUpperCase() + name.substring(1));
        setter.setFlags(Flags.AccPublic);
        setter.setReturnType("void");
        setter.addParameter(typeName, name);
        setter.setBody("{ \n this." + name + "=" + name + ";\n }");

        target.addChild(setter);

        IDOMMethod getter = domFactory.createMethod();
        getter.setName("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
        getter.setFlags(Flags.AccPublic);
        getter.setReturnType(typeName);
        getter.setBody("{ \n return this." + name + ";\n }");

        target.addChild(getter);

    }

    //create Java file from UML class
    JavaClassOutputter.outputFile(context, target);

    return target;
}

From source file:ac.at.tuwien.dsg.uml.stereotype.export.transformation.rules.SignalTransformationRule.java

License:Open Source License

public Object createTarget(ITransformContext context) {
    DOMFactory domFactory = new DOMFactory();

    //create output content
    IDOMType target = domFactory.createClass();

    /**//  w w w  .  ja  v  a 2  s  .  c  o m
     * Get the transformation UML Class
     */
    Signal signal = (Signal) context.getSource();
    target.setName(signal.getName());

    //get all properties
    for (Property attribute : signal.getAllAttributes()) {

        String name = attribute.getName();
        Type type = attribute.getType();
        String typeName = type.getName();

        org.eclipse.uml2.uml.Package packageType = type.getPackage();
        /**
         * Create Java field/class variable for each attribute 
         */
        Namespace namespace = attribute.getNamespace();

        IDOMField field = domFactory.createField();
        field.setName(name);
        field.setFlags(Flags.AccPrivate);
        /**
         * In case the model is incomplete, we add the field with Object as type
         */
        if (typeName == null) {
            field.setType("Object");
            //add in the generated code a comment explaining why field type is Object
            field.setComment(
                    "/*Type for attribute \"" + name + "\" on stereotype \"" + signal + "\" is null */");
            System.err.println("Type for attribute \"" + name + "\" on stereotype \"" + signal + "\" is null");
        } else {
            field.setType(typeName);
        }

        target.addChild(field);

        /**
         * Add setter/getter for the added field
         */
        IDOMMethod setter = domFactory.createMethod();
        /**
         * Capitalize the first letter of the variable name so we have nice camel-case 
         */
        setter.setName("set" + name.substring(0, 1).toUpperCase() + name.substring(1));
        setter.setFlags(Flags.AccPublic);
        setter.setReturnType("void");
        setter.addParameter(typeName, name);
        setter.setBody("{ \n this." + name + "=" + name + ";\n }");

        target.addChild(setter);

        IDOMMethod getter = domFactory.createMethod();
        getter.setName("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
        getter.setFlags(Flags.AccPublic);
        getter.setReturnType(typeName);
        getter.setBody("{ \n return this." + name + ";\n }");

        target.addChild(getter);

    }

    //create Java file from UML class
    JavaClassOutputter.outputFile(context, target);

    return target;
}

From source file:ac.at.tuwien.dsg.uml.stereotype.export.transformation.rules.StereotypeTransformationRule.java

License:Open Source License

public Object createTarget(ITransformContext context) {
    DOMFactory domFactory = new DOMFactory();

    //create output content
    IDOMType target = domFactory.createClass();

    /**//from   www. ja v  a 2  s.co m
     * Get the transformation UML Class
     */
    Class umlCls = (Class) context.getSource();
    target.setName(umlCls.getName());

    Stereotype stereotype = (Stereotype) context.getSource();

    //get all properties
    for (Property attribute : stereotype.getAllAttributes()) {

        String name = attribute.getName();
        Type type = attribute.getType();
        String typeName = type.getName();

        org.eclipse.uml2.uml.Package packageType = type.getPackage();
        /**
         * Create Java field/class variable for each attribute 
         */
        Namespace namespace = attribute.getNamespace();

        IDOMField field = domFactory.createField();
        field.setName(name);
        field.setFlags(Flags.AccPrivate);
        /**
         * In case the model is incomplete, we add the field with Object as type
         */
        if (typeName == null) {
            field.setType("Object");
            //add in the generated code a comment explaining why field type is Object
            field.setComment(
                    "/*Type for attribute \"" + name + "\" on stereotype \"" + stereotype + "\" is null */");
            System.err.println(
                    "Type for attribute \"" + name + "\" on stereotype \"" + stereotype + "\" is null");
        } else {
            field.setType(typeName);
        }

        target.addChild(field);

        /**
         * Add setter/getter for the added field
         */
        IDOMMethod setter = domFactory.createMethod();
        /**
         * Capitalize the first letter of the variable name so we have nice camel-case 
         */
        setter.setName("set" + name.substring(0, 1).toUpperCase() + name.substring(1));
        setter.setFlags(Flags.AccPublic);
        setter.setReturnType("void");
        setter.addParameter(typeName, name);
        setter.setBody("{ \n this." + name + "=" + name + ";\n }");

        target.addChild(setter);

        IDOMMethod getter = domFactory.createMethod();
        getter.setName("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
        getter.setFlags(Flags.AccPublic);
        getter.setReturnType(typeName);
        getter.setBody("{ \n return this." + name + ";\n }");

        target.addChild(getter);

    }

    //create Java file from UML class
    JavaClassOutputter.outputFile(context, target);

    return target;
}

From source file:ac.at.tuwien.dsg.uml2java.stereotype.export.ClassWithStereotypeExportRule.java

License:Open Source License

protected Object createTarget(ITransformContext context) {

    /**// w ww.  j  a va  2 s. c  o  m
     * Retrieve the target DOM generated by the UML2Java standard transformation
     */
    DOMNode target = (DOMNode) context.getTarget();

    /**
     * Create a DOMFactory to be used in instantiating new DOM elements
     */
    DOMFactory domFactory = new DOMFactory();

    /**
     * Get the transformation UML Class
     */
    Class umlCls = (Class) context.getSource();

    /**
     * We want to add something as default imports. Mainly org.eclipse.uml2.uml.* but maybe something more in the future.
     * To do that, as in this rule the Class java file is already partially generated by super ClassRule, we get its parent (i.e., the CompilationUnit), 
     * remove all parent children (actually just one, the generated class), add all import statements we need, and add the removed children back.
     * 
     * Otherwise the imports are added after the end of the class, which triggers compilation errors.
     */

    List<IDOMNode> children = new ArrayList<>();
    IDOMNode parent = target.getParent();
    Enumeration enumeration = parent.getChildren();

    //remove all parent children
    while (enumeration.hasMoreElements()) {
        IDOMNode node = (IDOMNode) enumeration.nextElement();
        children.add(node);
        node.remove();
    }

    //add all import statements
    for (String importStatement : defaultImports) {
        IDOMImport importEclipseUML = domFactory.createImport();
        importEclipseUML.setName(importStatement);
        parent.addChild(importEclipseUML);
    }

    //add children back
    for (IDOMNode child : children) {
        parent.addChild(child);
    }

    /**
     * We go through all applied stereotypes and for each we go through each attribute, and create a class field.
     */

    for (Stereotype stereotype : umlCls.getAppliedStereotypes()) {

        //get all properties
        for (Property attribute : stereotype.getAllAttributes()) {

            String name = attribute.getName();
            Type type = attribute.getType();
            String typeName = type.getName();

            org.eclipse.uml2.uml.Package packageType = type.getPackage();
            /**
             * Create Java field/class variable for each attribute 
             */
            Namespace namespace = attribute.getNamespace();

            //create import statement for each added field 
            //            IDOMImport importStatement = domFactory.createImport();
            //            importStatement.setName(packageType+"."+typeName);
            //            
            //            target.addChild(importStatement);

            IDOMField field = domFactory.createField();
            field.setName(name);
            field.setFlags(Flags.AccPrivate);
            /**
             * In case the model is incomplete, we add the field with Object as type
             */
            if (typeName == null) {
                field.setType("Object");
                //add in the generated code a comment explaining why field type is Object
                field.setComment("/*Type for attribute \"" + name + "\" on stereotype \"" + stereotype
                        + "\" is null */");
                System.err.println(
                        "Type for attribute \"" + name + "\" on stereotype \"" + stereotype + "\" is null");
            } else {
                field.setType(typeName);
            }

            target.addChild(field);

            /**
             * Add setter/getter for the added field
             */
            IDOMMethod setter = domFactory.createMethod();
            /**
             * Capitalize the first letter of the variable name so we have nice camel-case 
             */
            setter.setName("set" + name.substring(0, 1).toUpperCase() + name.substring(1));
            setter.setFlags(Flags.AccPublic);
            setter.setReturnType("void");
            setter.addParameter(typeName, name);
            setter.setBody("{ \n this." + name + "=" + name + ";\n }");

            target.addChild(setter);

            IDOMMethod getter = domFactory.createMethod();
            getter.setName("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
            getter.setFlags(Flags.AccPublic);
            getter.setReturnType(typeName);
            getter.setBody("{ \n return this." + name + ";\n }");

            target.addChild(getter);

        }

    }
    return target;
}