Java Code Examples for org.eclipse.jdt.core.IImportDeclaration#getElementName()

The following examples show how to use org.eclipse.jdt.core.IImportDeclaration#getElementName() . 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: MoveCuUpdateCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void addStaticImport(ICompilationUnit movedUnit, IImportDeclaration importDecl, ImportRewrite rewrite) {
	String old = importDecl.getElementName();
	int oldPackLength = movedUnit.getParent().getElementName().length();

	StringBuilder result = new StringBuilder(fDestination.getElementName());
	if (oldPackLength == 0) {
		result.append('.').append(old);
	} else if (result.length() == 0) {
		result.append(old.substring(oldPackLength + 1)); // cut "."
	} else {
		result.append(old.substring(oldPackLength));
	}
	int index = result.lastIndexOf("."); //$NON-NLS-1$
	if (index > 0 && index < result.length() - 1) {
		rewrite.addStaticImport(result.substring(0, index), result.substring(index + 1, result.length()), true);
	}
}
 
Example 2
Source File: MoveCuUpdateCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private String createStringForNewImport(ICompilationUnit movedUnit, IImportDeclaration importDecl) {
	String old = importDecl.getElementName();
	int oldPackLength = movedUnit.getParent().getElementName().length();

	StringBuilder result = new StringBuilder(fDestination.getElementName());
	if (oldPackLength == 0) {
		result.append('.').append(old);
	} else if (result.length() == 0) {
		result.append(old.substring(oldPackLength + 1)); // cut "."
	} else {
		result.append(old.substring(oldPackLength));
	}
	return result.toString();
}
 
Example 3
Source File: MoveCuUpdateCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void addStaticImport(ICompilationUnit movedUnit, IImportDeclaration importDecl, ImportRewrite rewrite) {
	String old= importDecl.getElementName();
	int oldPackLength= movedUnit.getParent().getElementName().length();

	StringBuffer result= new StringBuffer(fDestination.getElementName());
	if (oldPackLength == 0) // move FROM default package
		result.append('.').append(old);
	else if (result.length() == 0) // move TO default package
		result.append(old.substring(oldPackLength + 1)); // cut "."
	else
		result.append(old.substring(oldPackLength));
	int index= result.lastIndexOf("."); //$NON-NLS-1$
	if (index > 0 && index < result.length() - 1)
		rewrite.addStaticImport(result.substring(0, index), result.substring(index + 1, result.length()), true);
}
 
Example 4
Source File: MoveCuUpdateCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String createStringForNewImport(ICompilationUnit movedUnit, IImportDeclaration importDecl) {
  	String old= importDecl.getElementName();
int oldPackLength= movedUnit.getParent().getElementName().length();

StringBuffer result= new StringBuffer(fDestination.getElementName());
if (oldPackLength == 0) // move FROM default package
	result.append('.').append(old);
else if (result.length() == 0) // move TO default package
	result.append(old.substring(oldPackLength + 1)); // cut "."
else
	result.append(old.substring(oldPackLength));
return result.toString();
  }
 
Example 5
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 5 votes vote down vote up
private String resolve(IMethod operation, String typename) throws JavaModelException {
	if (Signature.C_UNRESOLVED == Signature.getElementType(typename).charAt(0)) {
		final ICompilationUnit unit = operation.getCompilationUnit();
		if (unit != null) {
			final String post = "." + Signature.toString(typename); //$NON-NLS-1$
			for (final IImportDeclaration decl : unit.getImports()) {
				if (decl.getElementName().endsWith(post)) {
					return decl.getElementName();
				}
			}
		}
	}
	return Signature.toString(typename);
}
 
