Java Code Examples for org.eclipse.jdt.core.ICompilationUnit#isStructureKnown()

The following examples show how to use org.eclipse.jdt.core.ICompilationUnit#isStructureKnown() . 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: Checks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * From SearchResultGroup[] passed as the parameter
 * this method removes all those that correspond to a non-parsable ICompilationUnit
 * and returns it as a result.
 * @param grouped the array of search result groups from which non parsable compilation
 *  units are to be removed.
 * @param status a refactoring status to collect errors and problems
 * @return the array of search result groups
 * @throws JavaModelException
 */
public static SearchResultGroup[] excludeCompilationUnits(SearchResultGroup[] grouped, RefactoringStatus status) throws JavaModelException{
	List<SearchResultGroup> result= new ArrayList<SearchResultGroup>();
	boolean wasEmpty= grouped.length == 0;
	for (int i= 0; i < grouped.length; i++){
		IResource resource= grouped[i].getResource();
		IJavaElement element= JavaCore.create(resource);
		if (! (element instanceof ICompilationUnit))
			continue;
		//XXX this is a workaround 	for a jcore feature that shows errors in cus only when you get the original element
		ICompilationUnit cu= (ICompilationUnit)JavaCore.create(resource);
		if (! cu.isStructureKnown()){
			status.addError(Messages.format(RefactoringCoreMessages.Checks_cannot_be_parsed, BasicElementLabels.getPathLabel(cu.getPath(), false)));
			continue; //removed, go to the next one
		}
		result.add(grouped[i]);
	}

	if ((!wasEmpty) && result.isEmpty())
		status.addFatalError(RefactoringCoreMessages.Checks_all_excluded);

	return result.toArray(new SearchResultGroup[result.size()]);
}
 
Example 2
Source File: StringCleanUp.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int computeNumberOfFixes(CompilationUnit compilationUnit) {
	try {
		ICompilationUnit cu= (ICompilationUnit)compilationUnit.getJavaElement();
		if (!cu.isStructureKnown())
			return 0; //[clean up] 'Remove unnecessary $NLS-TAGS$' removes necessary ones in case of syntax errors: https://bugs.eclipse.org/bugs/show_bug.cgi?id=285814 : 
	} catch (JavaModelException e) {
		return 0;
	}
	
	int result= 0;
	IProblem[] problems= compilationUnit.getProblems();
	if (isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS))
		result+= getNumberOfProblems(problems, IProblem.NonExternalizedStringLiteral);

	if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS))
		result+= getNumberOfProblems(problems, IProblem.UnnecessaryNLSTag);

	return result;
}
 
Example 3
Source File: BuilderUtils.java    From JReFrameworker with MIT License 5 votes vote down vote up
/**
 * Returns a collection of K_SOURCE Compilation units in the project's package fragments
 * Reference: https://www.eclipse.org/forums/index.php/t/68072/
 * @param javaproject
 * @return
 */
public static final ICompilationUnit[] getSourceCompilationUnits(IJavaProject jProject) {
	ArrayList<ICompilationUnit> sourceCompilationUnits = new ArrayList<ICompilationUnit>();
	try {
		IPackageFragmentRoot[] roots = jProject.getPackageFragmentRoots();
		for (int i = 0; i < roots.length; i++) {
			IPackageFragmentRoot root = roots[i];
			if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
				IJavaElement[] javaElements = root.getChildren();
				for (int j = 0; j < javaElements.length; j++) {
					IJavaElement javaElement = javaElements[j];
					if (javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
						IPackageFragment pf = (IPackageFragment) javaElement;
						ICompilationUnit[] compilationUnits = pf.getCompilationUnits();
						for (int k = 0; k < compilationUnits.length; k++) {
							ICompilationUnit unit = compilationUnits[k];
							if (unit.isStructureKnown()) {
								sourceCompilationUnits.add(unit);
							}
						}
					}
				}
			}
		}
	} catch (CoreException e) {
		e.printStackTrace();
	}
	ICompilationUnit[] sourceCompilationUnitsArray = new ICompilationUnit[sourceCompilationUnits.size()];
	sourceCompilationUnits.toArray(sourceCompilationUnitsArray);
	return sourceCompilationUnitsArray;
}
 
Example 4
Source File: Checks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static RefactoringStatus checkIfCuBroken(IMember member) throws JavaModelException{
	ICompilationUnit cu= (ICompilationUnit)JavaCore.create(member.getCompilationUnit().getResource());
	if (cu == null)
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.Checks_cu_not_created);
	else if (! cu.isStructureKnown())
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.Checks_cu_not_parsed);
	return new RefactoringStatus();
}