001 /*
002 * Copyright 2010-2014 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
017 package org.jetbrains.jet.codegen.when;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.jet.codegen.binding.CodegenBinding;
021 import org.jetbrains.jet.codegen.state.GenerationState;
022 import org.jetbrains.jet.lang.psi.JetWhenExpression;
023 import org.jetbrains.org.objectweb.asm.Type;
024
025 import java.util.HashSet;
026 import java.util.List;
027 import java.util.Set;
028
029 public class MappingsClassesForWhenByEnum {
030 private final GenerationState state;
031 private final Set<String> generatedMappingClasses = new HashSet<String>();
032 private final MappingClassesForWhenByEnumCodegen mappingsCodegen;
033
034 public MappingsClassesForWhenByEnum(@NotNull GenerationState state) {
035 this.state = state;
036 this.mappingsCodegen = new MappingClassesForWhenByEnumCodegen(state);
037 }
038
039 public void generateMappingsClassForExpression(@NotNull JetWhenExpression expression) {
040 WhenByEnumsMapping mapping = state.getBindingContext().get(CodegenBinding.MAPPING_FOR_WHEN_BY_ENUM, expression);
041
042 assert mapping != null : "mapping class should not be requested for non enum when";
043
044 if (!generatedMappingClasses.contains(mapping.getMappingsClassInternalName())) {
045 List<WhenByEnumsMapping> mappings = state.getBindingContext().get(
046 CodegenBinding.MAPPINGS_FOR_WHENS_BY_ENUM_IN_CLASS_FILE,
047 mapping.getOuterClassInternalNameForExpression()
048 );
049
050 assert mappings != null : "guaranteed by usage contract of EnumSwitchCodegen";
051
052 Type mappingsClassType = Type.getObjectType(mapping.getMappingsClassInternalName());
053
054 mappingsCodegen.generate(mappings, mappingsClassType, expression.getContainingJetFile());
055 generatedMappingClasses.add(mapping.getMappingsClassInternalName());
056 }
057 }
058 }