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.inline;
018    
019    import com.intellij.psi.PsiElement;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.jet.codegen.ClassBuilder;
023    import org.jetbrains.jet.codegen.DelegatingClassBuilder;
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                @Nullable PsiElement 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                @Nullable PsiElement 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, builder.newMethod(origin, access, name, remapper.mapMethodDesc(desc), signature, exceptions), remapper);
069        }
070    
071        @Override
072        @NotNull
073        public AnnotationVisitor newAnnotation(@NotNull String desc, boolean visible) {
074            return new RemappingAnnotationAdapter(builder.newAnnotation(remapper.mapDesc(desc), visible), remapper);
075        }
076    
077        @Override
078        @NotNull
079        public ClassVisitor getVisitor() {
080            return new RemappingClassAdapter(builder.getVisitor(), remapper);
081        }
082    }