[jbosstools-commits] JBoss Tools SVN: r35677 - in trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model: util and 1 other directory.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Oct 14 16:30:21 EDT 2011


Author: scabanovich
Date: 2011-10-14 16:30:20 -0400 (Fri, 14 Oct 2011)
New Revision: 35677

Modified:
   trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/Libs.java
   trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/util/EclipseResourceUtil.java
Log:
JBIDE-9906
https://issues.jboss.org/browse/JBIDE-9906
Jars exported by parent projects are taken into account.

Modified: trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/Libs.java
===================================================================
--- trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/Libs.java	2011-10-14 18:56:11 UTC (rev 35676)
+++ trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/filesystems/impl/Libs.java	2011-10-14 20:30:20 UTC (rev 35677)
@@ -11,7 +11,6 @@
 package org.jboss.tools.common.model.filesystems.impl;
 
 import java.io.File;
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -20,15 +19,18 @@
 import java.util.Set;
 
 import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.jdt.core.ElementChangedEvent;
+import org.eclipse.jdt.core.IClasspathEntry;
 import org.eclipse.jdt.core.IElementChangedListener;
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.IJavaElementDelta;
 import org.eclipse.jdt.core.IJavaProject;
 import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
 import org.jboss.tools.common.model.XModelObject;
 import org.jboss.tools.common.model.XModelObjectConstants;
 import org.jboss.tools.common.model.plugin.ModelPlugin;
@@ -44,6 +46,7 @@
 	protected FileSystemsImpl object;
 	protected List<String> paths = null;
 	Map<IPath, String> paths2 = new HashMap<IPath, String>();
+	Set<String> projects = new HashSet<String>();
 
 	LibraryNames libraryNames = new LibraryNames();
 
@@ -58,11 +61,6 @@
 	}
 
 	private IProject getProjectResource() {
-		try {
-			EclipseResourceUtil.getProject(object);
-		} catch (NullPointerException e) {
-			e.printStackTrace();
-		}
 		return EclipseResourceUtil.getProject(object);
 	}
 
@@ -107,17 +105,33 @@
 	private List<String> getNewPaths() {
 		List<String> result = null;
 		try {
-			result = EclipseResourceUtil.getClassPath(getProjectResource());
+			result = EclipseResourceUtil.getAllVisibleLibraries(getProjectResource());
 			List<String> jre = EclipseResourceUtil.getJREClassPath(getProjectResource());
 			if(jre != null) result.removeAll(jre);
+			updateProjects();
 		} catch (CoreException e) {
 			ModelPlugin.getDefault().logError(e);
-		} catch(IOException e) {
-			ModelPlugin.getDefault().logError(e);			
 		}
 		return result;
 	}
 
