private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) { if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) { // e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation ... return false; } if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY || locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.MESSAGE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == SwitchStatement.EXPRESSION_PROPERTY || locationInParent == SwitchCase.EXPRESSION_PROPERTY || locationInParent == ArrayAccess.INDEX_PROPERTY || locationInParent == ThrowStatement.EXPRESSION_PROPERTY || locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY || locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) { return false; } return true; }
public static ChildListPropertyDescriptor getArgumentsProperty(ASTNode invocation) { switch (invocation.getNodeType()) { case ASTNode.METHOD_INVOCATION: return MethodInvocation.ARGUMENTS_PROPERTY; case ASTNode.SUPER_METHOD_INVOCATION: return SuperMethodInvocation.ARGUMENTS_PROPERTY; case ASTNode.CONSTRUCTOR_INVOCATION: return ConstructorInvocation.ARGUMENTS_PROPERTY; case ASTNode.SUPER_CONSTRUCTOR_INVOCATION: return SuperConstructorInvocation.ARGUMENTS_PROPERTY; case ASTNode.CLASS_INSTANCE_CREATION: return ClassInstanceCreation.ARGUMENTS_PROPERTY; case ASTNode.ENUM_CONSTANT_DECLARATION: return EnumConstantDeclaration.ARGUMENTS_PROPERTY; default: throw new IllegalArgumentException(invocation.toString()); } }
protected void handleManyMany(ASTNode[] replacements, TextEditGroup description) { ListRewrite container= fRewrite.getListRewrite(fToReplace[0].getParent(), (ChildListPropertyDescriptor)fDescriptor); if (fToReplace.length == replacements.length) { for (int i= 0; i < fToReplace.length; i++) { container.replace(fToReplace[i], replacements[i], description); } } else if (fToReplace.length < replacements.length) { for (int i= 0; i < fToReplace.length; i++) { container.replace(fToReplace[i], replacements[i], description); } for (int i= fToReplace.length; i < replacements.length; i++) { container.insertAfter(replacements[i], replacements[i - 1], description); } } else if (fToReplace.length > replacements.length) { int delta= fToReplace.length - replacements.length; for(int i= 0; i < delta; i++) { container.remove(fToReplace[i], description); } for (int i= delta, r= 0; i < fToReplace.length; i++, r++) { container.replace(fToReplace[i], replacements[r], description); } } }
@Override protected void handleOneMany(ASTNode[] replacements, TextEditGroup description) { AST ast= fToReplace[0].getAST(); // to replace == 1, but more than one replacement. Have to check if we // need to insert a block to not change structure if (ASTNodes.isControlStatementBody(fDescriptor)) { Block block= ast.newBlock(); ListRewrite statements= fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY); for (int i= 0; i < replacements.length; i++) { statements.insertLast(replacements[i], description); } fRewrite.replace(fToReplace[0], block, description); } else { ListRewrite container= fRewrite.getListRewrite(fToReplace[0].getParent(), (ChildListPropertyDescriptor)fDescriptor); container.replace(fToReplace[0], replacements[0], description); for (int i= 1; i < replacements.length; i++) { container.insertAfter(replacements[i], replacements[i - 1], description); } } }
/** * Removes all {@link Annotation} whose only {@link Target} is {@link ElementType#TYPE_USE} from * <code>node</code>'s <code>childListProperty</code>. * <p> * In a combination of {@link ElementType#TYPE_USE} and {@link ElementType#TYPE_PARAMETER} * the latter is ignored, because this is implied by the former and creates no ambiguity.</p> * * @param node ASTNode * @param childListProperty child list property * @param rewrite rewrite that removes the nodes * @param editGroup the edit group in which to collect the corresponding text edits, or null if * ungrouped */ public static void removePureTypeAnnotations(ASTNode node, ChildListPropertyDescriptor childListProperty, ASTRewrite rewrite, TextEditGroup editGroup) { CompilationUnit root= (CompilationUnit) node.getRoot(); if (!JavaModelUtil.is18OrHigher(root.getJavaElement().getJavaProject())) { return; } ListRewrite listRewrite= rewrite.getListRewrite(node, childListProperty); @SuppressWarnings("unchecked") List<? extends ASTNode> children= (List<? extends ASTNode>) node.getStructuralProperty(childListProperty); for (ASTNode child : children) { if (child instanceof Annotation) { Annotation annotation= (Annotation) child; if (isPureTypeAnnotation(annotation)) { listRewrite.remove(child, editGroup); } } } }
private void splitUpDeclarations(ASTRewrite rewrite, TextEditGroup group, VariableDeclarationFragment frag, VariableDeclarationStatement originalStatement, List<Expression> sideEffects) { if (sideEffects.size() > 0) { ListRewrite statementRewrite= rewrite.getListRewrite(originalStatement.getParent(), (ChildListPropertyDescriptor) originalStatement.getLocationInParent()); Statement previousStatement= originalStatement; for (int i= 0; i < sideEffects.size(); i++) { Expression sideEffect= sideEffects.get(i); Expression movedInit= (Expression) rewrite.createMoveTarget(sideEffect); ExpressionStatement wrapped= rewrite.getAST().newExpressionStatement(movedInit); statementRewrite.insertAfter(wrapped, previousStatement, group); previousStatement= wrapped; } VariableDeclarationStatement newDeclaration= null; List<VariableDeclarationFragment> fragments= originalStatement.fragments(); int fragIndex= fragments.indexOf(frag); ListIterator<VariableDeclarationFragment> fragmentIterator= fragments.listIterator(fragIndex+1); while (fragmentIterator.hasNext()) { VariableDeclarationFragment currentFragment= fragmentIterator.next(); VariableDeclarationFragment movedFragment= (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment); if (newDeclaration == null) { newDeclaration= rewrite.getAST().newVariableDeclarationStatement(movedFragment); Type copiedType= (Type) rewrite.createCopyTarget(originalStatement.getType()); newDeclaration.setType(copiedType); } else { newDeclaration.fragments().add(movedFragment); } } if (newDeclaration != null){ statementRewrite.insertAfter(newDeclaration, previousStatement, group); if (originalStatement.fragments().size() == newDeclaration.fragments().size() + 1){ rewrite.remove(originalStatement, group); } } } }
protected void handleManyMany(ASTNode[] replacements, TextEditGroup description) { ListRewrite container = fRewrite.getListRewrite( fToReplace[0].getParent(), (ChildListPropertyDescriptor) fDescriptor); if (fToReplace.length == replacements.length) { for (int i = 0; i < fToReplace.length; i++) { container.replace(fToReplace[i], replacements[i], description); } } else if (fToReplace.length < replacements.length) { for (int i = 0; i < fToReplace.length; i++) { container.replace(fToReplace[i], replacements[i], description); } for (int i = fToReplace.length; i < replacements.length; i++) { container.insertAfter(replacements[i], replacements[i - 1], description); } } else if (fToReplace.length > replacements.length) { int delta = fToReplace.length - replacements.length; for (int i = 0; i < delta; i++) { container.remove(fToReplace[i], description); } for (int i = delta, r = 0; i < fToReplace.length; i++, r++) { container.replace(fToReplace[i], replacements[r], description); } } }
@Override protected void handleOneMany(ASTNode[] replacements, TextEditGroup description) { AST ast = fToReplace[0].getAST(); // to replace == 1, but more than one replacement. Have to check if we // need to insert a block to not change structure if (ASTNodes.isControlStatementBody(fDescriptor)) { Block block = ast.newBlock(); ListRewrite statements = fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY); for (int i = 0; i < replacements.length; i++) { statements.insertLast(replacements[i], description); } fRewrite.replace(fToReplace[0], block, description); } else { ListRewrite container = fRewrite.getListRewrite( fToReplace[0].getParent(), (ChildListPropertyDescriptor) fDescriptor); container.replace(fToReplace[0], replacements[0], description); for (int i = 1; i < replacements.length; i++) { container.insertAfter(replacements[i], replacements[i - 1], description); } } }
private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) { if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) { // e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation // ... return false; } if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY || locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.MESSAGE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == SwitchStatement.EXPRESSION_PROPERTY || locationInParent == SwitchCase.EXPRESSION_PROPERTY || locationInParent == ArrayAccess.INDEX_PROPERTY || locationInParent == ThrowStatement.EXPRESSION_PROPERTY || locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY || locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) { return false; } return true; }
@Override protected ASTRewrite getRewrite() { AST ast = fCallerNode.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); ChildListPropertyDescriptor property = getProperty(); for (int i = 0; i < fInsertIndexes.length; i++) { int idx = fInsertIndexes[i]; String key = "newarg_" + i; // $NON-NLS-1$ Expression newArg = evaluateArgumentExpressions(ast, fParamTypes[idx], key); ListRewrite listRewriter = rewrite.getListRewrite(fCallerNode, property); listRewriter.insertAt(newArg, idx, null); addLinkedPosition(rewrite.track(newArg), i == 0, key); } return rewrite; }
private ASTRewrite doAddField(CompilationUnit astRoot) { SimpleName node = fOriginalNode; boolean isInDifferentCU = false; ASTNode newTypeDecl = astRoot.findDeclaringNode(fSenderBinding); if (newTypeDecl == null) { astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null); newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey()); isInDifferentCU = true; } ImportRewrite imports = createImportRewrite(astRoot); ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext( ASTResolving.findParentBodyDeclaration(node), imports); if (newTypeDecl != null) { AST ast = newTypeDecl.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment(); fragment.setName(ast.newSimpleName(node.getIdentifier())); Type type = evaluateVariableType(ast, imports, importRewriteContext, fSenderBinding); FieldDeclaration newDecl = ast.newFieldDeclaration(fragment); newDecl.setType(type); newDecl .modifiers() .addAll(ASTNodeFactory.newModifiers(ast, evaluateFieldModifiers(newTypeDecl))); if (fSenderBinding.isInterface() || fVariableKind == CONST_FIELD) { fragment.setInitializer(ASTNodeFactory.newDefaultExpression(ast, type, 0)); } ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl); List<BodyDeclaration> decls = ASTNodes.<BodyDeclaration>getChildListProperty(newTypeDecl, property); int maxOffset = isInDifferentCU ? -1 : node.getStartPosition(); int insertIndex = findFieldInsertIndex(decls, newDecl, maxOffset); ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, property); listRewriter.insertAt(newDecl, insertIndex, null); ModifierCorrectionSubProcessor.installLinkedVisibilityProposals( getLinkedProposalModel(), rewrite, newDecl.modifiers(), fSenderBinding.isInterface()); addLinkedPosition(rewrite.track(newDecl.getType()), false, KEY_TYPE); if (!isInDifferentCU) { addLinkedPosition(rewrite.track(node), true, KEY_NAME); } addLinkedPosition(rewrite.track(fragment.getName()), false, KEY_NAME); if (fragment.getInitializer() != null) { addLinkedPosition(rewrite.track(fragment.getInitializer()), false, KEY_INITIALIZER); } return rewrite; } return null; }
private VariableDeclarationFragment addFieldDeclaration( ASTRewrite rewrite, ASTNode newTypeDecl, int modifiers, Expression expression) { if (fExistingFragment != null) { return fExistingFragment; } ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl); List<BodyDeclaration> decls = ASTNodes.getBodyDeclarations(newTypeDecl); AST ast = newTypeDecl.getAST(); String[] varNames = suggestFieldNames(fTypeBinding, expression, modifiers); for (int i = 0; i < varNames.length; i++) { addLinkedPositionProposal(KEY_NAME, varNames[i], null); } String varName = varNames[0]; VariableDeclarationFragment newDeclFrag = ast.newVariableDeclarationFragment(); newDeclFrag.setName(ast.newSimpleName(varName)); FieldDeclaration newDecl = ast.newFieldDeclaration(newDeclFrag); Type type = evaluateType(ast); newDecl.setType(type); newDecl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, modifiers)); ModifierCorrectionSubProcessor.installLinkedVisibilityProposals( getLinkedProposalModel(), rewrite, newDecl.modifiers(), false); int insertIndex = findFieldInsertIndex(decls, fNodeToAssign.getStartPosition()); rewrite.getListRewrite(newTypeDecl, property).insertAt(newDecl, insertIndex, null); return newDeclFrag; }
@Override protected ASTRewrite getRewrite() { CompilationUnit targetAstRoot = ASTResolving.createQuickFixAST( getCompilationUnit(), null); createImportRewrite(targetAstRoot); // Find the target type declaration TypeDeclaration typeDecl = JavaASTUtils.findTypeDeclaration(targetAstRoot, targetQualifiedTypeName); if (typeDecl == null) { return null; } ASTRewrite rewrite = ASTRewrite.create(targetAstRoot.getAST()); // Generate the new method declaration MethodDeclaration methodDecl = createMethodDeclaration(rewrite.getAST()); // Add the new method declaration to the interface ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(typeDecl); ListRewrite listRewriter = rewrite.getListRewrite(typeDecl, property); listRewriter.insertLast(methodDecl, null); return rewrite; }
@Override protected ASTRewrite getRewrite() { AST ast= fCallerNode.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); ChildListPropertyDescriptor property= getProperty(); for (int i= 0; i < fInsertIndexes.length; i++) { int idx= fInsertIndexes[i]; String key= "newarg_" + i; //$NON-NLS-1$ Expression newArg= evaluateArgumentExpressions(ast, fParamTypes[idx], key); ListRewrite listRewriter= rewrite.getListRewrite(fCallerNode, property); listRewriter.insertAt(newArg, idx, null); addLinkedPosition(rewrite.track(newArg), i == 0, key); } return rewrite; }
public static ChildListPropertyDescriptor getThrownExceptionsPropertyDescriptor(MethodDeclaration node) { List<?> propertyDescriptors = MethodDeclaration.propertyDescriptors(node.getAST().apiLevel()); for (Object propertyDescriptor : propertyDescriptors) { if (propertyDescriptor instanceof ChildListPropertyDescriptor) { ChildListPropertyDescriptor childListPropertyDescriptor = (ChildListPropertyDescriptor) propertyDescriptor; if (DESCRIPTOR_NAME_AST4.equals(childListPropertyDescriptor.getId())) { if (OptimusLogger.logger.isLoggable(Level.FINE)) { OptimusLogger.logger.fine(String.format(DESCRIPTOR_RETURNED_MESSAGE, DESCRIPTOR_NAME_AST4)); } return childListPropertyDescriptor; } else if (DESCRIPTOR_NAME_AST8.equals(childListPropertyDescriptor.getId())) { if (OptimusLogger.logger.isLoggable(Level.FINE)) { OptimusLogger.logger.fine(String.format(DESCRIPTOR_RETURNED_MESSAGE, DESCRIPTOR_NAME_AST4)); } return childListPropertyDescriptor; } } } return null; }
protected ReplaceRewrite(ASTRewrite rewrite, ASTNode[] nodes) { Assert.isNotNull(rewrite); Assert.isNotNull(nodes); Assert.isTrue(nodes.length > 0); fRewrite= rewrite; fToReplace= nodes; fDescriptor= fToReplace[0].getLocationInParent(); if (nodes.length > 1) { Assert.isTrue(fDescriptor instanceof ChildListPropertyDescriptor); } }
/** * Returns the list that contains the given ASTNode. If the node * isn't part of any list, <code>null</code> is returned. * * @param node the node in question * @return the list that contains the node or <code>null</code> */ public static List<? extends ASTNode> getContainingList(ASTNode node) { StructuralPropertyDescriptor locationInParent= node.getLocationInParent(); if (locationInParent != null && locationInParent.isChildListProperty()) { return getChildListProperty(node.getParent(), (ChildListPropertyDescriptor) locationInParent); } return null; }
/** * Returns the structural property descriptor for the "bodyDeclarations" property * of this node (element type: {@link BodyDeclaration}). * * @param node the node, either an {@link AbstractTypeDeclaration} or an {@link AnonymousClassDeclaration} * @return the property descriptor */ public static ChildListPropertyDescriptor getBodyDeclarationsProperty(ASTNode node) { if (node instanceof AbstractTypeDeclaration) { return ((AbstractTypeDeclaration)node).getBodyDeclarationsProperty(); } else if (node instanceof AnonymousClassDeclaration) { return AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY; } // should not happen. Assert.isTrue(false); return null; }
/** * Removes all children in <code>node</code>'s <code>childListProperty</code>. * * @param node ASTNode * @param childListProperty child list property * @param rewrite rewrite that removes the nodes * @param editGroup the edit group in which to collect the corresponding text edits, or null if ungrouped */ public static void removeAllChildren(ASTNode node, ChildListPropertyDescriptor childListProperty, ASTRewrite rewrite, TextEditGroup editGroup) { ListRewrite listRewrite= rewrite.getListRewrite(node, childListProperty); @SuppressWarnings("unchecked") List<? extends ASTNode> children= (List<? extends ASTNode>) node.getStructuralProperty(childListProperty); for (ASTNode child : children) { listRewrite.remove(child, editGroup); } }
@Override protected ASTRewrite getRewrite() throws CoreException { CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fNode); ASTNode typeDecl = astRoot.findDeclaringNode(fSenderBinding); ASTNode newTypeDecl = null; if (typeDecl != null) { newTypeDecl = typeDecl; } else { astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null); newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey()); } createImportRewrite(astRoot); if (newTypeDecl != null) { ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl); List<BodyDeclaration> members = ASTNodes.getBodyDeclarations(newTypeDecl); int insertIndex = members.size(); ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, property); isGetter = true; MethodDeclaration newStub = getStub(rewrite, newTypeDecl); listRewriter.insertAt(newStub, insertIndex, null); isGetter = false; newStub = getStub(rewrite, newTypeDecl); listRewriter.insertAt(newStub, insertIndex + 1, null); return rewrite; } return null; }
@Override protected ASTRewrite getRewrite() { AST ast= fCallerNode.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); ChildListPropertyDescriptor property= getProperty(); for (int i= 0; i < fInsertIndexes.length; i++) { int idx= fInsertIndexes[i]; String key= "newarg_" + i; //$NON-NLS-1$ Expression newArg= evaluateArgumentExpressions(ast, fParamTypes[idx], key); ListRewrite listRewriter= rewrite.getListRewrite(fCallerNode, property); listRewriter.insertAt(newArg, idx, null); } return rewrite; }
private ChildListPropertyDescriptor getProperty() { List<StructuralPropertyDescriptor> list= fCallerNode.structuralPropertiesForType(); for (int i= 0; i < list.size(); i++) { StructuralPropertyDescriptor curr= list.get(i); if (curr.isChildListProperty() && "arguments".equals(curr.getId())) { //$NON-NLS-1$ return (ChildListPropertyDescriptor) curr; } } return null; }
private ASTRewrite doAddField(CompilationUnit astRoot) { SimpleName node= fOriginalNode; boolean isInDifferentCU= false; ASTNode newTypeDecl= astRoot.findDeclaringNode(fSenderBinding); if (newTypeDecl == null) { astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null); newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey()); isInDifferentCU= true; } ImportRewrite imports= createImportRewrite(astRoot); ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(node), imports); if (newTypeDecl != null) { AST ast= newTypeDecl.getAST(); ASTRewrite rewrite= ASTRewrite.create(ast); VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment(); fragment.setName(ast.newSimpleName(node.getIdentifier())); Type type= evaluateVariableType(ast, imports, importRewriteContext, fSenderBinding, TypeLocation.FIELD); FieldDeclaration newDecl= ast.newFieldDeclaration(fragment); newDecl.setType(type); newDecl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, evaluateFieldModifiers(newTypeDecl))); if (fSenderBinding.isInterface() || fVariableKind == CONST_FIELD) { fragment.setInitializer(ASTNodeFactory.newDefaultExpression(ast, type, 0)); } ChildListPropertyDescriptor property= ASTNodes.getBodyDeclarationsProperty(newTypeDecl); List<BodyDeclaration> decls= ASTNodes.<BodyDeclaration>getChildListProperty(newTypeDecl, property); int maxOffset= isInDifferentCU ? -1 : node.getStartPosition(); int insertIndex= findFieldInsertIndex(decls, newDecl, maxOffset); ListRewrite listRewriter= rewrite.getListRewrite(newTypeDecl, property); listRewriter.insertAt(newDecl, insertIndex, null); return rewrite; } return null; }
@Override protected ASTRewrite getRewrite() throws CoreException { CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fNode); ASTNode typeDecl= astRoot.findDeclaringNode(fSenderBinding); ASTNode newTypeDecl= null; boolean isInDifferentCU; if (typeDecl != null) { isInDifferentCU= false; newTypeDecl= typeDecl; } else { isInDifferentCU= true; astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null); newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey()); } createImportRewrite(astRoot); if (newTypeDecl != null) { ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); MethodDeclaration newStub= getStub(rewrite, newTypeDecl); ChildListPropertyDescriptor property= ASTNodes.getBodyDeclarationsProperty(newTypeDecl); List<BodyDeclaration> members= ASTNodes.getBodyDeclarations(newTypeDecl); int insertIndex; if (isConstructor()) { insertIndex= findConstructorInsertIndex(members); } else if (!isInDifferentCU) { insertIndex= findMethodInsertIndex(members, fNode.getStartPosition()); } else { insertIndex= members.size(); } ListRewrite listRewriter= rewrite.getListRewrite(newTypeDecl, property); listRewriter.insertAt(newStub, insertIndex, null); return rewrite; } return null; }
@Override /** * Annotate local variables by visiting the enclosing statement. * * Handles both VariableDeclarationStatement and VariableDeclarationExpression */ public boolean visit(VariableDeclarationFragment varDecl) { ASTNode parent = varDecl.getParent(); Type declaredType = null; List parentMods = null; if(parent instanceof VariableDeclarationStatement ) { VariableDeclarationStatement varDeclStmt = (VariableDeclarationStatement)parent; parent = varDeclStmt; declaredType = varDeclStmt.getType(); parentMods = varDeclStmt.modifiers(); } else if (parent instanceof VariableDeclarationExpression ) { VariableDeclarationExpression varDeclExpr = (VariableDeclarationExpression)parent; parent = varDeclExpr; declaredType = varDeclExpr.getType(); // Not allowed to call varExpr.modifiers(); parentMods = null; } else { parent = null; // We handle FieldDeclaration elsewhere... return false; } ITypeBinding type = declaredType.resolveBinding(); // XXX. Why would type be null here? Need to investigate. if (type != null && !type.isPrimitive()) { String annotation = "lent"; // XXX. Bad hard-coded default. What about <p>? if (HARD_CODE_STATICS && isStaticScope(parent)) { annotation = "shared<shared>"; } else { annotation = strategy.getAnnotationForLocalVariable(varDecl); } ChildListPropertyDescriptor property = parent instanceof VariableDeclarationStatement ? VariableDeclarationStatement.MODIFIERS2_PROPERTY : VariableDeclarationExpression.MODIFIERS2_PROPERTY; SingleMemberAnnotation currentAnnotation = getCurrentAnnotation(parentMods); if (currentAnnotation == null) { setVariableAnnotation2(parent, annotation, property); } else { updateVariableAnnotation2(parent, annotation, currentAnnotation, property); } // XXX. Lookup enclosing type declaration from local variable String srcType = type.getQualifiedName(); String dstType = Utils.getEnclosingTypeName(varDecl); // XXX. Come up with a better key trackChanges(varDecl.toString(), annotation, getValue(currentAnnotation), CSVConst.VARIABLE, srcType, dstType); } return super.visit(varDecl); }
protected ReplaceRewrite(ASTRewrite rewrite, ASTNode[] nodes) { Assert.isNotNull(rewrite); Assert.isNotNull(nodes); Assert.isTrue(nodes.length > 0); fRewrite = rewrite; fToReplace = nodes; fDescriptor = fToReplace[0].getLocationInParent(); if (nodes.length > 1) { Assert.isTrue(fDescriptor instanceof ChildListPropertyDescriptor); } }
/** * Removes all children in <code>node</code>'s <code>childListProperty</code>. * * @param node ASTNode * @param childListProperty child list property * @param rewrite rewrite that removes the nodes * @param editGroup the edit group in which to collect the corresponding text edits, or null if * ungrouped */ public static void removeAllChildren( ASTNode node, ChildListPropertyDescriptor childListProperty, ASTRewrite rewrite, TextEditGroup editGroup) { ListRewrite listRewrite = rewrite.getListRewrite(node, childListProperty); @SuppressWarnings("unchecked") List<? extends ASTNode> children = (List<? extends ASTNode>) node.getStructuralProperty(childListProperty); for (ASTNode child : children) { listRewrite.remove(child, editGroup); } }
private void splitUpDeclarations( ASTRewrite rewrite, TextEditGroup group, VariableDeclarationFragment frag, VariableDeclarationStatement originalStatement, List<Expression> sideEffects) { if (sideEffects.size() > 0) { ListRewrite statementRewrite = rewrite.getListRewrite( originalStatement.getParent(), (ChildListPropertyDescriptor) originalStatement.getLocationInParent()); Statement previousStatement = originalStatement; for (int i = 0; i < sideEffects.size(); i++) { Expression sideEffect = sideEffects.get(i); Expression movedInit = (Expression) rewrite.createMoveTarget(sideEffect); ExpressionStatement wrapped = rewrite.getAST().newExpressionStatement(movedInit); statementRewrite.insertAfter(wrapped, previousStatement, group); previousStatement = wrapped; } VariableDeclarationStatement newDeclaration = null; List<VariableDeclarationFragment> fragments = originalStatement.fragments(); int fragIndex = fragments.indexOf(frag); ListIterator<VariableDeclarationFragment> fragmentIterator = fragments.listIterator(fragIndex + 1); while (fragmentIterator.hasNext()) { VariableDeclarationFragment currentFragment = fragmentIterator.next(); VariableDeclarationFragment movedFragment = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment); if (newDeclaration == null) { newDeclaration = rewrite.getAST().newVariableDeclarationStatement(movedFragment); Type copiedType = (Type) rewrite.createCopyTarget(originalStatement.getType()); newDeclaration.setType(copiedType); } else { newDeclaration.fragments().add(movedFragment); } } if (newDeclaration != null) { statementRewrite.insertAfter(newDeclaration, previousStatement, group); if (originalStatement.fragments().size() == newDeclaration.fragments().size() + 1) { rewrite.remove(originalStatement, group); } } } }
private ChildListPropertyDescriptor getTypeBodyDeclarationsProperty() { ASTNode parent = fDeclaration.getParent(); if (parent instanceof AbstractTypeDeclaration) return ((AbstractTypeDeclaration) parent).getBodyDeclarationsProperty(); else if (parent instanceof AnonymousClassDeclaration) return AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY; Assert.isTrue(false); return null; }