Example usage for org.eclipse.jdt.core.dom CompilationUnit toString

List of usage examples for org.eclipse.jdt.core.dom CompilationUnit toString

Introduction

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

Prototype

@Override
public final String toString() 

Source Link

Document

Returns a string representation of this node suitable for debugging purposes only.

Usage

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

License:Apache License

/**
 * This is not a real unit test. It is a trial to use the ITextFileBufferManager to
 * save the changes in a CompilationUnit.<br />
 * Test for/*from   www .j a va  2s.c o m*/
 * {@link JavaInterfaceGenerator#updateJavadocInAST(de.akra.idocit.java.structure.JavaInterfaceArtifact)}
 * .
 * 
 * @throws FileNotFoundException
 * @throws IOException
 * @throws BadLocationException
 * @throws MalformedTreeException
 * @throws CoreException
 * @throws ParsingException
 */
@Ignore
public void testJavaInterfaceGenerator2() throws FileNotFoundException, IOException, MalformedTreeException,
        BadLocationException, CoreException, ParsingException {

    ParserOutput output = JavaTestUtils
            .createCompilationUnit(AllIDocItJavaTests.SOURCE_DIR + "ParsingService2.java");
    CompilationUnit unit = output.getCompilationUnit();
    String originalCU = unit.toString();

    TypeDeclaration typeDecl = (TypeDeclaration) unit.types().get(0);
    AST ast = typeDecl.getAST();
    ASTRewrite rewriter = ASTRewrite.create(ast);

    List<JavadocTagElement> jDocTags = new ArrayList<JavadocTagElement>();
    JavadocTagElement tagElem = new JavadocTagElement(TagElement.TAG_PARAM, "paramName",
            createParamDocumentations(), SignatureElement.EMPTY_SIGNATURE_ELEMENT);
    jDocTags.add(tagElem);

    List<TagElement> additionalTags = Collections.emptyList();
    Javadoc javadoc = JavaInterfaceGenerator.createOrUpdateJavadoc(jDocTags, additionalTags, ast.newJavadoc(),
            ast, null, JavadocGenerator.INSTANCE, null);

    rewriter.set(typeDecl, TypeDeclaration.JAVADOC_PROPERTY, javadoc, null);

    // get the buffer manager
    ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    IPath path = unit.getJavaElement().getPath();

    try {
        bufferManager.connect(path, LocationKind.IFILE, null);
        // retrieve the buffer
        ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE);

        if (textFileBuffer != null) {
            logger.log(Level.INFO, "isSynchronized=" + textFileBuffer.isSynchronized());

            IDocument document = textFileBuffer.getDocument();

            logger.log(Level.INFO, document.toString());

            // ... edit the document ...
            TextEdit edit = rewriter.rewriteAST(document, null);
            edit.apply(document);

            // commit changes to underlying file
            textFileBuffer.commit(null /* ProgressMonitor */, false /* Overwrite */);
        } else {
            logger.log(Level.SEVERE, "textFileBuffer == null");
        }
    } finally {
        bufferManager.disconnect(path, LocationKind.IFILE, null);
    }

    String changedCU = unit.toString();

    logger.log(Level.INFO, originalCU);
    logger.log(Level.INFO, changedCU);
}

From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGenerator.java

License:Apache License

/**
 * Generate the Class Interface. This is the main generation part for this
 * SimpleJavaTransformer./*www.  j a  v a2  s.  co m*/
 * 
 * @param Class
 *            clazz the UML class
 * @return CompilationUnit the complete class with its content as a String
 */
public CompilationUnit generateEnum(Classifier clazz, String sourceDirectoryPackageName) {
    this.sourceDirectoryPackageName = sourceDirectoryPackageName;

    AST ast = AST.newAST(AST.JLS3);
    CompilationUnit cu = ast.newCompilationUnit();

    generatePackage(clazz, ast, cu);
    EnumDeclaration ed = generateEnum(clazz, ast, cu);
    generateAttributes(clazz, ast, ed);
    generateConstructor(clazz, ast, ed);
    generateConstants(clazz, ast, ed);
    generateGetterMethod(clazz, ast, ed);

    logger.log(Level.INFO, "Compilation unit: \n\n" + cu.toString());
    return cu;
}

From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java

License:Apache License

@Test
public void testGeneratePackage() {
    AST ast = AST.newAST(AST.JLS3);/*from  w w  w.j a  va  2s  . c o  m*/
    CompilationUnit cu = ast.newCompilationUnit();

    enumGenerator.generatePackage(clazz, ast, cu);

    String onlyPackage = cu.toString();
    String expectedResult = "package de.crowdcode.kissmda.testapp.components;\n";

    boolean isInside = onlyPackage.contains(expectedResult);

    assertTrue(isInside);
}

From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java

License:Apache License

