Example usage for org.eclipse.jdt.core.dom.rewrite ASTRewrite toString

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

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Returns a string suitable for debugging purposes (only).

Usage

From source file:org.evosuite.eclipse.quickfixes.ResolutionMarkerEvoIgnoreForClass.java

License:Open Source License

@Override
public void run(IMarker marker) {
    IResource res = marker.getResource();

    try {/*from  w  w  w  .  j a va2s  .  co  m*/

        CompilationUnit compunit = CompilationUnitManager.getCompilationUnit(res);

        int position = marker.getAttribute(IMarker.CHAR_START, 0) + 1;
        if (position == 0) {
            int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
            position = compunit.getPosition(line, 0);
        }

        AST ast = compunit.getAST();

        ASTRewrite rewriter = ASTRewrite.create(ast);

        Annotation annotation = ast.newNormalAnnotation();
        annotation.setTypeName(ast.newName("EvoIgnore"));
        ImportDeclaration id = ast.newImportDeclaration();
        id.setName(ast.newName("org.evosuite.quickfixes.annotations.EvoIgnore"));
        ListRewrite lr = rewriter.getListRewrite(compunit, CompilationUnit.TYPES_PROPERTY);

        //lr.insertFirst(annotation, null);
        lr.insertAt(annotation, 0, null);
        lr.insertAt(id, 0, null);
        ITextFileBufferManager bm = FileBuffers.getTextFileBufferManager();
        IPath path = compunit.getJavaElement().getPath();
        try {
            bm.connect(path, null, null);
            ITextFileBuffer textFileBuffer = bm.getTextFileBuffer(path, null);
            IDocument document = textFileBuffer.getDocument();
            TextEdit edits = rewriter.rewriteAST(document, null);
            edits.apply(document);
            textFileBuffer.commit(null, false);

        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedTreeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadLocationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bm.disconnect(path, null, null);
            } catch (CoreException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // (4)
        }
        System.out
                .println(lr.getRewrittenList() + "\nPosition: " + position + "\nEdits: " + rewriter.toString());
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        marker.delete();
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.evosuite.eclipse.quickfixes.ResolutionMarkerEvoIgnoreForMethod.java

License:Open Source License

@Override
public void run(IMarker marker) {
    // TODO Auto-generated method stub
    IResource res = marker.getResource();

    try {/*  w w  w .  j a va  2  s . co m*/

        ICompilationUnit icomp = CompilationUnitManager.getICompilationUnit(res);

        CompilationUnit compunit = CompilationUnitManager.getCompilationUnit(res);

        int position = marker.getAttribute(IMarker.CHAR_START, 0) + 1;
        if (position == 1) {
            int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
            position = compunit.getPosition(line, 0);
        }

        AST ast = compunit.getAST();
        ASTRewrite rewriter = ASTRewrite.create(ast);

        IJavaElement element = icomp.getElementAt(position);
        IJavaElement method = getMethod(element);

        //         TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);
        TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);

        MethodDeclaration md = td.getMethods()[0];

        int counter = 1;
        while (!md.getName().getFullyQualifiedName().equals(method.getElementName())
                && counter < td.getMethods().length) {
            md = td.getMethods()[counter];
            System.out.println(md.getName().getFullyQualifiedName() + " " + method.getElementName());
            counter++;
        }

        Annotation annotation = ast.newNormalAnnotation();
        annotation.setTypeName(ast.newName("EvoIgnore"));
        ImportDeclaration id = ast.newImportDeclaration();

        id.setName(ast.newName("org.evosuite.quickfixes.annotations.EvoIgnore"));
        ListRewrite lr = rewriter.getListRewrite(md.getParent(), TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
        ListRewrite lr2 = rewriter.getListRewrite(compunit, CompilationUnit.TYPES_PROPERTY);

        //lr.insertFirst(annotation, null);
        lr.insertBefore(annotation, md, null);
        lr2.insertAt(id, 0, null);
        ITextFileBufferManager bm = FileBuffers.getTextFileBufferManager();
        IPath path = compunit.getJavaElement().getPath();
        try {
            bm.connect(path, null, null);
            ITextFileBuffer textFileBuffer = bm.getTextFileBuffer(path, null);
            IDocument document = textFileBuffer.getDocument();
            TextEdit edits = rewriter.rewriteAST(document, null);
            edits.apply(document);
            textFileBuffer.commit(null, false);

        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedTreeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadLocationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bm.disconnect(path, null, null);
            } catch (CoreException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // (4)
        }
        System.out
                .println(lr.getRewrittenList() + "\nPosition: " + position + "\nEdits: " + rewriter.toString());
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        marker.delete();
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.evosuite.eclipse.quickfixes.ResolutionMarkerThrowsException.java

License:Open Source License

@Override
public void run(IMarker marker) {
    // TODO Auto-generated method stub
    IResource res = marker.getResource();

    try {/*w ww .  j ava2 s  .c o  m*/
        String markerMessage = (String) marker.getAttribute(IMarker.MESSAGE);
        String exception = markerMessage.split(" ")[0];
        String[] exceptionPackageArray = exception.split("\\.");
        String exceptionPackage = "";
        for (int i = 0; i < exceptionPackageArray.length - 1; i++) {
            exceptionPackage += exceptionPackageArray[i] + ".";
        }
        exception = exception.substring(exceptionPackage.length());

        System.out.println("Package: " + exceptionPackage + ", Exception: " + exception);

        ICompilationUnit icomp = CompilationUnitManager.getICompilationUnit(res);

        CompilationUnit compunit = CompilationUnitManager.getCompilationUnit(res);

        int position = marker.getAttribute(IMarker.CHAR_START, 0) + 1;
        if (position == 1) {
            int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
            position = compunit.getPosition(line, 0);
        }

        AST ast = compunit.getAST();
        ASTRewrite rewriter = ASTRewrite.create(ast);

        IJavaElement element = icomp.getElementAt(position);
        IJavaElement method = getMethod(element);

        //         TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);
        TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);

        MethodDeclaration md = td.getMethods()[0];

        int counter = 1;
        while (!md.getName().getFullyQualifiedName().equals(method.getElementName())
                && counter < td.getMethods().length) {
            md = td.getMethods()[counter];
            System.out.println(md.getName().getFullyQualifiedName() + " " + method.getElementName());
            counter++;
        }

        ListRewrite lr = rewriter.getListRewrite(md, MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY);
        ImportDeclaration id = ast.newImportDeclaration();
        id.setName(ast.newName(exceptionPackage + exception));
        ListRewrite lrClass = rewriter.getListRewrite(compunit, CompilationUnit.TYPES_PROPERTY);
        lrClass.insertAt(id, 0, null);

        Statement s = (Statement) rewriter.createStringPlaceholder(exception, ASTNode.EMPTY_STATEMENT);

        lr.insertAt(s, 0, null);
        System.out.println("MD: " + md.getName() + "\nList: " + lr.getOriginalList());

        ITextFileBufferManager bm = FileBuffers.getTextFileBufferManager();
        IPath path = compunit.getJavaElement().getPath();
        try {
            bm.connect(path, null, null);
            ITextFileBuffer textFileBuffer = bm.getTextFileBuffer(path, null);
            IDocument document = textFileBuffer.getDocument();
            TextEdit edits = rewriter.rewriteAST(document, null);
            edits.apply(document);
            textFileBuffer.commit(null, false);

        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedTreeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadLocationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bm.disconnect(path, null, null);
            } catch (CoreException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // (4)
        }
        System.out
                .println(lr.getRewrittenList() + "\nPosition: " + position + "\nEdits: " + rewriter.toString());
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CoreException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

}