[英]Janino dynamic compile interface class
我已經構建了一個動態編譯java源代碼的應用程序,並獲取已編譯的類信息並存儲到對象。
應用程序需要源目錄和完全限定類名(例如MOCG.entity.Person)才能將文件添加到應用程序。
我在這個應用程序中使用Janino編譯器。 我曾經通過javax.tools.ToolProvider編譯器實現,但我不知道如何編譯多個文件,它不能自動編譯相關的類。
現在我的代碼工作得很好但是當我嘗試編譯接口類或抽象類時,它總是返回錯誤:
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
這是一個輸入文件:
/**
* 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);
}
這是我剪輯的編譯代碼:
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;
}
每個建議都非常感謝。 任何代碼示例都會很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.