@Test
public void testGenerateEnum() {
    AST ast = AST.newAST(AST.JLS3);//from w  ww .  j  a  v a  2s. c  o  m
    CompilationUnit cu = ast.newCompilationUnit();

    enumGenerator.generateEnum(clazz, ast, cu);

    assertEquals("public enum Company {}\n", cu.toString());
}

From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test//  ww w .  j a  va  2 s  . c o m
public void testGenerateGetterMethod() {
    AST ast = AST.newAST(AST.JLS3);
    CompilationUnit cu = ast.newCompilationUnit();
    EnumDeclaration ed = enumGenerator.generateEnum(clazz, ast, cu);

    EList<Property> properties = mock(EList.class);
    Iterator<Property> propertyIter = mock(Iterator.class);
    Property property = mock(Property.class);
    Type type = mock(Type.class);
    String name = "type";

    EList<Comment> comments = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get());

    when(clazz.getAttributes()).thenReturn(properties);
    when(properties.iterator()).thenReturn(propertyIter);
    when(propertyIter.hasNext()).thenReturn(true).thenReturn(false);
    when(propertyIter.next()).thenReturn(property);
    when(property.getType()).thenReturn(type);
    when(property.getName()).thenReturn(name);
    when(property.getUpper()).thenReturn(1);
    when(property.getLower()).thenReturn(1);
    when(property.getOwnedComments()).thenReturn(comments);
    when(type.getName()).thenReturn("String");
    when(type.getQualifiedName()).thenReturn("String");

    enumGenerator.generateGetterMethod(clazz, ast, ed);

    assertEquals("public enum Company {; public String getType(){\n  return type;\n}\n}\n", cu.toString());
}

From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test/*w ww .  j a  v a2  s .  c o m*/
public void testGenerateGetterMethodWithSourceDirectoryPackageNameWrong() {
    AST ast = AST.newAST(AST.JLS3);
    CompilationUnit cu = ast.newCompilationUnit();
    EnumDeclaration ed = enumGenerator.generateEnum(clazz, ast, cu);

    EList<Property> properties = mock(EList.class);
    Iterator<Property> propertyIter = mock(Iterator.class);
    Property property = mock(Property.class);
    Type type = mock(Type.class);
    String name = "type";

    EList<Comment> comments = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get());

    when(clazz.getAttributes()).thenReturn(properties);
    when(properties.iterator()).thenReturn(propertyIter);
    when(propertyIter.hasNext()).thenReturn(true).thenReturn(false);
    when(propertyIter.next()).thenReturn(property);
    when(property.getType()).thenReturn(type);
    when(property.getName()).thenReturn(name);
    when(property.getUpper()).thenReturn(1);
    when(property.getLower()).thenReturn(1);
    when(property.getOwnedComments()).thenReturn(comments);
    when(type.getName()).thenReturn("Data::String");
    when(type.getQualifiedName()).thenReturn("Data::String");

    enumGenerator.generateGetterMethod(clazz, ast, ed);

    assertEquals("public enum Company {; public Data.String getType(){\n  return type;\n}\n}\n", cu.toString());
}

From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test/*w  w w. j a  va  2s.  c  om*/
public void testGenerateGetterMethodWithSourceDirectoryPackageNameCorrect() {
    AST ast = AST.newAST(AST.JLS3);
    CompilationUnit cu = ast.newCompilationUnit();
    EnumDeclaration ed = enumGenerator.generateEnum(clazz, ast, cu);

    EList<Property> properties = mock(EList.class);
    Iterator<Property> propertyIter = mock(Iterator.class);
    Property property = mock(Property.class);
    Type type = mock(Type.class);
    String name = "type";

    EList<Comment> comments = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get());

    when(clazz.getAttributes()).thenReturn(properties);
    when(properties.iterator()).thenReturn(propertyIter);
    when(propertyIter.hasNext()).thenReturn(true).thenReturn(false);
    when(propertyIter.next()).thenReturn(property);
    when(property.getType()).thenReturn(type);
    when(property.getName()).thenReturn(name);
    when(property.getUpper()).thenReturn(1);
    when(property.getLower()).thenReturn(1);
    when(property.getOwnedComments()).thenReturn(comments);
    when(type.getName()).thenReturn("Data::String");
    when(type.getQualifiedName()).thenReturn("Data::String");

    enumGenerator.generateGetterMethod(clazz, ast, ed, "Data");

    assertEquals("public enum Company {; public String getType(){\n  return type;\n}\n}\n", cu.toString());
}

