Example usage for org.eclipse.jdt.internal.core.util Util editedString

List of usage examples for org.eclipse.jdt.internal.core.util Util editedString

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.util Util editedString.

Prototype

public final static String editedString(String original, TextEdit edit) 

Source Link

Document

Apply the given edit on the given string and return the updated string.

Usage

From source file:com.liferay.blade.upgrade.liferay70.ImportStatementMigrator.java

License:Open Source License

@Override
public void correctProblems(File file, List<Problem> problems) throws AutoMigrateException {
    final List<String> importsToRewrite = new ArrayList<>();

    for (Problem problem : problems) {
        if (problem.autoCorrectContext instanceof String) {
            final String importData = problem.autoCorrectContext;

            if (importData != null && importData.startsWith(PREFIX)) {
                final String importValue = importData.substring(PREFIX.length());

                if (_imports.containsKey(importValue)) {
                    importsToRewrite.add(importValue);
                }//from   w  ww .  j  av  a  2  s  .c o m
            }
        }
    }

    if (importsToRewrite.size() > 0) {
        ICompilationUnit source = null;
        final IFile javaFile;

        final JavaFile javaFileService = _context.getService(_context.getServiceReference(JavaFile.class));

        javaFile = javaFileService.getIFile(file);

        if (javaFile == null) {
            throw new AutoMigrateException("Unable to get java file as IFile, is project a java project?");
        }

        try {
            source = JavaCore.createCompilationUnitFrom(javaFile);
        } catch (Exception e) {
            throw new AutoMigrateException("Could not get compilation unit for file: " + file.getName(), e);
        }

        final ImportRewrite importRewrite;

        try {
            importRewrite = ImportRewrite.create(source, true);
        } catch (JavaModelException e1) {
            e1.printStackTrace();
            throw new AutoMigrateException("Unable to create import rewrite action: " + file.getName(), e1);
        }

        for (String importToRewrite : importsToRewrite) {
            importRewrite.removeImport(importToRewrite);

            final String newImport = _imports.get(importToRewrite);

            importRewrite.addImport(newImport);
        }

        if (importRewrite.hasRecordedChanges()) {
            try {
                final TextEdit textEdit = importRewrite.rewriteImports(new NullProgressMonitor());
                final String newSource = Util.editedString(source.getSource(), textEdit);

                javaFile.setContents(new ByteArrayInputStream(newSource.getBytes()), IResource.FORCE, null);
            } catch (CoreException e) {
                throw new AutoMigrateException("Auto correct failed to rewrite imports", e);
            }
        }
    }
}