1.1 --- a/openide.util/src/org/openide/util/lookup/ProxyLookup.java Fri Oct 30 11:02:38 2009 +0100
1.2 +++ b/openide.util/src/org/openide/util/lookup/ProxyLookup.java Fri Oct 30 10:41:18 2009 -0400
1.3 @@ -394,12 +394,14 @@
1.4 return weakL.getResults();
1.5 }
1.6
1.7 + for (int i = 0; i < arr.length; i++) {
1.8 + arr[i].addLookupListener(weakL);
1.9 + }
1.10 +
1.11 weakL.setResults(arr);
1.12 +
1.13 + return arr;
1.14 }
1.15 - for (int i = 0; i < arr.length; i++) {
1.16 - arr[i].addLookupListener(weakL);
1.17 - }
1.18 - return arr;
1.19 }
1.20 }
1.21
1.22 @@ -469,7 +471,6 @@
1.23 }
1.24
1.25 listeners.add(LookupListener.class, l);
1.26 - initResults();
1.27 }
1.28
1.29 /** Just delegates.
1.30 @@ -518,9 +519,8 @@
1.31
1.32 // if the call to beforeLookup resulted in deletion of caches
1.33 synchronized (proxy()) {
1.34 - Collection[] cc = getCache();
1.35 - if (cc != null && cc != NO_CACHE) {
1.36 - Collection result = cc[indexToCache];
1.37 + if (getCache() != null) {
1.38 + Collection result = getCache()[indexToCache];
1.39 if (result != null) {
1.40 return result;
1.41 }
1.42 @@ -561,16 +561,15 @@
1.43
1.44
1.45 synchronized (proxy()) {
1.46 - Collection[] cc = getCache();
1.47 - if (cc == null || cc == NO_CACHE) {
1.48 + if (getCache() == null) {
1.49 // initialize the cache to indicate this result is in use
1.50 - setCache(cc = new Collection[3]);
1.51 + setCache(new Collection[3]);
1.52 }
1.53
1.54 if (arr == weakL.getResults()) {
1.55 // updates the results, if the results have not been
1.56 // changed during the computation of allInstances
1.57 - cc[indexToCache] = ret;
1.58 + getCache()[indexToCache] = ret;
1.59 }
1.60 }
1.61
1.62 @@ -584,58 +583,49 @@
1.63 }
1.64
1.65 protected void collectFires(Collection<Object> evAndListeners) {
1.66 + // clear cached instances
1.67 + Collection oldItems;
1.68 + Collection oldInstances;
1.69 + synchronized (proxy()) {
1.70 + if (getCache() == null) {
1.71 + // nobody queried the result yet
1.72 + return;
1.73 + }
1.74 + oldInstances = getCache()[0];
1.75 + oldItems = getCache()[2];
1.76 +
1.77 +
1.78 + if (listeners == null || listeners.getListenerCount() == 0) {
1.79 + // clear the cache
1.80 + setCache(new Collection[3]);
1.81 + return;
1.82 + }
1.83 +
1.84 + // ignore events if they arrive as a result of call to allItems
1.85 + // or allInstances, bellow...
1.86 + setCache(null);
1.87 + }
1.88 +
1.89 boolean modified = true;
1.90
1.91 - try {
1.92 - // clear cached instances
1.93 - Collection oldItems;
1.94 - Collection oldInstances;
1.95 - synchronized (proxy()) {
1.96 - final Collection[] cc = getCache();
1.97 - if (cc == NO_CACHE) {
1.98 - return;
1.99 - }
1.100 -
1.101 - oldInstances = cc == null ? null : cc[0];
1.102 - oldItems = cc == null ? null : cc[2];
1.103 -
1.104 -
1.105 - if (listeners == null || listeners.getListenerCount() == 0) {
1.106 - // clear the cache
1.107 - setCache(new Collection[3]);
1.108 - return;
1.109 - }
1.110 -
1.111 - // ignore events if they arrive as a result of call to allItems
1.112 - // or allInstances, bellow...
1.113 - setCache(NO_CACHE);
1.114 + if (oldItems != null) {
1.115 + Collection newItems = allItems();
1.116 + if (oldItems.equals(newItems)) {
1.117 + modified = false;
1.118 }
1.119 -
1.120 - if (oldItems != null) {
1.121 - Collection newItems = allItems();
1.122 - if (oldItems.equals(newItems)) {
1.123 + } else {
1.124 + if (oldInstances != null) {
1.125 + Collection newInstances = allInstances();
1.126 + if (oldInstances.equals(newInstances)) {
1.127 modified = false;
1.128 }
1.129 } else {
1.130 - if (oldInstances != null) {
1.131 - Collection newInstances = allInstances();
1.132 - if (oldInstances.equals(newInstances)) {
1.133 - modified = false;
1.134 + synchronized (proxy()) {
1.135 + if (getCache() == null) {
1.136 + // we have to initialize the cache
1.137 + // to show that the result has been initialized
1.138 + setCache(new Collection[3]);
1.139 }
1.140 - } else {
1.141 - synchronized (proxy()) {
1.142 - if (getCache() == NO_CACHE) {
1.143 - // we have to initialize the cache
1.144 - // to show that the result has been initialized
1.145 - setCache(new Collection[3]);
1.146 - }
1.147 - }
1.148 - }
1.149 - }
1.150 - } finally {
1.151 - synchronized (proxy()) {
1.152 - if (getCache() == NO_CACHE) {
1.153 - setCache(null);
1.154 }
1.155 }
1.156 }
1.157 @@ -683,7 +673,6 @@
1.158 assert Thread.holdsLock(proxy());
1.159 this.cache = cache;
1.160 }
1.161 - private static final Collection[] NO_CACHE = new Collection[0];
1.162 }
1.163 private static final class WeakRef<T> extends WeakReference<R> implements Runnable {
1.164 final WeakResult<T> result;
2.1 --- a/openide.util/test/unit/src/org/openide/util/lookup/AbstractLookupBaseHid.java Fri Oct 30 11:02:38 2009 +0100
2.2 +++ b/openide.util/test/unit/src/org/openide/util/lookup/AbstractLookupBaseHid.java Fri Oct 30 10:41:18 2009 -0400
2.3 @@ -1386,12 +1386,12 @@
2.4 public void testChangeOfNodeDoesNotFireChangeInActionMap() {
2.5 ActionMap am = new ActionMap();
2.6 Lookup s = Lookups.singleton(am);
2.7 - doChangeOfNodeDoesNotFireChangeInActionMap(am, s, false, 0);
2.8 + doChangeOfNodeDoesNotFireChangeInActionMap(am, s, false);
2.9 }
2.10 public void testChangeOfNodeDoesNotFireChangeInActionMapSimple() {
2.11 ActionMap am = new ActionMap();
2.12 Lookup s = Lookups.singleton(am);
2.13 - doChangeOfNodeDoesNotFireChangeInActionMap(am, s, true, 0);
2.14 + doChangeOfNodeDoesNotFireChangeInActionMap(am, s, true);
2.15 }
2.16
2.17 public void testChangeOfNodeDoesNotFireChangeInActionMapWithBeforeLookupSimple() {
2.18 @@ -1424,12 +1424,12 @@
2.19 }
2.20
2.21 Before s = new Before();
2.22 - doChangeOfNodeDoesNotFireChangeInActionMap(am, s, wrapBySimple, 1);
2.23 + doChangeOfNodeDoesNotFireChangeInActionMap(am, s, wrapBySimple);
2.24
2.25 assertNull("beforeLookup called once", s.ic);
2.26 }
2.27
2.28 - private void doChangeOfNodeDoesNotFireChangeInActionMap(final ActionMap am, Lookup actionMapLookup, final boolean wrapBySimple, int firstChange) {
2.29 + private void doChangeOfNodeDoesNotFireChangeInActionMap(final ActionMap am, Lookup actionMapLookup, final boolean wrapBySimple) {
2.30 Lookup[] lookups = { lookup, actionMapLookup };
2.31
2.32 class Provider implements Lookup.Provider {
2.33 @@ -1470,7 +1470,7 @@
2.34 ActionMap am1 = (ActionMap)c.iterator().next();
2.35 assertEquals("Am is there", am, am1);
2.36
2.37 - assertEquals("Correct # of changes in first get", firstChange, ll.getCount());
2.38 + assertEquals("No change in first get", 0, ll.getCount());
2.39
2.40 Object m1 = new InputMap();
2.41 Object m2 = new InputMap();
3.1 --- a/openide.util/test/unit/src/org/openide/util/lookup/AbstractLookupTest.java Fri Oct 30 11:02:38 2009 +0100
3.2 +++ b/openide.util/test/unit/src/org/openide/util/lookup/AbstractLookupTest.java Fri Oct 30 10:41:18 2009 -0400
3.3 @@ -47,7 +47,6 @@
3.4 import java.util.*;
3.5 import java.util.concurrent.Executors;
3.6 import java.util.concurrent.TimeUnit;
3.7 -import junit.framework.Test;
3.8 import org.netbeans.junit.*;
3.9 import org.openide.util.Lookup;
3.10 import org.openide.util.lookup.AbstractLookup.Pair;
3.11 @@ -79,7 +78,11 @@
3.12
3.13 public void clearCaches () {
3.14 }
3.15 -
3.16 +
3.17 + public static void main(java.lang.String[] args) {
3.18 + junit.textui.TestRunner.run(new NbTestSuite(AbstractLookupTest.class));
3.19 + }
3.20 +
3.21 static class LkpResultCanBeGargageCollectedAndClearsTheResult extends AbstractLookup {
3.22 public int cleared;
3.23 public int dirty;
4.1 --- a/openide.util/test/unit/src/org/openide/util/lookup/ProxyLookup173975Test.java Fri Oct 30 11:02:38 2009 +0100
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,145 +0,0 @@
4.4 -/*
4.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4.6 - *
4.7 - * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
4.8 - *
4.9 - * The contents of this file are subject to the terms of either the GNU
4.10 - * General Public License Version 2 only ("GPL") or the Common
4.11 - * Development and Distribution License("CDDL") (collectively, the
4.12 - * "License"). You may not use this file except in compliance with the
4.13 - * License. You can obtain a copy of the License at
4.14 - * http://www.netbeans.org/cddl-gplv2.html
4.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
4.16 - * specific language governing permissions and limitations under the
4.17 - * License. When distributing the software, include this License Header
4.18 - * Notice in each file and include the License file at
4.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
4.20 - * particular file as subject to the "Classpath" exception as provided
4.21 - * by Sun in the GPL Version 2 section of the License file that
4.22 - * accompanied this code. If applicable, add the following below the
4.23 - * License Header, with the fields enclosed by brackets [] replaced by
4.24 - * your own identifying information:
4.25 - * "Portions Copyrighted [year] [name of copyright owner]"
4.26 - *
4.27 - * Contributor(s):
4.28 - *
4.29 - * The Original Software is NetBeans. The Initial Developer of the Original
4.30 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
4.31 - * Microsystems, Inc. All Rights Reserved.
4.32 - *
4.33 - * If you wish your version of this file to be governed by only the CDDL
4.34 - * or only the GPL Version 2, indicate your decision by adding
4.35 - * "[Contributor] elects to include this software in this distribution
4.36 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
4.37 - * single choice of license, a recipient has the option to distribute
4.38 - * your version of this file under either the CDDL, the GPL Version 2 or
4.39 - * to extend the choice of license to its licensees as provided above.
4.40 - * However, if you add GPL Version 2 code and therefore, elected the GPL
4.41 - * Version 2 license, then the option applies only if the new code is
4.42 - * made subject to such option by the copyright holder.
4.43 - */
4.44 -
4.45 -package org.openide.util.lookup;
4.46 -
4.47 -import java.util.Collection;
4.48 -import java.util.Collections;
4.49 -import org.junit.Assert;
4.50 -import org.junit.Test;
4.51 -import org.openide.util.Lookup;
4.52 -import org.openide.util.LookupEvent;
4.53 -import org.openide.util.LookupListener;
4.54 -import org.openide.util.lookup.AbstractLookup.Storage;
4.55 -
4.56 -public class ProxyLookup173975Test {
4.57 -
4.58 - public ProxyLookup173975Test() {
4.59 - }
4.60 -
4.61 - boolean called = false;
4.62 -
4.63 - @Test
4.64 - public void testAbstractLookupWithoutAllInstances() {
4.65 - registerLookupListenerAndAddSomething(false, false, false);
4.66 - }
4.67 -
4.68 - @Test
4.69 - public void testAbstractLookupWithAllInstances() {
4.70 - registerLookupListenerAndAddSomething(false, true, false);
4.71 - }
4.72 -
4.73 - @Test
4.74 - public void testAbstractLookupInheritanceTreeWithoutAllInstances() {
4.75 - registerLookupListenerAndAddSomething(false, false, true);
4.76 - }
4.77 -
4.78 - @Test
4.79 - public void testAbstractLookupInheritanceTreeWithAllInstances() {
4.80 - registerLookupListenerAndAddSomething(false, true, true);
4.81 - }
4.82 -
4.83 - @Test
4.84 - public void testProxyLookupWithoutAllInstances() {
4.85 - registerLookupListenerAndAddSomething(true, false, false);
4.86 - }
4.87 -
4.88 - @Test
4.89 - public void testProxyLookupWithAllInstances() {
4.90 - registerLookupListenerAndAddSomething(true, true, false);
4.91 - }
4.92 -
4.93 - @Test
4.94 - public void testProxyLookupInheritanceTreeWithoutAllInstances() {
4.95 - registerLookupListenerAndAddSomething(true, false, true);
4.96 - }
4.97 -
4.98 - @Test
4.99 - public void testProxyLookupInheritanceTreeWithAllInstances() {
4.100 - registerLookupListenerAndAddSomething(true, true, true);
4.101 - }
4.102 -
4.103 - private void registerLookupListenerAndAddSomething(boolean useProxy, boolean callAllInstances, boolean inheritanceTree) {
4.104 - called = false;
4.105 - InstanceContent aInstanceContent = new InstanceContent();
4.106 - Storage<?> s = inheritanceTree ? new InheritanceTree() : new ArrayStorage();
4.107 - Lookup aLookup = new AbstractLookup(aInstanceContent, s);
4.108 - if (useProxy) {
4.109 - aLookup = new ProxyLookup(aLookup);
4.110 - }
4.111 - Lookup.Result<ObjectInLookup> result = aLookup.lookupResult(ObjectInLookup.class);
4.112 - if (callAllInstances) {
4.113 - result.allInstances(); // TO GET SUCCESS
4.114 - }
4.115 - result.addLookupListener(new LookupListener() {
4.116 -
4.117 - public void resultChanged(LookupEvent ev) {
4.118 - Lookup.Result aResult = (Lookup.Result) ev.getSource();
4.119 - Collection c = aResult.allInstances();
4.120 - if (!c.isEmpty()) {
4.121 - called = true;
4.122 - }
4.123 - }
4.124 - });
4.125 -
4.126 - aInstanceContent.set(Collections.singleton(
4.127 - new ObjectInLookup("Set Object in Lookup)")), null);
4.128 - Assert.assertTrue("Listener was notified", called);
4.129 - }
4.130 -
4.131 - public class ObjectInLookup {
4.132 -
4.133 - private final String name;
4.134 -
4.135 - public ObjectInLookup(String name) {
4.136 - this.name = name;
4.137 - }
4.138 -
4.139 - public String getName() {
4.140 - return this.name;
4.141 - }
4.142 -
4.143 - @Override
4.144 - public String toString() {
4.145 - return "objectinlookup:" + getName();
4.146 - }
4.147 - }
4.148 -}