From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test//ww w .jav a2  s  .c o  m
public void testGenerateAttribute() {
    AST ast = AST.newAST(AST.JLS3);
    CompilationUnit cu = ast.newCompilationUnit();
    EnumDeclaration ed = enumGenerator.generateEnum(clazz, ast, cu);

    EList<Property> properties = mock(EList.class);
    Iterator<Property> propertyIter = mock(Iterator.class);
    Property property = mock(Property.class);
    Type type = mock(Type.class);
    String name = "type";

    EList<Comment> comments = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get());

    when(clazz.getAttributes()).thenReturn(properties);
    when(properties.iterator()).thenReturn(propertyIter);
    when(propertyIter.hasNext()).thenReturn(true).thenReturn(false);
    when(propertyIter.next()).thenReturn(property);
    when(property.getType()).thenReturn(type);
    when(property.getName()).thenReturn(name);
    when(property.getUpper()).thenReturn(1);
    when(property.getLower()).thenReturn(1);
    when(property.getOwnedComments()).thenReturn(comments);
    when(type.getName()).thenReturn("String");
    when(type.getQualifiedName()).thenReturn("String");

    enumGenerator.generateAttributes(clazz, ast, ed);

    assertEquals("public enum Company {; String type;\n}\n", cu.toString());
}

From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test/*from  w ww . j a  v a  2 s . co m*/
public void testGenerateConstructorWithOneParameter() {
    AST ast = AST.newAST(AST.JLS3);
    CompilationUnit cu = ast.newCompilationUnit();
    EnumDeclaration ed = enumGenerator.generateEnum(clazz, ast, cu);

    EList<Property> firstProperties = mock(EList.class);
    Iterator<Property> firstPropertyIter = mock(Iterator.class);

    EList<Property> secondProperties = mock(EList.class);
    Iterator<Property> secondPropertyIter = mock(Iterator.class);

    Property property = mock(Property.class);
    Type type = mock(Type.class);
    String name = "type";

    EList<Comment> comments = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get());

    when(clazz.getAttributes()).thenReturn(firstProperties).thenReturn(secondProperties);
    when(firstProperties.iterator()).thenReturn(firstPropertyIter);
    when(firstPropertyIter.hasNext()).thenReturn(true).thenReturn(false);
    when(firstPropertyIter.next()).thenReturn(property);

    when(secondProperties.iterator()).thenReturn(secondPropertyIter);
    when(secondPropertyIter.hasNext()).thenReturn(true).thenReturn(false);
    when(secondPropertyIter.next()).thenReturn(property);

    when(property.getType()).thenReturn(type);
    when(property.getName()).thenReturn(name);
    when(property.getUpper()).thenReturn(1);
    when(property.getLower()).thenReturn(1);
    when(property.getOwnedComments()).thenReturn(comments);
    when(type.getName()).thenReturn("String");
    when(type.getQualifiedName()).thenReturn("String");

    enumGenerator.generateConstructor(clazz, ast, ed);

    assertEquals("public enum Company {; private Company(String type){\n  this.type=type;\n}\n}\n",
            cu.toString());
}

From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test//from   w  w  w . j a  v a 2 s  . c  om
public void testGenerateConstructorWithTwoParameters() {
    AST ast = AST.newAST(AST.JLS3);
    CompilationUnit cu = ast.newCompilationUnit();
    EnumDeclaration ed = enumGenerator.generateEnum(clazz, ast, cu);

    EList<Property> firstProperties = mock(EList.class);
    Iterator<Property> firstPropertyIter = mock(Iterator.class);

    EList<Property> secondProperties = mock(EList.class);
    Iterator<Property> secondPropertyIter = mock(Iterator.class);

    Property firstProperty = mock(Property.class);
    Type firstType = mock(Type.class);
    String firstName = "type";

    Property secondProperty = mock(Property.class);
    Type secondType = mock(Type.class);
    String secondName = "count";

    EList<Comment> comments = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get());

    when(clazz.getAttributes()).thenReturn(firstProperties).thenReturn(secondProperties);
    when(firstProperties.iterator()).thenReturn(firstPropertyIter);
    when(firstPropertyIter.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
    when(firstPropertyIter.next()).thenReturn(firstProperty).thenReturn(secondProperty);

    when(secondProperties.iterator()).thenReturn(secondPropertyIter);
    when(secondPropertyIter.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
    when(secondPropertyIter.next()).thenReturn(firstProperty).thenReturn(secondProperty);

    when(firstProperty.getType()).thenReturn(firstType);
    when(firstProperty.getName()).thenReturn(firstName);
    when(firstProperty.getUpper()).thenReturn(1);
    when(firstProperty.getLower()).thenReturn(1);
    when(firstProperty.getOwnedComments()).thenReturn(comments);
    when(firstType.getName()).thenReturn("String");
    when(firstType.getQualifiedName()).thenReturn("String");

    when(secondProperty.getType()).thenReturn(secondType);
    when(secondProperty.getName()).thenReturn(secondName);
    when(secondProperty.getUpper()).thenReturn(1);
    when(secondProperty.getLower()).thenReturn(1);
    when(secondProperty.getOwnedComments()).thenReturn(comments);
    when(secondType.getName()).thenReturn("Integer");
    when(secondType.getQualifiedName()).thenReturn("Integer");

    enumGenerator.generateConstructor(clazz, ast, ed);

    assertEquals(
            "public enum Company {; private Company(String type,Integer count){\n  this.type=type;\n  this.count=count;\n}\n}\n",
            cu.toString());
}