Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2000, 2013 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation /
  
  
  package org.eclipse.jdt.core.dom.rewrite;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import  org.eclipse.core.runtime.CoreException;
  import  org.eclipse.core.runtime.IProgressMonitor;
  import  org.eclipse.core.runtime.NullProgressMonitor;
  import  org.eclipse.core.runtime.SubProgressMonitor;
  import  org.eclipse.text.edits.MultiTextEdit;
  import  org.eclipse.text.edits.TextEdit;


The ImportRewrite helps updating imports following a import order and on-demand imports threshold as configured by a project.

The import rewrite is created on a compilation unit and collects references to types that are added or removed. When adding imports, e.g. using addImport(String), the import rewrite evaluates if the type can be imported and returns the a reference to the type that can be used in code. This reference is either unqualified if the import could be added, or fully qualified if the import failed due to a conflict with another element of the same name.

On rewriteImports(IProgressMonitor) the rewrite translates these descriptions into text edits that can then be applied to the original source. The rewrite infrastructure tries to generate minimal text changes and only works on the import statements. It is possible to combine the result of an import rewrite with the result of a org.eclipse.jdt.core.dom.rewrite.ASTRewrite as long as no import statements are modified by the AST rewrite.

The options controlling the import order and on-demand thresholds are:

  • setImportOrder(String[]) specifies the import groups and their preferred order
  • setOnDemandImportThreshold(int) specifies the number of imports in a group needed for a on-demand import statement (star import)
  • setStaticOnDemandImportThreshold(int) specifies the number of static imports in a group needed for a on-demand import statement (star import)
This class is not intended to be subclassed.

