org.eclipse.jdt.core.IImportDeclaration Java Examples

The following examples show how to use org.eclipse.jdt.core.IImportDeclaration. 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: CreateImportOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Sets the correct position for the new import:<ul>
 * <li> after the last import
 * <li> if no imports, before the first type
 * <li> if no type, after the package statement
 * <li> and if no package statement - first thing in the CU
 */
protected void initializeDefaultPosition() {
	try {
		ICompilationUnit cu = getCompilationUnit();
		IImportDeclaration[] imports = cu.getImports();
		if (imports.length > 0) {
			createAfter(imports[imports.length - 1]);
			return;
		}
		IType[] types = cu.getTypes();
		if (types.length > 0) {
			createBefore(types[0]);
			return;
		}
		IJavaElement[] children = cu.getChildren();
		//look for the package declaration
		for (int i = 0; i < children.length; i++) {
			if (children[i].getElementType() == IJavaElement.PACKAGE_DECLARATION) {
				createAfter(children[i]);
				return;
			}
		}
	} catch (JavaModelException e) {
		// cu doesn't exit: ignore
	}
}
 
Example #2
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void analyzeImportDeclaration(IImportDeclaration imp, RefactoringStatus result) throws CoreException {
	if (!imp.isOnDemand()) {
		return; //analyzed earlier
	}

	IJavaElement imported = convertFromImportDeclaration(imp);
	if (imported == null) {
		return;
	}

	if (imported instanceof IPackageFragment) {
		ICompilationUnit[] cus = ((IPackageFragment) imported).getCompilationUnits();
		for (int i = 0; i < cus.length; i++) {
			analyzeImportedTypes(cus[i].getTypes(), result, imp);
		}
	} else {
		//cast safe: see JavaModelUtility.convertFromImportDeclaration
		analyzeImportedTypes(((IType) imported).getTypes(), result, imp);
	}
}
 
Example #3
Source File: RenamePackageProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Add new imports to types in <code>typeReferences</code> with package <code>fPackage</code>.
 * @param typeReferences type references
 * @throws CoreException should not happen
 */
private void addTypeImports(SearchResultGroup typeReferences) throws CoreException {
	SearchMatch[] searchResults= typeReferences.getSearchResults();
	for (int i= 0; i < searchResults.length; i++) {
		SearchMatch result= searchResults[i];
		IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(result);
		if (! (enclosingElement instanceof IImportDeclaration)) {
			String reference= getNormalizedTypeReference(result);
			if (! reference.startsWith(fPackage.getElementName())) {
				// is unqualified
				reference= cutOffInnerTypes(reference);
				ImportChange importChange= fImportsManager.getImportChange(typeReferences.getCompilationUnit());
				importChange.addImport(fPackage.getElementName() + '.' + reference);
			}
		}
	}
}
 
Example #4
Source File: RenamePackageProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Add new imports to types in <code>typeReferences</code> with package <code>fNewElementName</code>
 * and remove old import with <code>fPackage</code>.
 * @param typeReferences type references
 * @throws CoreException should not happen
 */
private void updateTypeImports(SearchResultGroup typeReferences) throws CoreException {
	SearchMatch[] searchResults= typeReferences.getSearchResults();
	for (int i= 0; i < searchResults.length; i++) {
		SearchMatch result= searchResults[i];
		IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(result);
		if (enclosingElement instanceof IImportDeclaration) {
			IImportDeclaration importDeclaration= (IImportDeclaration) enclosingElement;
			updateImport(typeReferences.getCompilationUnit(), importDeclaration, getUpdatedImport(importDeclaration));
		} else {
			String reference= getNormalizedTypeReference(result);
			if (! reference.startsWith(fPackage.getElementName())) {
				reference= cutOffInnerTypes(reference);
				ImportChange importChange= fImportsManager.getImportChange(typeReferences.getCompilationUnit());
				importChange.removeImport(fPackage.getElementName() + '.' + reference);
				importChange.addImport(getNewPackageName() + '.' + reference);
			} // else: already found & updated with package reference search
		}
	}
}
 
