Example usage for org.eclipse.jdt.core.dom AST newTextElement

List of usage examples for org.eclipse.jdt.core.dom AST newTextElement

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom AST newTextElement.

Prototype

public TextElement newTextElement() 

Source Link

Document

Creates and returns a new text element node.

Usage

From source file:ac.at.tuwien.dsg.uml.statemachine.export.transformation.engines.AbstractStateMachineTestStrategy.java

License:Open Source License

/**
 * Method which for any type of trigger Event, from CallEvent, to ChangeEvent, TimeEvent, etc, creates an abstract method
 * @param trigger//  www. ja v a2s.  com
 * @param ast
 * @return
 */
protected MethodDeclaration createAbstractTriggerInvocation(Trigger trigger, AST ast) {

    //get trigger event
    Event event = trigger.getEvent();
    //if UML operation triggers event (so CLass method)

    String methodName = GENERATE_EVENT_LEADING;
    Javadoc javadoc = ast.newJavadoc();
    TagElement tag = ast.newTagElement();
    TextElement textElement = ast.newTextElement();

    if (event instanceof CallEvent) {

        CallEvent callEvent = (CallEvent) event;
        Operation operation = callEvent.getOperation();
        Class operationClass = (Class) operation.eContainer();

        methodName = operationClass.getName().replaceAll("\\W", "") + "_"
                + operation.getName().replaceAll("\\W", "");
        textElement.setText("Method must return true if method invocation is successful."
                + " Method designed to allow particular implementation  call of \"" + operation.getName() + "\""
                + " on class \"" + operationClass.getName()
                + "\" so we can assert if transition after event is correct");

    } else if (event instanceof ChangeEvent) {

        ChangeEvent changeEvent = (ChangeEvent) event;
        OpaqueExpression changeExpression = (OpaqueExpression) changeEvent.getChangeExpression();
        if (changeExpression.getBodies().isEmpty()) {
            System.err.println("Event " + event.getName() + " has no body/expression");
        } else {
            String body = changeExpression.getBodies().iterator().next();
            body = StringFormatter.convertNonAlphanumericalSymbolsToUnderscore(
                    StringFormatter.convertMathSymbolsToText(body));

            methodName = changeEvent.getName().replaceAll("\\W", "") + "_" + body;
            textElement.setText("Method must return true if event invocation is successful"
                    + " Method designed to allow particular implementation  for forcing condition \"" + body
                    + "\" encountered on event \"" + changeEvent.getName()
                    + "\" to true so we can assert if transition after event is correct");
        }
    } else if (event instanceof TimeEvent) {

        TimeEvent timeEvent = (TimeEvent) event;

        if (timeEvent.getWhen() == null || timeEvent.getWhen().getExpr() == null) {
            System.err.println("Event " + event.getName() + " has no when/expression");
        } else {
            String expression = ((LiteralString) timeEvent.getWhen().getExpr()).getValue(); // maybe can do something more with TimeExpression
            expression = StringFormatter.convertNonAlphanumericalSymbolsToUnderscore(
                    StringFormatter.convertMathSymbolsToText(expression));

            methodName = timeEvent.getName().replaceAll("\\W", "") + "_" + expression;
            textElement.setText("Method must return true if event invocation is successful"
                    + " Method designed to allow particular implementation  for forcing the time event "
                    + timeEvent.getName() + " with body \"" + expression
                    + "\" to happen so we can assert if transition after event is correct");
        }

    } else if (event instanceof SignalEvent) {

        SignalEvent signalEvent = (SignalEvent) event;
        Signal signal = signalEvent.getSignal();

        methodName = signal.getName().replaceAll("\\W", "");
        textElement.setText("Method must return true if method invocation is successful."
                + " Method designed to allow particular implementation call of \"" + signal.getName()
                + "\" so we can assert if transition after event is correct");
    } else {

        System.err.println("Event type of " + event.getClass() + " not supported yet ");
        methodName = "invoke" + event.getName().replaceAll("\\W", "_");
        textElement.setText("Event type of " + event.getClass()
                + " not supported yet so generated javadoc not very usefull. Method must ensure the event is called so we can assert if transition after event is correct.");
    }

    tag.fragments().add(textElement);
    javadoc.tags().add(tag);

    MethodDeclaration method = createAbstractAssertMethod(javadoc, methodName, ast);
    return method;
}