Since:
3.2
  
  public final class ImportRewrite {

A ImportRewrite.ImportRewriteContext can optionally be used in e.g. ImportRewrite.addImport(String, ImportRewrite.ImportRewriteContext) to give more information about the types visible in the scope. These types can be for example inherited inner types where it is unnecessary to add import statements for.

This class can be implemented by clients.

  
  	public static abstract class ImportRewriteContext {

Result constant signaling that the given element is know in the context.
  
  		public final static int RES_NAME_FOUND= 1;

Result constant signaling that the given element is not know in the context.
  
  		public final static int RES_NAME_UNKNOWN= 2;

Result constant signaling that the given element is conflicting with an other element in the context.
  
  		public final static int RES_NAME_CONFLICT= 3;

Kind constant specifying that the element is a type import.
  
  		public final static int KIND_TYPE= 1;

Kind constant specifying that the element is a static field import.
  
  		public final static int KIND_STATIC_FIELD= 2;

Kind constant specifying that the element is a static method import.
 
 		public final static int KIND_STATIC_METHOD= 3;

Searches for the given element in the context and reports if the element is known (RES_NAME_FOUND), unknown (RES_NAME_UNKNOWN) or if its name conflicts (RES_NAME_CONFLICT) with an other element.

Parameters:
qualifier The qualifier of the element, can be package or the qualified name of a type
name The simple name of the element; either a type, method or field name or * for on-demand imports.
kind The kind of the element. Can be either KIND_TYPE, KIND_STATIC_FIELD or KIND_STATIC_METHOD. Implementors should be prepared for new, currently unspecified kinds and return RES_NAME_UNKNOWN by default.
Returns:
Returns the result of the lookup. Can be either RES_NAME_FOUND, RES_NAME_UNKNOWN or RES_NAME_CONFLICT.
 
 		public abstract int findInContext(String qualifierString nameint kind);
 	}
 
 	private static final char STATIC_PREFIX's';
 	private static final char NORMAL_PREFIX'n';
 
 
 	private final CompilationUnit astRoot;
 
 	private final boolean restoreExistingImports;
 	private final List existingImports;
 	private final Map importsKindMap;
 
 	private String[] importOrder;
 	private int importOnDemandThreshold;
 
 	private List addedImports;
 	private List removedImports;
 
 	private String[] createdImports;
 
 	private boolean filterImplicitImports;
Creates a ImportRewrite from a ICompilationUnit. If restoreExistingImports is true, all existing imports are kept, and new imports will be inserted at best matching locations. If restoreExistingImports is false, the existing imports will be removed and only the newly added imports will be created.

Note that create(ICompilationUnit, boolean) is more efficient than this method if an AST for the compilation unit is already available.

Parameters:
cu the compilation unit to create the imports for
restoreExistingImports specifies if the existing imports should be kept or removed.
Returns:
the created import rewriter.
Throws:
JavaModelException thrown when the compilation unit could not be accessed.
 
 	public static ImportRewrite create(ICompilationUnit cuboolean restoreExistingImportsthrows JavaModelException {
 		if (cu == null) {
 			throw new IllegalArgumentException("Compilation unit must not be null"); //$NON-NLS-1$
 		}
 		List existingImportnull;
 		if (restoreExistingImports) {
 			existingImportnew ArrayList();
 			IImportDeclaration[] importscu.getImports();
 			for (int i= 0; i < imports.lengthi++) {
 				IImportDeclaration currimports[i];
 				char prefix= Flags.isStatic(curr.getFlags()) ?  : ;
 				existingImport.add(prefix + curr.getElementName());
 			}
 		}
 		return new ImportRewrite(cunullexistingImport);
 	}

Creates a ImportRewrite from a an AST (CompilationUnit). The AST has to be created from a ICompilationUnit, that means ASTParser.setSource(ICompilationUnit) has been used when creating the AST. If restoreExistingImports is true, all existing imports are kept, and new imports will be inserted at best matching locations. If restoreExistingImports is false, the existing imports will be removed and only the newly added imports will be created.

Note that this method is more efficient than using create(ICompilationUnit, boolean) if an AST is already available.

Parameters:
astRoot the AST root node to create the imports for
restoreExistingImports specifies if the existing imports should be kept or removed.
Returns:
the created import rewriter.
Throws:
IllegalArgumentException thrown when the passed AST is null or was not created from a compilation unit.
 
 	public static ImportRewrite create(CompilationUnit astRootboolean restoreExistingImports) {
 		if (astRoot == null) {
 			throw new IllegalArgumentException("AST must not be null"); //$NON-NLS-1$
 		}
 		ITypeRoot typeRoot = astRoot.getTypeRoot();
 		if (!(typeRoot instanceof ICompilationUnit)) {
 			throw new IllegalArgumentException("AST must have been constructed from a Java element"); //$NON-NLS-1$
 		}
 		List existingImportnull;
 		if (restoreExistingImports) {
 			existingImportnew ArrayList();
 			List importsastRoot.imports();
 			for (int i= 0; i < imports.size(); i++) {
 				ImportDeclaration curr= (ImportDeclarationimports.get(i);
 				StringBuffer bufnew StringBuffer();
 				if (curr.isOnDemand()) {
 					if (buf.length() > 1)
 						buf.append('.');
 					buf.append('*');
 				}
 				existingImport.add(buf.toString());
 			}
 		}
 		return new ImportRewrite((ICompilationUnittypeRootastRootexistingImport);
 	}
 
 	private ImportRewrite(ICompilationUnit cuCompilationUnit astRootList existingImports) {
 		this.cu;
 		this.astRoot// might be null
 		if (existingImports != null) {
 			this.existingImports;
 			this.= !existingImports.isEmpty();
 		} else {
 			this.new ArrayList();
 			this.false;
 		}
 		this.true;
 		// consider that no contexts are used
 
 			public int findInContext(String qualifierString nameint kind) {
 				return findInImports(qualifiernamekind);
 			}
 		};
 		this.null// Initialized on use
 		this.null// Initialized on use
 		this.null;
 		this.null;
 
 		
 		this. = new HashMap();
 	}


 
Defines the import groups and order to be used by the ImportRewrite. Imports are added to the group matching their qualified name most. The empty group name groups all imports not matching any other group. Static imports are managed in separate groups. Static import group names are prefixed with a '#' character.

Parameters:
order A list of strings defining the import groups. A group name must be a valid package name or empty. If can be prefixed by the '#' character for static import groups
 
 	public void setImportOrder(String[] order) {
 		if (order == null)
 			throw new IllegalArgumentException("Order must not be null"); //$NON-NLS-1$
 		this.order;
 	}

 
Sets the on-demand import threshold for normal (non-static) imports. This threshold defines the number of imports that need to be in a group to use a on-demand (star) import declaration instead.

Parameters:
threshold a positive number defining the on-demand import threshold for normal (non-static) imports.
Throws:
IllegalArgumentException a IllegalArgumentException is thrown if the number is not positive.
 
 	public void setOnDemandImportThreshold(int threshold) {
 		if (threshold <= 0)
 			throw new IllegalArgumentException("Threshold must be positive."); //$NON-NLS-1$
 		this.threshold;
 	}

 
Sets the on-demand import threshold for static imports. This threshold defines the number of imports that need to be in a group to use a on-demand (star) import declaration instead.

Parameters:
threshold a positive number defining the on-demand import threshold for normal (non-static) imports.
Throws:
IllegalArgumentException a IllegalArgumentException is thrown if the number is not positive.
 
 	public void setStaticOnDemandImportThreshold(int threshold) {
 		if (threshold <= 0)
 			throw new IllegalArgumentException("Threshold must be positive."); //$NON-NLS-1$
 	}

The compilation unit for which this import rewrite was created for.

Returns:
the compilation unit for which this import rewrite was created for.
 
 		return this.;
 	}

Returns the default rewrite context that only knows about the imported types. Clients can write their own context and use the default context for the default behavior.

Returns:
the default import rewrite context.
 
 		return this.;
 	}

Specifies that implicit imports (for types in java.lang, types in the same package as the rewrite compilation unit, and types in the compilation unit's main type) should not be created, except if necessary to resolve an on-demand import conflict.

The filter is enabled by default.

Note: setUseContextToFilterImplicitImports(boolean) can be used to filter implicit imports when a context is used.

Parameters:
filterImplicitImports if true, implicit imports will be filtered
See also:
setUseContextToFilterImplicitImports(boolean)
 
 	public void setFilterImplicitImports(boolean filterImplicitImports) {
 		this.filterImplicitImports;
 	}

Sets whether a context should be used to properly filter implicit imports.

By default, the option is disabled to preserve pre-3.6 behavior.

When this option is set, the context passed to the addImport*(...) methods is used to determine whether an import can be filtered because the type is implicitly visible. Note that too many imports may be kept if this option is set and addImport*(...) methods are called without a context.

Parameters:
useContextToFilterImplicitImports the given setting
Since:
3.6
See also:
setFilterImplicitImports(boolean)
 
 	public void setUseContextToFilterImplicitImports(boolean useContextToFilterImplicitImports) {
 		this. = useContextToFilterImplicitImports;
 	}
 	
 	private static int compareImport(char prefixString qualifierString nameString curr) {
 		if (curr.charAt(0) != prefix || !curr.endsWith(name)) {
 		}
 
 		currcurr.substring(1); // remove the prefix
 
 		if (curr.length() == name.length()) {
 			if (qualifier.length() == 0) {
 			}
 		}
 		// at this place: curr.length > name.length
 
 		int dotPoscurr.length() - name.length() - 1;
 		if (curr.charAt(dotPos) != '.') {
 		}
 		if (qualifier.length() != dotPos || !curr.startsWith(qualifier)) {
 		}
 	}

Not API, package visibility as accessed from an anonymous type
 
 	/* package */ final int findInImports(String qualifierString nameint kind) {
 		boolean allowAmbiguity=  (kind == .) || (name.length() == 1 && name.charAt(0) == '*');
 		List importsthis.;
 		char prefix= (kind == .) ?  : ;
 
 		for (int iimports.size() - 1; i >= 0 ; i--) {
 			String curr= (Stringimports.get(i);
 			int rescompareImport(prefixqualifiernamecurr);
 				if (!allowAmbiguity || res == .) {
 					if (prefix != ) {
 						return res;
 					}
 					Object currKind = this..get(curr.substring(1));
 					if (currKind != null && currKind.equals(this..get(qualifier + '.' + name))) {
 						return res;
 					}
 				}
 			}
 		}
 			String fPackageNamethis..getParent().getElementName();
 			String mainTypeSimpleName= JavaCore.removeJavaLikeExtension(this..getElementName());
 			String fMainTypeName= Util.concatenateName(fPackageNamemainTypeSimpleName'.');
 					&& (qualifier.equals(fPackageName)
 							|| fMainTypeName.equals(Util.concatenateName(qualifiername'.'))))
 		}
 	}