Example #5
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void copyImportsToDestination(IImportContainer container, ASTRewrite rewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException {
	ListRewrite listRewrite= rewrite.getListRewrite(destinationCuNode, CompilationUnit.IMPORTS_PROPERTY);

	IJavaElement[] importDeclarations= container.getChildren();
	for (int i= 0; i < importDeclarations.length; i++) {
		IImportDeclaration declaration= (IImportDeclaration) importDeclarations[i];

		ImportDeclaration sourceNode= ASTNodeSearchUtil.getImportDeclarationNode(declaration, sourceCuNode);
		ImportDeclaration copiedNode= (ImportDeclaration) ASTNode.copySubtree(rewrite.getAST(), sourceNode);

		if (getLocation() == IReorgDestination.LOCATION_BEFORE) {
			listRewrite.insertFirst(copiedNode, null);
		} else {
			listRewrite.insertLast(copiedNode, null);
		}
	}
}
 
Example #6
Source File: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test1() throws Exception {
	ICompilationUnit cu = fPackageP.getCompilationUnit("Test1.java");
	IType testClass = cu.createType("public class Test1 extends A implements B {\n}\n", null, true, null);

	List<OverridableMethod> overridableMethods = getOverridableMethods(testClass);
	checkUnimplementedMethods(new String[] { "a()", "b(Vector<Date>)", "c(Hashtable)" }, overridableMethods);

	addAndApplyOverridableMethods(testClass, overridableMethods);

	IMethod[] methods = testClass.getMethods();
	checkMethods(new String[] { "a", "b", "c", "equals", "clone", "toString", "finalize", "hashCode" }, methods);

	IImportDeclaration[] imports = cu.getImports();
	checkImports(new String[] { "java.util.Date", "java.util.Hashtable", "java.util.Vector" }, imports);
}
 
Example #7
Source File: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test2() throws Exception {
	ICompilationUnit cu = fPackageP.getCompilationUnit("Test2.java");
	IType testClass = cu.createType("public class Test2 extends C implements B {\n}\n", null, true, null);

	List<OverridableMethod> overridableMethods = getOverridableMethods(testClass);
	checkUnimplementedMethods(new String[] { "d(Hashtable)" }, overridableMethods);
	checkOverridableMethods(new String[] { "c(Hashtable)" }, overridableMethods);

	addAndApplyOverridableMethods(testClass, overridableMethods);

	IMethod[] methods = testClass.getMethods();
	checkMethods(new String[] { "c", "d", "equals", "clone", "toString", "finalize", "hashCode" }, methods);

	IImportDeclaration[] imports = cu.getImports();
	checkImports(new String[] { "java.util.Enumeration", "java.util.Hashtable" }, imports);
}
 
Example #8
Source File: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test3() throws Exception {
	ICompilationUnit cu = fPackageP.getCompilationUnit("Test3.java");
	IType testClass = cu.createType("public class Test3 extends D {\n}\n", null, true, null);

	List<OverridableMethod> overridableMethods = getOverridableMethods(testClass);
	checkUnimplementedMethods(new String[] { "c(Hashtable)", "d(Hashtable)" }, overridableMethods);

	addAndApplyOverridableMethods(testClass, overridableMethods);

	IMethod[] methods = testClass.getMethods();
	checkMethods(new String[] { "c", "d", "equals", "clone", "toString", "finalize", "hashCode" }, methods);

	IImportDeclaration[] imports = cu.getImports();
	checkImports(new String[] { "java.util.Hashtable", "java.util.Enumeration" }, imports);
}
 
Example #9
Source File: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void checkImports(String[] expected, IImportDeclaration[] imports) {
	int nImports = imports.length;
	int nExpected = expected.length;
	if (nExpected != nImports) {
		StringBuilder buf = new StringBuilder();
		buf.append(nExpected).append(" imports expected, is ").append(nImports).append("\n");
		buf.append("expected:\n");
		for (int i = 0; i < expected.length; i++) {
			buf.append(expected[i]).append("\n");
		}
		buf.append("actual:\n");
		for (int i = 0; i < imports.length; i++) {
			buf.append(imports[i]).append("\n");
		}
		assertTrue(buf.toString(), false);
	}
	for (int i = 0; i < nExpected; i++) {
		String impName = expected[i];
		assertTrue("import " + impName + " expected", nameContained(impName, imports));
	}
}
 
Example #10
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void analyzeImportDeclaration(IImportDeclaration imp, RefactoringStatus result) throws CoreException{
	if (!imp.isOnDemand())
		return; //analyzed earlier

	IJavaElement imported= convertFromImportDeclaration(imp);
	if (imported == null)
		return;

	if (imported instanceof IPackageFragment){
		ICompilationUnit[] cus= ((IPackageFragment)imported).getCompilationUnits();
		for (int i= 0; i < cus.length; i++) {
			analyzeImportedTypes(cus[i].getTypes(), result, imp);
		}
	} else {
		//cast safe: see JavaModelUtility.convertFromImportDeclaration
		analyzeImportedTypes(((IType)imported).getTypes(), result, imp);
	}
}
 
Example #11
Source File: RenamePackageProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Add new imports to types in <code>typeReferences</code> with package <code>fPackage</code>.
 * @param typeReferences type references
 * @throws CoreException should not happen
 */
private void addTypeImports(SearchResultGroup typeReferences) throws CoreException {
	SearchMatch[] searchResults= typeReferences.getSearchResults();
	for (int i= 0; i < searchResults.length; i++) {
		SearchMatch result= searchResults[i];
		IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(result);
		if (! (enclosingElement instanceof IImportDeclaration)) {
			String reference= getNormalizedTypeReference(result);
			if (! reference.startsWith(fPackage.getElementName())) {
				// is unqualified
				reference= cutOffInnerTypes(reference);
				ImportChange importChange= fImportsManager.getImportChange(typeReferences.getCompilationUnit());
				importChange.addImport(fPackage.getElementName() + '.' + reference);
			}
		}
	}
}
 
Example #12
Source File: RenamePackageProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Add new imports to types in <code>typeReferences</code> with package <code>fNewElementName</code>
 * and remove old import with <code>fPackage</code>.
 * @param typeReferences type references
 * @throws CoreException should not happen
 */
private void updateTypeImports(SearchResultGroup typeReferences) throws CoreException {
	SearchMatch[] searchResults= typeReferences.getSearchResults();
	for (int i= 0; i < searchResults.length; i++) {
		SearchMatch result= searchResults[i];
		IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(result);
		if (enclosingElement instanceof IImportDeclaration) {
			IImportDeclaration importDeclaration= (IImportDeclaration) enclosingElement;
			updateImport(typeReferences.getCompilationUnit(), importDeclaration, getUpdatedImport(importDeclaration));
		} else {
			String reference= getNormalizedTypeReference(result);
			if (! reference.startsWith(fPackage.getElementName())) {
				reference= cutOffInnerTypes(reference);
				ImportChange importChange= fImportsManager.getImportChange(typeReferences.getCompilationUnit());
				importChange.removeImport(fPackage.getElementName() + '.' + reference);
				importChange.addImport(getNewPackageName() + '.' + reference);
			} // else: already found & updated with package reference search
		}
	}
}
 
Example #13
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void copyImportsToDestination(IImportContainer container, ASTRewrite rewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException {
	ListRewrite listRewrite= rewrite.getListRewrite(destinationCuNode, CompilationUnit.IMPORTS_PROPERTY);

	IJavaElement[] importDeclarations= container.getChildren();
	for (int i= 0; i < importDeclarations.length; i++) {
		IImportDeclaration declaration= (IImportDeclaration) importDeclarations[i];

		ImportDeclaration sourceNode= ASTNodeSearchUtil.getImportDeclarationNode(declaration, sourceCuNode);
		ImportDeclaration copiedNode= (ImportDeclaration) ASTNode.copySubtree(rewrite.getAST(), sourceNode);

		if (getLocation() == IReorgDestination.LOCATION_BEFORE) {
			listRewrite.insertFirst(copiedNode, null);
		} else {
			listRewrite.insertLast(copiedNode, null);
		}
	}
}
 
Example #14
Source File: JavaEditorBreadcrumb.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the input for the given element. The Java breadcrumb does not show some elements of
 * the model:
 * <ul>
 * 		<li><code>ITypeRoots</li>
 * 		<li><code>IPackageDeclaration</li>
 * 		<li><code>IImportContainer</li>
 * 		<li><code>IImportDeclaration</li>
 * </ul>
 *
 * @param element the potential input element
 * @return the element to use as input
 */
private IJavaElement getInput(IJavaElement element) {
	try {
		if (element instanceof IImportDeclaration)
			element= element.getParent();

		if (element instanceof IImportContainer)
			element= element.getParent();

		if (element instanceof IPackageDeclaration)
			element= element.getParent();

		if (element instanceof ICompilationUnit) {
			IType[] types= ((ICompilationUnit) element).getTypes();
			if (types.length > 0)
				element= types[0];
		}

		if (element instanceof IClassFile)
			element= ((IClassFile) element).getType();

		return element;
	} catch (JavaModelException e) {
		return null;
	}
}
 
Example #15
Source File: MembersView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Answers if the given <code>element</code> is a valid
 * element for this part.
 *
 * @param 	element	the object to test
 * @return	<true> if the given element is a valid element
 */
@Override
protected boolean isValidElement(Object element) {
	if (element instanceof IMember)
		return super.isValidElement(((IMember)element).getDeclaringType());
	else if (element instanceof IImportDeclaration)
		return isValidElement(((IJavaElement)element).getParent());
	else if (element instanceof IImportContainer) {
		Object input= getViewer().getInput();
		if (input instanceof IJavaElement) {
			ICompilationUnit cu= (ICompilationUnit)((IJavaElement)input).getAncestor(IJavaElement.COMPILATION_UNIT);
			if (cu != null) {
				ICompilationUnit importContainerCu= (ICompilationUnit)((IJavaElement)element).getAncestor(IJavaElement.COMPILATION_UNIT);
				return cu.equals(importContainerCu);
			} else {
				IClassFile cf= (IClassFile)((IJavaElement)input).getAncestor(IJavaElement.CLASS_FILE);
				IClassFile importContainerCf= (IClassFile)((IJavaElement)element).getAncestor(IJavaElement.CLASS_FILE);
				return cf != null && cf.equals(importContainerCf);
			}
		}
	}
	return false;
}
 
Example #16
Source File: ASTNodeSearchUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static ASTNode[] getDeclarationNodes(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
	switch(element.getElementType()){
		case IJavaElement.FIELD:
			return new ASTNode[]{getFieldOrEnumConstantDeclaration((IField) element, cuNode)};
		case IJavaElement.IMPORT_CONTAINER:
			return getImportNodes((IImportContainer)element, cuNode);
		case IJavaElement.IMPORT_DECLARATION:
			return new ASTNode[]{getImportDeclarationNode((IImportDeclaration)element, cuNode)};
		case IJavaElement.INITIALIZER:
			return new ASTNode[]{getInitializerNode((IInitializer)element, cuNode)};
		case IJavaElement.METHOD:
			return new ASTNode[]{getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) element, cuNode)};
		case IJavaElement.PACKAGE_DECLARATION:
			return new ASTNode[]{getPackageDeclarationNode((IPackageDeclaration)element, cuNode)};
		case IJavaElement.TYPE:
			return new ASTNode[]{getAbstractTypeDeclarationNode((IType) element, cuNode)};
		default:
			Assert.isTrue(false, String.valueOf(element.getElementType()));
			return null;
	}
}
 
