Java Code Examples for org.eclipse.jdt.core.dom.rewrite.ListRewrite#remove()

The following examples show how to use org.eclipse.jdt.core.dom.rewrite.ListRewrite#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MoveMethodRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
private void removeParamTagElementFromJavadoc(MethodDeclaration newMethodDeclaration, ASTRewrite targetRewriter, String parameterToBeRemoved) {
	if(newMethodDeclaration.getJavadoc() != null) {
		Javadoc javadoc = newMethodDeclaration.getJavadoc();
		List<TagElement> tags = javadoc.tags();
		for(TagElement tag : tags) {
			if(tag.getTagName() != null && tag.getTagName().equals(TagElement.TAG_PARAM)) {
				List<ASTNode> tagFragments = tag.fragments();
				boolean paramFound = false;
				for(ASTNode node : tagFragments) {
					if(node instanceof SimpleName) {
						SimpleName simpleName = (SimpleName)node;
						if(simpleName.getIdentifier().equals(parameterToBeRemoved)) {
							paramFound = true;
							break;
						}
					}
				}
				if(paramFound) {
					ListRewrite tagsRewrite = targetRewriter.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
					tagsRewrite.remove(tag, null);
					break;
				}
			}
		}
	}
}
 
Example 2
Source File: ReplaceRewrite.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
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);
		}
	}
}
 
Example 3
Source File: MoveMethodRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
private void removeSourceMethod() {
	ASTRewrite sourceRewriter = ASTRewrite.create(sourceCompilationUnit.getAST());
	ListRewrite classBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	classBodyRewrite.remove(sourceMethod, null);
	Set<MethodDeclaration> methodsToBeMoved = new LinkedHashSet<MethodDeclaration>(additionalMethodsToBeMoved.values());
	for(MethodDeclaration methodDeclaration : methodsToBeMoved) {
		classBodyRewrite.remove(methodDeclaration, null);
	}
	try {
		TextEdit sourceEdit = sourceRewriter.rewriteAST();
		sourceMultiTextEdit.addChild(sourceEdit);
		sourceCompilationUnitChange.addTextEditGroup(new TextEditGroup("Remove moved method", new TextEdit[] {sourceEdit}));
	}
	catch(JavaModelException javaModelException) {
		javaModelException.printStackTrace();
	}
}
 
Example 4
Source File: TypeAnnotationRewrite.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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);
			}
		}
	}
}
 
Example 5
Source File: MoveMethodRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
private void removeAdditionalMethodsFromSourceClass() {
	Set<MethodDeclaration> methodsToBeMoved = new LinkedHashSet<MethodDeclaration>(additionalMethodsToBeMoved.values());
	for(MethodDeclaration methodDeclaration : methodsToBeMoved) {
		ASTRewrite sourceRewriter = ASTRewrite.create(sourceCompilationUnit.getAST());
		ListRewrite sourceClassBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
		sourceClassBodyRewrite.remove(methodDeclaration, null);
		try {
			TextEdit sourceEdit = sourceRewriter.rewriteAST();
			sourceMultiTextEdit.addChild(sourceEdit);
			sourceCompilationUnitChange.addTextEditGroup(new TextEditGroup("Remove additional moved method", new TextEdit[] {sourceEdit}));
		}
		catch(JavaModelException javaModelException) {
			javaModelException.printStackTrace();
		}
	}
}
 
Example 6
Source File: ReplaceRewrite.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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);
		}
	}
}
 