Adds a new import to the rewriter's record and returns a Type node that can be used in the code as a reference to the type. The type binding can be an array binding, type variable or wildcard. If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard of wildcards are ignored.

No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.

Parameters:
typeSig the signature of the type to be added.
ast the AST to create the returned type for.
Returns:
returns a type to which the type binding can be assigned to. The returned type contains is unqualified when an import could be added or was already known. It is fully qualified, if an import conflict prevented the import.
 
 	public Type addImportFromSignature(String typeSigAST ast) {
 		return addImportFromSignature(typeSigastthis.);
 	}

Adds a new import to the rewriter's record and returns a Type node that can be used in the code as a reference to the type. The type binding can be an array binding, type variable or wildcard. If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard of wildcards are ignored.

No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.

Parameters:
typeSig the signature of the type to be added.
ast the AST to create the returned type for.
context an optional context that knows about types visible in the current scope or null to use the default context only using the available imports.
Returns:
returns a type to which the type binding can be assigned to. The returned type contains is unqualified when an import could be added or was already known. It is fully qualified, if an import conflict prevented the import.
 
 	public Type addImportFromSignature(String typeSigAST astImportRewriteContext context) {
 		if (typeSig == null || typeSig.length() == 0) {
 			throw new IllegalArgumentException("Invalid type signature: empty or null"); //$NON-NLS-1$
 		}
 		int sigKind= Signature.getTypeSignatureKind(typeSig);
 		switch (sigKind) {
 				return ast.newPrimitiveType(PrimitiveType.toCode(Signature.toString(typeSig)));
 				Type elementTypeaddImportFromSignature(Signature.getElementType(typeSig), astcontext);
 				return ast.newArrayType(elementType, Signature.getArrayCount(typeSig));
 				String erasureSig= Signature.getTypeErasure(typeSig);
 
 				String erasureName= Signature.toString(erasureSig);
 				if (erasureSig.charAt(0) == .) {
 					erasureNameinternalAddImport(erasureNamecontext);
 				}
 				Type baseTypeast.newSimpleType(ast.newName(erasureName));
 				String[] typeArguments= Signature.getTypeArguments(typeSig);
 				if (typeArguments.length > 0) {
 					ParameterizedType typeast.newParameterizedType(baseType);
 					List argNodestype.typeArguments();
 					for (int i= 0; i < typeArguments.lengthi++) {
 						String currtypeArguments[i];
 						if (containsNestedCapture(curr)) { // see bug 103044
 							argNodes.add(ast.newWildcardType());
 						} else {
 							argNodes.add(addImportFromSignature(currastcontext));
 						}
 					}
 					return type;
 				}
 				return baseType;
 				return ast.newSimpleType(ast.newSimpleName(Signature.toString(typeSig)));
 				WildcardType wildcardTypeast.newWildcardType();
 				char chtypeSig.charAt(0);
 				if (ch != .) {
 					Type boundaddImportFromSignature(typeSig.substring(1), astcontext);
 					wildcardType.setBound(boundch == .);
 				}
 				return wildcardType;
 				return addImportFromSignature(typeSig.substring(1), astcontext);
 			default:
 				throw new IllegalArgumentException("Unknown type signature kind: " + typeSig); //$NON-NLS-1$
 		}
 	}



