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

The following examples show how to use org.eclipse.jdt.core.IImportDeclaration#isOnDemand() . 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: 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 2
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 3
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.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 4
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 5
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 6
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 7
Source File: OpenTypeHierarchyAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static IStatus compileCandidates(List<IJavaElement> result, List<IJavaElement> elements) {
	IStatus ok= Status.OK_STATUS;
	boolean onlyContainers= true;
	for (Iterator<IJavaElement> iter= elements.iterator(); iter.hasNext();) {
		IJavaElement elem= iter.next();
		try {
			switch (elem.getElementType()) {
				case IJavaElement.INITIALIZER:
				case IJavaElement.METHOD:
				case IJavaElement.FIELD:
				case IJavaElement.TYPE:
					onlyContainers= false;
					//$FALL-THROUGH$
				case IJavaElement.PACKAGE_FRAGMENT_ROOT:
				case IJavaElement.JAVA_PROJECT:
					result.add(elem);
					break;
				case IJavaElement.PACKAGE_FRAGMENT:
					if (((IPackageFragment)elem).containsJavaResources())
						result.add(elem);
					break;
				case IJavaElement.PACKAGE_DECLARATION:
					result.add(elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT));
					break;
				case IJavaElement.IMPORT_DECLARATION:
					IImportDeclaration decl= (IImportDeclaration)elem;
					if (decl.isOnDemand())
						elem= JavaModelUtil.findTypeContainer(elem.getJavaProject(), Signature.getQualifier(elem.getElementName()));
					else
						elem= elem.getJavaProject().findType(elem.getElementName());
					if (elem != null) {
						onlyContainers= false;
						result.add(elem);
					}
					break;
				case IJavaElement.CLASS_FILE:
					onlyContainers= false;
					result.add(((IClassFile)elem).getType());
					break;
				case IJavaElement.COMPILATION_UNIT:
					ICompilationUnit cu= (ICompilationUnit)elem;
					IType[] types= cu.getTypes();
					if (types.length > 0) {
						onlyContainers= false;
						result.addAll(Arrays.asList(types));
					}
			}
		} catch (JavaModelException e) {
			return e.getStatus();
		}
	}
	int size= result.size();
	if (size == 0 || (size > 1 && !onlyContainers))
		return createStatus(ActionMessages.OpenTypeHierarchyAction_messages_no_valid_java_element);
	return ok;
}
 
Example 8
Source File: OpenTypeHierarchyUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Converts the input to a possible input candidates
 * @param input input
 * @return the possible candidates
 */
public static IJavaElement[] getCandidates(Object input) {
	if (!(input instanceof IJavaElement)) {
		return null;
	}
	try {
		IJavaElement elem= (IJavaElement) input;
		switch (elem.getElementType()) {
			case IJavaElement.INITIALIZER:
			case IJavaElement.METHOD:
			case IJavaElement.FIELD:
			case IJavaElement.TYPE:
			case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			case IJavaElement.JAVA_PROJECT:
				return new IJavaElement[] { elem };
			case IJavaElement.PACKAGE_FRAGMENT:
				if (((IPackageFragment)elem).containsJavaResources())
					return new IJavaElement[] {elem};
				break;
			case IJavaElement.PACKAGE_DECLARATION:
				return new IJavaElement[] { elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT) };
			case IJavaElement.IMPORT_DECLARATION:
				IImportDeclaration decl= (IImportDeclaration) elem;
				if (decl.isOnDemand()) {
					elem= JavaModelUtil.findTypeContainer(elem.getJavaProject(), Signature.getQualifier(elem.getElementName()));
				} else {
					elem= elem.getJavaProject().findType(elem.getElementName());
				}
				if (elem == null)
					return null;
				return new IJavaElement[] {elem};

			case IJavaElement.CLASS_FILE:
				return new IJavaElement[] { ((IClassFile)input).getType() };
			case IJavaElement.COMPILATION_UNIT: {
				ICompilationUnit cu= (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
				if (cu != null) {
					IType[] types= cu.getTypes();
					if (types.length > 0) {
						return types;
					}
				}
				break;
			}
			default:
		}
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
	}
	return null;
}