use of org.eclipse.jdt.core.dom.IExtendedModifier 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.IExtendedModifier in project che by eclipse.
the class TypeContextChecker method appendModifiers.
private static void appendModifiers(StringBuffer buf, List<IExtendedModifier> modifiers) {
for (Iterator<IExtendedModifier> iterator = modifiers.iterator(); iterator.hasNext(); ) {
IExtendedModifier extendedModifier = iterator.next();
if (extendedModifier.isModifier()) {
Modifier modifier = (Modifier) extendedModifier;
buf.append(modifier.getKeyword().toString()).append(' ');
}
}
}
use of org.eclipse.jdt.core.dom.IExtendedModifier in project che by eclipse.
the class IntroduceIndirectionRefactoring method createIntermediaryMethod.
private void createIntermediaryMethod() throws CoreException {
CompilationUnitRewrite imRewrite = getCachedCURewrite(fIntermediaryType.getCompilationUnit());
AST ast = imRewrite.getAST();
MethodDeclaration intermediary = ast.newMethodDeclaration();
// Intermediary class is non-anonymous
AbstractTypeDeclaration type = (AbstractTypeDeclaration) typeToDeclaration(fIntermediaryType, imRewrite.getRoot());
// Name
intermediary.setName(ast.newSimpleName(fIntermediaryMethodName));
// Flags
List<IExtendedModifier> modifiers = intermediary.modifiers();
if (!fIntermediaryType.isInterface()) {
modifiers.add(imRewrite.getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD));
}
modifiers.add(imRewrite.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD));
// Parameters
String targetParameterName = StubUtility.suggestArgumentName(getProject(), fIntermediaryFirstParameterType.getName(), fTargetMethod.getParameterNames());
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(type, imRewrite.getImportRewrite());
if (!isStaticTarget()) {
// Add first param
SingleVariableDeclaration parameter = imRewrite.getAST().newSingleVariableDeclaration();
Type t = imRewrite.getImportRewrite().addImport(fIntermediaryFirstParameterType, imRewrite.getAST(), context);
if (fIntermediaryFirstParameterType.isGenericType()) {
ParameterizedType parameterized = imRewrite.getAST().newParameterizedType(t);
ITypeBinding[] typeParameters = fIntermediaryFirstParameterType.getTypeParameters();
for (int i = 0; i < typeParameters.length; i++) parameterized.typeArguments().add(imRewrite.getImportRewrite().addImport(typeParameters[i], imRewrite.getAST()));
t = parameterized;
}
parameter.setType(t);
parameter.setName(imRewrite.getAST().newSimpleName(targetParameterName));
intermediary.parameters().add(parameter);
}
// Add other params
copyArguments(intermediary, imRewrite);
// Add type parameters of declaring type (and enclosing types)
if (!isStaticTarget() && fIntermediaryFirstParameterType.isGenericType())
addTypeParameters(imRewrite, intermediary.typeParameters(), fIntermediaryFirstParameterType);
// Add type params of method
copyTypeParameters(intermediary, imRewrite);
// Return type
intermediary.setReturnType2(imRewrite.getImportRewrite().addImport(fTargetMethodBinding.getReturnType(), ast, context));
// Exceptions
copyExceptions(intermediary, imRewrite);
// Body
MethodInvocation invocation = imRewrite.getAST().newMethodInvocation();
invocation.setName(imRewrite.getAST().newSimpleName(fTargetMethod.getElementName()));
if (isStaticTarget()) {
Type importedType = imRewrite.getImportRewrite().addImport(fTargetMethodBinding.getDeclaringClass(), ast, context);
invocation.setExpression(ASTNodeFactory.newName(ast, ASTNodes.asString(importedType)));
} else {
invocation.setExpression(imRewrite.getAST().newSimpleName(targetParameterName));
}
copyInvocationParameters(invocation, ast);
Statement call = encapsulateInvocation(intermediary, invocation);
final Block body = imRewrite.getAST().newBlock();
body.statements().add(call);
intermediary.setBody(body);
// method comment
ICompilationUnit targetCU = imRewrite.getCu();
if (StubUtility.doAddComments(targetCU.getJavaProject())) {
String comment = CodeGeneration.getMethodComment(targetCU, getIntermediaryTypeName(), intermediary, null, StubUtility.getLineDelimiterUsed(targetCU));
if (comment != null) {
Javadoc javadoc = (Javadoc) imRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
intermediary.setJavadoc(javadoc);
}
}
// Add the completed method to the intermediary type:
ChildListPropertyDescriptor typeBodyDeclarationsProperty = typeToBodyDeclarationProperty(fIntermediaryType, imRewrite.getRoot());
ListRewrite bodyDeclarationsListRewrite = imRewrite.getASTRewrite().getListRewrite(type, typeBodyDeclarationsProperty);
bodyDeclarationsListRewrite.insertAt(intermediary, ASTNodes.getInsertionIndex(intermediary, type.bodyDeclarations()), imRewrite.createGroupDescription(RefactoringCoreMessages.IntroduceIndirectionRefactoring_group_description_create_new_method));
}
use of org.eclipse.jdt.core.dom.IExtendedModifier in project che by eclipse.
the class ExtractConstantRefactoring method getReplacementScope.
/*
* Elements returned by next() are BodyDeclaration or Annotation instances.
*/
private Iterator<ASTNode> getReplacementScope() throws JavaModelException {
boolean declPredecessorReached = false;
Collection<ASTNode> scope = new ArrayList<ASTNode>();
AbstractTypeDeclaration containingType = getContainingTypeDeclarationNode();
if (containingType instanceof EnumDeclaration) {
// replace in all enum constants bodies
EnumDeclaration enumDeclaration = (EnumDeclaration) containingType;
scope.addAll(enumDeclaration.enumConstants());
}
for (Iterator<IExtendedModifier> iter = containingType.modifiers().iterator(); iter.hasNext(); ) {
IExtendedModifier modifier = iter.next();
if (modifier instanceof Annotation) {
scope.add((ASTNode) modifier);
}
}
for (Iterator<BodyDeclaration> bodyDeclarations = containingType.bodyDeclarations().iterator(); bodyDeclarations.hasNext(); ) {
BodyDeclaration bodyDeclaration = bodyDeclarations.next();
if (bodyDeclaration == getNodeToInsertConstantDeclarationAfter())
declPredecessorReached = true;
if (insertFirst() || declPredecessorReached || !isStaticFieldOrStaticInitializer(bodyDeclaration))
scope.add(bodyDeclaration);
}
return scope.iterator();
}
use of org.eclipse.jdt.core.dom.IExtendedModifier 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);
}
}
Aggregations