diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatingBindingTrace.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatingBindingTrace.kt index 5803afc4770..793720687a6 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatingBindingTrace.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatingBindingTrace.kt @@ -14,179 +14,131 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve; +package org.jetbrains.kotlin.resolve -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.annotations.TestOnly; -import org.jetbrains.kotlin.diagnostics.Diagnostic; -import org.jetbrains.kotlin.psi.KtExpression; -import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics; -import org.jetbrains.kotlin.resolve.diagnostics.MutableDiagnosticsWithSuppression; -import org.jetbrains.kotlin.types.KotlinType; -import org.jetbrains.kotlin.types.expressions.KotlinTypeInfo; -import org.jetbrains.kotlin.types.expressions.typeInfoFactory.TypeInfoFactoryKt; -import org.jetbrains.kotlin.util.slicedMap.*; +import com.google.common.collect.ImmutableMap +import org.jetbrains.annotations.TestOnly +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.psi.KtExpression +import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics +import org.jetbrains.kotlin.resolve.diagnostics.MutableDiagnosticsWithSuppression +import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.expressions.typeInfoFactory.createTypeInfo +import org.jetbrains.kotlin.util.slicedMap.* -import java.util.Collection; -import java.util.List; -import java.util.Map; +open class DelegatingBindingTrace(private val parentContext: BindingContext, + withParentDiagnostics: Boolean, + private val name: String) : BindingTrace { + private val map = if (BindingTraceContext.TRACK_REWRITES) TrackingSlicedMap(BindingTraceContext.TRACK_WITH_STACK_TRACES) else SlicedMapImpl.create() + private val mutableDiagnostics: MutableDiagnosticsWithSuppression -public class DelegatingBindingTrace implements BindingTrace { - @SuppressWarnings("ConstantConditions") - private final MutableSlicedMap map = BindingTraceContext.TRACK_REWRITES ? new TrackingSlicedMap(BindingTraceContext.TRACK_WITH_STACK_TRACES) : SlicedMapImpl.create(); + private inner class MyBindingContext : BindingContext { + override fun getDiagnostics(): Diagnostics = mutableDiagnostics - private final BindingContext parentContext; - private final String name; - private final MutableDiagnosticsWithSuppression mutableDiagnostics; - - private final BindingContext bindingContext = new BindingContext() { - @NotNull - @Override - public Diagnostics getDiagnostics() { - return mutableDiagnostics; + override fun get(slice: ReadOnlySlice, key: K): V? { + return this@DelegatingBindingTrace.get(slice, key) } - @Override - public V get(ReadOnlySlice slice, K key) { - return DelegatingBindingTrace.this.get(slice, key); + override fun getType(expression: KtExpression): KotlinType? { + return this@DelegatingBindingTrace.getType(expression) } - - @Nullable - @Override - public KotlinType getType(@NotNull KtExpression expression) { - return DelegatingBindingTrace.this.getType(expression); + override fun getKeys(slice: WritableSlice): Collection { + return this@DelegatingBindingTrace.getKeys(slice) } - @NotNull - @Override - public Collection getKeys(WritableSlice slice) { - return DelegatingBindingTrace.this.getKeys(slice); + override fun addOwnDataTo(trace: BindingTrace, commitDiagnostics: Boolean) { + BindingContextUtils.addOwnDataTo(trace, null, commitDiagnostics, map, mutableDiagnostics) } - @Override - public void addOwnDataTo(@NotNull BindingTrace trace, boolean commitDiagnostics) { - BindingContextUtils.addOwnDataTo(trace, null, commitDiagnostics, map, mutableDiagnostics); - } - - @NotNull @TestOnly - @Override - public ImmutableMap getSliceContents(@NotNull ReadOnlySlice slice) { - Map result = Maps.newHashMap(); - result.putAll(parentContext.getSliceContents(slice)); - result.putAll(map.getSliceContents(slice)); - return ImmutableMap.copyOf(result); + override fun getSliceContents(slice: ReadOnlySlice): ImmutableMap { + return ImmutableMap.copyOf(parentContext.getSliceContents(slice) + map.getSliceContents(slice)) } - }; - - public DelegatingBindingTrace(BindingContext parentContext, String debugName) { - this(parentContext, true, debugName); } - public DelegatingBindingTrace(BindingContext parentContext, boolean withParentDiagnostics, String debugName) { - this.parentContext = parentContext; - this.name = debugName; - this.mutableDiagnostics = withParentDiagnostics ? - new MutableDiagnosticsWithSuppression(bindingContext, parentContext.getDiagnostics()) : - new MutableDiagnosticsWithSuppression(bindingContext); + private val bindingContext = MyBindingContext() + + constructor(parentContext: BindingContext, debugName: String) : this(parentContext, true, debugName) { } - public DelegatingBindingTrace(BindingContext parentContext, String debugName, @Nullable Object resolutionSubjectForMessage) { - this(parentContext, AnalyzingUtils.formDebugNameForBindingTrace(debugName, resolutionSubjectForMessage)); + init { + this.mutableDiagnostics = if (withParentDiagnostics) + MutableDiagnosticsWithSuppression(bindingContext, parentContext.diagnostics) + else + MutableDiagnosticsWithSuppression(bindingContext) } - @Override - @NotNull - public BindingContext getBindingContext() { - return bindingContext; + constructor(parentContext: BindingContext, + debugName: String, + resolutionSubjectForMessage: Any?) : this(parentContext, AnalyzingUtils.formDebugNameForBindingTrace(debugName, resolutionSubjectForMessage)) { } - @Override - public void record(WritableSlice slice, K key, V value) { - map.put(slice, key, value); + override fun getBindingContext(): BindingContext = bindingContext + + override fun record(slice: WritableSlice, key: K, value: V) { + map.put(slice, key, value) } - @Override - public void record(WritableSlice slice, K key) { - record(slice, key, true); + override fun record(slice: WritableSlice, key: K) { + record(slice, key, true) } - @Override - public V get(ReadOnlySlice slice, K key) { - V value = map.get(slice, key); - if (slice instanceof SetSlice) { - assert value != null; - if (!value.equals(SetSlice.DEFAULT)) return value; + override fun get(slice: ReadOnlySlice, key: K): V? { + val value = map.get(slice, key) + if (slice is SetSlice<*>) { + assert(value != null) + if (value != SetSlice.DEFAULT) return value } else if (value != null) { - return value; + return value } - return parentContext.get(slice, key); + return parentContext.get(slice, key) } - @NotNull - @Override - public Collection getKeys(WritableSlice slice) { - Collection keys = map.getKeys(slice); - Collection fromParent = parentContext.getKeys(slice); - if (keys.isEmpty()) return fromParent; - if (fromParent.isEmpty()) return keys; + override fun getKeys(slice: WritableSlice): Collection { + val keys = map.getKeys(slice) + val fromParent = parentContext.getKeys(slice) + if (keys.isEmpty()) return fromParent + if (fromParent.isEmpty()) return keys - List result = Lists.newArrayList(keys); - result.addAll(fromParent); - return result; + return keys + fromParent } - @Nullable - @Override - public KotlinType getType(@NotNull KtExpression expression) { - KotlinTypeInfo typeInfo = get(BindingContext.EXPRESSION_TYPE_INFO, expression); - return typeInfo != null ? typeInfo.getType() : null; + override fun getType(expression: KtExpression): KotlinType? { + val typeInfo = get(BindingContext.EXPRESSION_TYPE_INFO, expression) + return typeInfo?.type } - @Override - public void recordType(@NotNull KtExpression expression, @Nullable KotlinType type) { - KotlinTypeInfo typeInfo = get(BindingContext.EXPRESSION_TYPE_INFO, expression); + override fun recordType(expression: KtExpression, type: KotlinType?) { + var typeInfo = get(BindingContext.EXPRESSION_TYPE_INFO, expression) if (typeInfo == null) { - typeInfo = TypeInfoFactoryKt.createTypeInfo(type); + typeInfo = createTypeInfo(type) } else { - typeInfo = typeInfo.replaceType(type); + typeInfo = typeInfo.replaceType(type) } - record(BindingContext.EXPRESSION_TYPE_INFO, expression, typeInfo); + record(BindingContext.EXPRESSION_TYPE_INFO, expression, typeInfo) } - public void addOwnDataTo(@NotNull BindingTrace trace) { - addOwnDataTo(trace, null, true); + fun moveAllMyDataTo(trace: BindingTrace) { + addOwnDataTo(trace, null, true) + clear() } - public void moveAllMyDataTo(@NotNull BindingTrace trace) { - addOwnDataTo(trace, null, true); - clear(); + @JvmOverloads fun addOwnDataTo(trace: BindingTrace, filter: TraceEntryFilter? = null, commitDiagnostics: Boolean = true) { + BindingContextUtils.addOwnDataTo(trace, filter, commitDiagnostics, map, mutableDiagnostics) } - public void addOwnDataTo(@NotNull BindingTrace trace, @Nullable TraceEntryFilter filter, boolean commitDiagnostics) { - BindingContextUtils.addOwnDataTo(trace, filter, commitDiagnostics, map, mutableDiagnostics); + fun clear() { + map.clear() + mutableDiagnostics.clear() } - public void clear() { - map.clear(); - mutableDiagnostics.clear(); + override fun report(diagnostic: Diagnostic) { + mutableDiagnostics.report(diagnostic) } - @Override - public void report(@NotNull Diagnostic diagnostic) { - mutableDiagnostics.report(diagnostic); - } - - @Override - public String toString() { - return name; - } + override fun toString(): String = name }