@Override protected void initialize(ITypeBinding binding) { Assert.isTrue(binding.isGenericType()); super.initialize(binding); TypeEnvironment environment = getEnvironment(); ITypeBinding[] typeParameters = binding.getTypeParameters(); fTypeParameters = new TypeVariable[typeParameters.length]; for (int i = 0; i < typeParameters.length; i++) { fTypeParameters[i] = (TypeVariable) environment.create(typeParameters[i]); } }
public static void rewriteModifiers( final VariableDeclarationStatement declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, ASTRewrite rewrite, final TextEditGroup group) { final List<VariableDeclarationFragment> fragmentsToChange = Arrays.asList(toChange); AST ast = declarationNode.getAST(); List<VariableDeclarationFragment> fragments = declarationNode.fragments(); Iterator<VariableDeclarationFragment> iter = fragments.iterator(); ListRewrite blockRewrite = null; ASTNode parentStatement = declarationNode.getParent(); if (parentStatement instanceof SwitchStatement) { blockRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY); } else if (parentStatement instanceof Block) { blockRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY); } else { // should not happen. VariableDeclaration's can not be in a control statement body Assert.isTrue(false); } VariableDeclarationFragment lastFragment = iter.next(); ASTNode lastStatement = declarationNode; boolean modifiersModified = false; if (fragmentsToChange.contains(lastFragment)) { ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, declarationNode); modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group); modifiersModified = true; } ListRewrite fragmentsRewrite = null; while (iter.hasNext()) { VariableDeclarationFragment currentFragment = iter.next(); if (fragmentsToChange.contains(lastFragment) != fragmentsToChange.contains(currentFragment)) { VariableDeclarationStatement newStatement = ast.newVariableDeclarationStatement( (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment)); newStatement.setType((Type) rewrite.createCopyTarget(declarationNode.getType())); ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, newStatement); if (fragmentsToChange.contains(currentFragment)) { modifierRewrite.copyAllAnnotations(declarationNode, group); int newModifiers = (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers; modifierRewrite.setModifiers(newModifiers, excludedModifiers, group); } else { modifierRewrite.copyAllModifiers(declarationNode, group, modifiersModified); } blockRewrite.insertAfter(newStatement, lastStatement, group); fragmentsRewrite = rewrite.getListRewrite(newStatement, VariableDeclarationStatement.FRAGMENTS_PROPERTY); lastStatement = newStatement; } else if (fragmentsRewrite != null) { ASTNode fragment0 = rewrite.createMoveTarget(currentFragment); fragmentsRewrite.insertLast(fragment0, group); } lastFragment = currentFragment; } }
/** * Remove the field or variable declaration including the initializer. * * @param rewrite the AST rewriter to use * @param reference a reference to the variable to remove * @param group the text edit group to use */ private void removeVariableReferences( ASTRewrite rewrite, SimpleName reference, TextEditGroup group) { 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, group); } else { rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), group); } } else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) { rewrite.remove(parent, group); } 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(); } Expression initializer = frag.getInitializer(); if (initializer instanceof CastExpression) { initializer = ((CastExpression) initializer).getExpression(); } boolean sideEffectInitializer = initializer instanceof MethodInvocation || initializer instanceof ClassInstanceCreation; if (fragments.size() == fUnusedNames.length) { if (fForceRemove) { rewrite.remove(varDecl, group); return; } if (parent.getParent() instanceof FieldDeclaration) { rewrite.remove(varDecl, group); return; } if (sideEffectInitializer) { Expression movedInit = (Expression) rewrite.createMoveTarget(initializer); ExpressionStatement wrapped = rewrite.getAST().newExpressionStatement(movedInit); rewrite.replace(varDecl, wrapped, group); } else { rewrite.remove(varDecl, group); } } else { if (fForceRemove) { rewrite.remove(frag, group); return; } // multiple declarations in one line ASTNode declaration = parent.getParent(); if (declaration instanceof FieldDeclaration) { rewrite.remove(frag, group); return; } if (declaration instanceof VariableDeclarationStatement) { ASTNode lst = declaration.getParent(); ListRewrite listRewrite = null; if (lst instanceof Block) { listRewrite = rewrite.getListRewrite(lst, Block.STATEMENTS_PROPERTY); } else if (lst instanceof SwitchStatement) { listRewrite = rewrite.getListRewrite(lst, SwitchStatement.STATEMENTS_PROPERTY); } else { Assert.isTrue(false); } splitUpDeclarations( rewrite, group, frag, listRewrite, (VariableDeclarationStatement) declaration); rewrite.remove(frag, group); return; } if (declaration instanceof VariableDeclarationExpression) { // keep constructors and method invocations if (!sideEffectInitializer) { rewrite.remove(frag, group); } } } } else if (nameParentType == ASTNode.POSTFIX_EXPRESSION || nameParentType == ASTNode.PREFIX_EXPRESSION) { Expression expression = (Expression) parent; ASTNode expressionParent = expression.getParent(); if (expressionParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT) { removeStatement(rewrite, expressionParent, group); } else { rewrite.remove(expression, group); } } }