Adds a new import to the rewriter's record and returns a type reference that can be used in the code. The type binding can be an array binding, type variable or wildcard. If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard of wildcards are ignored.

No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.

Parameters:
binding the signature of the type to be added.
Returns:
returns a type to which the type binding can be assigned to. The returned type contains is unqualified when an import could be added or was already known. It is fully qualified, if an import conflict prevented the import.
 
 	public String addImport(ITypeBinding binding) {
 		return addImport(bindingthis.);
 	}

Adds a new import to the rewriter's record and returns a type reference that can be used in the code. The type binding can be an array binding, type variable or wildcard. If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard of wildcards are ignored.

No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.

Parameters:
binding the signature of the type to be added.
context an optional context that knows about types visible in the current scope or null to use the default context only using the available imports.
Returns:
returns a type to which the type binding can be assigned to. The returned type contains is unqualified when an import could be added or was already known. It is fully qualified, if an import conflict prevented the import.
 
 	public String addImport(ITypeBinding bindingImportRewriteContext context) {
 		if (binding.isPrimitive() || binding.isTypeVariable() || binding.isRecovered()) {
 			return binding.getName();
 		}
 
 		ITypeBinding normalizedBindingnormalizeTypeBinding(binding);
 		if (normalizedBinding == null) {
 			return "invalid"//$NON-NLS-1$
 		}
 		if (normalizedBinding.isWildcardType()) {
 			StringBuffer resnew StringBuffer("?"); //$NON-NLS-1$
 			ITypeBinding boundnormalizedBinding.getBound();
 			if (bound != null && !bound.isWildcardType() && !bound.isCapture()) { // bug 95942
 				if (normalizedBinding.isUpperbound()) {
 					res.append(" extends "); //$NON-NLS-1$
 				} else {
 					res.append(" super "); //$NON-NLS-1$
 				}
 				res.append(addImport(boundcontext));
 			}
 			return res.toString();
 		}
 
 		if (normalizedBinding.isArray()) {
 			StringBuffer resnew StringBuffer(addImport(normalizedBinding.getElementType(), context));
 			for (int inormalizedBinding.getDimensions(); i > 0; i--) {
 				res.append("[]"); //$NON-NLS-1$
 			}
 			return res.toString();
 		}
 
 		String qualifiedNamegetRawQualifiedName(normalizedBinding);
 		if (qualifiedName.length() > 0) {
 			String strinternalAddImport(qualifiedNamecontext);
 
 			ITypeBinding[] typeArgumentsnormalizedBinding.getTypeArguments();
 			if (typeArguments.length > 0) {
 				StringBuffer resnew StringBuffer(str);
 				res.append('<');
 				for (int i= 0; i < typeArguments.lengthi++) {
 					if (i > 0) {
 						res.append(',');
 					}
 					ITypeBinding currtypeArguments[i];
 					if (containsNestedCapture(currfalse)) { // see bug 103044
 						res.append('?');
 					} else {
 						res.append(addImport(currcontext));
 					}
 				}
 				res.append('>');
 				return res.toString();
 			}
 			return str;
 		}
 		return getRawName(normalizedBinding);
 	}
 
 	private boolean containsNestedCapture(ITypeBinding bindingboolean isNested) {
 		if (binding == null || binding.isPrimitive() || binding.isTypeVariable()) {
 			return false;
 		}
 		if (binding.isCapture()) {
 			if (isNested) {
 				return true;
 			}
 			return containsNestedCapture(binding.getWildcard(), true);
 		}
 		if (binding.isWildcardType()) {
 			return containsNestedCapture(binding.getBound(), true);
 		}
 		if (binding.isArray()) {
 			return containsNestedCapture(binding.getElementType(), true);
 		}
 		ITypeBinding[] typeArgumentsbinding.getTypeArguments();
 		for (int i= 0; i < typeArguments.lengthi++) {
 			if (containsNestedCapture(typeArguments[i], true)) {
 				return true;
 			}
 		}
 		return false;
 	}
 
 	private boolean containsNestedCapture(String signature) {
 		return signature.length() > 1 && signature.indexOf(., 1) != -1;
 	}
 
 	private static ITypeBinding normalizeTypeBinding(ITypeBinding binding) {
 		if (binding != null && !binding.isNullType() && !"void".equals(binding.getName())) { //$NON-NLS-1$
 			if (binding.isAnonymous()) {
 				ITypeBinding[] baseBindingsbinding.getInterfaces();
 				if (baseBindings.length > 0) {
 					return baseBindings[0];
 				}
 				return binding.getSuperclass();
 			}
 			if (binding.isCapture()) {
 				return binding.getWildcard();
 			}
 			return binding;
 		}
 		return null;
 	}

