Example usage for org.eclipse.jdt.core.dom MethodDeclaration JAVADOC_PROPERTY

List of usage examples for org.eclipse.jdt.core.dom MethodDeclaration JAVADOC_PROPERTY

Introduction

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

Prototype

ChildPropertyDescriptor JAVADOC_PROPERTY

To view the source code for org.eclipse.jdt.core.dom MethodDeclaration JAVADOC_PROPERTY.

Click Source Link

Document

The "javadoc" structural property of this node type (child type: Javadoc ).

Usage

From source file:org.eclipse.emf.test.tools.merger.ASTTest.java

License:Open Source License

/**
 * Some test examples on using ASTRewrite to rewrite the code
 */// ww w . j a  va 2s  .  c  o m
@Test
public void testWrite() throws Exception {
    // read
    String source = TestUtil.readFile(CLASS_FILE, false);
    ASTParser astParser = CodeGenUtil.EclipseUtil.newASTParser();
    astParser.setSource(source.toCharArray());
    CompilationUnit sourceCu = (CompilationUnit) astParser.createAST(null);
    astParser.setSource(source.toCharArray());
    CompilationUnit targetCu = (CompilationUnit) astParser.createAST(null);

    // make modifications
    TypeDeclaration sourceClass = (TypeDeclaration) sourceCu.types().get(1);
    TypeDeclaration targetClass = (TypeDeclaration) targetCu.types().get(1);

    ASTRewrite rewriter = ASTRewrite.create(targetCu.getAST());

    IDocument targetDoc = new Document(new String(source.toCharArray()));

    // copy whole method using strings and placeholder rewrite
    ASTNode sourceMethodToCopy = sourceClass.getMethods()[4];
    ASTNode targetMethodToCopy = rewriter.createStringPlaceholder(
            source.substring(sourceMethodToCopy.getStartPosition(),
                    sourceMethodToCopy.getStartPosition() + sourceMethodToCopy.getLength()),
            ASTNode.METHOD_DECLARATION);
    ListRewrite lrw = rewriter.getListRewrite(targetClass, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
    lrw.insertFirst(targetMethodToCopy, null);

    // setBody() - replace body of the method
    ASTNode sourceMethodBodyToCopy = sourceClass.getMethods()[5].getBody();
    ASTNode targetMethodBodyToCopy = rewriter.createStringPlaceholder(
            source.substring(sourceMethodBodyToCopy.getStartPosition(),
                    sourceMethodBodyToCopy.getStartPosition() + sourceMethodBodyToCopy.getLength()),
            ASTNode.BLOCK);
    rewriter.replace(targetClass.getMethods()[6].getBody(), targetMethodBodyToCopy, null);

    // setExceptions() replace all exceptions
    @SuppressWarnings("deprecation")
    List<?> exceptionsToSet = sourceClass.getMethods()[5].thrownExceptions();
    @SuppressWarnings("deprecation")
    List<?> targetExceptins = targetClass.getMethods()[6].thrownExceptions();
    @SuppressWarnings("deprecation")
    ListRewrite listRewrite = rewriter.getListRewrite(targetClass.getMethods()[6],
            MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY);
    lrw = listRewrite;
    // remove all exceptions
    for (Iterator<?> it = targetExceptins.iterator(); it.hasNext();)
        lrw.remove((ASTNode) it.next(), null);
    // add all exceptions
    for (Iterator<?> it = exceptionsToSet.iterator(); it.hasNext();)
        lrw.insertLast((ASTNode) it.next(), null);

    // copy comment
    ASTNode comment = sourceClass.getMethods()[3].getJavadoc();
    rewriter.set(targetClass.getMethods()[6], MethodDeclaration.JAVADOC_PROPERTY, comment, null);

    // apply changes
    TextEdit editsInWriter = rewriter.rewriteAST(targetDoc, null);
    editsInWriter.apply(targetDoc);
    String result = targetDoc.get();

    File expectedOutputFile = new File(
            TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/Example1Changed.java").getAbsoluteFile();

    String expectedResult = TestUtil.readFile(expectedOutputFile, false);

    assertEquals(expectedResult, result);
}

From source file:org.moe.natjgen.MethodEditor.java

License:Apache License

public void setJavaDoc(XcodeDocumentation doc, ArrayList<CalleeArgument> args) {
    getRewrite().set(methodDecl, MethodDeclaration.JAVADOC_PROPERTY, doc.getJavaDoc(getRewrite(), args),
            getEditGroup());/*w w  w . ja v  a  2  s  .c  om*/
}