001    /*
002     * Copyright 2010-2015 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.kotlin.codegen.inline;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.codegen.ClassBuilder;
022    import org.jetbrains.kotlin.codegen.DelegatingClassBuilder;
023    import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin;
024    import org.jetbrains.org.objectweb.asm.AnnotationVisitor;
025    import org.jetbrains.org.objectweb.asm.ClassVisitor;
026    import org.jetbrains.org.objectweb.asm.FieldVisitor;
027    import org.jetbrains.org.objectweb.asm.MethodVisitor;
028    import org.jetbrains.org.objectweb.asm.commons.*;
029    
030    public class RemappingClassBuilder extends DelegatingClassBuilder {
031        private final ClassBuilder builder;
032        private final Remapper remapper;
033    
034        public RemappingClassBuilder(@NotNull ClassBuilder builder, @NotNull Remapper remapper) {
035            this.builder = builder;
036            this.remapper = remapper;
037        }
038    
039        @Override
040        @NotNull
041        protected ClassBuilder getDelegate() {
042            return builder;
043        }
044    
045        @Override
046        @NotNull
047        public FieldVisitor newField(
048                @NotNull JvmDeclarationOrigin origin,
049                int access,
050                @NotNull String name,
051                @NotNull String desc,
052                @Nullable String signature,
053                @Nullable Object value
054        ) {
055            return new RemappingFieldAdapter(builder.newField(origin, access, name, remapper.mapDesc(desc), signature, value), remapper);
056        }
057    
058        @Override
059        @NotNull
060        public MethodVisitor newMethod(
061                @NotNull JvmDeclarationOrigin origin,
062                int access,
063                @NotNull String name,
064                @NotNull String desc,
065                @Nullable String signature,
066                @Nullable String[] exceptions
067        ) {
068            return new RemappingMethodAdapter(access, desc,
069                                              builder.newMethod(origin, access, name, remapper.mapMethodDesc(desc), signature, exceptions),
070                                              remapper);
071        }
072    
073        @Override
074        @NotNull
075        public AnnotationVisitor newAnnotation(@NotNull String desc, boolean visible) {
076            return new RemappingAnnotationAdapter(builder.newAnnotation(remapper.mapDesc(desc), visible), remapper);
077        }
078    
079        @Override
080        @NotNull
081        public ClassVisitor getVisitor() {
082            return new RemappingClassAdapter(builder.getVisitor(), remapper);
083        }
084    
085    }