001/*
002 * Copyright 2010-2013 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
017package org.jetbrains.jet.codegen;
018
019import com.intellij.psi.PsiElement;
020import org.jetbrains.annotations.Nullable;
021import org.jetbrains.asm4.*;
022
023public abstract class ClassBuilder {
024    private String thisName;
025
026    public static class Concrete extends ClassBuilder {
027        private final ClassVisitor v;
028
029        public Concrete(ClassVisitor v) {
030            this.v = v;
031        }
032
033        @Override
034        public ClassVisitor getVisitor() {
035            return v;
036        }
037    }
038
039    public FieldVisitor newField(
040            @Nullable PsiElement origin,
041            int access,
042            String name,
043            String desc,
044            @Nullable String signature,
045            @Nullable Object value
046    ) {
047        return getVisitor().visitField(access, name, desc, signature, value);
048    }
049
050    public MethodVisitor newMethod(
051            @Nullable PsiElement origin,
052            int access,
053            String name,
054            String desc,
055            @Nullable String signature,
056            @Nullable String[] exceptions
057    ) {
058        return getVisitor().visitMethod(access, name, desc, signature, exceptions);
059    }
060
061    public AnnotationVisitor newAnnotation(
062            String desc,
063            boolean visible
064    ) {
065        return getVisitor().visitAnnotation(desc, visible);
066    }
067
068    public void done() {
069        getVisitor().visitEnd();
070    }
071
072    public abstract ClassVisitor getVisitor();
073
074    public void defineClass(
075            PsiElement origin,
076            int version,
077            int access,
078            String name,
079            @Nullable String signature,
080            String superName,
081            String[] interfaces
082    ) {
083        thisName = name;
084        getVisitor().visit(version, access, name, signature, superName, interfaces);
085    }
086
087    public void visitSource(String name, @Nullable String debug) {
088        getVisitor().visitSource(name, debug);
089    }
090
091    public void visitOuterClass(String owner, @Nullable String name, @Nullable String desc) {
092        getVisitor().visitOuterClass(owner, name, desc);
093    }
094
095    public void visitInnerClass(String name, String outerName, String innerName, int access) {
096        getVisitor().visitInnerClass(name, outerName, innerName, access);
097    }
098
099    public String getThisName() {
100        return thisName;
101    }
102}