Example 7
Source File: NullAnnotationsRewriteOperations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
boolean checkExisting(List<IExtendedModifier> existingModifiers, ListRewrite listRewrite, TextEditGroup editGroup) {
	for (Object mod : existingModifiers) {
		if (mod instanceof MarkerAnnotation) {
			MarkerAnnotation annotation= (MarkerAnnotation) mod;
			String existingName= annotation.getTypeName().getFullyQualifiedName();
			int lastDot= fAnnotationToRemove.lastIndexOf('.');
			if (existingName.equals(fAnnotationToRemove) || (lastDot != -1 && fAnnotationToRemove.substring(lastDot + 1).equals(existingName))) {
				if (!fAllowRemove)
					return false; // veto this change
				listRewrite.remove(annotation, editGroup);
				return true;
			}
			// paranoia: check if by accident the annotation is already present (shouldn't happen):
			lastDot= fAnnotationToAdd.lastIndexOf('.');
			if (existingName.equals(fAnnotationToAdd) || (lastDot != -1 && fAnnotationToAdd.substring(lastDot + 1).equals(existingName))) {
				return false; // already present
			}
		}
	}
	return true;
}
 
Example 8
Source File: DimensionRewrite.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 9
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 5 votes vote down vote up
private void removePrimitiveStateField() {
	ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST());
	AST contextAST = sourceTypeDeclaration.getAST();
	ListRewrite contextBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	FieldDeclaration[] fieldDeclarations = sourceTypeDeclaration.getFields();
	for(FieldDeclaration fieldDeclaration : fieldDeclarations) {
		List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
		for(VariableDeclarationFragment fragment : fragments) {
			if(fragment.equals(typeCheckElimination.getTypeField())) {
				if(fragments.size() == 1) {
					contextBodyRewrite.remove(fragment.getParent(), null);
				}
				else {
					ListRewrite fragmentRewrite = sourceRewriter.getListRewrite(fragment.getParent(), FieldDeclaration.FRAGMENTS_PROPERTY);
					fragmentRewrite.remove(fragment, null);
				}
			}
		}
	}
	try {
		TextEdit sourceEdit = sourceRewriter.rewriteAST();
		ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
		CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
		change.getEdit().addChild(sourceEdit);
		change.addTextEditGroup(new TextEditGroup("Remove primitive field holding the current state", new TextEdit[] {sourceEdit}));
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
}
 
Example 10
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 5 votes vote down vote up
private void replacePrimitiveStateField() {
	ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST());
	AST contextAST = sourceTypeDeclaration.getAST();
	ListRewrite contextBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	FieldDeclaration[] fieldDeclarations = sourceTypeDeclaration.getFields();
	for(FieldDeclaration fieldDeclaration : fieldDeclarations) {
		List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
		for(VariableDeclarationFragment fragment : fragments) {
			if(fragment.equals(typeCheckElimination.getTypeField())) {
				if(fragments.size() == 1) {
					ListRewrite fragmentsRewriter = sourceRewriter.getListRewrite(fieldDeclaration, FieldDeclaration.FRAGMENTS_PROPERTY);
					fragmentsRewriter.remove(fragment, null);
					VariableDeclarationFragment typeFragment = createStateFieldVariableDeclarationFragment(sourceRewriter, contextAST);
					fragmentsRewriter.insertLast(typeFragment, null);
					sourceRewriter.set(fieldDeclaration, FieldDeclaration.TYPE_PROPERTY, contextAST.newSimpleName(abstractClassName), null);
				}
			}
		}
	}
	try {
		TextEdit sourceEdit = sourceRewriter.rewriteAST();
		ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
		CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
		change.getEdit().addChild(sourceEdit);
		change.addTextEditGroup(new TextEditGroup("Replace primitive type with State type", new TextEdit[] {sourceEdit}));
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
}
 
Example 11
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void removeException(ASTRewrite rewrite, UnionType unionType, Type exception) {
	ListRewrite listRewrite= rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY);
	List<Type> types= unionType.types();
	for (Iterator<Type> iterator= types.iterator(); iterator.hasNext();) {
		Type type= iterator.next();
		if (type.equals(exception)) {
			listRewrite.remove(type, null);
		}
	}
}
 
