use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class UpdateSetRatherThanTestingFirstRefactoring method visit.
@Override
public boolean visit(IfStatement node) {
final Statement elseStmt = node.getElseStatement();
final Statement thenStmt = node.getThenStatement();
final PrefixExpression pe = as(node.getExpression(), PrefixExpression.class);
if (hasOperator(pe, NOT)) {
return maybeReplaceSetContains(node, pe.getOperand(), thenStmt, elseStmt, false);
} else {
return maybeReplaceSetContains(node, node.getExpression(), elseStmt, thenStmt, true);
}
}
use of org.eclipse.jdt.core.dom.Statement in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final SwitchStatement node) {
this.appendLineWrapToBuffer();
this.appendToBuffer("switch (");
node.getExpression().accept(this);
this.appendToBuffer(") ");
this.appendToBuffer("{");
this.increaseIndent();
final Function2<Map<SwitchCase, ArrayList<Statement>>, Statement, Map<SwitchCase, ArrayList<Statement>>> _function = (Map<SwitchCase, ArrayList<Statement>> map, Statement currStatement) -> {
if ((currStatement instanceof SwitchCase)) {
map.put(((SwitchCase) currStatement), CollectionLiterals.<Statement>newArrayList());
} else {
map.get(IterableExtensions.<SwitchCase>last(map.keySet())).add(currStatement);
}
return map;
};
final Map<SwitchCase, ArrayList<Statement>> foldedCases = IterableExtensions.<Statement, Map<SwitchCase, ArrayList<Statement>>>fold(node.statements(), CollectionLiterals.<SwitchCase, ArrayList<Statement>>newLinkedHashMap(), _function);
final BiConsumer<SwitchCase, ArrayList<Statement>> _function_1 = (SwitchCase switchCase, ArrayList<Statement> statements) -> {
switchCase.accept(this);
final boolean isLastCase = switchCase.equals(IterableExtensions.<SwitchCase>last(foldedCases.keySet()));
if ((statements.isEmpty() && (!isLastCase))) {
this.appendToBuffer(",");
} else {
this.appendToBuffer(":");
final boolean probablyReturns = ((IterableExtensions.<Statement>last(statements) instanceof ReturnStatement) || ((IterableExtensions.<Statement>last(statements) instanceof Block) && (IterableExtensions.<Object>last(((Block) IterableExtensions.<Statement>last(statements)).statements()) instanceof ReturnStatement)));
if (((!isLastCase) && (!probablyReturns))) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("/* FIXME unsupported fall-through */");
this.appendToBuffer(_builder.toString());
this.addProblem(node, "Unsupported fall-through case in switch expression");
}
}
final boolean surround = ((isLastCase && statements.isEmpty()) || ((!statements.isEmpty()) && (!(statements.get(0) instanceof Block))));
if (surround) {
this.appendToBuffer("{");
this.increaseIndent();
this.appendLineWrapToBuffer();
}
this.visitAll(statements);
if (surround) {
this.decreaseIndent();
this.appendLineWrapToBuffer();
this.appendToBuffer("}");
}
};
foldedCases.forEach(_function_1);
this.decreaseIndent();
this.appendLineWrapToBuffer();
this.appendToBuffer("}");
return false;
}
use of org.eclipse.jdt.core.dom.Statement in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final IfStatement node) {
this.appendToBuffer("if (");
node.getExpression().accept(this);
this.appendToBuffer(") ");
node.getThenStatement().accept(this);
Statement _elseStatement = node.getElseStatement();
boolean _tripleNotEquals = (_elseStatement != null);
if (_tripleNotEquals) {
this.appendToBuffer(" else ");
node.getElseStatement().accept(this);
}
return false;
}
use of org.eclipse.jdt.core.dom.Statement in project xtext-xtend by eclipse.
the class JavaCodeAnalyzer method determinateJavaType.
public JavaCodeAnalyzer.JavaParseResult<? extends ASTNode> determinateJavaType(final String javaCode) {
ASTParser parser = this.parserFactory.createDefaultJavaParser(this.parserFactory.minParserApiLevel);
parser.setSource(javaCode.toCharArray());
parser.setStatementsRecovery(true);
ASTNode _createAST = parser.createAST(null);
CompilationUnit unit = ((CompilationUnit) _createAST);
int _size = unit.types().size();
boolean _greaterThan = (_size > 0);
if (_greaterThan) {
List<CompilationUnit> _singletonList = Collections.<CompilationUnit>singletonList(unit);
return new JavaCodeAnalyzer.JavaParseResult<CompilationUnit>(javaCode, ASTParser.K_COMPILATION_UNIT, _singletonList);
}
parser.setSource(javaCode.toCharArray());
parser.setStatementsRecovery(false);
parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
ASTNode root = parser.createAST(null);
if ((root instanceof TypeDeclaration)) {
List<BodyDeclaration> bodyDeclarations = ((TypeDeclaration) root).bodyDeclarations();
int _size_1 = bodyDeclarations.size();
boolean _greaterThan_1 = (_size_1 > 0);
if (_greaterThan_1) {
return new JavaCodeAnalyzer.JavaParseResult<BodyDeclaration>(javaCode, ASTParser.K_CLASS_BODY_DECLARATIONS, bodyDeclarations);
}
}
parser.setSource(javaCode.toCharArray());
parser.setStatementsRecovery(false);
parser.setKind(ASTParser.K_STATEMENTS);
root = parser.createAST(null);
if ((root instanceof Block)) {
List<Statement> statements = ((Block) root).statements();
int _size_2 = statements.size();
boolean _greaterThan_2 = (_size_2 > 0);
if (_greaterThan_2) {
return new JavaCodeAnalyzer.JavaParseResult<Statement>(javaCode, ASTParser.K_STATEMENTS, statements);
}
}
parser.setSource(javaCode.toCharArray());
parser.setStatementsRecovery(false);
parser.setKind(ASTParser.K_EXPRESSION);
root = parser.createAST(null);
if ((root instanceof Expression)) {
List<Expression> _singletonList_1 = Collections.<Expression>singletonList(((Expression) root));
return new JavaCodeAnalyzer.JavaParseResult<Expression>(javaCode, ASTParser.K_EXPRESSION, _singletonList_1);
}
return null;
}
use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.
the class AllInOneMethodRatherThanLoopRefactoring method visit.
@Override
public boolean visit(ForStatement node) {
final ForLoopContent loopContent = iterateOverContainer(node);
final List<Statement> stmts = asList(node.getBody());
if (loopContent != null && loopContent.getLoopVariable() != null && stmts.size() == 1) {
final SimpleName loopVariable = (SimpleName) loopContent.getLoopVariable();
final IVariableBinding loopVariableName = (IVariableBinding) loopVariable.resolveBinding();
// As we replace only one, there should be no more than one occurrence
if (getVariableUseCount(loopVariableName, node.getBody()) == 1) {
final MethodInvocation mi = asExpression(stmts.get(0), MethodInvocation.class);
switch(loopContent.getContainerType()) {
case COLLECTION:
if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object")) {
return maybeReplaceWithCollectionMethod(node, loopContent, "addAll", mi);
} else if (isMethod(mi, "java.util.Collection", "remove", "java.lang.Object")) {
return maybeReplaceWithCollectionMethod(node, loopContent, "removeAll", mi);
}
break;
case ARRAY:
if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object") && areTypeCompatible(mi.getExpression(), loopContent.getContainerVariable())) {
final Expression addArg0 = arg0(mi);
final ArrayAccess aa = as(addArg0, ArrayAccess.class);
if (isSameVariable(loopContent, aa)) {
replaceWithCollectionsAddAll(node, loopContent.getContainerVariable(), mi);
return DO_NOT_VISIT_SUBTREE;
}
}
break;
}
}
}
return VISIT_SUBTREE;
}
Aggregations