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(
056                    builder.newField(origin, access, name, remapper.mapDesc(desc), remapper.mapSignature(signature, true), value),
057                    remapper
058            );
059        }
060    
061        @Override
062        @NotNull
063        public MethodVisitor newMethod(
064                @NotNull JvmDeclarationOrigin origin,
065                int access,
066                @NotNull String name,
067                @NotNull String desc,
068                @Nullable String signature,
069                @Nullable String[] exceptions
070        ) {
071            return new RemappingMethodAdapter(
072                    access, desc,
073                    builder.newMethod(origin, access, name, remapper.mapMethodDesc(desc), remapper.mapSignature(signature, false), exceptions),
074                    remapper
075            );
076        }
077    
078        @Override
079        @NotNull
080        public AnnotationVisitor newAnnotation(@NotNull String desc, boolean visible) {
081            return new RemappingAnnotationAdapter(builder.newAnnotation(remapper.mapDesc(desc), visible), remapper);
082        }
083    
084        @Override
085        @NotNull
086        public ClassVisitor getVisitor() {
087            return new RemappingClassAdapter(builder.getVisitor(), remapper);
088        }
089    }