简体   繁体   中英

Janino dynamic compile interface class

I have build an application for dynamic compile java source code and fetch the compiled class information and stored to object.

The application required source directory and full qualify class name (ex. MOCG.entity.Person) for adding file to the application.

I use the Janino compiler in this application. I used to implement by javax.tools.ToolProvider compiler but I dont know how to compile multiple file, and it cannot automatically compile related class.

For now my code work just fine but when I try to compile an interface class or abstract class it always return error :

Caused by: org.codehaus.commons.compiler.CompileException: File /Users/chillyprig/IdeaProjects/Mockito/src/lab05/p1/dao/CourseDAO.java, Line 22, Column 9: Identifier expected in member declaration
at org.codehaus.janino.Parser.throwCompileException(Parser.java:2593)
at org.codehaus.janino.Parser.parseInterfaceBody(Parser.java:613)
at org.codehaus.janino.Parser.parseInterfaceDeclarationRest(Parser.java:518)
at org.codehaus.janino.Parser.parsePackageMemberTypeDeclaration(Parser.java:186)
at org.codehaus.janino.Parser.parseCompilationUnit(Parser.java:74)
at org.codehaus.janino.JavaSourceIClassLoader.findIClass(JavaSourceIClassLoader.java:150)
... 46 more

This is an input file :

/**
 * Created with IntelliJ IDEA.
 * User: Dto
 * Date: 12/2/12
 * Time: 8:26 AM
 * To change this template use File | Settings | File Templates.
 */
package lab05.p1.dao;

import java.util.List;
import java.util.Set;

/**
 * This is the example of the DAO interface, you have to implement the implementation class to complete the DAO classes
 * @author  dto
 */
public interface CourseDAO {
    /**
     * Get all the courses
     * @return all courses stored in the persistence
     */
    List<Course> getCourses();

    /**
     * Get all students which enroll to the courses
     * @return all students in the persistence
     */
    Set<Student> getStudents();

    /**
     * Get the course by query the name provided
     * @param name the name of the course which the user wants
     * @return the course which contains the same name
     *          null if the course with specific name is not existed
     */
    Course getCourseByName(String name);

    /**
     * Get the Student by id
      * @param id the id of the student which we want to find
     * @return the student object with the specific id
     *          The empty student object if the student with the specific id is not exist
     */
    Student getStudentById(String id);
}

This is my snipped code for compilation :

private Class compile() throws ClassNotFoundException, SourceDirectoryNotfoundException {
        ClassLoader classLoader = null;
        try{
            classLoader = new JavaSourceClassLoader(
                    Thread.currentThread().getContextClassLoader(),
                    new File[] {new File(sourceDir)},
                    (String) null
            );
        } catch (NullPointerException e){
            throw new SourceDirectoryNotfoundException();
        }
        Class<?> c = classLoader.loadClass(fullname);        
        return c;
    }

Every suggestion is very appreciate. Any code example would be nice.

Janino是Java 1.4兼容的编译器 - 也就是说, 它无法处理 Java 5中引入的泛型 。第22行是开始List<Course> - 使用泛型,这个编译器无法处理。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM