use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class SelfEncapsulateFieldRefactoring method makeDeclarationPrivate.
private TextEditGroup makeDeclarationPrivate(ASTRewrite rewriter, FieldDeclaration decl) {
TextEditGroup description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_change_visibility);
VariableDeclarationFragment[] vdfs = new VariableDeclarationFragment[] { fFieldDeclaration };
int includedModifiers = Modifier.PRIVATE;
int excludedModifiers = Modifier.PROTECTED | Modifier.PUBLIC;
VariableDeclarationRewrite.rewriteModifiers(decl, vdfs, includedModifiers, excludedModifiers, rewriter, description);
return description;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method updateAndMoveBodyDeclarations.
private void updateAndMoveBodyDeclarations(CompilationUnitRewrite rewriter, IVariableBinding[] bindings, String[] fieldNames, List<BodyDeclaration> newBodyDeclarations, MethodDeclaration newConstructorDecl) {
final ASTRewrite astRewrite = rewriter.getASTRewrite();
final AST ast = astRewrite.getAST();
final boolean useThisAccess = useThisForFieldAccess();
int fieldInsertIndex = newConstructorDecl != null ? newBodyDeclarations.lastIndexOf(newConstructorDecl) : newBodyDeclarations.size();
for (Iterator<BodyDeclaration> iterator = fAnonymousInnerClassNode.bodyDeclarations().iterator(); iterator.hasNext(); ) {
BodyDeclaration body = iterator.next();
for (int i = 0; i < bindings.length; i++) {
SimpleName[] names = LinkedNodeFinder.findByBinding(body, bindings[i]);
String fieldName = fieldNames[i];
for (int k = 0; k < names.length; k++) {
SimpleName newNode = ast.newSimpleName(fieldName);
if (useThisAccess) {
FieldAccess access = ast.newFieldAccess();
access.setExpression(ast.newThisExpression());
access.setName(newNode);
astRewrite.replace(names[k], access, null);
} else {
astRewrite.replace(names[k], newNode, null);
}
addLinkedPosition(KEY_FIELD_NAME_EXT + i, newNode, astRewrite, false);
}
}
if (body instanceof Initializer || body instanceof FieldDeclaration) {
newBodyDeclarations.add(fieldInsertIndex++, (BodyDeclaration) astRewrite.createMoveTarget(body));
} else {
newBodyDeclarations.add((BodyDeclaration) astRewrite.createMoveTarget(body));
}
}
if (newConstructorDecl != null) {
// move initialization of existing fields to constructor if an outer is referenced
List<Statement> bodyStatements = newConstructorDecl.getBody().statements();
List<VariableDeclarationFragment> fieldsToInitializeInConstructor = getFieldsToInitializeInConstructor();
for (Iterator<VariableDeclarationFragment> iter = fieldsToInitializeInConstructor.iterator(); iter.hasNext(); ) {
VariableDeclarationFragment fragment = iter.next();
Expression initializer = fragment.getInitializer();
Expression replacement = (Expression) astRewrite.get(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY);
if (replacement == initializer) {
replacement = (Expression) astRewrite.createMoveTarget(initializer);
}
astRewrite.remove(initializer, null);
SimpleName fieldNameNode = ast.newSimpleName(fragment.getName().getIdentifier());
bodyStatements.add(newFieldAssignment(ast, fieldNameNode, replacement, useThisAccess));
}
}
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class CallInliner method computeRealArguments.
private void computeRealArguments() {
List<Expression> arguments = Invocations.getArguments(fInvocation);
Set<Expression> canNotInline = crossCheckArguments(arguments);
boolean needsVarargBoxing = needsVarargBoxing(arguments);
int varargIndex = fSourceProvider.getVarargIndex();
AST ast = fInvocation.getAST();
Expression[] realArguments = new Expression[needsVarargBoxing ? varargIndex + 1 : arguments.size()];
for (int i = 0; i < (needsVarargBoxing ? varargIndex : arguments.size()); i++) {
Expression expression = arguments.get(i);
ParameterData parameter = fSourceProvider.getParameterData(i);
if (canInline(expression, parameter) && !canNotInline.contains(expression)) {
realArguments[i] = expression;
} else {
String name = fInvocationScope.createName(parameter.getName(), true);
realArguments[i] = ast.newSimpleName(name);
VariableDeclarationStatement local = createLocalDeclaration(parameter.getTypeBinding(), name, (Expression) fRewrite.createCopyTarget(expression));
if (parameter.isFinal()) {
local.modifiers().add(fInvocation.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD));
}
fLocals.add(local);
}
}
if (needsVarargBoxing) {
ParameterData parameter = fSourceProvider.getParameterData(varargIndex);
String name = fInvocationScope.createName(parameter.getName(), true);
realArguments[varargIndex] = ast.newSimpleName(name);
Type type = fImportRewrite.addImport(parameter.getTypeBinding(), ast);
VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
fragment.setName(ast.newSimpleName(name));
ArrayInitializer initializer = ast.newArrayInitializer();
for (int i = varargIndex; i < arguments.size(); i++) {
initializer.expressions().add(fRewrite.createCopyTarget(arguments.get(i)));
}
fragment.setInitializer(initializer);
VariableDeclarationStatement decl = ast.newVariableDeclarationStatement(fragment);
decl.setType(type);
fLocals.add(decl);
}
fContext.compilationUnit = fCUnit;
fContext.arguments = realArguments;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class CallInliner method createLocalDeclaration.
private VariableDeclarationStatement createLocalDeclaration(ITypeBinding type, String name, Expression initializer) {
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fTargetNode, fImportRewrite);
String typeName = fImportRewrite.addImport(type, context);
VariableDeclarationStatement decl = (VariableDeclarationStatement) ASTNodeFactory.newStatement(fInvocation.getAST(), //$NON-NLS-1$ //$NON-NLS-2$
typeName + " " + name + ";");
((VariableDeclarationFragment) decl.fragments().get(0)).setInitializer(initializer);
return decl;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method createFieldsForAccessedLocals.
private void createFieldsForAccessedLocals(CompilationUnitRewrite rewrite, IVariableBinding[] varBindings, String[] fieldNames, List<BodyDeclaration> newBodyDeclarations) throws CoreException {
final ImportRewrite importRewrite = rewrite.getImportRewrite();
final ASTRewrite astRewrite = rewrite.getASTRewrite();
final AST ast = astRewrite.getAST();
for (int i = 0; i < varBindings.length; i++) {
VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
fragment.setInitializer(null);
fragment.setName(ast.newSimpleName(fieldNames[i]));
FieldDeclaration field = ast.newFieldDeclaration(fragment);
ITypeBinding varType = varBindings[i].getType();
field.setType(importRewrite.addImport(varType, ast));
field.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.FINAL));
if (doAddComments()) {
String string = CodeGeneration.getFieldComment(rewrite.getCu(), varType.getName(), fieldNames[i], StubUtility.getLineDelimiterUsed(fCu));
if (string != null) {
Javadoc javadoc = (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
field.setJavadoc(javadoc);
}
}
newBodyDeclarations.add(field);
addLinkedPosition(KEY_FIELD_NAME_EXT + i, fragment.getName(), astRewrite, false);
}
}
Aggregations