+	private void updateProjects() throws JavaModelException {
+		Set<String> result = new HashSet<String>();
+		IJavaProject javaProject = EclipseResourceUtil.getJavaProject(getProjectResource());
+		if(javaProject != null) {
+			result.add(getProjectResource().getName());
+			IClasspathEntry[] es = javaProject.getResolvedClasspath(true);
+			for (int i = 0; i < es.length; i++) {
+				if(es[i].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
+					IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(es[i].getPath().lastSegment());
+					if(p == null || !p.isAccessible()) continue;
+					result.add(p.getName());
+				}
+			}
+		}
+		projects = result;
+	}
+
 	private synchronized boolean updatePaths(List<String> newPaths, int cpv) {
 		if(cpv <= pathsVersion) {
 			return false;
@@ -247,32 +261,30 @@
 			return;
 		}
 
-		IJavaElementDelta d = event.getDelta();
-		IJavaElementDelta[] ds = d.getAffectedChildren();
-		IJavaElementDelta p = null;
-
-		for (IJavaElementDelta dc: ds) {
-			if(dc.getElement() instanceof IJavaProject && ((IJavaProject)dc.getElement()).getProject() == project) {
-				p = dc;
-			}
-		}
-		if(p == null) return;
-		int f = p.getFlags();
-		if((f & (IJavaElementDelta.F_CLASSPATH_CHANGED 
-			| IJavaElementDelta.F_RESOLVED_CLASSPATH_CHANGED)) != 0) {
-			requestForUpdate();
-		} else {
-			IJavaElementDelta[] ds1 = p.getAffectedChildren();
-			for (IJavaElementDelta d1: ds1) {
-				IJavaElement e = d1.getElement();
-				if(d1.getKind() == IJavaElementDelta.ADDED) {
+		for (IJavaElementDelta dc: event.getDelta().getAffectedChildren()) {
+			if(dc.getElement() instanceof IJavaProject && (isReleventProject(((IJavaProject)dc.getElement()).getProject()))) {
+				int f = dc.getFlags();
+				if((f & (IJavaElementDelta.F_CLASSPATH_CHANGED 
+					| IJavaElementDelta.F_RESOLVED_CLASSPATH_CHANGED)) != 0) {
 					requestForUpdate();
-					break;
+					return;
+				} else {
+					for (IJavaElementDelta d1: dc.getAffectedChildren()) {
+						IJavaElement e = d1.getElement();
+						if(d1.getKind() == IJavaElementDelta.ADDED) {
+							requestForUpdate();
+							return;
+						}
+					}
 				}
 			}
 		}
 	}
 
+	private boolean isReleventProject(IProject p) {
+		return projects.contains(p.getName());
+	}
+
 }
 
 class LibraryNames {

Modified: trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/util/EclipseResourceUtil.java
===================================================================
--- trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/util/EclipseResourceUtil.java	2011-10-14 18:56:11 UTC (rev 35676)
+++ trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/model/util/EclipseResourceUtil.java	2011-10-14 20:30:20 UTC (rev 35677)
@@ -635,58 +635,58 @@
 	 * @throws IOException 
 	 */	
 	public static List<String> getClassPath(IProject project) throws CoreException, IOException {
-		if(project == null || !project.isAccessible() || !project.hasNature(JavaCore.NATURE_ID)) return null;
+		IJavaProject javaProject = getJavaProject(project);
+		if(javaProject == null) {
+			return null;
+		}
+
 		ArrayList<String> l = new ArrayList<String>();
-		IJavaProject javaProject = JavaCore.create(project);		
-
 		IClasspathEntry[] es = javaProject.getResolvedClasspath(true);
 		for (int i = 0; i < es.length; i++) {
 			if(es[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
-				String s = null;
-				String path = es[i].getPath().toString();
-				try {
-					//First let's check if path is defined within Eclipse work space.
-					if(path.startsWith(XModelObjectConstants.SEPARATOR) && path.indexOf(XModelObjectConstants.SEPARATOR, 1) > 1) {
-						IResource findMember = ResourcesPlugin.getWorkspace().getRoot().findMember(es[i].getPath());
-						if(findMember != null) {
-							s = findMember.getLocation().toString();
-						}
-					}
-					//If search in Eclipse work space has failed, this is a useless attempt, but
-					//let keep it just in case (this is good old code that worked for a long while).
-					if(s == null && path.startsWith(XModelObjectConstants.SEPARATOR + project.getName() + XModelObjectConstants.SEPARATOR)) {
-						IResource findMember = project.findMember(es[i].getPath().removeFirstSegments(1));
-						if(findMember != null) {
-							s = findMember.getLocation().toString();
-						}
-					}
-				
-					//If we failed to find resource in Eclipse work space, 
-					//lets try the path as absolute on disk
-					if(s == null && new java.io.File(path).exists()) {
-						s = path;
-					}
-					if(s != null) {
-						l.add(new java.io.File(s).getCanonicalPath());
-					}				
-				} catch (IOException e) {
-					//ignore - we do not care about malformed URLs in classpath here.
+				String s = expandPath(es[i].getPath(), project);
+				if(s != null) {
+					l.add(s);
 				}
-			} else if(es[i].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
-//				IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(es[i].getPath().lastSegment());
-//				if(p == null || !p.isAccessible()) continue;
-//				if(p.hasNature(JavaCore.NATURE_ID) 
-//						&& !p.hasNature("org.jboss.tools.jst.web.kb.kbnature")
-//						&& project.hasNature("org.jboss.tools.jst.web.kb.kbnature")) {
-//					String[] srcs = getJavaProjectSrcLocations(p);
-//					for (String s: srcs) l.add(s);
-//				}
-
 			}
 		}
 		return l;
 	}
+
+	static String expandPath(IPath ipath, IProject project) {
+		String s = null;
+		String path = ipath.toString();
+		//First let's check if path is defined within Eclipse work space.
+		if(path.startsWith(XModelObjectConstants.SEPARATOR) && path.indexOf(XModelObjectConstants.SEPARATOR, 1) > 1) {
+			IResource findMember = ResourcesPlugin.getWorkspace().getRoot().findMember(ipath);
+			if(findMember != null) {
+				s = findMember.getLocation().toString();
+			}
+		}
+		//If search in Eclipse work space has failed, this is a useless attempt, but
+		//let keep it just in case (this is good old code that worked for a long while).
+		if(s == null && path.startsWith(XModelObjectConstants.SEPARATOR + project.getName() + XModelObjectConstants.SEPARATOR)) {
+			IResource findMember = project.findMember(ipath.removeFirstSegments(1));
+			if(findMember != null) {
+				s = findMember.getLocation().toString();
+			}
+		}
 	
+		//If we failed to find resource in Eclipse work space, 
+		//lets try the path as absolute on disk
+		if(s == null && new java.io.File(path).exists()) {
+			s = path;
+		}
+		try {
+			if(s != null) {
+				return new java.io.File(s).getCanonicalPath();
+			}				
+		} catch (IOException e) {
+			//ignore - we do not care about malformed URLs in classpath here.
+		}
+		return null;
+	}
+	
 	public static List<String> getJREClassPath(IProject project) throws CoreException {
 		if(project == null || !project.isAccessible() || !project.hasNature(JavaCore.NATURE_ID)) return null;
 		ArrayList<String> l = new ArrayList<String>();
@@ -904,6 +904,56 @@
 		return new SourceFoldersCollector(project).folders;
 	}
 
+	private static class LibraryCollector {
+		IProject project;
+		List<String> ordered = new ArrayList<String>();
+		Set<String> paths = new HashSet<String>();
+		Set<IProject> processed = new HashSet<IProject>();
+
+		LibraryCollector(IProject project) {
+			this.project = project;
+			process(project);
+		}
+		
+		void process(IProject project) {
+			if(processed.contains(project)) {
+				return;
+			}
+			processed.add(project);
+			IJavaProject javaProject = getJavaProject(project);
+			if(javaProject == null) {
+				return;
+			}
+			IClasspathEntry[] es = null;
+			try {
+				es = javaProject.getResolvedClasspath(true);
+			} catch (CoreException e) {
+				ModelPlugin.getDefault().logError(e);
+				return;
+			}
+			for (int i = 0; i < es.length; i++) {
+				if(project == this.project || es[i].isExported()) {
+					if(es[i].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
+						IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(es[i].getPath().lastSegment());
+						if(p != null && p.isAccessible()) {
+							process(p);
+						}
+					} else if(es[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
+						String s = expandPath(es[i].getPath(), project);
+						if(s != null && !paths.contains(s)) {
+							paths.add(s);
+							ordered.add(s);
+						}	
+					}
+				}
+			}
+		}
+	}
+
+	public static List<String> getAllVisibleLibraries(IProject project) {
+		return new LibraryCollector(project).ordered;
+	}
+
 	public static void openResource(IResource resource) {
 		XModelObject o = getObjectByResource(resource);
 		if(o == null) o = createObjectForResource(resource);



More information about the jbosstools-commits mailing list