Example #17
Source File: CreatePackageDeclarationOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Sets the correct position for new package declaration:<ul>
 * <li> before the first import
 * <li> if no imports, before the first type
 * <li> if no type - first thing in the CU
 * <li>
 */
protected void initializeDefaultPosition() {
	try {
		ICompilationUnit cu = getCompilationUnit();
		IImportDeclaration[] imports = cu.getImports();
		if (imports.length > 0) {
			createBefore(imports[0]);
			return;
		}
		IType[] types = cu.getTypes();
		if (types.length > 0) {
			createBefore(types[0]);
			return;
		}
	} catch (JavaModelException e) {
		// cu doesn't exist: ignore
	}
}
 
Example #18
Source File: ASTNodeSearchUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static ASTNode[] getDeclarationNodes(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
	switch(element.getElementType()){
		case IJavaElement.FIELD:
			return new ASTNode[]{getFieldOrEnumConstantDeclaration((IField) element, cuNode)};
		case IJavaElement.IMPORT_CONTAINER:
			return getImportNodes((IImportContainer)element, cuNode);
		case IJavaElement.IMPORT_DECLARATION:
			return new ASTNode[]{getImportDeclarationNode((IImportDeclaration)element, cuNode)};
		case IJavaElement.INITIALIZER:
			return new ASTNode[]{getInitializerNode((IInitializer)element, cuNode)};
		case IJavaElement.METHOD:
			return new ASTNode[]{getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) element, cuNode)};
		case IJavaElement.PACKAGE_DECLARATION:
			return new ASTNode[]{getPackageDeclarationNode((IPackageDeclaration)element, cuNode)};
		case IJavaElement.TYPE:
			return new ASTNode[]{getAbstractTypeDeclarationNode((IType) element, cuNode)};
		default:
			Assert.isTrue(false, String.valueOf(element.getElementType()));
			return null;
	}
}
 
