use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project che by eclipse.
the class ModifierChangeCorrectionProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fNode);
ASTNode boundNode = astRoot.findDeclaringNode(fBinding);
ASTNode declNode = null;
if (boundNode != null) {
// is same CU
declNode = boundNode;
} else {
//setSelectionDescription(selectionDescription);
CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode = newRoot.findDeclaringNode(fBinding.getKey());
}
if (declNode != null) {
AST ast = declNode.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
if (declNode.getNodeType() == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) declNode;
ASTNode parent = declNode.getParent();
if (parent instanceof FieldDeclaration) {
FieldDeclaration fieldDecl = (FieldDeclaration) parent;
if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) {
// split
VariableDeclarationRewrite.rewriteModifiers(fieldDecl, new VariableDeclarationFragment[] { fragment }, fIncludedModifiers, fExcludedModifiers, rewrite, null);
return rewrite;
}
} else if (parent instanceof VariableDeclarationStatement) {
VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent;
if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) {
// split
VariableDeclarationRewrite.rewriteModifiers(varDecl, new VariableDeclarationFragment[] { fragment }, fIncludedModifiers, fExcludedModifiers, rewrite, null);
return rewrite;
}
} else if (parent instanceof VariableDeclarationExpression) {
// can't separate
}
declNode = parent;
} else if (declNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
MethodDeclaration methodDecl = (MethodDeclaration) declNode;
if (!methodDecl.isConstructor()) {
IMethodBinding methodBinding = methodDecl.resolveBinding();
if (methodDecl.getBody() == null && methodBinding != null && Modifier.isAbstract(methodBinding.getModifiers()) && Modifier.isStatic(fIncludedModifiers)) {
// add body
ICompilationUnit unit = getCompilationUnit();
String delimiter = unit.findRecommendedLineSeparator();
//$NON-NLS-1$
String bodyStatement = "";
Block body = ast.newBlock();
rewrite.set(methodDecl, MethodDeclaration.BODY_PROPERTY, body, null);
Type returnType = methodDecl.getReturnType2();
if (returnType != null) {
Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, methodDecl.getExtraDimensions());
if (expression != null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, unit.getJavaProject().getOptions(true));
}
}
String placeHolder = CodeGeneration.getMethodBodyContent(unit, methodBinding.getDeclaringClass().getName(), methodBinding.getName(), false, bodyStatement, delimiter);
if (placeHolder != null) {
ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
body.statements().add(todoNode);
}
}
}
}
ModifierRewrite listRewrite = ModifierRewrite.create(rewrite, declNode);
PositionInformation trackedDeclNode = listRewrite.setModifiers(fIncludedModifiers, fExcludedModifiers, null);
//$NON-NLS-1$
LinkedProposalPositionGroup positionGroup = new LinkedProposalPositionGroup("group");
positionGroup.addPosition(trackedDeclNode);
getLinkedProposalModel().addPositionGroup(positionGroup);
if (boundNode != null) {
// only set end position if in same CU
setEndPosition(rewrite.track(fNode));
}
return rewrite;
}
return null;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project che by eclipse.
the class ConvertForLoopOperation method createParameterDeclaration.
private SingleVariableDeclaration createParameterDeclaration(String parameterName, VariableDeclarationFragment fragement, Expression arrayAccess, ForStatement statement, ImportRewrite importRewrite, ASTRewrite rewrite, TextEditGroup group, LinkedProposalPositionGroup pg, boolean makeFinal) {
CompilationUnit compilationUnit = (CompilationUnit) arrayAccess.getRoot();
AST ast = compilationUnit.getAST();
SingleVariableDeclaration result = ast.newSingleVariableDeclaration();
SimpleName name = ast.newSimpleName(parameterName);
pg.addPosition(rewrite.track(name), true);
result.setName(name);
ITypeBinding arrayTypeBinding = arrayAccess.resolveTypeBinding();
Type type = importType(arrayTypeBinding.getElementType(), statement, importRewrite, compilationUnit);
if (arrayTypeBinding.getDimensions() != 1) {
type = ast.newArrayType(type, arrayTypeBinding.getDimensions() - 1);
}
result.setType(type);
if (fragement != null) {
VariableDeclarationStatement declaration = (VariableDeclarationStatement) fragement.getParent();
ModifierRewrite.create(rewrite, result).copyAllModifiers(declaration, group);
}
if (makeFinal && (fragement == null || ASTNodes.findModifierNode(Modifier.FINAL, ASTNodes.getModifiers(fragement)) == null)) {
ModifierRewrite.create(rewrite, result).setModifiers(Modifier.FINAL, 0, group);
}
return result;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project che by eclipse.
the class VariableDeclarationFix method createAddFinalOperation.
private static ModifierChangeOperation createAddFinalOperation(SimpleName name, ASTNode decl) {
if (decl == null)
return null;
IBinding binding = name.resolveBinding();
if (!canAddFinal(binding, decl))
return null;
if (decl instanceof SingleVariableDeclaration) {
return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
} else if (decl instanceof VariableDeclarationExpression) {
return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
} else if (decl instanceof VariableDeclarationFragment) {
VariableDeclarationFragment frag = (VariableDeclarationFragment) decl;
decl = decl.getParent();
if (decl instanceof FieldDeclaration || decl instanceof VariableDeclarationStatement) {
List<VariableDeclarationFragment> list = new ArrayList<VariableDeclarationFragment>();
list.add(frag);
return new ModifierChangeOperation(decl, list, Modifier.FINAL, Modifier.NONE);
} else if (decl instanceof VariableDeclarationExpression) {
return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
}
}
return null;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project che by eclipse.
the class CallInliner method initialize.
public RefactoringStatus initialize(ASTNode invocation, int severity) {
RefactoringStatus result = new RefactoringStatus();
fInvocation = invocation;
fLocals = new ArrayList<VariableDeclarationStatement>(3);
checkMethodDeclaration(result, severity);
if (result.getSeverity() >= severity)
return result;
initializeRewriteState();
initializeTargetNode();
flowAnalysis();
fContext = new CallContext(fInvocation, fInvocationScope, fTargetNode.getNodeType(), fImportRewrite);
try {
computeRealArguments();
computeReceiver();
} catch (BadLocationException exception) {
JavaPlugin.log(exception);
}
checkInvocationContext(result, severity);
return result;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationStatement in project che by eclipse.
the class InlineTempRefactoring method removeTemp.
private void removeTemp(CompilationUnitRewrite cuRewrite) {
VariableDeclaration variableDeclaration = getVariableDeclaration();
TextEditGroup groupDesc = cuRewrite.createGroupDescription(RefactoringCoreMessages.InlineTempRefactoring_remove_edit_name);
ASTNode parent = variableDeclaration.getParent();
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
if (parent instanceof VariableDeclarationStatement && ((VariableDeclarationStatement) parent).fragments().size() == 1) {
sourceRangeComputer.addTightSourceNode(parent);
rewrite.remove(parent, groupDesc);
} else {
sourceRangeComputer.addTightSourceNode(variableDeclaration);
rewrite.remove(variableDeclaration, groupDesc);
}
}
Aggregations