Adds a new import to the rewriter's record and returns a Type that can be used in the code. The type binding can be an array binding, type variable or wildcard. If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard of wildcards are ignored.

No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.

Parameters:
binding the signature of the type to be added.
ast the AST to create the returned type for.
Returns:
returns a type to which the type binding can be assigned to. The returned type contains is unqualified when an import could be added or was already known. It is fully qualified, if an import conflict prevented the import.
 
 	public Type addImport(ITypeBinding bindingAST ast) {
 		return addImport(bindingastthis.);
 	}

Adds a new import to the rewriter's record and returns a Type that can be used in the code. The type binding can be an array binding, type variable or wildcard. If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard of wildcards are ignored.

No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.

Parameters:
binding the signature of the type to be added.
ast the AST to create the returned type for.
context an optional context that knows about types visible in the current scope or null to use the default context only using the available imports.
Returns:
returns a type to which the type binding can be assigned to. The returned type contains is unqualified when an import could be added or was already known. It is fully qualified, if an import conflict prevented the import.
 
 	public Type addImport(ITypeBinding bindingAST astImportRewriteContext context) {
 		if (binding.isPrimitive()) {
 			return ast.newPrimitiveType(PrimitiveType.toCode(binding.getName()));
 		}
 
 		ITypeBinding normalizedBindingnormalizeTypeBinding(binding);
 		if (normalizedBinding == null) {
 			return ast.newSimpleType(ast.newSimpleName("invalid")); //$NON-NLS-1$
 		}
 
 		if (normalizedBinding.isTypeVariable()) {
 			// no import
 			return ast.newSimpleType(ast.newSimpleName(binding.getName()));
 		}
 		if (normalizedBinding.isWildcardType()) {
 			WildcardType wcTypeast.newWildcardType();
 			ITypeBinding boundnormalizedBinding.getBound();
 			if (bound != null && !bound.isWildcardType() && !bound.isCapture()) { // bug 96942
 				Type boundTypeaddImport(boundastcontext);
 				wcType.setBound(boundTypenormalizedBinding.isUpperbound());
 			}
 			return wcType;
 		}
 
 		if (normalizedBinding.isArray()) {
 			Type elementTypeaddImport(normalizedBinding.getElementType(), astcontext);
 			return ast.newArrayType(elementTypenormalizedBinding.getDimensions());
 		}
 
 		String qualifiedNamegetRawQualifiedName(normalizedBinding);
 		if (qualifiedName.length() > 0) {
 			String resinternalAddImport(qualifiedNamecontext);
 
 			ITypeBinding[] typeArgumentsnormalizedBinding.getTypeArguments();
 			if (typeArguments.length > 0) {
 				Type erasureTypeast.newSimpleType(ast.newName(res));
 				ParameterizedType paramTypeast.newParameterizedType(erasureType);
 				List argumentsparamType.typeArguments();
 				for (int i= 0; i < typeArguments.lengthi++) {
 					ITypeBinding currtypeArguments[i];
 					if (containsNestedCapture(currfalse)) { // see bug 103044
 						arguments.add(ast.newWildcardType());
 					} else {
 						arguments.add(addImport(currastcontext));
 					}
 				}
 				return paramType;
 			}
 			return ast.newSimpleType(ast.newName(res));
 		}
 		return ast.newSimpleType(ast.newName(getRawName(normalizedBinding)));
 	}