Example #19
Source File: CopyQualifiedNameAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isValidElement(Object element) {
	if (element instanceof IMember)
		return true;

	if (element instanceof IClassFile)
		return true;

	if (element instanceof ICompilationUnit)
		return true;

	if (element instanceof IPackageDeclaration)
		return true;

	if (element instanceof IImportDeclaration)
		return true;

	if (element instanceof IPackageFragment)
		return true;

	if (element instanceof IPackageFragmentRoot)
		return true;

	if (element instanceof IJavaProject)
		return true;

	if (element instanceof IJarEntryResource)
		return true;

	if (element instanceof IResource)
		return true;

	if (element instanceof LogicalPackage)
		return true;

	return false;
}
 
Example #20
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void copyToDestination(IJavaElement element, CompilationUnitRewrite targetRewriter, CompilationUnit sourceCuNode, CompilationUnit targetCuNode) throws CoreException {
	final ASTRewrite rewrite= targetRewriter.getASTRewrite();
	switch (element.getElementType()) {
		case IJavaElement.FIELD:
			copyMemberToDestination((IMember) element, targetRewriter, sourceCuNode, targetCuNode, createNewFieldDeclarationNode(((IField) element), rewrite, sourceCuNode));
			break;
		case IJavaElement.IMPORT_CONTAINER:
			copyImportsToDestination((IImportContainer) element, rewrite, sourceCuNode, targetCuNode);
			break;
		case IJavaElement.IMPORT_DECLARATION:
			copyImportToDestination((IImportDeclaration) element, rewrite, sourceCuNode, targetCuNode);
			break;
		case IJavaElement.INITIALIZER:
			copyInitializerToDestination((IInitializer) element, targetRewriter, sourceCuNode, targetCuNode);
			break;
		case IJavaElement.METHOD:
			copyMethodToDestination((IMethod) element, targetRewriter, sourceCuNode, targetCuNode);
			break;
		case IJavaElement.PACKAGE_DECLARATION:
			copyPackageDeclarationToDestination((IPackageDeclaration) element, rewrite, sourceCuNode, targetCuNode);
			break;
		case IJavaElement.TYPE:
			copyTypeToDestination((IType) element, targetRewriter, sourceCuNode, targetCuNode);
			break;

		default:
			Assert.isTrue(false);
	}
}
 