Example 12
Source File: TypeParametersFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {
	TextEditGroup group= createTextEditGroup(FixMessages.TypeParametersFix_remove_redundant_type_arguments_description, cuRewrite);

	ASTRewrite rewrite= cuRewrite.getASTRewrite();
	rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());

	ListRewrite listRewrite= rewrite.getListRewrite(fParameterizedType, ParameterizedType.TYPE_ARGUMENTS_PROPERTY);

	List<Type> typeArguments= fParameterizedType.typeArguments();
	for (Iterator<Type> iterator= typeArguments.iterator(); iterator.hasNext();) {
		listRewrite.remove(iterator.next(), group);
	}
}
 
Example 13
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates the necessary changes to remove method type parameters if they
 * match with enclosing type parameters.
 *
 * @param rewrite
 *            the ast rewrite to use
 * @param declaration
 *            the method declaration to remove type parameters
 * @param status
 *            the refactoring status
 */
protected void createMethodTypeParameters(final ASTRewrite rewrite, final MethodDeclaration declaration, final RefactoringStatus status) {
	ITypeBinding binding= fTarget.getType();
	if (binding != null && binding.isParameterizedType()) {
		final IMethodBinding method= declaration.resolveBinding();
		if (method != null) {
			final ITypeBinding[] parameters= method.getTypeParameters();
			if (parameters.length > 0) {
				final ListRewrite rewriter= rewrite.getListRewrite(declaration, MethodDeclaration.TYPE_PARAMETERS_PROPERTY);
				boolean foundStatic= false;
				while (binding != null && !foundStatic) {
					if (Flags.isStatic(binding.getModifiers()))
						foundStatic= true;
					final ITypeBinding[] bindings= binding.getTypeArguments();
					for (int index= 0; index < bindings.length; index++) {
						for (int offset= 0; offset < parameters.length; offset++) {
							if (parameters[offset].getName().equals(bindings[index].getName())) {
								rewriter.remove((ASTNode) rewriter.getOriginalList().get(offset), null);
								status.addWarning(Messages.format(RefactoringCoreMessages.MoveInstanceMethodProcessor_present_type_parameter_warning, new Object[] { BasicElementLabels.getJavaElementName(parameters[offset].getName()), BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED) }), JavaStatusContext.create(fMethod));
							}
						}
					}
					binding= binding.getDeclaringClass();
				}
			}
		}
	}
}
 
Example 14
Source File: DimensionRewrite.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 15
Source File: QuickAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static void removeException(ASTRewrite rewrite, UnionType unionType, Type exception) {
	ListRewrite listRewrite = rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY);
	List<Type> types = unionType.types();
	for (Iterator<Type> iterator = types.iterator(); iterator.hasNext();) {
		Type type = iterator.next();
		if (type.equals(exception)) {
			listRewrite.remove(type, null);
		}
	}
}
 
Example 16
Source File: JsonDeserializeRemover.java    From SparkBuilderGenerator with MIT License 4 votes vote down vote up
private void removeAnnotation(NormalAnnotation annotation, ASTRewrite rewriter, TypeDeclaration mainType) {
    ListRewrite modifierRewrite = rewriter.getListRewrite(mainType, TypeDeclaration.MODIFIERS2_PROPERTY);
    modifierRewrite.remove(annotation, null);
}
 
Example 17
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void removeExtraDimensions(MethodDeclaration methDecl) {
	ListRewrite listRewrite= getASTRewrite().getListRewrite(methDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY);
	for (Dimension dimension : (List<Dimension>) methDecl.extraDimensions()) {
		listRewrite.remove(dimension, fDescription);
	}
}
 
Example 18
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void removeExtraDimensions(SingleVariableDeclaration oldParam) {
	ListRewrite listRewrite= getASTRewrite().getListRewrite(oldParam, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY);
	for (Dimension dimension : (List<Dimension>) oldParam.extraDimensions()) {
		listRewrite.remove(dimension, fDescription);
	}
}
 