Adds a new import to the rewriter's record and returns a type reference that can be used in the code. The type binding can only be an array or non-generic type.

No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.

Parameters:
qualifiedTypeName the qualified type name of the type to be added
context an optional context that knows about types visible in the current scope or null to use the default context only using the available imports.
Returns:
returns a type to which the type binding can be assigned to. The returned type contains is unqualified when an import could be added or was already known. It is fully qualified, if an import conflict prevented the import.
 
 	public String addImport(String qualifiedTypeNameImportRewriteContext context) {
 		int angleBracketOffsetqualifiedTypeName.indexOf('<');
 		if (angleBracketOffset != -1) {
 			return internalAddImport(qualifiedTypeName.substring(0, angleBracketOffset), context) + qualifiedTypeName.substring(angleBracketOffset);
 		}
 		int bracketOffsetqualifiedTypeName.indexOf('[');
 		if (bracketOffset != -1) {
 			return internalAddImport(qualifiedTypeName.substring(0, bracketOffset), context) + qualifiedTypeName.substring(bracketOffset);
 		}
 		return internalAddImport(qualifiedTypeNamecontext);
 	}

Adds a new import to the rewriter's record and returns a type reference that can be used in the code. The type binding can only be an array or non-generic type.

No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.

Parameters:
qualifiedTypeName the qualified type name of the type to be added
Returns:
returns a type to which the type binding can be assigned to. The returned type contains is unqualified when an import could be added or was already known. It is fully qualified, if an import conflict prevented the import.
 
 	public String addImport(String qualifiedTypeName) {
 		return addImport(qualifiedTypeNamethis.);
 	}

Adds a new static import to the rewriter's record and returns a reference that can be used in the code. The reference will be fully qualified if an import conflict prevented the import or unqualified if the import succeeded or was already existing.

No imports are added for members that are already known. If a import for a type is recorded to be removed, this record is discarded instead.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.

Parameters:
binding The binding of the static field or method to be added.
Returns:
returns either the simple member name if the import was successful or else the qualified name if an import conflict prevented the import.
Throws:
IllegalArgumentException an IllegalArgumentException is thrown if the binding is not a static field or method.
 
 	public String addStaticImport(IBinding binding) {
 		return addStaticImport(bindingthis.);
 	}

Adds a new static import to the rewriter's record and returns a reference that can be used in the code. The reference will be fully qualified if an import conflict prevented the import or unqualified if the import succeeded or was already existing.

No imports are added for members that are already known. If a import for a type is recorded to be removed, this record is discarded instead.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.