Example #21
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void copyImportToDestination(IImportDeclaration declaration, ASTRewrite targetRewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException {
	ImportDeclaration sourceNode= ASTNodeSearchUtil.getImportDeclarationNode(declaration, sourceCuNode);
	ImportDeclaration copiedNode= (ImportDeclaration) ASTNode.copySubtree(targetRewrite.getAST(), sourceNode);
	ListRewrite listRewrite= targetRewrite.getListRewrite(destinationCuNode, CompilationUnit.IMPORTS_PROPERTY);

	if (getJavaElementDestination() instanceof IImportDeclaration) {
		ImportDeclaration destinationNode= ASTNodeSearchUtil.getImportDeclarationNode((IImportDeclaration) getJavaElementDestination(), destinationCuNode);
		insertRelative(copiedNode, destinationNode, listRewrite);
	} else {
		//dropped on container, we could be better here
		listRewrite.insertLast(copiedNode, null);
	}
}
 
Example #22
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static IImportDeclaration getImportedType(ICompilationUnit cu, String typeName) throws CoreException {
	IImportDeclaration[] imports = cu.getImports();
	String dotTypeName = "." + typeName; //$NON-NLS-1$
	for (int i = 0; i < imports.length; i++) {
		if (imports[i].getElementName().endsWith(dotTypeName)) {
			return imports[i];
		}
	}
	return null;
}
 
Example #23
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private RefactoringStatus checkImportedTypes() throws CoreException {
	RefactoringStatus result = new RefactoringStatus();
	IImportDeclaration[] imports = fType.getCompilationUnit().getImports();
	for (int i = 0; i < imports.length; i++) {
		analyzeImportDeclaration(imports[i], result);
	}
	return result;
}
 
Example #24
Source File: RenamePackageProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void updateImport(ICompilationUnit cu, IImportDeclaration importDeclaration, String updatedImport) throws JavaModelException {
	ImportChange importChange= fImportsManager.getImportChange(cu);
	if (Flags.isStatic(importDeclaration.getFlags())) {
		importChange.removeStaticImport(importDeclaration.getElementName());
		importChange.addStaticImport(Signature.getQualifier(updatedImport), Signature.getSimpleName(updatedImport));
	} else {
		importChange.removeImport(importDeclaration.getElementName());
		importChange.addImport(updatedImport);
	}
}
 
Example #25
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void analyzeImportedTypes(IType[] types, RefactoringStatus result, IImportDeclaration imp) throws CoreException {
	for (int i = 0; i < types.length; i++) {
		//could this be a problem (same package imports)?
		if (JdtFlags.isPublic(types[i]) && types[i].getElementName().equals(getNewElementName())) {
			String msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_name_conflict1,
					new Object[] { JavaElementLabels.getElementLabel(types[i], JavaElementLabels.ALL_FULLY_QUALIFIED), BasicElementLabels.getPathLabel(getCompilationUnit(imp).getPath(), false) });
			result.addError(msg, JavaStatusContext.create(imp));
		}
	}
}
 