Example 6
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private RefactoringStatus checkConflictingTypes(IProgressMonitor pm) throws CoreException {
	RefactoringStatus result = new RefactoringStatus();
	IJavaSearchScope scope = RefactoringScopeFactory.create(fType);
	SearchPattern pattern = SearchPattern.createPattern(getNewElementName(), IJavaSearchConstants.TYPE, IJavaSearchConstants.ALL_OCCURRENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
	ICompilationUnit[] cusWithReferencesToConflictingTypes = RefactoringSearchEngine.findAffectedCompilationUnits(pattern, scope, pm, result);
	if (cusWithReferencesToConflictingTypes.length == 0) {
		return result;
	}
	ICompilationUnit[] cusWithReferencesToRenamedType = getCus(fReferences);

	Set<ICompilationUnit> conflicts = getIntersection(cusWithReferencesToRenamedType, cusWithReferencesToConflictingTypes);
	if (cusWithReferencesToConflictingTypes.length > 0) {
		cus: for (ICompilationUnit cu : cusWithReferencesToConflictingTypes) {
			String packageName = fType.getPackageFragment().getElementName();
			if (((IPackageFragment) cu.getParent()).getElementName().equals(packageName)) {
				boolean hasOnDemandImport = false;
				IImportDeclaration[] imports = cu.getImports();
				for (IImportDeclaration importDecl : imports) {
					if (importDecl.isOnDemand()) {
						hasOnDemandImport = true;
					} else {
						String importName = importDecl.getElementName();
						int packageLength = importName.length() - getNewElementName().length() - 1;
						if (packageLength > 0 && importName.endsWith(getNewElementName()) && importName.charAt(packageLength) == '.') {
							continue cus; // explicit import from another package => no problem
						}
					}
				}
				if (hasOnDemandImport) {
					// the renamed type in the same package will shadow the *-imported type
					conflicts.add(cu);
				}
			}
		}
	}

	for (ICompilationUnit conflict : conflicts) {
		RefactoringStatusContext context = JavaStatusContext.create(conflict);
		String message = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_another_type, new String[] { getNewElementLabel(), BasicElementLabels.getFileName(conflict) });
		result.addError(message, context);
	}
	return result;
}
 
Example 7
Source File: RenamePackageProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private String getUpdatedImport(IImportDeclaration importDeclaration) {
	String fullyQualifiedImportType= importDeclaration.getElementName();
	int offsetOfDotBeforeTypeName= fPackage.getElementName().length();
	String result= getNewPackageName() + fullyQualifiedImportType.substring(offsetOfDotBeforeTypeName);
	return result;
}
 
Example 8
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private RefactoringStatus checkConflictingTypes(IProgressMonitor pm) throws CoreException {
	RefactoringStatus result= new RefactoringStatus();
	IJavaSearchScope scope= RefactoringScopeFactory.create(fType);
	SearchPattern pattern= SearchPattern.createPattern(getNewElementName(),
			IJavaSearchConstants.TYPE, IJavaSearchConstants.ALL_OCCURRENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
	ICompilationUnit[] cusWithReferencesToConflictingTypes= RefactoringSearchEngine.findAffectedCompilationUnits(pattern, scope, pm, result);
	if (cusWithReferencesToConflictingTypes.length == 0)
		return result;
	ICompilationUnit[] 	cusWithReferencesToRenamedType= getCus(fReferences);

	Set<ICompilationUnit> conflicts= getIntersection(cusWithReferencesToRenamedType, cusWithReferencesToConflictingTypes);
	if (cusWithReferencesToConflictingTypes.length > 0) {
		cus: for (ICompilationUnit cu : cusWithReferencesToConflictingTypes) {
			String packageName= fType.getPackageFragment().getElementName();
			if (((IPackageFragment) cu.getParent()).getElementName().equals(packageName)) {
				boolean hasOnDemandImport= false;
				IImportDeclaration[] imports= cu.getImports();
				for (IImportDeclaration importDecl : imports) {
					if (importDecl.isOnDemand()) {
						hasOnDemandImport= true;
					} else {
						String importName= importDecl.getElementName();
						int packageLength= importName.length() - getNewElementName().length() - 1;
						if (packageLength > 0
								&& importName.endsWith(getNewElementName())
								&& importName.charAt(packageLength) == '.') {
							continue cus; // explicit import from another package => no problem
						}
					}
				}
				if (hasOnDemandImport) {
					// the renamed type in the same package will shadow the *-imported type
					conflicts.add(cu);
				}
			}
		}
	}
	
	for (ICompilationUnit conflict : conflicts) {
		RefactoringStatusContext context= JavaStatusContext.create(conflict);
		String message= Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_another_type,
			new String[] { getNewElementLabel(), BasicElementLabels.getFileName(conflict)});
		result.addError(message, context);
	}
	return result;
}
 
Example 9
Source File: RenamePackageProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private String getUpdatedImport(IImportDeclaration importDeclaration) {
	String fullyQualifiedImportType= importDeclaration.getElementName();
	int offsetOfDotBeforeTypeName= fPackage.getElementName().length();
	String result= getNewPackageName() + fullyQualifiedImportType.substring(offsetOfDotBeforeTypeName);
	return result;
}