From source file:ac.at.tuwien.dsg.uml.statemachine.export.transformation.engines.AbstractStateMachineTestStrategy.java

License:Open Source License

protected MethodDeclaration createAbstractMethodForState(StateMachineState state, AST ast) {

    String stateName = state.equals(StateMachineState.INITIAL_STATE) ? "InitialState"
            : state.getName().replaceAll("\\W", "");

    Javadoc javadoc = ast.newJavadoc();/*from   w ww .j a va 2  s . com*/
    TagElement tag = ast.newTagElement();
    TextElement textElement = ast.newTextElement();
    textElement.setText("Method must return true if the current state is " + stateName);
    tag.fragments().add(textElement);
    javadoc.tags().add(tag);

    MethodDeclaration method = createAbstractAssertMethod(javadoc, ASSERT_STATE_LEADING + stateName, ast);
    return method;
}

From source file:ac.at.tuwien.dsg.uml.statemachine.export.transformation.engines.AbstractStateMachineTestStrategy.java

License:Open Source License

protected MethodDeclaration createAbstractMethodForGuard(String condition, AST ast) {

    String conditionName = StringFormatter
            .convertNonAlphanumericalSymbolsToUnderscore(StringFormatter.convertMathSymbolsToText(condition));

    Javadoc javadoc = ast.newJavadoc();/*from w  w w. j av a  2s. c  o  m*/
    TagElement tag = ast.newTagElement();
    TextElement textElement = ast.newTextElement();
    textElement.setText(
            "Method must evaluate and return true if the following condition is true:  state is " + condition);
    tag.fragments().add(textElement);
    javadoc.tags().add(tag);

    MethodDeclaration method = createAbstractAssertMethod(javadoc,
            ASSERT_GUARD_LEADING + (conditionName.substring(0, 1).toUpperCase() + conditionName.substring(1)),
            ast);
    return method;
}

From source file:ac.at.tuwien.dsg.uml.statemachine.export.transformation.engines.AbstractStateMachineTestStrategy.java

License:Open Source License

