use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class PromoteTempToFieldRefactoring method addLocalDeclarationSplit.
private void addLocalDeclarationSplit(ASTRewrite rewrite) {
VariableDeclarationStatement tempDeclarationStatement = getTempDeclarationStatement();
ASTNode parentStatement = tempDeclarationStatement.getParent();
ListRewrite listRewrite;
if (parentStatement instanceof SwitchStatement) {
listRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY);
} else if (parentStatement instanceof Block) {
listRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY);
} else {
// should not happen. VariableDeclaration's can not be in a control statement body
listRewrite = null;
Assert.isTrue(false);
}
int statementIndex = listRewrite.getOriginalList().indexOf(tempDeclarationStatement);
Assert.isTrue(statementIndex != -1);
Statement newStatement = createNewAssignmentStatement(rewrite);
List<VariableDeclarationFragment> fragments = tempDeclarationStatement.fragments();
int fragmentIndex = fragments.indexOf(fTempDeclarationNode);
Assert.isTrue(fragmentIndex != -1);
if (fragments.size() == 1) {
rewrite.replace(tempDeclarationStatement, newStatement, null);
return;
}
for (int i1 = fragmentIndex, n = fragments.size(); i1 < n; i1++) {
VariableDeclarationFragment fragment = fragments.get(i1);
rewrite.remove(fragment, null);
}
if (fragmentIndex == 0)
rewrite.remove(tempDeclarationStatement, null);
Assert.isTrue(tempHasInitializer());
listRewrite.insertAt(newStatement, statementIndex + 1, null);
if (fragmentIndex + 1 < fragments.size()) {
VariableDeclarationFragment firstFragmentAfter = fragments.get(fragmentIndex + 1);
VariableDeclarationFragment copyfirstFragmentAfter = (VariableDeclarationFragment) rewrite.createCopyTarget(firstFragmentAfter);
VariableDeclarationStatement statement = getAST().newVariableDeclarationStatement(copyfirstFragmentAfter);
Type type = (Type) rewrite.createCopyTarget(tempDeclarationStatement.getType());
statement.setType(type);
List<IExtendedModifier> modifiers = tempDeclarationStatement.modifiers();
if (modifiers.size() > 0) {
ListRewrite modifiersRewrite = rewrite.getListRewrite(tempDeclarationStatement, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
ASTNode firstModifier = (ASTNode) modifiers.get(0);
ASTNode lastModifier = (ASTNode) modifiers.get(modifiers.size() - 1);
ASTNode modifiersCopy = modifiersRewrite.createCopyTarget(firstModifier, lastModifier);
statement.modifiers().add(modifiersCopy);
}
for (int i = fragmentIndex + 2; i < fragments.size(); i++) {
VariableDeclarationFragment fragment = fragments.get(i);
VariableDeclarationFragment fragmentCopy = (VariableDeclarationFragment) rewrite.createCopyTarget(fragment);
statement.fragments().add(fragmentCopy);
}
listRewrite.insertAt(statement, statementIndex + 2, null);
}
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class ExtractTempRefactoring method getForInitializedVariables.
// return List<IVariableBinding>
private static List<IVariableBinding> getForInitializedVariables(VariableDeclarationExpression variableDeclarations) {
List<IVariableBinding> forInitializerVariables = new ArrayList<IVariableBinding>(1);
for (Iterator<VariableDeclarationFragment> iter = variableDeclarations.fragments().iterator(); iter.hasNext(); ) {
VariableDeclarationFragment fragment = iter.next();
IVariableBinding binding = fragment.resolveBinding();
if (binding != null)
forInitializerVariables.add(binding);
}
return forInitializerVariables;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class InlineConstantRefactoring method findDeclaration.
private RefactoringStatus findDeclaration() throws JavaModelException {
fDeclarationSelectedChecked = true;
fDeclarationSelected = false;
ASTNode parent = fSelectedConstantName.getParent();
if (parent instanceof VariableDeclarationFragment) {
VariableDeclarationFragment parentDeclaration = (VariableDeclarationFragment) parent;
if (parentDeclaration.getName() == fSelectedConstantName) {
fDeclarationSelected = true;
fDeclarationCuRewrite = fSelectionCuRewrite;
fDeclaration = (VariableDeclarationFragment) fSelectedConstantName.getParent();
return null;
}
}
VariableDeclarationFragment declaration = (VariableDeclarationFragment) fSelectionCuRewrite.getRoot().findDeclaringNode(fSelectedConstantName.resolveBinding());
if (declaration != null) {
fDeclarationCuRewrite = fSelectionCuRewrite;
fDeclaration = declaration;
return null;
}
if (fField.getCompilationUnit() == null)
return RefactoringStatus.createStatus(RefactoringStatus.FATAL, RefactoringCoreMessages.InlineConstantRefactoring_binary_file, null, Corext.getPluginId(), RefactoringStatusCodes.DECLARED_IN_CLASSFILE, null);
fDeclarationCuRewrite = new CompilationUnitRewrite(fField.getCompilationUnit());
fDeclaration = ASTNodeSearchUtil.getFieldDeclarationFragmentNode(fField, fDeclarationCuRewrite.getRoot());
return null;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class JavaTextSelection method resolveInVariableInitializer.
public boolean resolveInVariableInitializer() {
if (fInVariableInitializerRequested)
return fInVariableInitializer;
fInVariableInitializerRequested = true;
resolveSelectedNodes();
ASTNode node = getStartNode();
ASTNode last = null;
while (node != null) {
int nodeType = node.getNodeType();
if (node instanceof AbstractTypeDeclaration) {
fInVariableInitializer = false;
break;
} else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
fInVariableInitializer = false;
break;
} else if (nodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT && ((VariableDeclarationFragment) node).getInitializer() == last) {
fInVariableInitializer = true;
break;
} else if (nodeType == ASTNode.SINGLE_VARIABLE_DECLARATION && ((SingleVariableDeclaration) node).getInitializer() == last) {
fInVariableInitializer = true;
break;
} else if (nodeType == ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION && ((AnnotationTypeMemberDeclaration) node).getDefault() == last) {
fInVariableInitializer = true;
break;
}
last = node;
node = node.getParent();
}
return fInVariableInitializer;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class InferTypeArgumentsConstraintCreator method endVisitFieldVariableDeclaration.
private void endVisitFieldVariableDeclaration(Type type, List<VariableDeclarationFragment> list) {
ConstraintVariable2 typeCv = getConstraintVariable(type);
if (typeCv == null)
return;
for (Iterator<VariableDeclarationFragment> iter = list.iterator(); iter.hasNext(); ) {
VariableDeclarationFragment fragment = iter.next();
ConstraintVariable2 fragmentCv = getConstraintVariable(fragment);
fTCModel.createElementEqualsConstraints(typeCv, fragmentCv);
}
}
Aggregations