use of org.eclipse.jdt.core.dom.VariableDeclarationExpression in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final Modifier it) {
boolean append = true;
int _flagValue = it.getKeyword().toFlagValue();
switch(_flagValue) {
case Modifier.PUBLIC:
if (((it.getParent() instanceof TypeDeclaration) || (it.getParent() instanceof MethodDeclaration))) {
append = false;
}
break;
case Modifier.PRIVATE:
ASTNode _parent = it.getParent();
if ((_parent instanceof FieldDeclaration)) {
append = false;
}
break;
case Modifier.FINAL:
if (((it.getParent() instanceof VariableDeclarationExpression) || (it.getParent() instanceof VariableDeclarationStatement))) {
append = false;
}
break;
default:
append = true;
break;
}
if (append) {
String valueToAppend = it.getKeyword().toString();
int _flagValue_1 = it.getKeyword().toFlagValue();
boolean _equals = (_flagValue_1 == 0);
if (_equals) {
valueToAppend = "package";
}
this.appendToBuffer(valueToAppend);
this.appendSpaceToBuffer();
}
return false;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationExpression in project AutoRefactor by JnRouvignac.
the class ReduceVariableScopeRefactoring method replace.
private void replace(VariableAccess varDecl, VariableAccess varAccess) {
final ASTBuilder b = this.ctx.getASTBuilder();
final AST ast = b.getAST();
final ASTNode scope = varAccess.getScope();
final Name varName = varAccess.getVariableName();
final Type varType = getType(varDecl.getVariableName().getParent());
if (scope instanceof Block) {
final List<Statement> stmts = statements((Block) scope);
for (int i = 0; i < stmts.size(); i++) {
final Statement stmt = stmts.get(i);
// FIXME i=0
final Expression parentExpr = getAncestor(varName, Expression.class);
// FIXME i=0
final Statement parentStmt = getAncestor(parentExpr, Statement.class);
if (stmt.equals(parentStmt)) {
final VariableDeclarationFragment vdf = getVariableDeclarationFragment(parentExpr, varName);
final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
vds.setType(varType);
this.ctx.getRefactorings().replace(stmt, vds);
break;
}
}
} else if (scope instanceof EnhancedForStatement) {
final EnhancedForStatement efs = (EnhancedForStatement) scope;
final EnhancedForStatement newEfs = b.copy(efs);
newEfs.setParameter(b.copy(efs.getParameter()));
newEfs.setExpression(b.copy(efs.getExpression()));
final Statement parentStmt = getAncestor(varName, Statement.class);
if (equalNotNull(efs.getBody(), parentStmt)) {
newEfs.setBody(copy(efs.getBody(), varName));
}
this.ctx.getRefactorings().replace(efs, newEfs);
} else if (scope instanceof ForStatement) {
final ForStatement fs = (ForStatement) scope;
final ForStatement newFs = b.copy(fs);
final List<Expression> initializers = initializers(newFs);
if (initializers.size() == 1) {
final Expression init = initializers.remove(0);
final VariableDeclarationFragment vdf = getVariableDeclarationFragment(init, varName);
final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(vdf);
vde.setType(varType);
initializers.add(vde);
this.ctx.getRefactorings().replace(fs, newFs);
// TODO JNR
// if (equalNotNull(fs.getBody(), parentStmt)) {
// newFs.setBody(copy(fs.getBody()));
// }
} else {
throw new NotImplementedException(scope, "for more than one initializer in for loop.");
}
} else if (scope instanceof WhileStatement) {
final WhileStatement ws = (WhileStatement) scope;
final WhileStatement newWs = ast.newWhileStatement();
newWs.setExpression(b.copy(ws.getExpression()));
final Statement parentStmt = getAncestor(varName, Statement.class);
if (equalNotNull(ws.getBody(), parentStmt)) {
newWs.setBody(copy(ws.getBody(), varName));
}
this.ctx.getRefactorings().replace(ws, newWs);
} else if (scope instanceof IfStatement) {
final IfStatement is = (IfStatement) scope;
final IfStatement newIs = ast.newIfStatement();
newIs.setExpression(b.copy(is.getExpression()));
final Statement parentStmt = getAncestor(varName, Statement.class);
if (equalNotNull(is.getThenStatement(), parentStmt)) {
newIs.setThenStatement(copy(is.getThenStatement(), varName));
if (is.getElseStatement() != null) {
newIs.setElseStatement(b.copy(is.getElseStatement()));
}
this.ctx.getRefactorings().replace(is, newIs);
} else if (equalNotNull(is.getElseStatement(), parentStmt)) {
if (is.getThenStatement() != null) {
newIs.setThenStatement(b.copy(is.getThenStatement()));
}
newIs.setElseStatement(copy(is.getElseStatement(), varName));
this.ctx.getRefactorings().replace(is, newIs);
} else {
throw new IllegalStateException(is, "Parent statement should be inside the then or else statement of this if statement: " + is);
}
} else {
throw new NotImplementedException(scope);
}
}
use of org.eclipse.jdt.core.dom.VariableDeclarationExpression in project AutoRefactor by JnRouvignac.
the class ForLoopHelper method decomposeInitializer.
/**
* Decomposes an initializer into a {@link Pair} with the name of the initialized variable
* and the initializing expression.
*
* @param init
* the initializer to decompose
* @return a {@link Pair} with the name of the initialized variable and the initializing
* expression, or {@code null} if the initializer could not be decomposed
*/
public static Pair<Name, Expression> decomposeInitializer(Expression init) {
if (init instanceof VariableDeclarationExpression) {
final VariableDeclarationExpression vde = (VariableDeclarationExpression) init;
final List<VariableDeclarationFragment> fragments = fragments(vde);
if (fragments.size() == 1) {
final VariableDeclarationFragment fragment = fragments.get(0);
return Pair.of((Name) fragment.getName(), fragment.getInitializer());
}
} else if (init instanceof Assignment) {
final Assignment as = (Assignment) init;
if (hasOperator(as, ASSIGN) && as.getLeftHandSide() instanceof Name) {
return Pair.of((Name) as.getLeftHandSide(), as.getRightHandSide());
}
}
return Pair.empty();
}
use of org.eclipse.jdt.core.dom.VariableDeclarationExpression in project AutoRefactor by JnRouvignac.
the class ASTBuilder method declareExpr.
/**
* Builds a new {@link VariableDeclarationExpression} instance.
*
* @param type
* the declared variable type
* @param fragment
* the variable declaration fragment
* @return a new variable declaration expression
*/
public VariableDeclarationExpression declareExpr(Type type, VariableDeclarationFragment fragment) {
final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(fragment);
vde.setType(type);
return vde;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationExpression in project AutoRefactor by JnRouvignac.
the class ASTBuilder method declareExpr.
/**
* Builds a new {@link VariableDeclarationExpression} instance.
*
* @param type
* the type of the variable being declared
* @param varName
* the name of the variable being declared
* @param initializer
* the variable initializer, can be null
* @return a new variable declaration expression
*/
public VariableDeclarationExpression declareExpr(Type type, SimpleName varName, Expression initializer) {
final VariableDeclarationFragment fragment = declareFragment(varName, initializer);
final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(fragment);
modifiers(vde).add(final0());
vde.setType(type);
return vde;
}
Aggregations