Example #26
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IJavaElement convertFromImportDeclaration(IImportDeclaration declaration) throws CoreException {
		if (declaration.isOnDemand()){
			String packageName= declaration.getElementName().substring(0, declaration.getElementName().length() - 2);
			return JavaModelUtil.findTypeContainer(declaration.getJavaProject(), packageName);
		} else
			return JavaModelUtil.findTypeContainer(declaration.getJavaProject(), declaration.getElementName());
}
 
Example #27
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void analyzeImportedTypes(IType[] types, RefactoringStatus result, IImportDeclaration imp) throws CoreException {
	for (int i= 0; i < types.length; i++) {
		//could this be a problem (same package imports)?
		if (JdtFlags.isPublic(types[i]) && types[i].getElementName().equals(getNewElementName())){
			String msg= Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_name_conflict1,
																		new Object[]{ JavaElementLabels.getElementLabel(types[i], JavaElementLabels.ALL_FULLY_QUALIFIED), BasicElementLabels.getPathLabel(getCompilationUnit(imp).getPath(), false)});
			result.addError(msg, JavaStatusContext.create(imp));
		}
	}
}
 
Example #28
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus checkImportedTypes() throws CoreException {
	RefactoringStatus result= new RefactoringStatus();
	IImportDeclaration[] imports= fType.getCompilationUnit().getImports();
	for (int i= 0; i < imports.length; i++)
		analyzeImportDeclaration(imports[i], result);
	return result;
}
 
Example #29
Source File: RenamePackageProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void updateImport(ICompilationUnit cu, IImportDeclaration importDeclaration, String updatedImport) throws JavaModelException {
	ImportChange importChange= fImportsManager.getImportChange(cu);
	if (Flags.isStatic(importDeclaration.getFlags())) {
		importChange.removeStaticImport(importDeclaration.getElementName());
		importChange.addStaticImport(Signature.getQualifier(updatedImport), Signature.getSimpleName(updatedImport));
	} else {
		importChange.removeImport(importDeclaration.getElementName());
		importChange.addImport(updatedImport);
	}
}
 
Example #30
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IImportDeclaration getImportedType(ICompilationUnit cu, String typeName) throws CoreException {
	IImportDeclaration[] imports= cu.getImports();
	String dotTypeName= "." + typeName; //$NON-NLS-1$
	for (int i= 0; i < imports.length; i++){
		if (imports[i].getElementName().endsWith(dotTypeName))
			return imports[i];
	}
	return null;
}