use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class ExtractConstantRefactoring method canReplace.
// !! - like one in ExtractTempRefactoring
private static boolean canReplace(IASTFragment fragment) {
ASTNode node = fragment.getAssociatedNode();
ASTNode parent = node.getParent();
if (parent instanceof VariableDeclarationFragment) {
VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
if (node.equals(vdf.getName()))
return false;
}
if (parent instanceof ExpressionStatement)
return false;
if (parent instanceof SwitchCase) {
if (node instanceof Name) {
Name name = (Name) node;
ITypeBinding typeBinding = name.resolveTypeBinding();
if (typeBinding != null) {
return !typeBinding.isEnum();
}
}
}
return true;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class FullConstraintCreator method getConstraintsFromFragmentList.
private ITypeConstraint[] getConstraintsFromFragmentList(List<VariableDeclarationFragment> list, Type type) {
int size = list.size();
ConstraintVariable typeVariable = fConstraintVariableFactory.makeTypeVariable(type);
List<ITypeConstraint> result = new ArrayList<ITypeConstraint>((size * (size - 1)) / 2);
for (int i = 0; i < size; i++) {
VariableDeclarationFragment fragment1 = list.get(i);
SimpleName fragment1Name = fragment1.getName();
result.addAll(Arrays.asList(fTypeConstraintFactory.createDefinesConstraint(fConstraintVariableFactory.makeExpressionOrTypeVariable(fragment1Name, getContext()), typeVariable)));
for (int j = i + 1; j < size; j++) {
VariableDeclarationFragment fragment2 = list.get(j);
result.addAll(Arrays.asList(fTypeConstraintFactory.createEqualsConstraint(fConstraintVariableFactory.makeExpressionOrTypeVariable(fragment1Name, getContext()), fConstraintVariableFactory.makeExpressionOrTypeVariable(fragment2.getName(), getContext()))));
}
}
return result.toArray(new ITypeConstraint[result.size()]);
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class RemoveDeclarationCorrectionProposal method removeVariableReferences.
/**
* Remove the field or variable declaration including the initializer.
* @param rewrite the ast rewrite
* @param reference the reference
*/
private void removeVariableReferences(ASTRewrite rewrite, SimpleName reference) {
ASTNode parent = reference.getParent();
while (parent instanceof QualifiedName) {
parent = parent.getParent();
}
if (parent instanceof FieldAccess) {
parent = parent.getParent();
}
int nameParentType = parent.getNodeType();
if (nameParentType == ASTNode.ASSIGNMENT) {
Assignment assignment = (Assignment) parent;
Expression rightHand = assignment.getRightHandSide();
ASTNode assignParent = assignment.getParent();
if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT && rightHand.getNodeType() != ASTNode.ASSIGNMENT) {
removeVariableWithInitializer(rewrite, rightHand, assignParent);
} else {
rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), null);
}
} else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
rewrite.remove(parent, null);
} else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
ASTNode varDecl = frag.getParent();
List<VariableDeclarationFragment> fragments;
if (varDecl instanceof VariableDeclarationExpression) {
fragments = ((VariableDeclarationExpression) varDecl).fragments();
} else if (varDecl instanceof FieldDeclaration) {
fragments = ((FieldDeclaration) varDecl).fragments();
} else {
fragments = ((VariableDeclarationStatement) varDecl).fragments();
}
if (fragments.size() == 1) {
rewrite.remove(varDecl, null);
} else {
// don't try to preserve
rewrite.remove(frag, null);
}
}
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project generator by mybatis.
the class ExistingJavaFileVisitor method visit.
/**
* Find the generated fields and delete them
*/
@Override
public boolean visit(FieldDeclaration node) {
if (isGenerated(node)) {
List<Annotation> annotations = retrieveAnnotations(node);
if (!annotations.isEmpty()) {
VariableDeclarationFragment variable = (VariableDeclarationFragment) node.fragments().get(0);
fieldAnnotations.put(variable.getName().getIdentifier(), annotations);
}
node.delete();
}
return false;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class InlineTempRefactoring method getModifiedInitializerSource.
private Expression getModifiedInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException {
VariableDeclaration varDecl = getVariableDeclaration();
Expression initializer = varDecl.getInitializer();
ASTNode referenceContext = reference.getParent();
if (Invocations.isResolvedTypeInferredFromExpectedType(initializer)) {
if (!(referenceContext instanceof VariableDeclarationFragment || referenceContext instanceof SingleVariableDeclaration || referenceContext instanceof Assignment)) {
ITypeBinding[] typeArguments = Invocations.getInferredTypeArguments(initializer);
if (typeArguments != null) {
String newSource = createParameterizedInvocation(initializer, typeArguments, rewrite);
return (Expression) rewrite.getASTRewrite().createStringPlaceholder(newSource, initializer.getNodeType());
}
}
}
Expression copy = (Expression) rewrite.getASTRewrite().createCopyTarget(initializer);
AST ast = rewrite.getAST();
if (NecessaryParenthesesChecker.needsParentheses(initializer, reference.getParent(), reference.getLocationInParent())) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(copy);
copy = parenthesized;
}
ITypeBinding explicitCast = ASTNodes.getExplicitCast(initializer, reference);
if (explicitCast != null) {
CastExpression cast = ast.newCastExpression();
if (NecessaryParenthesesChecker.needsParentheses(copy, cast, CastExpression.EXPRESSION_PROPERTY)) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(copy);
copy = parenthesized;
}
cast.setExpression(copy);
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(reference, rewrite.getImportRewrite());
cast.setType(rewrite.getImportRewrite().addImport(explicitCast, ast, context));
copy = cast;
} else if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(varDecl) > 0) {
ArrayType newType = (ArrayType) ASTNodeFactory.newType(ast, varDecl);
ArrayCreation newArrayCreation = ast.newArrayCreation();
newArrayCreation.setType(newType);
newArrayCreation.setInitializer((ArrayInitializer) copy);
return newArrayCreation;
}
return copy;
}
Aggregations