use of org.autorefactor.jdt.internal.corext.dom.JavaProjectOptions 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;
}
use of org.autorefactor.jdt.internal.corext.dom.JavaProjectOptions in project AutoRefactor by JnRouvignac.
the class CFGBuilderTest method testCFGBuilder.
@Test
public void testCFGBuilder() throws Exception {
// $NON-NLS-1$
final String sampleName = testName + ".java";
// $NON-NLS-1$
final File javaFile = new File("src/test/java/org/autorefactor/cfg", sampleName);
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(testName + ": sample in java file " + javaFile + " should exist", javaFile.exists());
// $NON-NLS-1$ //$NON-NLS-2$
final File dotFile = new File("src/test/resources/org/autorefactor/cfg", testName + ".dot");
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(testName + ": sample out dot file " + dotFile + " should exist", dotFile.exists());
final String dotSource = readAll(dotFile).trim();
final String javaSource = readAll(javaFile);
// $NON-NLS-1$
final IPackageFragment packageFragment = JavaCoreHelper.getPackageFragment("org.autorefactor.cfg");
final ICompilationUnit cu = packageFragment.createCompilationUnit(sampleName, javaSource, true, null);
cu.getBuffer().setContents(javaSource);
cu.save(null, true);
// $NON-NLS-1$
final JavaProjectOptions options = newJavaProjectOptions(Release.javaSE("1.8"), 4);
@SuppressWarnings("deprecation") final ASTParser parser = ASTParser.newParser(AST.JLS8);
autoRefactorHandlerResetParser(cu, parser, options);
final CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
final CFGBuilder builder = new CFGBuilder(javaSource, options);
final List<CFGBasicBlock> blocks = builder.buildCFG(astRoot);
final CFGBasicBlock block = blocks.get(methodDeclarationNb);
final String actual = new CFGDotPrinter().toDot(block).trim();
// $NON-NLS-1$ //$NON-NLS-2$
final File dotFileOut = new File("src/test/resources/org/autorefactor/cfg", testName + "_out.dot");
writeAll(dotFileOut, actual);
// $NON-NLS-1$
assertEquals(testName + ": wrong output;", dotSource, actual);
}
Aggregations