Search in sources :

Example 1 with ApplyRefactoringsJob

use of org.autorefactor.jdt.internal.corext.dom.ApplyRefactoringsJob in project AutoRefactor by JnRouvignac.

the class AutoRefactorFix method createCleanUp.

/**
 * Create the clean up.
 *
 * @param compilationUnit compilation unit
 * @param enabled         enabled
 * @param fOptions        options
 * @return Clean up fix
 */
public static ICleanUpFix createCleanUp(final CompilationUnit compilationUnit, final boolean enabled, final CleanUpOptions fOptions) {
    boolean hasChanges = false;
    final ICompilationUnit iCompilationUnit = (ICompilationUnit) compilationUnit.getJavaElement();
    // $NON-NLS-1$
    final CleanUpChange cleanUpChange = new CleanUpChange("AutoRefactor", iCompilationUnit);
    TextEdit allEdits = null;
    if (enabled) {
        final IJavaProject javaProject = PrepareApplyRefactoringsJob.getIJavaProject(iCompilationUnit);
        final JavaProjectOptions options = new JavaProjectOptionsImpl(javaProject.getOptions(true));
        final Environment environment = AutoRefactorPlugin.getEnvironment();
        final List<RefactoringRule> refactoringRules = getConfiguredRefactoringRules(fOptions);
        final SubMonitor loopMonitor = SubMonitor.convert(null, 1);
        final Queue<RefactoringUnit> refactoringUnits = new ConcurrentLinkedQueue<>();
        refactoringUnits.add(new RefactoringUnit(iCompilationUnit, options));
        final ApplyRefactoringsJob applyRefactoringsJob = new ApplyRefactoringsJob(refactoringUnits, refactoringRules, environment);
        final AggregateASTVisitor visitor = new AggregateASTVisitor(refactoringRules);
        try {
            List<TextEdit> textEdits = applyRefactoringsJob.applyRefactoring(iCompilationUnit, visitor, options, loopMonitor, false);
            for (TextEdit textEdit : textEdits) {
                if (hasChanges) {
                    allEdits = TextEditUtil.merge(allEdits, textEdit);
                } else {
                    hasChanges = true;
                    allEdits = textEdit;
                }
            }
        } catch (Exception e) {
            if (!hasChanges) {
                return null;
            }
        }
    }
    if (!hasChanges) {
        return null;
    }
    cleanUpChange.setEdit(allEdits);
    AutoRefactorFix autoRefactorFix = new AutoRefactorFix();
    autoRefactorFix.cleanUpChange = cleanUpChange;
    return autoRefactorFix;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaProjectOptions(org.autorefactor.jdt.internal.corext.dom.JavaProjectOptions) SubMonitor(org.eclipse.core.runtime.SubMonitor) RefactoringUnit(org.autorefactor.jdt.internal.corext.dom.RefactoringUnit) CoreException(org.eclipse.core.runtime.CoreException) JavaProjectOptionsImpl(org.autorefactor.jdt.internal.corext.dom.JavaProjectOptionsImpl) ApplyRefactoringsJob(org.autorefactor.jdt.internal.corext.dom.ApplyRefactoringsJob) PrepareApplyRefactoringsJob(org.autorefactor.jdt.internal.corext.dom.PrepareApplyRefactoringsJob) RefactoringRule(org.autorefactor.jdt.internal.corext.dom.RefactoringRule) IJavaProject(org.eclipse.jdt.core.IJavaProject) TextEdit(org.eclipse.text.edits.TextEdit) CleanUpChange(org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring.CleanUpChange) Environment(org.autorefactor.environment.Environment) AggregateASTVisitor(org.autorefactor.jdt.internal.ui.fix.AggregateASTVisitor) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 2 with ApplyRefactoringsJob

use of org.autorefactor.jdt.internal.corext.dom.ApplyRefactoringsJob in project AutoRefactor by JnRouvignac.

the class CleanUpTest method when.

private IDocument when(final String sampleInSource) throws Exception, JavaModelException {
    final IPackageFragment packageFragment = JavaCoreHelper.getPackageFragment(PACKAGE_NAME);
    final ICompilationUnit cu = packageFragment.createCompilationUnit(sampleName, sampleInSource, true, null);
    cu.getBuffer().setContents(sampleInSource);
    cu.save(null, true);
    final IDocument doc = new Document(sampleInSource);
    new ApplyRefactoringsJob(null, null, TEST_ENVIRONMENT).applyRefactoring(doc, cu, new AggregateASTVisitor(AllCleanUpRules.getAllCleanUpRules()), newJavaProjectOptions(Release.javaSE("1.8.0"), 4), SubMonitor.convert(new NullProgressMonitor()), // $NON-NLS-1$
    true);
    return doc;
}
Also used : ApplyRefactoringsJob(org.autorefactor.jdt.internal.corext.dom.ApplyRefactoringsJob) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AggregateASTVisitor(org.autorefactor.jdt.internal.ui.fix.AggregateASTVisitor) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument)

Example 3 with ApplyRefactoringsJob

use of org.autorefactor.jdt.internal.corext.dom.ApplyRefactoringsJob in project AutoRefactor by JnRouvignac.

the class RefactoringRulesTest method when.

private IDocument when(final String sampleName, final RefactoringRule refactoring, final String sampleInSource) throws Exception {
    IPackageFragment packageFragment = JavaCoreHelper.getPackageFragment(PACKAGE_NAME);
    ICompilationUnit cu = packageFragment.createCompilationUnit(sampleName, sampleInSource, true, null);
    cu.getBuffer().setContents(sampleInSource);
    cu.save(null, true);
    IDocument doc = new Document(sampleInSource);
    new ApplyRefactoringsJob(null, null, TEST_ENVIRONMENT).applyRefactoring(doc, cu, // $NON-NLS-1$
    new AggregateASTVisitor(Arrays.asList(refactoring)), // $NON-NLS-1$
    newJavaProjectOptions(Release.javaSE("1.8.0"), 4), SubMonitor.convert(new NullProgressMonitor()), true);
    return doc;
}
Also used : ApplyRefactoringsJob(org.autorefactor.jdt.internal.corext.dom.ApplyRefactoringsJob) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AggregateASTVisitor(org.autorefactor.jdt.internal.ui.fix.AggregateASTVisitor) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

ApplyRefactoringsJob (org.autorefactor.jdt.internal.corext.dom.ApplyRefactoringsJob)3 AggregateASTVisitor (org.autorefactor.jdt.internal.ui.fix.AggregateASTVisitor)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 Document (org.eclipse.jface.text.Document)2 IDocument (org.eclipse.jface.text.IDocument)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 Environment (org.autorefactor.environment.Environment)1 JavaProjectOptions (org.autorefactor.jdt.internal.corext.dom.JavaProjectOptions)1 JavaProjectOptionsImpl (org.autorefactor.jdt.internal.corext.dom.JavaProjectOptionsImpl)1 PrepareApplyRefactoringsJob (org.autorefactor.jdt.internal.corext.dom.PrepareApplyRefactoringsJob)1 RefactoringRule (org.autorefactor.jdt.internal.corext.dom.RefactoringRule)1 RefactoringUnit (org.autorefactor.jdt.internal.corext.dom.RefactoringUnit)1 CoreException (org.eclipse.core.runtime.CoreException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 CleanUpChange (org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring.CleanUpChange)1 TextEdit (org.eclipse.text.edits.TextEdit)1