001/* 002 * Copyright 2010-2013 JetBrains s.r.o. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.jetbrains.jet.lang.resolve.calls.inference; 018 019import com.google.common.collect.Maps; 020import org.jetbrains.annotations.NotNull; 021import org.jetbrains.jet.lang.descriptors.CallableDescriptor; 022import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor; 023import org.jetbrains.jet.lang.resolve.calls.results.ResolutionDebugInfo; 024import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; 025import org.jetbrains.jet.lang.types.JetType; 026 027import java.util.Map; 028import java.util.Set; 029 030import static org.jetbrains.jet.lang.resolve.calls.results.ResolutionDebugInfo.*; 031 032public class DebugConstraintResolutionListener implements ConstraintResolutionListener { 033 034 private final ResolutionDebugInfo.Data debugInfo; 035 private final ResolvedCall<? extends CallableDescriptor> candidateCall; 036 037 public DebugConstraintResolutionListener(@NotNull ResolvedCall<? extends CallableDescriptor> candidateCall, @NotNull ResolutionDebugInfo.Data debugInfo) { 038 this.debugInfo = debugInfo; 039 this.candidateCall = candidateCall; 040 } 041 042 @Override 043 public void constraintsForUnknown(TypeParameterDescriptor typeParameterDescriptor, BoundsOwner typeValue) { 044 if (!ResolutionDebugInfo.isResolutionDebugEnabled()) return; 045 Map<TypeParameterDescriptor, BoundsOwner> map = debugInfo.getByKey(BOUNDS_FOR_UNKNOWNS, candidateCall); 046 if (map == null) { 047 map = Maps.newLinkedHashMap(); 048 debugInfo.putByKey(BOUNDS_FOR_UNKNOWNS, candidateCall, map); 049 } 050 map.put(typeParameterDescriptor, typeValue); 051 } 052 053 @Override 054 public void constraintsForKnownType(JetType type, BoundsOwner typeValue) { 055 if (!ResolutionDebugInfo.isResolutionDebugEnabled()) return; 056 Map<JetType,BoundsOwner> map = debugInfo.getByKey(BOUNDS_FOR_KNOWNS, candidateCall); 057 if (map == null) { 058 map = Maps.newLinkedHashMap(); 059 debugInfo.putByKey(BOUNDS_FOR_KNOWNS, candidateCall, map); 060 } 061 map.put(type, typeValue); 062 } 063 064 @Override 065 public void done(ConstraintSystemSolution solution, Set<TypeParameterDescriptor> typeParameterDescriptors) { 066 if (!ResolutionDebugInfo.isResolutionDebugEnabled()) return; 067 debugInfo.putByKey(SOLUTION, candidateCall, solution); 068 debugInfo.putByKey(UNKNOWNS, candidateCall, typeParameterDescriptors); 069 } 070 071 @Override 072 public void log(Object... messageFragments) { 073 if (!ResolutionDebugInfo.isResolutionDebugEnabled()) return; 074 StringBuilder stringBuilder = debugInfo.getByKey(LOG, candidateCall); 075 if (stringBuilder == null) { 076 stringBuilder = new StringBuilder(); 077 debugInfo.putByKey(LOG, candidateCall, stringBuilder); 078 } 079 for (Object m : messageFragments) { 080 stringBuilder.append(m); 081 } 082 stringBuilder.append("\n"); 083 } 084 085 @Override 086 public void error(Object... messageFragments) { 087 if (!ResolutionDebugInfo.isResolutionDebugEnabled()) return; 088 StringBuilder stringBuilder = debugInfo.getByKey(ERRORS, candidateCall); 089 if (stringBuilder == null) { 090 stringBuilder = new StringBuilder(); 091 debugInfo.putByKey(ERRORS, candidateCall, stringBuilder); 092 } 093 for (Object m : messageFragments) { 094 stringBuilder.append(m); 095 } 096 stringBuilder.append("\n"); 097 } 098}