Package org.gocha.jdk

Source Code of org.gocha.jdk.JaninoCompiler

package org.gocha.jdk;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.codehaus.commons.compiler.CompileException;
import org.codehaus.janino.ByteArrayClassLoader;
import org.codehaus.janino.SimpleCompiler;
import org.gocha.jvm.MemoryByteCode;

/**
* Компилятор Janino
* @author gocha
*/
public class JaninoCompiler implements Compiler
{
    @Override
    public Iterable<? extends ByteCode> compile(Iterable<? extends SourceCode> sources) {
        if (sources== null) {           
            throw new IllegalArgumentException("sources==null");
        }
       
        SimpleCompiler compiler = new SimpleCompiler();
        for( SourceCode sc : sources ){
            try {
                compiler.cook(sc.getSourceCode());
            } catch (CompileException ex) {
                Logger.getLogger(JaninoCompiler.class.getName()).log(Level.SEVERE, null, ex);
                throw new Error(ex);
            }
        }
       
        //...........
        ClassLoader cl = compiler.getClassLoader();
        if( !(cl instanceof ByteArrayClassLoader) )
            throw new Error("class loader not instance of org.codehaus.janino.ByteArrayClassLoader");
       
        ByteArrayClassLoader bcl = (ByteArrayClassLoader)cl;
       
        ArrayList<MemoryByteCode> result = new ArrayList<MemoryByteCode>();
        for( Object oClassName : bcl.getClasses().keySet() ){
            if( oClassName==null )continue;
           
            String sClassName = oClassName.toString();
            Object _data = bcl.getClasses().get(oClassName);
            byte[] data = (byte[])_data;
           
            result.add(new MemoryByteCode(sClassName, data));
        }
       
        return result;
    }
}
TOP

Related Classes of org.gocha.jdk.JaninoCompiler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.