use of org.autorefactor.util.IllegalStateException in project AutoRefactor by JnRouvignac.
the class BooleanRefactoring method getReturnExpression.
private Expression getReturnExpression(MethodDeclaration md, Expression ifCondition) {
final IMethodBinding methodBinding = md.resolveBinding();
if (methodBinding == null) {
return null;
}
final String qualifiedName = methodBinding.getReturnType().getQualifiedName();
final Expression newE = getExpression(ifCondition, qualifiedName, getBooleanName(md));
if (newE != null) {
return newE;
}
// compilationUnit.java:line number: error message
throw new IllegalStateException(md, "Did not expect any other return type than boolean or java.lang.Boolean for method " + md.getName().getIdentifier() + ", but found " + qualifiedName);
}
use of org.autorefactor.util.IllegalStateException in project AutoRefactor by JnRouvignac.
the class ASTHelper method getEnclosingType.
/**
* Returns the enclosing type of the provided node.
* <p>
* i.e. this returns the most immediate type declaration surrounding the provided node.
*
* @param node the start node
* @return the enclosing type of the provided node, or {@code null}
*/
public static ASTNode getEnclosingType(ASTNode node) {
final Class<?>[] ancestorClasses = { AbstractTypeDeclaration.class, AnonymousClassDeclaration.class };
final ASTNode ancestor = getFirstAncestorOrNull(node, ancestorClasses);
if (ancestor == null) {
throw new IllegalStateException(node, "Could not find any ancestor for " + Arrays.toString(ancestorClasses) + " and node type " + (node != null ? node.getClass().getSimpleName() : null) + " node.toString() " + node);
}
return ancestor;
}
use of org.autorefactor.util.IllegalStateException in project AutoRefactor by JnRouvignac.
the class ApplyRefactoringsJob method applyRefactoring.
/**
* Applies the cleanups provided inside the {@link AggregateASTVisitor} to
* the provided {@link ICompilationUnit}.
*
* @param document the document where the compilation unit comes from
* @param compilationUnit the compilation unit to refactor
* @param refactoring the {@link AggregateASTVisitor} to apply to the
* compilation unit
* @param options the Java project options used to compile the project
* @param monitor the progress monitor of the current job
* @param hasToSave hasToSave
* @return TextEdit
* @throws Exception if any problem occurs
*
* @see <a href=
* "http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Fguide%2Fjdt_api_manip.htm"
* >Eclipse JDT core - Manipulating Java code</a>
* @see <a href=
* "http://help.eclipse.org/indigo/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/workbench_cmd_menus.htm"
* > Eclipse Platform Plug-in Developer Guide > Plugging into the workbench
* > Basic workbench extension points using commands >
* org.eclipse.ui.menus</a>
* @see <a href=
* "http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html"
* >Abstract Syntax Tree > Write it down</a>
*/
public List<TextEdit> applyRefactoring(final IDocument document, final ICompilationUnit compilationUnit, final AggregateASTVisitor refactoring, final JavaProjectOptions options, final SubMonitor monitor, final boolean hasToSave) throws Exception {
// Creation of DOM/AST from a ICompilationUnit
@SuppressWarnings("deprecation") ASTParser parser = ASTParser.newParser(AST.JLS8);
int maxIterations = 100;
int iterationCount = 0;
Set<ASTVisitor> lastLoopVisitors = Collections.emptySet();
int nbLoopsWithSameVisitors = 0;
List<TextEdit> textEdits = new ArrayList<>();
monitor.setWorkRemaining(maxIterations);
CompilationUnit astRoot;
do {
// I did not find any other way to directly modify the AST
// while still keeping the resolved type bindings working.
// Using astRoot.recordModifications() did not work:
// type bindings were lost. Is there a way to recover them?
// FIXME we should find a way to apply all the changes at
// the AST level and refresh the bindings
resetParser(compilationUnit, parser, options);
astRoot = (CompilationUnit) parser.createAST(null);
if (iterationCount > maxIterations) {
// Oops! Something went wrong.
String errorMsg = // $NON-NLS-1$ //$NON-NLS-2$
"An infinite loop has been detected for file " + ASTNodes.getFileName(astRoot) + "." + // $NON-NLS-1$
" A possible cause is that code is being incorrectly" + " refactored one way then refactored back to what it was." + // $NON-NLS-1$ //$NON-NLS-2$
" Fix the code before pursuing." + getPossibleCulprits(nbLoopsWithSameVisitors, lastLoopVisitors);
environment.getLogger().error(errorMsg, new IllegalStateException(astRoot, errorMsg));
break;
}
CompilationUnitRewrite cuRewrite = new CompilationUnitRewrite(compilationUnit, astRoot, options, monitor, environment);
refactoring.setRefactoringContext(cuRewrite);
ASTRewrite refactorings = refactoring.getRefactorings(astRoot);
if (!refactorings.hasRefactorings()) {
// We are done with applying the cleanups.
break;
}
// Apply the cleanups and save the compilation unit
refactorings.applyTo(document, hasToSave);
textEdits.add(refactorings.getEdits());
if (!hasToSave) {
return textEdits;
}
boolean hadUnsavedChanges = compilationUnit.hasUnsavedChanges();
compilationUnit.getBuffer().setContents(document.get());
// , null, null);
if (!hadUnsavedChanges && hasToSave) {
compilationUnit.save(null, true);
}
iterationCount++;
Set<ASTVisitor> thisLoopVisitors = refactoring.getVisitorsContributingRefactoring();
if (thisLoopVisitors.equals(lastLoopVisitors)) {
nbLoopsWithSameVisitors++;
} else {
lastLoopVisitors = new HashSet<>(thisLoopVisitors);
nbLoopsWithSameVisitors = 0;
}
} while (true);
return textEdits;
}
use of org.autorefactor.util.IllegalStateException in project AutoRefactor by JnRouvignac.
the class ASTNodes method getEnclosingType.
/**
* Returns the enclosing type of the provided node.
* <p>
* i.e. this returns the most immediate type declaration surrounding the
* provided node.
*
* @param node the start node
* @return the enclosing type of the provided node, or {@code null}
*/
public static ASTNode getEnclosingType(final ASTNode node) {
Class<? extends ASTNode>[] ancestorClasses = new Class[] { AbstractTypeDeclaration.class, AnonymousClassDeclaration.class };
ASTNode ancestor = getFirstAncestorOrNull(node, ancestorClasses);
if (ancestor == null) {
throw new IllegalStateException(node, // $NON-NLS-1$ //$NON-NLS-2$
"Could not find any ancestor for " + Arrays.toString(ancestorClasses) + " and node type " + (node != null ? node.getClass().getSimpleName() : null) + " node.toString() " + // $NON-NLS-1$
node);
}
return ancestor;
}
Aggregations