/** * Update cycle markers for all java projects */ public static void updateAllCycleMarkers() throws JavaModelException { JavaModelManager manager = JavaModelManager.getJavaModelManager(); IJavaProject[] projects = manager.getJavaModel().getJavaProjects(); IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); HashSet cycleParticipants = new HashSet(); HashSet alreadyTraversed = new HashSet(); int length = projects.length; // compute cycle participants ArrayList prereqChain = new ArrayList(); for (int i = 0; i < length; i++){ JavaProject project = (JavaProject)projects[i]; if (!alreadyTraversed.contains(project)){ prereqChain.clear(); project.updateCycleParticipants(null, prereqChain, cycleParticipants, workspaceRoot, alreadyTraversed); } } for (int i = 0; i < length; i++){ JavaProject project = (JavaProject)projects[i]; if (cycleParticipants.contains(project)){ IMarker cycleMarker = project.getCycleMarker(); String circularCPOption = project.getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true); int circularCPSeverity = JavaCore.ERROR.equals(circularCPOption) ? IMarker.SEVERITY_ERROR : IMarker.SEVERITY_WARNING; if (cycleMarker != null) { // update existing cycle marker if needed try { int existingSeverity = ((Integer)cycleMarker.getAttribute(IMarker.SEVERITY)).intValue(); if (existingSeverity != circularCPSeverity) { cycleMarker.setAttribute(IMarker.SEVERITY, circularCPSeverity); } } catch (CoreException e) { throw new JavaModelException(e); } } else { // create new marker project.createClasspathProblemMarker( new JavaModelStatus(IJavaModelStatusConstants.CLASSPATH_CYCLE, project)); } } else { project.flushClasspathProblemMarkers(true, false); } } } /** * If a cycle is detected, then cycleParticipants contains all the project involved in this cycle (directly and indirectly), * no cycle if the set is empty (and started empty) */ public void updateCycleParticipants( IClasspathEntry[] preferredClasspath, ArrayList prereqChain, HashSet cycleParticipants, IWorkspaceRoot workspaceRoot, HashSet alreadyTraversed){ prereqChain.add(this); try { IClasspathEntry[] classpath = preferredClasspath == null ? getResolvedClasspath(true) : preferredClasspath; for (int i = 0, length = classpath.length; i < length; i++) { IClasspathEntry entry = classpath[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT){ IPath entryPath = entry.getPath(); IResource member = workspaceRoot.findMember(entryPath); if (member != null && member.getType() == IResource.PROJECT){ JavaProject project = (JavaProject)JavaCore.create((IProject)member); int index = cycleParticipants.contains(project) ? 0 : prereqChain.indexOf(project); if (index >= 0) { // refer to cycle, or in cycle itself for (int size = prereqChain.size(); index < size; index++) { cycleParticipants.add(prereqChain.get(index)); } } else { if (!alreadyTraversed.contains(project)) { project.updateCycleParticipants(null, prereqChain, cycleParticipants, workspaceRoot, alreadyTraversed); } } } } } } catch(JavaModelException e){ } prereqChain.remove(this); alreadyTraversed.add(this); }