Parameters:
binding The binding of the static field or method to be added.
context an optional context that knows about members visible in the current scope or null to use the default context only using the available imports.
Returns:
returns either the simple member name if the import was successful or else the qualified name if an import conflict prevented the import.
Throws:
IllegalArgumentException an IllegalArgumentException is thrown if the binding is not a static field or method.
 
 	public String addStaticImport(IBinding bindingImportRewriteContext context) {
 		if (Modifier.isStatic(binding.getModifiers())) {
 			if (binding instanceof IVariableBinding) {
 				IVariableBinding variableBinding= (IVariableBindingbinding;
 				if (variableBinding.isField()) {
 					ITypeBinding declaringTypevariableBinding.getDeclaringClass();
 					return addStaticImport(getRawQualifiedName(declaringType), binding.getName(), truecontext);
 				}
 			} else if (binding instanceof IMethodBinding) {
 				ITypeBinding declaringType= ((IMethodBindingbinding).getDeclaringClass();
 				return addStaticImport(getRawQualifiedName(declaringType), binding.getName(), falsecontext);
 			}
 		}
 		throw new IllegalArgumentException("Binding must be a static field or method."); //$NON-NLS-1$
 	}

Adds a new static import to the rewriter's record and returns a reference that can be used in the code. The reference will be fully qualified if an import conflict prevented the import or unqualified if the import succeeded or was already existing.

No imports are added for members that are already known. If a import for a type is recorded to be removed, this record is discarded instead.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.

Parameters:
declaringTypeName The qualified name of the static's member declaring type
simpleName the simple name of the member; either a field or a method name.
isField true specifies that the member is a field, false if it is a method.
Returns:
returns either the simple member name if the import was successful or else the qualified name if an import conflict prevented the import.
 
 	public String addStaticImport(String declaringTypeNameString simpleNameboolean isField) {
 		return addStaticImport(declaringTypeNamesimpleNameisFieldthis.);
 	}

Adds a new static import to the rewriter's record and returns a reference that can be used in the code. The reference will be fully qualified if an import conflict prevented the import or unqualified if the import succeeded or was already existing.

No imports are added for members that are already known. If a import for a type is recorded to be removed, this record is discarded instead.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been added.

Parameters:
declaringTypeName The qualified name of the static's member declaring type
simpleName the simple name of the member; either a field or a method name.
isField true specifies that the member is a field, false if it is a method.
context an optional context that knows about members visible in the current scope or null to use the default context only using the available imports.
Returns:
returns either the simple member name if the import was successful or else the qualified name if an import conflict prevented the import.
 
 	public String addStaticImport(String declaringTypeNameString simpleNameboolean isFieldImportRewriteContext context) {
 		String key = declaringTypeName + '.' + simpleName;
 		if (declaringTypeName.indexOf('.') == -1) {
 			return key;
 		}
 		if (context == null) {
 			contextthis.;
 		}
 		this..put(keynew Integer(kind));
 		int rescontext.findInContext(declaringTypeNamesimpleNamekind);
 			return key;
 		}
 		}
 		return simpleName;
 	}
 
 	private String internalAddImport(String fullTypeNameImportRewriteContext context) {
 		int idxfullTypeName.lastIndexOf('.');
 		String typeContainerNametypeName;
 		if (idx != -1) {
 			typeContainerNamefullTypeName.substring(0, idx);
 			typeNamefullTypeName.substring(idx + 1);
 		} else {
 			typeContainerName""//$NON-NLS-1$
 			typeNamefullTypeName;
 		}
 
 		if (typeContainerName.length() == 0 && PrimitiveType.toCode(typeName) != null) {
 			return fullTypeName;
 		}
 
 		if (context == null)
 			contextthis.;
 
 		int rescontext.findInContext(typeContainerNametypeName.);
 			return fullTypeName;
 		}
 			addEntry( + fullTypeName);
 		}
 		return typeName;
 	}
 
 	private void addEntry(String entry) {
 		this..add(entry);
 
 		if (this. != null) {
 			if (this..remove(entry)) {
 				return;
 			}
 		}
 
 		if (this. == null) {
 			this.new ArrayList();
 		}
 		this..add(entry);
 	}
 
 	private boolean removeEntry(String entry) {
 		if (this..remove(entry)) {
 			if (this. != null) {
 				if (this..remove(entry)) {
 					return true;
 				}
 			}
 			if (this. == null) {
 				this.new ArrayList();
 			}
 			this..add(entry);
 			return true;
 		}
 		return false;
 	}

Records to remove a import. No remove is recorded if no such import exists or if such an import is recorded to be added. In that case the record of the addition is discarded.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that an import has been removed.

