8282import edu .rice .cs .plt .reflect .ReflectUtil ;
8383import edu .rice .cs .plt .iter .IterUtil ;
8484import edu .rice .cs .plt .io .IOUtil ;
85+ import edu .rice .cs .plt .tuple .Pair ;
8586
8687import edu .rice .cs .util .FileOpenSelector ;
8788import edu .rice .cs .util .FileOps ;
@@ -248,21 +249,69 @@ private static JavaVersion.FullVersion coarsenVersion(JavaVersion.FullVersion tV
248249 return tVersion ;
249250 }
250251
252+ // A pair of version and descriptor.
253+ // If the descriptor is something different than JDKDescriptor.NONE, then this pair will always
254+ // return false for equals(), except if it is compared to the identical pair.
255+ private static class VerDescPair implements Comparable <VerDescPair > {
256+ protected final JavaVersion .FullVersion _first ;
257+ protected final JDKDescriptor _second ;
258+
259+ public VerDescPair (JavaVersion .FullVersion first , JDKDescriptor second ) {
260+ _first = first ;
261+ _second = second ;
262+ }
263+
264+ public boolean equals (Object o ) {
265+ // identity --> true
266+ if (this == o ) { return true ; }
267+ // different class --> false
268+ else if (o == null || !getClass ().equals (o .getClass ())) { return false ; }
269+ else {
270+ VerDescPair cast = (VerDescPair ) o ;
271+ // only true if both versions are equal and both descriptors are NONE
272+ return
273+ (_first == null ? cast ._first == null : _first .equals (cast ._first )) &&
274+ (_second == null ? cast ._second == null :
275+ ((_second ==JDKDescriptor .NONE ) && (cast ._second ==JDKDescriptor .NONE )));
276+ }
277+ }
278+
279+ public int hashCode () {
280+ return
281+ (_first == null ? 0 : _first .hashCode ()) ^
282+ (_second == null ? 0 : _second .hashCode () << 1 ) ^
283+ getClass ().hashCode ();
284+ }
285+
286+ public int compareTo (VerDescPair o ) {
287+ int result = _first .compareTo (o ._first );
288+ if (result == 0 ) {
289+ result = System .identityHashCode (_second ) - System .identityHashCode (o ._second );
290+ }
291+ return result ;
292+ }
293+ }
294+
295+ // return a new version-descriptor pair for a library
296+ private VerDescPair getVerDescPair (JDKToolsLibrary lib ) {
297+ return new VerDescPair (coarsenVersion (lib .version ()), lib .jdkDescriptor ());
298+ }
299+
251300 private Iterable <JDKToolsLibrary > findLibraries () {
252301 // Order to return: config setting, runtime (if different version), from search (if different versions)
253302
254303 // We could give priority to libraries that have both available compilers and debuggers, but since this will
255304 // almost always be true, it seems like more trouble than it is worth
256305
257306 // map is sorted by version, lowest-to-highest
258- Map <JavaVersion . FullVersion , JDKToolsLibrary > results = new TreeMap <JavaVersion . FullVersion , JDKToolsLibrary >();
307+ Map <VerDescPair , JDKToolsLibrary > results = new TreeMap <VerDescPair , JDKToolsLibrary >();
259308
260309 File configTools = DrJava .getConfig ().getSetting (JAVAC_LOCATION );
261310 if (configTools != FileOps .NULL_FILE ) {
262311 JDKToolsLibrary fromConfig = JarJDKToolsLibrary .makeFromFile (configTools , this , JDKDescriptor .NONE );
263312 if (fromConfig .isValid ()) {
264313 JarJDKToolsLibrary .msg ("From config: " +fromConfig );
265- results .put (coarsenVersion (fromConfig . version () ), fromConfig );
314+ results .put (getVerDescPair (fromConfig ), fromConfig );
266315 }
267316 else { JarJDKToolsLibrary .msg ("From config: invalid " +fromConfig ); }
268317 }
@@ -271,11 +320,10 @@ private Iterable<JDKToolsLibrary> findLibraries() {
271320 Iterable <JDKToolsLibrary > allFromRuntime = JDKToolsLibrary .makeFromRuntime (this );
272321
273322 for (JDKToolsLibrary fromRuntime : allFromRuntime ) {
274- JavaVersion .FullVersion runtimeVersion = fromRuntime .version ();
275323 if (fromRuntime .isValid ()) {
276- if (!results .containsKey (coarsenVersion ( runtimeVersion ))) {
324+ if (!results .containsKey (getVerDescPair ( fromRuntime ))) {
277325 JarJDKToolsLibrary .msg ("From runtime: " +fromRuntime );
278- results .put (coarsenVersion ( runtimeVersion ), fromRuntime );
326+ results .put (getVerDescPair ( fromRuntime ), fromRuntime );
279327 }
280328 else { JarJDKToolsLibrary .msg ("From runtime: duplicate " +fromRuntime ); }
281329 }
@@ -289,9 +337,9 @@ private Iterable<JDKToolsLibrary> findLibraries() {
289337 JavaVersion .FullVersion coarsenedVersion = coarsenVersion (tVersion );
290338 JarJDKToolsLibrary .msg ("\t tVersion: " +tVersion +" " +tVersion .vendor ());
291339 JarJDKToolsLibrary .msg ("\t coarsenedVersion: " +coarsenedVersion +" " +coarsenedVersion .vendor ());
292- if (!results .containsKey (coarsenedVersion )) {
340+ if (!results .containsKey (getVerDescPair ( t ) )) {
293341 JarJDKToolsLibrary .msg ("\t added" );
294- results .put (coarsenedVersion , t );
342+ results .put (getVerDescPair ( t ) , t );
295343 }
296344 else { JarJDKToolsLibrary .msg ("\t duplicate" ); }
297345 }
0 commit comments