Example usage for org.eclipse.jdt.core.jdom IDOMMethod setBody

List of usage examples for org.eclipse.jdt.core.jdom IDOMMethod setBody

Introduction

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

Prototype

public void setBody(String body);

Source Link

Document

Sets the body of this method.

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   w  w  w . j a va 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();

    /**/*from  w  ww . jav a2  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 ww  w.ja va 2s.  c  o 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  w  w.jav a 2s  . 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;
}

From source file:com.iw.plugins.spindle.wizards.project.ApplicationWizardPage.java

License:Mozilla Public License

protected void populateMethodSource(IType servletType, IProgressMonitor monitor) throws JavaModelException {
    String methodName = MessageUtil.getString("Tapestry.servlet.getAppMethodName");
    ICompilationUnit parentCU = servletType.getCompilationUnit();
    String gensource = parentCU.getSource();

    DOMFactory domFactory = new DOMFactory();
    IDOMCompilationUnit unit = domFactory.createCompilationUnit(gensource, servletType.getElementName());
    IDOMNode current = unit.getFirstChild();
    while (current.getNodeType() != IDOMNode.TYPE) {
        current = current.getNextNode();
    }//from www .j  a va 2  s  . com
    IDOMNode theType = current;
    IDOMMethod method = findMethod(theType, methodName);
    if (method == null) {
        method = domFactory.createMethod("protected String getApplicationSpecificationPath() {}");
        theType.addChild(method);
    }
    method.setBody(getAppMethodBody());

    String newContents = Utils.formatJavaCode(unit.getContents(), 0,
            StubUtility.getLineDelimiterUsed(parentCU));
    parentCU.getBuffer().setContents(newContents);
    parentCU.save(monitor, true);
}

From source file:org.eclipse.emf.codegen.jet.JETSkeleton.java

License:Open Source License

public void setBody(List<String> lines) {
    IDOMMethod method = getLastMethod();
    if (method != null) {
        StringBuffer body = new StringBuffer();
        body.append(NL + "  {" + NL);
        body.append(STRING_BUFFER_DECLARATION);
        for (String line : lines) {
            body.append("    ");
            body.append(convertLineFeed(line));
            body.append(NL);//  w  w  w .  java2  s.c o m
        }
        body.append(STRING_BUFFER_RETURN);
        body.append("  }" + NL);

        method.setBody(body.toString());
    }
}