use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class Java50Fix method getRawReference.
private static SimpleType getRawReference(SimpleName name, CompilationUnit compilationUnit) {
SimpleName[] names = LinkedNodeFinder.findByNode(compilationUnit, name);
for (int j = 0; j < names.length; j++) {
if (names[j].getParent() instanceof VariableDeclarationFragment) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) names[j].getParent();
if (fragment.getParent() instanceof VariableDeclarationStatement) {
VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
ASTNode result = (ASTNode) statement.getStructuralProperty(VariableDeclarationStatement.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
} else if (fragment.getParent() instanceof FieldDeclaration) {
FieldDeclaration declaration = (FieldDeclaration) fragment.getParent();
ASTNode result = (ASTNode) declaration.getStructuralProperty(FieldDeclaration.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
}
} else if (names[j].getParent() instanceof SingleVariableDeclaration) {
SingleVariableDeclaration declaration = (SingleVariableDeclaration) names[j].getParent();
ASTNode result = (ASTNode) declaration.getStructuralProperty(SingleVariableDeclaration.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
} else if (names[j].getParent() instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) names[j].getParent();
ASTNode result = (ASTNode) methodDecl.getStructuralProperty(MethodDeclaration.RETURN_TYPE2_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
}
}
return null;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class VariableDeclarationRewrite method rewriteModifiers.
public static void rewriteModifiers(final FieldDeclaration declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, final ASTRewrite rewrite, final TextEditGroup group) {
final List<VariableDeclarationFragment> fragmentsToChange = Arrays.asList(toChange);
AST ast = declarationNode.getAST();
/*
* Problem: Same declarationNode can be the subject of multiple calls to this method.
* For the 2nd++ calls, the original declarationNode has already been rewritten, and this has to be taken into account.
*
* Assumption:
* - Modifiers for each VariableDeclarationFragment are modified at most once.
*
* Solution:
* - Maintain a map from original VariableDeclarationFragments to their new FieldDeclaration.
* - Original modifiers in declarationNode belong to the first fragment.
* - When a later fragment needs different modifiers, we create a new FieldDeclaration and move all successive fragments into that declaration
* - When a fragment has been moved to a new declaration, make sure we don't create a new move target again, but instead use the already created one
*/
List<VariableDeclarationFragment> fragments = declarationNode.fragments();
Iterator<VariableDeclarationFragment> iter = fragments.iterator();
ListRewrite blockRewrite;
if (declarationNode.getParent() instanceof AbstractTypeDeclaration) {
blockRewrite = rewrite.getListRewrite(declarationNode.getParent(), ((AbstractTypeDeclaration) declarationNode.getParent()).getBodyDeclarationsProperty());
} else {
blockRewrite = rewrite.getListRewrite(declarationNode.getParent(), AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
}
VariableDeclarationFragment lastFragment = iter.next();
ASTNode lastStatement = declarationNode;
if (fragmentsToChange.contains(lastFragment)) {
ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, declarationNode);
modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group);
}
ListRewrite fragmentsRewrite = null;
while (iter.hasNext()) {
VariableDeclarationFragment currentFragment = iter.next();
@SuppressWarnings("unchecked") Map<VariableDeclarationFragment, MovedFragment> lookup = (Map<VariableDeclarationFragment, MovedFragment>) rewrite.getProperty(MovedFragment.class.getName());
if (lookup == null) {
lookup = new HashMap<VariableDeclarationFragment, MovedFragment>();
rewrite.setProperty(MovedFragment.class.getName(), lookup);
}
MovedFragment currentMovedFragment = lookup.get(currentFragment);
boolean changeLast = fragmentsToChange.contains(lastFragment);
boolean changeCurrent = fragmentsToChange.contains(currentFragment);
if (changeLast != changeCurrent || lookup.containsKey(lastFragment)) {
ModifierRewrite modifierRewrite = null;
if (currentMovedFragment != null) {
if (currentMovedFragment.fUsesOriginalModifiers) {
// Need to put in the right modifiers (removing any existing ones).
modifierRewrite = ModifierRewrite.create(rewrite, currentMovedFragment.fDeclaration);
ListRewrite listRewrite = rewrite.getListRewrite(currentMovedFragment.fDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY);
List<IExtendedModifier> extendedList = listRewrite.getRewrittenList();
for (int i = 0; i < extendedList.size(); i++) {
ASTNode curr = (ASTNode) extendedList.get(i);
if (curr instanceof Modifier)
rewrite.remove(curr, group);
}
}
// otherwise, don't need to touch the modifiers, so leave modifierRewrite null
} else {
// need to split an existing field declaration
VariableDeclarationFragment moveTarget;
moveTarget = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
FieldDeclaration newStatement = (FieldDeclaration) ast.createInstance(FieldDeclaration.class);
rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY).insertLast(moveTarget, group);
lookup.put(currentFragment, new MovedFragment(moveTarget, newStatement, !changeCurrent));
rewrite.set(newStatement, FieldDeclaration.TYPE_PROPERTY, rewrite.createCopyTarget(declarationNode.getType()), group);
modifierRewrite = ModifierRewrite.create(rewrite, newStatement);
modifierRewrite.copyAllAnnotations(declarationNode, group);
blockRewrite.insertAfter(newStatement, lastStatement, group);
fragmentsRewrite = rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY);
lastStatement = newStatement;
}
if (modifierRewrite != null) {
if (changeCurrent) {
int newModifiers = (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers;
modifierRewrite.setModifiers(newModifiers, excludedModifiers, group);
} else {
int newModifiers = declarationNode.getModifiers();
modifierRewrite.setModifiers(newModifiers, Modifier.NONE, group);
}
}
} else if (fragmentsRewrite != null) {
VariableDeclarationFragment fragment0;
boolean usesOriginalModifiers = true;
if (currentMovedFragment != null) {
fragment0 = currentMovedFragment.fMoveTarget;
usesOriginalModifiers = currentMovedFragment.fUsesOriginalModifiers;
rewrite.getListRewrite(currentMovedFragment.fDeclaration, FieldDeclaration.FRAGMENTS_PROPERTY).remove(fragment0, group);
} else {
fragment0 = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
}
lookup.put(currentFragment, new MovedFragment(fragment0, lastStatement, usesOriginalModifiers));
fragmentsRewrite.insertLast(fragment0, group);
}
lastFragment = currentFragment;
}
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class SelfEncapsulateFieldRefactoring method addGetterSetterChanges.
private List<TextEditGroup> addGetterSetterChanges(CompilationUnit root, ASTRewrite rewriter, String lineDelimiter, boolean usingLocalSetter, boolean usingLocalGetter) throws CoreException {
List<TextEditGroup> result = new ArrayList<TextEditGroup>(2);
AST ast = root.getAST();
FieldDeclaration decl = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, ASTNode.FIELD_DECLARATION);
int position = 0;
int numberOfMethods = 0;
List<BodyDeclaration> members = ASTNodes.getBodyDeclarations(decl.getParent());
for (Iterator<BodyDeclaration> iter = members.iterator(); iter.hasNext(); ) {
BodyDeclaration element = iter.next();
if (element.getNodeType() == ASTNode.METHOD_DECLARATION) {
if (fInsertionIndex == -1) {
break;
} else if (fInsertionIndex == numberOfMethods) {
position++;
break;
}
numberOfMethods++;
}
position++;
}
TextEditGroup description;
ListRewrite rewrite = fRewriter.getListRewrite(decl.getParent(), getBodyDeclarationsProperty(decl.getParent()));
if (!usingLocalGetter) {
description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_add_getter);
result.add(description);
rewrite.insertAt(createGetterMethod(ast, rewriter, lineDelimiter), position++, description);
}
if (!JdtFlags.isFinal(fField) && !usingLocalSetter) {
description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_add_setter);
result.add(description);
rewrite.insertAt(createSetterMethod(ast, rewriter, lineDelimiter), position, description);
}
if (!JdtFlags.isPrivate(fField))
result.add(makeDeclarationPrivate(rewriter, decl));
return result;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class SelfEncapsulateFieldRefactoring method createGetterMethod.
private MethodDeclaration createGetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException {
FieldDeclaration field = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class);
Type type = field.getType();
MethodDeclaration result = ast.newMethodDeclaration();
result.setName(ast.newSimpleName(fGetterName));
result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
Type returnType = DimensionRewrite.copyTypeAndAddDimensions(type, fFieldDeclaration.extraDimensions(), rewriter);
result.setReturnType2(returnType);
Block block = ast.newBlock();
result.setBody(block);
String body = CodeGeneration.getGetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), lineDelimiter);
if (body != null) {
ASTNode getterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK);
block.statements().add(getterNode);
} else {
ReturnStatement rs = ast.newReturnStatement();
rs.setExpression(ast.newSimpleName(fField.getElementName()));
block.statements().add(rs);
}
if (fGenerateJavadoc) {
String string = CodeGeneration.getGetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), ASTNodes.asString(type), StubUtility.getBaseName(fField), lineDelimiter);
if (string != null) {
Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
result.setJavadoc(javadoc);
}
}
return result;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration 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));
}
}
}
Aggregations