001    /*
002     * Copyright 2010-2016 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 com.intellij.psi.PsiElement;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.kotlin.codegen.ClassBuilder;
023    import org.jetbrains.kotlin.codegen.DelegatingClassBuilder;
024    import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin;
025    import org.jetbrains.org.objectweb.asm.AnnotationVisitor;
026    import org.jetbrains.org.objectweb.asm.ClassVisitor;
027    import org.jetbrains.org.objectweb.asm.FieldVisitor;
028    import org.jetbrains.org.objectweb.asm.MethodVisitor;
029    import org.jetbrains.org.objectweb.asm.commons.*;
030    
031    public class RemappingClassBuilder extends DelegatingClassBuilder {
032        private final ClassBuilder builder;
033        private final Remapper remapper;
034    
035        public RemappingClassBuilder(@NotNull ClassBuilder builder, @NotNull Remapper remapper) {
036            this.builder = builder;
037            this.remapper = remapper;
038        }
039    
040        @Override
041        @NotNull
042        protected ClassBuilder getDelegate() {
043            return builder;
044        }
045    
046        @Override
047        public void defineClass(
048                @Nullable PsiElement origin,
049                int version,
050                int access,
051                @NotNull String name,
052                @Nullable String signature,
053                @NotNull String superName,
054                @NotNull String[] interfaces
055        ) {
056            super.defineClass(origin, version, access, remapper.mapType(name), remapper.mapSignature(signature, false), remapper.mapType(superName), remapper.mapTypes(interfaces));
057        }
058    
059        @Override
060        @NotNull
061        public FieldVisitor newField(
062                @NotNull JvmDeclarationOrigin origin,
063                int access,
064                @NotNull String name,
065                @NotNull String desc,
066                @Nullable String signature,
067                @Nullable Object value
068        ) {
069            return new RemappingFieldAdapter(
070                    builder.newField(origin, access, name, remapper.mapDesc(desc), remapper.mapSignature(signature, true), value),
071                    remapper
072            );
073        }
074    
075        @Override
076        @NotNull
077        public MethodVisitor newMethod(
078                @NotNull JvmDeclarationOrigin origin,
079                int access,
080                @NotNull String name,
081                @NotNull String desc,
082                @Nullable String signature,
083                @Nullable String[] exceptions
084        ) {
085            return new RemappingMethodAdapter(
086                    access, desc,
087                    builder.newMethod(origin, access, name, remapper.mapMethodDesc(desc), remapper.mapSignature(signature, false), exceptions),
088                    remapper
089            );
090        }
091    
092        @Override
093        @NotNull
094        public AnnotationVisitor newAnnotation(@NotNull String desc, boolean visible) {
095            return new RemappingAnnotationAdapter(builder.newAnnotation(remapper.mapDesc(desc), visible), remapper);
096        }
097    
098        @Override
099        @NotNull
100        public ClassVisitor getVisitor() {
101            return new RemappingClassAdapter(builder.getVisitor(), remapper);
102        }
103    }