Migrate existing code to LockBasedStorageManager.NO_LOCKS
This commit is contained in:
@@ -19,6 +19,7 @@ package org.jetbrains.jet.lang.resolve;
|
|||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import com.intellij.lang.ASTNode;
|
import com.intellij.lang.ASTNode;
|
||||||
|
import com.intellij.openapi.util.Computable;
|
||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.psi.PsiElement;
|
||||||
import com.intellij.psi.tree.IElementType;
|
import com.intellij.psi.tree.IElementType;
|
||||||
import com.intellij.psi.util.PsiTreeUtil;
|
import com.intellij.psi.util.PsiTreeUtil;
|
||||||
@@ -42,8 +43,6 @@ import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices;
|
|||||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||||
import org.jetbrains.jet.lexer.JetKeywordToken;
|
import org.jetbrains.jet.lexer.JetKeywordToken;
|
||||||
import org.jetbrains.jet.lexer.JetTokens;
|
import org.jetbrains.jet.lexer.JetTokens;
|
||||||
import org.jetbrains.jet.storage.NotNullLazyValueImpl;
|
|
||||||
import org.jetbrains.jet.storage.NotNullLazyValueWithDefault;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -54,6 +53,7 @@ import static org.jetbrains.jet.lang.resolve.BindingContext.CONSTRUCTOR;
|
|||||||
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.*;
|
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.*;
|
||||||
import static org.jetbrains.jet.lang.resolve.ModifiersChecker.*;
|
import static org.jetbrains.jet.lang.resolve.ModifiersChecker.*;
|
||||||
import static org.jetbrains.jet.lexer.JetTokens.OVERRIDE_KEYWORD;
|
import static org.jetbrains.jet.lexer.JetTokens.OVERRIDE_KEYWORD;
|
||||||
|
import static org.jetbrains.jet.storage.StorageUtil.createRecursionIntolerantLazyValueWithDefault;
|
||||||
|
|
||||||
public class DescriptorResolver {
|
public class DescriptorResolver {
|
||||||
public static final Name COPY_METHOD_NAME = Name.identifier("copy");
|
public static final Name COPY_METHOD_NAME = Name.identifier("copy");
|
||||||
@@ -313,14 +313,19 @@ public class DescriptorResolver {
|
|||||||
JetExpression bodyExpression = function.getBodyExpression();
|
JetExpression bodyExpression = function.getBodyExpression();
|
||||||
if (bodyExpression != null) {
|
if (bodyExpression != null) {
|
||||||
returnType =
|
returnType =
|
||||||
DeferredType.create(trace, new NotNullLazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
|
DeferredType.create(trace,
|
||||||
@NotNull
|
createRecursionIntolerantLazyValueWithDefault(
|
||||||
@Override
|
ErrorUtils.createErrorType("Recursive dependency"),
|
||||||
protected JetType doCompute() {
|
new Computable<JetType>() {
|
||||||
JetType type = expressionTypingServices.getBodyExpressionType(trace, scope, dataFlowInfo, function, functionDescriptor);
|
@Override
|
||||||
return transformAnonymousTypeIfNeeded(functionDescriptor, function, type, trace);
|
public JetType compute() {
|
||||||
}
|
JetType type = expressionTypingServices
|
||||||
});
|
.getBodyExpressionType(trace, scope, dataFlowInfo, function,
|
||||||
|
functionDescriptor);
|
||||||
|
return transformAnonymousTypeIfNeeded(functionDescriptor, function, type,
|
||||||
|
trace);
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
returnType = ErrorUtils.createErrorType("No type, no body");
|
returnType = ErrorUtils.createErrorType("No type, no body");
|
||||||
@@ -1028,14 +1033,17 @@ public class DescriptorResolver {
|
|||||||
if (hasDelegate && variableDescriptor instanceof PropertyDescriptor) {
|
if (hasDelegate && variableDescriptor instanceof PropertyDescriptor) {
|
||||||
final JetExpression propertyDelegateExpression = ((JetProperty) variable).getDelegateExpression();
|
final JetExpression propertyDelegateExpression = ((JetProperty) variable).getDelegateExpression();
|
||||||
if (propertyDelegateExpression != null) {
|
if (propertyDelegateExpression != null) {
|
||||||
return DeferredType.create(trace, new NotNullLazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
|
return DeferredType.create(
|
||||||
@NotNull
|
trace,
|
||||||
@Override
|
createRecursionIntolerantLazyValueWithDefault(
|
||||||
protected JetType doCompute() {
|
ErrorUtils.createErrorType("Recursive dependency"),
|
||||||
return resolveDelegatedPropertyType((PropertyDescriptor) variableDescriptor, scope,
|
new Computable<JetType>() {
|
||||||
propertyDelegateExpression, dataFlowInfo, trace);
|
@Override
|
||||||
}
|
public JetType compute() {
|
||||||
});
|
return resolveDelegatedPropertyType((PropertyDescriptor) variableDescriptor, scope,
|
||||||
|
propertyDelegateExpression, dataFlowInfo, trace);
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!notLocal) {
|
if (!notLocal) {
|
||||||
@@ -1045,14 +1053,20 @@ public class DescriptorResolver {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (notLocal) {
|
if (notLocal) {
|
||||||
return DeferredType.create(trace, new NotNullLazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
|
return DeferredType.create(trace,
|
||||||
@Override
|
createRecursionIntolerantLazyValueWithDefault(
|
||||||
protected JetType doCompute() {
|
ErrorUtils.createErrorType("Recursive dependency"),
|
||||||
JetType type = resolveInitializerType(scope, initializer, dataFlowInfo, trace);
|
new Computable<JetType>() {
|
||||||
|
@Override
|
||||||
|
public JetType compute() {
|
||||||
|
JetType type =
|
||||||
|
resolveInitializerType(scope, initializer, dataFlowInfo, trace);
|
||||||
|
|
||||||
return transformAnonymousTypeIfNeeded(variableDescriptor, variable, type, trace);
|
return transformAnonymousTypeIfNeeded(variableDescriptor, variable, type,
|
||||||
}
|
trace);
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return resolveInitializerType(scope, initializer, dataFlowInfo, trace);
|
return resolveInitializerType(scope, initializer, dataFlowInfo, trace);
|
||||||
@@ -1370,10 +1384,9 @@ public class DescriptorResolver {
|
|||||||
final ClassDescriptor enumClassDescriptor = (ClassDescriptor) classObject.getContainingDeclaration();
|
final ClassDescriptor enumClassDescriptor = (ClassDescriptor) classObject.getContainingDeclaration();
|
||||||
assert DescriptorUtils.isEnumClass(enumClassDescriptor) : "values should be created in enum class: " + enumClassDescriptor;
|
assert DescriptorUtils.isEnumClass(enumClassDescriptor) : "values should be created in enum class: " + enumClassDescriptor;
|
||||||
return DescriptorFactory
|
return DescriptorFactory
|
||||||
.createEnumClassObjectValuesMethod(classObject, DeferredType.create(trace, new NotNullLazyValueImpl<JetType>() {
|
.createEnumClassObjectValuesMethod(classObject, DeferredType.create(trace, new Computable<JetType>() {
|
||||||
@NotNull
|
|
||||||
@Override
|
@Override
|
||||||
protected JetType doCompute() {
|
public JetType compute() {
|
||||||
return KotlinBuiltIns.getInstance().getArrayType(enumClassDescriptor.getDefaultType());
|
return KotlinBuiltIns.getInstance().getArrayType(enumClassDescriptor.getDefaultType());
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
@@ -1388,10 +1401,9 @@ public class DescriptorResolver {
|
|||||||
final ClassDescriptor enumClassDescriptor = (ClassDescriptor) classObject.getContainingDeclaration();
|
final ClassDescriptor enumClassDescriptor = (ClassDescriptor) classObject.getContainingDeclaration();
|
||||||
assert DescriptorUtils.isEnumClass(enumClassDescriptor) : "valueOf should be created in enum class: " + enumClassDescriptor;
|
assert DescriptorUtils.isEnumClass(enumClassDescriptor) : "valueOf should be created in enum class: " + enumClassDescriptor;
|
||||||
return DescriptorFactory
|
return DescriptorFactory
|
||||||
.createEnumClassObjectValueOfMethod(classObject, DeferredType.create(trace, new NotNullLazyValueImpl<JetType>() {
|
.createEnumClassObjectValueOfMethod(classObject, DeferredType.create(trace, new Computable<JetType>() {
|
||||||
@NotNull
|
|
||||||
@Override
|
@Override
|
||||||
protected JetType doCompute() {
|
public JetType compute() {
|
||||||
return enumClassDescriptor.getDefaultType();
|
return enumClassDescriptor.getDefaultType();
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.lang.resolve;
|
package org.jetbrains.jet.lang.resolve;
|
||||||
|
|
||||||
|
import com.intellij.openapi.util.Computable;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.descriptors.*;
|
import org.jetbrains.jet.lang.descriptors.*;
|
||||||
@@ -25,7 +26,6 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
|||||||
import org.jetbrains.jet.lang.resolve.scopes.LazyScopeAdapter;
|
import org.jetbrains.jet.lang.resolve.scopes.LazyScopeAdapter;
|
||||||
import org.jetbrains.jet.lang.types.*;
|
import org.jetbrains.jet.lang.types.*;
|
||||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||||
import org.jetbrains.jet.storage.NotNullLazyValueImpl;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -36,6 +36,7 @@ import java.util.List;
|
|||||||
import static org.jetbrains.jet.lang.diagnostics.Errors.*;
|
import static org.jetbrains.jet.lang.diagnostics.Errors.*;
|
||||||
import static org.jetbrains.jet.lang.resolve.PossiblyBareType.type;
|
import static org.jetbrains.jet.lang.resolve.PossiblyBareType.type;
|
||||||
import static org.jetbrains.jet.lang.types.Variance.*;
|
import static org.jetbrains.jet.lang.types.Variance.*;
|
||||||
|
import static org.jetbrains.jet.storage.LockBasedStorageManager.NO_LOCKS;
|
||||||
|
|
||||||
public class TypeResolver {
|
public class TypeResolver {
|
||||||
|
|
||||||
@@ -239,13 +240,12 @@ public class TypeResolver {
|
|||||||
return typeParameterDescriptor.getUpperBoundsAsType().getMemberScope();
|
return typeParameterDescriptor.getUpperBoundsAsType().getMemberScope();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return new LazyScopeAdapter(new NotNullLazyValueImpl<JetScope>() {
|
return new LazyScopeAdapter(NO_LOCKS.createLazyValue(new Computable<JetScope>() {
|
||||||
@NotNull
|
|
||||||
@Override
|
@Override
|
||||||
protected JetScope doCompute() {
|
public JetScope compute() {
|
||||||
return typeParameterDescriptor.getUpperBoundsAsType().getMemberScope();
|
return typeParameterDescriptor.getUpperBoundsAsType().getMemberScope();
|
||||||
}
|
}
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,10 +16,12 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.lang.types;
|
package org.jetbrains.jet.lang.types;
|
||||||
|
|
||||||
|
import com.intellij.openapi.util.Computable;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||||
|
import org.jetbrains.jet.storage.LockBasedStorageManager;
|
||||||
import org.jetbrains.jet.storage.NotNullLazyValue;
|
import org.jetbrains.jet.storage.NotNullLazyValue;
|
||||||
import org.jetbrains.jet.storage.ReenteringLazyValueComputationException;
|
import org.jetbrains.jet.storage.ReenteringLazyValueComputationException;
|
||||||
import org.jetbrains.jet.util.Box;
|
import org.jetbrains.jet.util.Box;
|
||||||
@@ -36,6 +38,10 @@ public class DeferredType implements JetType {
|
|||||||
return deferredType;
|
return deferredType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static DeferredType create(BindingTrace trace, Computable<JetType> compute) {
|
||||||
|
return create(trace, LockBasedStorageManager.NO_LOCKS.createLazyValue(compute));
|
||||||
|
}
|
||||||
|
|
||||||
private final NotNullLazyValue<JetType> lazyValue;
|
private final NotNullLazyValue<JetType> lazyValue;
|
||||||
|
|
||||||
private DeferredType(NotNullLazyValue<JetType> lazyValue) {
|
private DeferredType(NotNullLazyValue<JetType> lazyValue) {
|
||||||
|
|||||||
+10
-8
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.jet.lang.types.expressions;
|
package org.jetbrains.jet.lang.types.expressions;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import com.intellij.openapi.util.Computable;
|
||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.psi.PsiElement;
|
||||||
import com.intellij.util.Function;
|
import com.intellij.util.Function;
|
||||||
import com.intellij.util.containers.ContainerUtil;
|
import com.intellij.util.containers.ContainerUtil;
|
||||||
@@ -33,7 +34,6 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
|||||||
import org.jetbrains.jet.lang.types.*;
|
import org.jetbrains.jet.lang.types.*;
|
||||||
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
|
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
|
||||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||||
import org.jetbrains.jet.storage.NotNullLazyValueWithDefault;
|
|
||||||
import org.jetbrains.jet.util.slicedmap.WritableSlice;
|
import org.jetbrains.jet.util.slicedmap.WritableSlice;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@@ -45,6 +45,7 @@ import static org.jetbrains.jet.lang.resolve.BindingContext.*;
|
|||||||
import static org.jetbrains.jet.lang.resolve.calls.context.ContextDependency.INDEPENDENT;
|
import static org.jetbrains.jet.lang.resolve.calls.context.ContextDependency.INDEPENDENT;
|
||||||
import static org.jetbrains.jet.lang.types.TypeUtils.*;
|
import static org.jetbrains.jet.lang.types.TypeUtils.*;
|
||||||
import static org.jetbrains.jet.lang.types.expressions.CoercionStrategy.COERCION_TO_UNIT;
|
import static org.jetbrains.jet.lang.types.expressions.CoercionStrategy.COERCION_TO_UNIT;
|
||||||
|
import static org.jetbrains.jet.storage.StorageUtil.createRecursionIntolerantLazyValueWithDefault;
|
||||||
|
|
||||||
public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
|
public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
|
||||||
protected ClosureExpressionsTypingVisitor(@NotNull ExpressionTypingInternals facade) {
|
protected ClosureExpressionsTypingVisitor(@NotNull ExpressionTypingInternals facade) {
|
||||||
@@ -66,13 +67,14 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public void handleRecord(WritableSlice<PsiElement, ClassDescriptor> slice, PsiElement declaration, final ClassDescriptor descriptor) {
|
public void handleRecord(WritableSlice<PsiElement, ClassDescriptor> slice, PsiElement declaration, final ClassDescriptor descriptor) {
|
||||||
if (slice == CLASS && declaration == expression.getObjectDeclaration()) {
|
if (slice == CLASS && declaration == expression.getObjectDeclaration()) {
|
||||||
JetType defaultType = DeferredType.create(context.trace, new NotNullLazyValueWithDefault<JetType>(ErrorUtils.createErrorType("Recursive dependency")) {
|
JetType defaultType = DeferredType.create(context.trace, createRecursionIntolerantLazyValueWithDefault(
|
||||||
@NotNull
|
ErrorUtils.createErrorType("Recursive dependency"),
|
||||||
@Override
|
new Computable<JetType>() {
|
||||||
protected JetType doCompute() {
|
@Override
|
||||||
return descriptor.getDefaultType();
|
public JetType compute() {
|
||||||
}
|
return descriptor.getDefaultType();
|
||||||
});
|
}
|
||||||
|
}));
|
||||||
result[0] = defaultType;
|
result[0] = defaultType;
|
||||||
if (!context.trace.get(PROCESSED, expression)) {
|
if (!context.trace.get(PROCESSED, expression)) {
|
||||||
temporaryTrace.record(EXPRESSION_TYPE, expression, defaultType);
|
temporaryTrace.record(EXPRESSION_TYPE, expression, defaultType);
|
||||||
|
|||||||
+6
-5
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.jet.lang.descriptors.impl;
|
package org.jetbrains.jet.lang.descriptors.impl;
|
||||||
|
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
|
import com.intellij.openapi.util.Computable;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorVisitor;
|
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorVisitor;
|
||||||
@@ -30,12 +31,13 @@ import org.jetbrains.jet.lang.types.*;
|
|||||||
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
|
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
|
||||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||||
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
||||||
import org.jetbrains.jet.storage.NotNullLazyValueImpl;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.jetbrains.jet.storage.LockBasedStorageManager.NO_LOCKS;
|
||||||
|
|
||||||
public class TypeParameterDescriptorImpl extends DeclarationDescriptorNonRootImpl implements TypeParameterDescriptor {
|
public class TypeParameterDescriptorImpl extends DeclarationDescriptorNonRootImpl implements TypeParameterDescriptor {
|
||||||
public static TypeParameterDescriptor createWithDefaultBound(
|
public static TypeParameterDescriptor createWithDefaultBound(
|
||||||
@NotNull DeclarationDescriptor containingDeclaration,
|
@NotNull DeclarationDescriptor containingDeclaration,
|
||||||
@@ -222,13 +224,12 @@ public class TypeParameterDescriptorImpl extends DeclarationDescriptorNonRootImp
|
|||||||
getTypeConstructor(),
|
getTypeConstructor(),
|
||||||
TypeUtils.hasNullableLowerBound(this),
|
TypeUtils.hasNullableLowerBound(this),
|
||||||
Collections.<TypeProjection>emptyList(),
|
Collections.<TypeProjection>emptyList(),
|
||||||
new LazyScopeAdapter(new NotNullLazyValueImpl<JetScope>() {
|
new LazyScopeAdapter(NO_LOCKS.createLazyValue(new Computable<JetScope>() {
|
||||||
@NotNull
|
|
||||||
@Override
|
@Override
|
||||||
protected JetScope doCompute() {
|
public JetScope compute() {
|
||||||
return getUpperBoundsAsType().getMemberScope();
|
return getUpperBoundsAsType().getMemberScope();
|
||||||
}
|
}
|
||||||
}));
|
})));
|
||||||
}
|
}
|
||||||
return defaultType;
|
return defaultType;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2013 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.jet.storage;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public abstract class MemoizedFunctionToNotNullImpl<K, V> extends MemoizedFunctionToNullableImpl<K, V> implements
|
|
||||||
MemoizedFunctionToNotNull<K, V> {
|
|
||||||
|
|
||||||
public MemoizedFunctionToNotNullImpl() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public MemoizedFunctionToNotNullImpl(@NotNull Map<K, Object> map) {
|
|
||||||
super(map);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public V fun(K input) {
|
|
||||||
V result = super.fun(input);
|
|
||||||
assert result != null : "compute() returned null";
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
protected abstract V doCompute(K input);
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2013 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.jet.storage;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.jet.storage.MemoizedFunctionToNullable;
|
|
||||||
import org.jetbrains.jet.utils.WrappedValues;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public abstract class MemoizedFunctionToNullableImpl<K, V> implements MemoizedFunctionToNullable<K, V> {
|
|
||||||
private final Map<K, Object> cache;
|
|
||||||
|
|
||||||
public MemoizedFunctionToNullableImpl() {
|
|
||||||
this(new HashMap<K, Object>());
|
|
||||||
}
|
|
||||||
|
|
||||||
public MemoizedFunctionToNullableImpl(@NotNull Map<K, Object> map) {
|
|
||||||
this.cache = map;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public V fun(K input) {
|
|
||||||
Object value = cache.get(input);
|
|
||||||
if (value != null) return WrappedValues.unescapeNull(value);
|
|
||||||
|
|
||||||
V typedValue = doCompute(input);
|
|
||||||
|
|
||||||
Object oldValue = cache.put(input, WrappedValues.escapeNull(typedValue));
|
|
||||||
assert oldValue == null : "Race condition detected";
|
|
||||||
|
|
||||||
return typedValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
protected abstract V doCompute(K input);
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2013 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.jet.storage;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
public abstract class NotNullLazyValueImpl<T> extends NullableLazyValueImpl<T> implements NotNullLazyValue<T> {
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public T compute() {
|
|
||||||
T result = super.compute();
|
|
||||||
assert result != null : "compute() returned null";
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
protected abstract T doCompute();
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package org.jetbrains.jet.storage;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
public abstract class NotNullLazyValueWithDefault<T> extends NotNullLazyValueImpl<T> {
|
|
||||||
private final T defaultValue;
|
|
||||||
|
|
||||||
protected NotNullLazyValueWithDefault(@NotNull T defaultValue) {
|
|
||||||
this.defaultValue = defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object recursionDetected(boolean firstTime) {
|
|
||||||
if (firstTime) return super.recursionDetected(firstTime);
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2013 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.jet.storage;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.jet.utils.ExceptionUtils;
|
|
||||||
import org.jetbrains.jet.utils.WrappedValues;
|
|
||||||
|
|
||||||
public abstract class NullableLazyValueImpl<T> implements NullableLazyValue<T> {
|
|
||||||
private enum State {
|
|
||||||
NOT_COMPUTED,
|
|
||||||
COMPUTING,
|
|
||||||
RECURSION_WAS_DETECTED
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private Object value = State.NOT_COMPUTED;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isComputed() {
|
|
||||||
return value != State.NOT_COMPUTED && value != State.COMPUTING;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T compute() {
|
|
||||||
if (!(value instanceof State)) {
|
|
||||||
// Computed already
|
|
||||||
return WrappedValues.unescapeThrowable(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value == State.COMPUTING) {
|
|
||||||
Object result = recursionDetected(true);
|
|
||||||
if (result != State.NOT_COMPUTED) {
|
|
||||||
value = State.RECURSION_WAS_DETECTED;
|
|
||||||
return WrappedValues.unescapeThrowable(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (value == State.RECURSION_WAS_DETECTED) {
|
|
||||||
Object result = recursionDetected(false);
|
|
||||||
if (result != State.NOT_COMPUTED) {
|
|
||||||
return WrappedValues.unescapeThrowable(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
value = State.COMPUTING;
|
|
||||||
try {
|
|
||||||
T typedValue = doCompute();
|
|
||||||
value = typedValue;
|
|
||||||
postCompute(typedValue);
|
|
||||||
return typedValue;
|
|
||||||
}
|
|
||||||
catch (Throwable e) {
|
|
||||||
value = WrappedValues.escapeThrowable(e);
|
|
||||||
throw ExceptionUtils.rethrow(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return {@code NOT_COMPUTED} to proceed, a value or wrapped exception otherwise, see WrappedValues
|
|
||||||
* @throws DO NOT throw exceptions from implementations of this method, instead return WrappedValues.escapeThrowable(exception)
|
|
||||||
*/
|
|
||||||
public Object recursionDetected(boolean firstTime) {
|
|
||||||
return WrappedValues.escapeThrowable(new ReenteringLazyValueComputationException());
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract T doCompute();
|
|
||||||
|
|
||||||
protected void postCompute(T value) {
|
|
||||||
// Doing something in post-compute helps prevent infinite recursion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2013 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.jet.storage;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public class StorageUtil {
|
||||||
|
public static <T> NotNullLazyValue<T> createRecursionIntolerantLazyValueWithDefault(
|
||||||
|
@NotNull final T defaultValue,
|
||||||
|
@NotNull Computable<T> compute
|
||||||
|
) {
|
||||||
|
//noinspection unchecked
|
||||||
|
return LockBasedStorageManager.NO_LOCKS.createLazyValueWithPostCompute(
|
||||||
|
compute,
|
||||||
|
new Function<Boolean, Object>() {
|
||||||
|
@Override
|
||||||
|
public Object fun(Boolean firstTime) {
|
||||||
|
if (firstTime) return WrappedValues.escapeThrowable(new ReenteringLazyValueComputationException());
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Consumer.EMPTY_CONSUMER
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user