Parameters:
qualifiedName The import name to remove.
Returns:
true is returned of an import of the given name could be found.
 
 	public boolean removeImport(String qualifiedName) {
 		return removeEntry( + qualifiedName);
 	}

Records to remove a static import. No remove is recorded if no such import exists or if such an import is recorded to be added. In that case the record of the addition is discarded.

The content of the compilation unit itself is actually not modified in any way by this method; rather, the rewriter just records that a new import has been removed.

Parameters:
qualifiedName The import name to remove.
Returns:
true is returned of an import of the given name could be found.
 
 	public boolean removeStaticImport(String qualifiedName) {
 		return removeEntry( + qualifiedName);
 	}
 
 	private static String getRawName(ITypeBinding normalizedBinding) {
 		return normalizedBinding.getTypeDeclaration().getName();
 	}
 
	private static String getRawQualifiedName(ITypeBinding normalizedBinding) {
		return normalizedBinding.getTypeDeclaration().getQualifiedName();
	}


Converts all modifications recorded by this rewriter into an object representing the corresponding text edits to the source code of the rewrite's compilation unit. The compilation unit itself is not modified.

Calling this methods does not discard the modifications on record. Subsequence modifications are added to the ones already on record. If this method is called again later, the resulting text edit object will accurately reflect the net cumulative effect of all those changes.

Parameters:
monitor the progress monitor or null
Returns:
text edit object describing the changes to the document corresponding to the changes recorded by this rewriter
Throws:
CoreException the exception is thrown if the rewrite fails.
	public final TextEdit rewriteImports(IProgressMonitor monitorthrows CoreException {
		if (monitor == null) {
			monitornew NullProgressMonitor();
		try {
			monitor.beginTask(Messages.bind(.), 2);
				return new MultiTextEdit();
			CompilationUnit usedAstRootthis.;
			if (usedAstRoot == null) {
				ASTParser parser= ASTParser.newParser(.);
				parser.setFocalPosition(0); // reduced AST
				parser.setResolveBindings(false);
				usedAstRoot= (CompilationUnitparser.createAST(new SubProgressMonitor(monitor, 1));
						usedAstRoot,
			if (this. != null) {
				for (int i= 0; i < this..size(); i++) {
					String curr= (Stringthis..get(i);
					computer.addImport(curr.substring(1),  == curr.charAt(0), usedAstRootthis.);
			if (this. != null) {
				for (int i= 0; i < this..size(); i++) {
					String curr= (Stringthis..get(i);
					computer.removeImport(curr.substring(1),  == curr.charAt(0));
			TextEdit resultcomputer.getResultingEdits(new SubProgressMonitor(monitor, 1));
			return result;
finally {
			monitor.done();
	}

Returns all new non-static imports created by the last invocation of rewriteImports(IProgressMonitor) or null if these methods have not been called yet.

Note that this list doesn't need to be the same as the added imports (see getAddedImports()) as implicit imports are not created and some imports are represented by on-demand imports instead.

Returns:
the created imports
	public String[] getCreatedImports() {
		return this.;
	}

Returns all new static imports created by the last invocation of rewriteImports(IProgressMonitor) or null if these methods have not been called yet.

Note that this list doesn't need to be the same as the added static imports (getAddedStaticImports()) as implicit imports are not created and some imports are represented by on-demand imports instead.

Returns:
the created imports
	}

Returns all non-static imports that are recorded to be added.

Returns:
the imports recorded to be added.
	public String[] getAddedImports() {
	}

Returns all static imports that are recorded to be added.

Returns:
the static imports recorded to be added.
	}

Returns all non-static imports that are recorded to be removed.

Returns:
the imports recorded to be removed.
	public String[] getRemovedImports() {
	}

Returns all static imports that are recorded to be removed.

Returns:
the static imports recorded to be removed.
	}

Returns true if imports have been recorded to be added or removed.

Returns:
boolean returns if any changes to imports have been recorded.
	public boolean hasRecordedChanges() {
		return !this. ||
			(this. != null && !this..isEmpty()) ||
			(this. != null && !this..isEmpty());
	private static String[] filterFromList(List importschar prefix) {
		if (imports == null) {
		ArrayList resnew ArrayList();
		for (int i= 0; i < imports.size(); i++) {
			String curr= (Stringimports.get(i);
			if (prefix == curr.charAt(0)) {
				res.add(curr.substring(1));
		return (String[]) res.toArray(new String[res.size()]);
New to GrepCode? Check out our FAQ X