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.cli.jvm.repl;
018
019import com.google.common.collect.Maps;
020import org.jetbrains.annotations.NotNull;
021import org.jetbrains.jet.lang.resolve.java.JvmClassName;
022import org.jetbrains.asm4.ClassReader;
023import org.jetbrains.asm4.util.TraceClassVisitor;
024
025import java.io.PrintWriter;
026import java.util.Map;
027
028public class ReplClassLoader extends ClassLoader {
029
030    private Map<JvmClassName, byte[]> classes = Maps.newLinkedHashMap();
031
032    public ReplClassLoader(@NotNull ClassLoader parent) {
033        super(parent);
034    }
035
036    public ReplClassLoader() {
037    }
038
039    @Override
040    protected Class<?> findClass(String name) throws ClassNotFoundException {
041        byte[] classBytes = classes.get(JvmClassName.byFqNameWithoutInnerClasses(name));
042        if (classBytes != null) {
043            return defineClass(name, classBytes, 0, classBytes.length);
044        }
045        else {
046            return super.findClass(name);
047        }
048    }
049
050    public void addClass(@NotNull JvmClassName className, @NotNull byte[] bytes) {
051        byte[] oldBytes = classes.put(className, bytes);
052        if (oldBytes != null) {
053            throw new IllegalStateException("Rewrite at key " + className);
054        }
055    }
056
057    public void dumpClasses(@NotNull PrintWriter writer) {
058        for (byte[] classBytes : classes.values()) {
059            new ClassReader(classBytes).accept(new TraceClassVisitor(writer), 0);
060        }
061    }
062
063}