1414package org .eclipse .jdt .internal .ui .refactoring .reorg ;
1515
1616import java .util .ArrayList ;
17+ import java .util .Collections ;
18+ import java .util .HashMap ;
1719import java .util .HashSet ;
1820import java .util .List ;
21+ import java .util .Map ;
1922import java .util .Set ;
2023
2124import org .eclipse .core .runtime .CoreException ;
4447public final class DestinationContentProvider extends StandardJavaElementContentProvider {
4548
4649 private IReorgDestinationValidator fValidator ;
50+ private final Map <IPath , Set <IPath >> classPathRoots = new HashMap <>();
4751
4852 public DestinationContentProvider (IReorgDestinationValidator validator ) {
4953 super (true );
@@ -71,8 +75,8 @@ public boolean hasChildren(Object element) {
7175 @ Override
7276 public Object [] getChildren (Object element ) {
7377 try {
74- if (element instanceof IJavaModel ) {
75- return concatenate (getJavaProjects (( IJavaModel ) element ) , getOpenNonJavaProjects (( IJavaModel ) element ));
78+ if (element instanceof IJavaModel javaModel ) {
79+ return concatenate (getJavaProjects (javaModel ) , getOpenNonJavaProjects (javaModel ));
7680 } else {
7781 Object [] children = doGetChildren (element );
7882 ArrayList <Object > result = new ArrayList <>(children .length );
@@ -91,8 +95,7 @@ public Object[] getChildren(Object element) {
9195 }
9296
9397 private Object [] doGetChildren (Object parentElement ) {
94- if (parentElement instanceof IContainer ) {
95- final IContainer container = (IContainer ) parentElement ;
98+ if (parentElement instanceof IContainer container ) {
9699 return getResources (container );
97100 }
98101 return super .getChildren (parentElement );
@@ -103,17 +106,12 @@ private Object[] getResources(IContainer container) {
103106 try {
104107 IResource [] members = container .members ();
105108 IJavaProject javaProject = JavaCore .create (container .getProject ());
106- if (javaProject == null || !javaProject .exists ())
109+ if (javaProject == null || !javaProject .exists ()) {
107110 return members ;
111+ }
108112 boolean isFolderOnClasspath = javaProject .isOnClasspath (container );
113+ Set <IPath > classRootPaths = getClassPathRoots (javaProject );
109114 List <IResource > nonJavaResources = new ArrayList <>();
110- Set <IPath > classRootPaths = new HashSet <>();
111- for (IPackageFragmentRoot classpathRoot : javaProject .getAllPackageFragmentRoots ()) {
112- IPath classRootPath = classpathRoot .getPath ();
113- if (classRootPath != null ) {
114- classRootPaths .add (classRootPath );
115- }
116- }
117115 // Can be on classpath but as a member of non-java resource folder
118116 for (IResource member : members ) {
119117 // A resource can also be a java element
@@ -134,6 +132,24 @@ private Object[] getResources(IContainer container) {
134132 }
135133 }
136134
135+ private synchronized Set <IPath > getClassPathRoots (IJavaProject javaProject ) {
136+ return classPathRoots .computeIfAbsent (javaProject .getPath (), key -> {
137+ Set <IPath > classRootPaths = new HashSet <>();
138+ try {
139+ for (IPackageFragmentRoot classpathRoot : javaProject .getAllPackageFragmentRoots ()) {
140+ IPath classRootPath = classpathRoot .getPath ();
141+ if (classRootPath != null ) {
142+ classRootPaths .add (classRootPath );
143+ }
144+ }
145+ return classRootPaths ;
146+ } catch (JavaModelException e ) {
147+ // Nothing else to do, this was the original behavior too.
148+ return Collections .emptySet ();
149+ }
150+ });
151+ }
152+
137153 private static Object [] getOpenNonJavaProjects (IJavaModel model ) throws JavaModelException {
138154 Object [] nonJavaProjects = model .getNonJavaResources ();
139155 ArrayList <IProject > result = new ArrayList <>(nonJavaProjects .length );
0 commit comments