protected MethodDeclaration createAbstractForceConditionMethod(String condition, AST ast) {

    String conditionName = StringFormatter
            .convertNonAlphanumericalSymbolsToUnderscore(StringFormatter.convertMathSymbolsToText(condition));

    Javadoc javadoc = ast.newJavadoc();//from   ww  w  .  ja  v  a 2s .  c o  m
    TagElement tag = ast.newTagElement();
    TextElement textElement = ast.newTextElement();
    textElement.setText(
            "Method must call tested system and ensure the following condition is true so the test can progress on the current test branch: "
                    + condition);
    tag.fragments().add(textElement);
    javadoc.tags().add(tag);

    MethodDeclaration method = createAbstractAssertMethod(javadoc,
            FORCE_GUARD_LEADING + (conditionName.substring(0, 1).toUpperCase() + conditionName.substring(1)),
            ast);
    return method;
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.forward.ForwardJavaDeclaration.java

License:Open Source License

/**
 * @throws CodeSyncException //from ww w.j  a  v  a 2  s  .  com
 * @flowerModelElementId _zbgpOpiOEd6aNMdNFvR5WQ
 */
@SuppressWarnings("unchecked")
@Override
protected void setASTFeatureValue(EStructuralFeature feature, TDeclaration astElement, Object value)
        throws CodeSyncException {
    if (astElement == null)
        throw new IllegalArgumentException("astElement null ");
    AST ast = astElement.getAST();

    switch (feature.getFeatureID()) {
    case UMLPackage.NAMED_ELEMENT__VISIBILITY:
        JavaSyncUtils.updateVisibilityFromModelToJavaClass(astElement, (VisibilityKind) value);
        break;
    case UMLPackage.ELEMENT__OWNED_COMMENT:
        List<Comment> listComments = (List<Comment>) value;
        if (listComments.size() > 1)
            throw new IllegalArgumentException("setting more than one documentation ");
        else if (listComments.size() == 1) {
            String documentation = listComments.get(0).getBody();
            Javadoc javadoc = ast.newJavadoc();
            if (documentation != null) {
                javadoc.setProperty("comment", "\r\n" + documentation + "\r\n");
                documentation = documentation.replace("\n", "\n * ");
            }
            TagElement tag = ast.newTagElement();
            TextElement te = ast.newTextElement();
            tag.fragments().add(te);
            te.setText(documentation);
            javadoc.tags().add(tag);
            astElement.setJavadoc(javadoc);
        } else if (listComments.isEmpty())
            astElement.setJavadoc(null);
        break;
    case UMLPackage.FEATURE__IS_STATIC:
        JavaSyncUtils.updateModifierFromModelToJavaClass(astElement, (Boolean) value,
                JavaSyncUtils.MODIFIER_STATIC);
        break;
    case CodeSyncPackage.FINAL_FEATURE__IS_FINAL:
        JavaSyncUtils.updateModifierFromModelToJavaClass(astElement, (Boolean) value,
                JavaSyncUtils.MODIFIER_FINAL);
        break;
    default:
        throw new IllegalArgumentException("Cannot get value for feature in ForwardJavaDeclaration:" + feature);
    }
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.forward.ForwardJavaType.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  ww  w . j a  va  2s. c  om*/
protected void setASTFeatureValue(EStructuralFeature feature, CompilationUnit astElement, Object value)
        throws CodeSyncException {
    if (astElement == null)
        throw new IllegalArgumentException("astElement null ");
    AST ast = astElement.getAST();
    TypeDeclaration masterClass = JavaSyncUtils.getMasterClass(astElement);

    switch (feature.getFeatureID()) {
    case UMLPackage.NAMED_ELEMENT__NAME:
        if (value == null)
            throw new IllegalArgumentException("setting name to null value ");

        String oldName = masterClass.getName().toString();
        if (!oldName.equals(value) && !oldName.equals("MISSING")) { // rename case
            try {
                SyncUtils.renameFile(oldName.toString(), (String) value, ".java", parentFolder);
            } catch (CoreException e) {
                throw new RuntimeException("Error during folder refresh: " + parentFolder, e);
            } catch (Exception e) {
                throw new RuntimeException("Error during file rename ", e);
            }
        }
        try {
            masterClass.setName(ast.newSimpleName((String) value));
        } catch (Exception e) {
            e.printStackTrace();
        }
        break;
    case UMLPackage.NAMED_ELEMENT__VISIBILITY:
        JavaSyncUtils.updateVisibilityFromModelToJavaClass(masterClass, (VisibilityKind) value);
        break;
    case UMLPackage.CLASSIFIER__IS_ABSTRACT:
        JavaSyncUtils.updateModifierFromModelToJavaClass(masterClass, (Boolean) value,
                JavaSyncUtils.MODIFIER_ABSTRACT);
        break;
    case UMLPackage.ELEMENT__OWNED_COMMENT:
        List<Comment> listComments = (List<Comment>) value;
        if (listComments.size() > 1)
            throw new IllegalArgumentException("setting more than one documentation ");
        else if (listComments.size() == 1) {
            String documentation = listComments.get(0).getBody();
            Javadoc javadoc = ast.newJavadoc();
            if (documentation != null) {
                javadoc.setProperty("comment", "\r\n" + documentation + "\r\n");
                documentation = documentation.replace("\n", "\n * ");
            }
            TagElement tag = ast.newTagElement();
            TextElement te = ast.newTextElement();
            tag.fragments().add(te);
            te.setText(documentation);
            javadoc.tags().add(tag);
            JavaSyncUtils.getMasterClass(astElement).setJavadoc(javadoc);
        } else if (listComments.isEmpty())
            JavaSyncUtils.getMasterClass(astElement).setJavadoc(null);
        break;
    default:
        throw new IllegalArgumentException("Cannot get value for feature in ForwardJavaType:" + feature);
    }
}

From source file:com.google.currysrc.api.process.ast.AstNodes.java

License:Apache License

public static TextElement createTextElement(AST ast, String text) {
    TextElement textElement = ast.newTextElement();
    textElement.setText(text);/*from w  w w.  j  a v a  2  s .c o  m*/
    return textElement;
}

From source file:com.idega.eclipse.ejbwizards.BeanCreator.java

License:Open Source License

protected Javadoc getJavadoc(AST ast, IMethod method) {
    Javadoc jc = ast.newJavadoc();/*from www.  j  av a2s.co  m*/
    TagElement tag = ast.newTagElement();
    tag.setTagName(TagElement.TAG_SEE);
    TextElement te = ast.newTextElement();
    te.setText(getType().getFullyQualifiedName() + "#" + method.getElementName());
    tag.fragments().add(te);
    jc.tags().add(tag);

    return jc;
}

From source file:de.akra.idocit.java.services.JavadocGenerator.java

License:Apache License

/**
 * Append the information out of <code>documentations</code> to the {@link Javadoc}
 * block comment. If <code>tagName != null</code> the documentations are added to a
 * new {@link TagElement} with that name. Add first the general description text with
 * <code>tagName == null</code>. After that all other wished tags.
 * /* w  ww .j  a  va 2s.  c o  m*/
 * @param documentations
 *            The list of {@link Documentation}s which should be converted to
 *            {@link Javadoc}.
 * @param tagName
 *            The name of the tag element, or <code>null</code> (@see
 *            {@link TagElement#setTagName(String)} ).
 * @param paramName
 *            The name of the described parameter, or <code>null</code> if no name is
 *            needed.
 * @param javadoc
 *            The {@link Javadoc} reference to which the {@link TagElement}s should be
 *            added.
 */
@Override
public void appendDocsToJavadoc(List<Documentation> documentations, String tagName, String paramName,
        String thematicGridName, Javadoc javadoc, List<TagElement> additionalTagElements, JavaMethod method) {
    @SuppressWarnings("unchecked")
    List<TagElement> tags = javadoc.tags();
    AST jdocAST = javadoc.getAST();

    boolean tagChanged = false;

    TagElement newTag = jdocAST.newTagElement();

    if (tagName != null) {
        tagChanged = true;
        newTag.setTagName(tagName + de.akra.idocit.common.utils.StringUtils.EMPTY);
    }

    @SuppressWarnings("unchecked")
    List<ASTNode> fragments = newTag.fragments();

    if (paramName != null) {
        tagChanged = true;
        TextElement paramNameElement = jdocAST.newTextElement();
        paramNameElement.setText(paramName + de.akra.idocit.common.utils.StringUtils.EMPTY);
        fragments.add(paramNameElement);
    }

    final String tableStartTag = "<table name=\"" + IDOCIT_HTML_TABLE_NAME
            + "\" border=\"1\" cellspacing=\"0\">\n";

    for (Documentation doc : documentations) {
        // write only if there is something to write
        if (doc.getThematicRole() != null || !doc.getDocumentation().isEmpty()) {
            tagChanged = true;
            StringBuffer textElem = new StringBuffer();
            if (fragments.size() >= 1) {
                textElem.append("\n<br />");
            }
            textElem.append(tableStartTag);

            if (doc.getSignatureElementIdentifier() != null) {
                textElem.append("<tr><td>Element:</td><td>");
                textElem.append(quoteGenericsInIdentifier(doc.getSignatureElementIdentifier()));
                textElem.append("</td></tr>\n");
            }

            if (doc.getThematicRole() != null) {
                StringBuffer thematicRoleBuffer = new StringBuffer();
                thematicRoleBuffer.append(doc.getThematicRole().getName());

                if (doc.isErrorCase()) {
                    thematicRoleBuffer.append(JavadocUtils.getComplexErrorFlagPostfix());
                }

                textElem.append("<tr><td>Role:</td><td>");
                textElem.append(thematicRoleBuffer.toString());
                textElem.append("</td></tr>\n");
            }

            Map<Addressee, String> docMap = doc.getDocumentation();
            for (Addressee addressee : doc.getAddresseeSequence()) {
                String text = docMap.get(addressee);
                if (!text.isEmpty()) {
                    textElem.append("<tr><td><b>");
                    textElem.append(addressee.getName());
                    textElem.append("</b>:</td><td>");
                    textElem.append(StringUtils.escapeHtml(docMap.get(addressee)));
                    textElem.append("</td></tr>\n");
                }
            }
            textElem.append("</table>");

            TextElement textElement = jdocAST.newTextElement();
            textElement.setText(textElem.toString());
            fragments.add(textElement);
        }
    }
    if (tagChanged) {
        tags.add(newTag);
    }
}

From source file:de.akra.idocit.java.services.JavadocGeneratorTest.java

License:Apache License

/**
 * Tests {@link JavadocGenerator#appendDocsToJavadoc(List, String, String, Javadoc)} .
 * /* ww w  .java2  s  .com*/
 * @throws IOException
 * @throws FileNotFoundException
 */
@SuppressWarnings("unchecked")
@Test
public void testGenerateJavadoc() throws FileNotFoundException, IOException {
    ParserOutput output = JavaTestUtils
            .createCompilationUnit(AllIDocItJavaTests.SOURCE_DIR + "ParsingService.java");
    CompilationUnit cu = output.getCompilationUnit();

    AbstractTypeDeclaration absTypeDecl = (AbstractTypeDeclaration) cu.types().get(0);
    AST ast = absTypeDecl.getAST();
    Javadoc javadoc = ast.newJavadoc();

    List<Documentation> documentations = createParamDocumentations();

    JavaMethod methodParam = new JavaMethod(SignatureElement.EMPTY_SIGNATURE_ELEMENT, "Method",
            "Searching Operations", Numerus.SINGULAR);

    for (Documentation documentation : documentations) {
        methodParam.addDocpart(documentation);
    }

    JavadocGenerator.INSTANCE.appendDocsToJavadoc(methodParam.getDocumentations(), null, null,
            "Searching Operations", javadoc, new ArrayList<TagElement>(), null);
    JavadocGenerator.INSTANCE.appendDocsToJavadoc(methodParam.getDocumentations(), TagElement.TAG_PARAM,
            "person", "Searching Operations", javadoc, new ArrayList<TagElement>(), null);

    JavaMethod methodReturn = new JavaMethod(SignatureElement.EMPTY_SIGNATURE_ELEMENT, "Method",
            "Searching Operations", Numerus.SINGULAR);

    for (Documentation documentation : createReturnDocumentations()) {
        methodReturn.addDocpart(documentation);
    }

    JavadocGenerator.INSTANCE.appendDocsToJavadoc(methodReturn.getDocumentations(), TagElement.TAG_RETURN, null,
            "Searching Operations", javadoc, new ArrayList<TagElement>(), null);

    TagElement tagElement = ast.newTagElement();
    tagElement.setTagName(JavadocParser.JAVADOC_TAG_THEMATICGRID);

    List<ASTNode> fragments = (List<ASTNode>) tagElement.fragments();
    TextElement textElement = ast.newTextElement();
    textElement.setText(" Searching Operations");
    fragments.add(textElement);

    List<TagElement> tags = javadoc.tags();
    tags.add(tagElement);

    Assert.assertEquals(EXPECTED_JAVADOC, javadoc.toString());
}