Example 19
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates the method comment for the target method declaration.
 *
 * @param rewrite
 *            the source ast rewrite
 * @param declaration
 *            the source method declaration
 * @throws JavaModelException
 *             if the argument references could not be generated
 */
protected void createMethodComment(final ASTRewrite rewrite, final MethodDeclaration declaration) throws JavaModelException {
	Assert.isNotNull(rewrite);
	Assert.isNotNull(declaration);
	final Javadoc comment= declaration.getJavadoc();
	if (comment != null) {
		final List<TagElement> tags= new LinkedList<TagElement>(comment.tags());
		final IVariableBinding[] bindings= getArgumentBindings(declaration);
		final Map<String, TagElement> elements= new HashMap<String, TagElement>(bindings.length);
		String name= null;
		List<? extends ASTNode> fragments= null;
		TagElement element= null;
		TagElement reference= null;
		IVariableBinding binding= null;
		for (int index= 0; index < bindings.length; index++) {
			binding= bindings[index];
			for (final Iterator<TagElement> iterator= comment.tags().iterator(); iterator.hasNext();) {
				element= iterator.next();
				name= element.getTagName();
				fragments= element.fragments();
				if (name != null) {
					if (name.equals(TagElement.TAG_PARAM) && !fragments.isEmpty() && fragments.get(0) instanceof SimpleName) {
						final SimpleName simple= (SimpleName) fragments.get(0);
						if (binding.getName().equals(simple.getIdentifier())) {
							elements.put(binding.getKey(), element);
							tags.remove(element);
						}
					} else if (reference == null)
						reference= element;
				}
			}
		}
		if (bindings.length == 0 && reference == null) {
			for (final Iterator<TagElement> iterator= comment.tags().iterator(); iterator.hasNext();) {
				element= iterator.next();
				name= element.getTagName();
				fragments= element.fragments();
				if (name != null && !name.equals(TagElement.TAG_PARAM))
					reference= element;
			}
		}
		final List<ASTNode> arguments= new ArrayList<ASTNode>(bindings.length + 1);
		createArgumentList(declaration, arguments, new IArgumentFactory() {

			public final ASTNode getArgumentNode(final IVariableBinding argument, final boolean last) throws JavaModelException {
				Assert.isNotNull(argument);
				if (elements.containsKey(argument.getKey()))
					return rewrite.createCopyTarget(elements.get(argument.getKey()));
				return JavadocUtil.createParamTag(argument.getName(), declaration.getAST(), fMethod.getJavaProject());
			}

			public final ASTNode getTargetNode() throws JavaModelException {
				return JavadocUtil.createParamTag(fTargetName, declaration.getAST(), fMethod.getJavaProject());
			}
		});
		final ListRewrite rewriter= rewrite.getListRewrite(comment, Javadoc.TAGS_PROPERTY);
		ASTNode tag= null;
		for (final Iterator<TagElement> iterator= comment.tags().iterator(); iterator.hasNext();) {
			tag= iterator.next();
			if (!tags.contains(tag))
				rewriter.remove(tag, null);
		}
		for (final Iterator<ASTNode> iterator= arguments.iterator(); iterator.hasNext();) {
			tag= iterator.next();
			if (reference != null)
				rewriter.insertBefore(tag, reference, null);
			else
				rewriter.insertLast(tag, null);
		}
	}
}
 
Example 20
Source File: AddGetterSetterOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Removes an existing accessor method.
 *
 * @param accessor the accessor method to remove
 * @param rewrite the list rewrite to use
 * @throws JavaModelException if an error occurs
 */
private void removeExistingAccessor(final IMethod accessor, final ListRewrite rewrite) throws JavaModelException {
	final MethodDeclaration declaration= (MethodDeclaration) ASTNodes.getParent(NodeFinder.perform(rewrite.getParent().getRoot(), accessor.getNameRange()), MethodDeclaration.class);
	if (declaration != null)
		rewrite.remove(declaration, null);
}