No need to escape exceptions in recursionDetected()

This commit is contained in:
Andrey Breslav
2013-10-03 13:12:38 +04:00
parent db34868b6e
commit 4cd5c66c27
5 changed files with 25 additions and 27 deletions
@@ -50,7 +50,6 @@ import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.storage.NotNullLazyValue;
import org.jetbrains.jet.storage.NullableLazyValue;
import org.jetbrains.jet.storage.StorageManager;
import org.jetbrains.jet.utils.WrappedValues;
import java.util.*;
@@ -397,10 +396,10 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements LazyDesc
}
}
},
new Function<Boolean, Object>() {
new Function<Boolean, Collection<JetType>>() {
@Override
public Object fun(Boolean firstTime) {
return WrappedValues.unescapeExceptionOrNull(Collections.emptyList());
public Collection<JetType> fun(Boolean firstTime) {
return Collections.emptyList();
}
},
new Consumer<Collection<JetType>>() {
@@ -4,7 +4,6 @@ import com.intellij.openapi.util.Computable;
import com.intellij.util.Consumer;
import com.intellij.util.Function;
import junit.framework.TestCase;
import org.jetbrains.jet.utils.WrappedValues;
import java.util.ArrayList;
import java.util.Arrays;
@@ -264,9 +263,9 @@ public class StorageManagerTest extends TestCase {
return rec.compute();
}
},
new Function<Boolean, Object>() {
new Function<Boolean, String>() {
@Override
public Object fun(Boolean aBoolean) {
public String fun(Boolean aBoolean) {
return "tolerant";
}
},
@@ -347,11 +346,11 @@ public class StorageManagerTest extends TestCase {
return rec.compute();
}
},
new Function<Boolean, Object>() {
new Function<Boolean, String>() {
@Override
public Object fun(Boolean firstTime) {
public String fun(Boolean firstTime) {
if (firstTime) {
return WrappedValues.escapeThrowable(new ReenteringLazyValueComputationException());
throw new ReenteringLazyValueComputationException();
}
return "second";
}
@@ -84,7 +84,7 @@ public class LockBasedStorageManager implements StorageManager {
) {
return new LockBasedNotNullLazyValue<T>(lock, computable) {
@Override
protected Object recursionDetected(boolean firstTime) {
protected T recursionDetected(boolean firstTime) {
return onRecursiveCall;
}
};
@@ -94,13 +94,13 @@ public class LockBasedStorageManager implements StorageManager {
@Override
public <T> NotNullLazyValue<T> createLazyValueWithPostCompute(
@NotNull Computable<T> computable,
final Function<Boolean, Object> onRecursiveCall,
final Function<Boolean, T> onRecursiveCall,
@NotNull final Consumer<T> postCompute
) {
return new LockBasedNotNullLazyValue<T>(lock, computable) {
@Nullable
@Override
protected Object recursionDetected(boolean firstTime) {
protected T recursionDetected(boolean firstTime) {
if (onRecursiveCall == null) {
return super.recursionDetected(firstTime);
}
@@ -125,7 +125,7 @@ public class LockBasedStorageManager implements StorageManager {
public <T> NullableLazyValue<T> createRecursionTolerantNullableLazyValue(@NotNull Computable<T> computable, final T onRecursiveCall) {
return new LockBasedLazyValue<T>(lock, computable) {
@Override
protected Object recursionDetected(boolean firstTime) {
protected T recursionDetected(boolean firstTime) {
return onRecursiveCall;
}
};
@@ -191,11 +191,11 @@ public class LockBasedStorageManager implements StorageManager {
if (_value == NotValue.COMPUTING) {
value = NotValue.RECURSION_WAS_DETECTED;
return WrappedValues.unescapeThrowable(recursionDetected(/*firstTime = */ true));
return recursionDetected(/*firstTime = */ true);
}
if (_value == NotValue.RECURSION_WAS_DETECTED) {
return WrappedValues.unescapeThrowable(recursionDetected(/*firstTime = */ false));
return recursionDetected(/*firstTime = */ false);
}
value = NotValue.COMPUTING;
@@ -220,12 +220,11 @@ public class LockBasedStorageManager implements StorageManager {
/**
* @param firstTime {@code true} when recursion has been just detected, {@code false} otherwise
* @return a value or wrapped exception, see WrappedValues
* @throws DO NOT throw exceptions from implementations of this method, instead return WrappedValues.escapeThrowable(exception)
* @return a value to be returned on a recursive call or subsequent calls
*/
@Nullable
protected Object recursionDetected(boolean firstTime) {
return WrappedValues.escapeThrowable(new IllegalStateException("Recursive call in a lazy value"));
protected T recursionDetected(boolean firstTime) {
throw new IllegalStateException("Recursive call in a lazy value");
}
protected void postCompute(T value) {
@@ -46,13 +46,13 @@ public interface StorageManager {
* @param onRecursiveCall is called if the computation calls itself recursively.
* The parameter to it is {@code true} for the first call, {@code false} otherwise.
* If {@code onRecursiveCall} is {@code null}, an exception will be thrown on a recursive call,
* otherwise it should return a result of WrappedValues.escapeThrowable() method
* otherwise it's executed and its result is returned
* @param postCompute is called after the value is computed, but before any other thread sees it
*/
@NotNull
<T> NotNullLazyValue<T> createLazyValueWithPostCompute(
@NotNull Computable<T> computable,
@Nullable Function<Boolean, Object> onRecursiveCall,
@Nullable Function<Boolean, T> onRecursiveCall,
@NotNull Consumer<T> postCompute
);
@@ -20,7 +20,8 @@ import com.intellij.openapi.util.Computable;
import com.intellij.util.Consumer;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.utils.WrappedValues;
import static org.jetbrains.jet.storage.LockBasedStorageManager.NO_LOCKS;
public class StorageUtil {
public static <T> NotNullLazyValue<T> createRecursionIntolerantLazyValueWithDefault(
@@ -28,12 +29,12 @@ public class StorageUtil {
@NotNull Computable<T> compute
) {
//noinspection unchecked
return LockBasedStorageManager.NO_LOCKS.createLazyValueWithPostCompute(
return NO_LOCKS.createLazyValueWithPostCompute(
compute,
new Function<Boolean, Object>() {
new Function<Boolean, T>() {
@Override
public Object fun(Boolean firstTime) {
if (firstTime) return WrappedValues.escapeThrowable(new ReenteringLazyValueComputationException());
public T fun(Boolean firstTime) {
if (firstTime) throw new ReenteringLazyValueComputationException();
return defaultValue;
}
},