首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Janino动态编译接口类

Janino动态编译接口类
EN

Stack Overflow用户
提问于 2013-05-21 02:07:24
回答 1查看 1.6K关注 0票数 3

我已经构建了一个动态编译java源代码的应用程序,并获取编译后的类信息并存储到object中。

应用程序需要源目录和完整的限定类名(例如MOCG.entity.Person)用于将文件添加到应用程序。

我在这个应用程序中使用了Janino编译器。我以前是用javax.tools.ToolProvider编译器实现的,但是我不知道如何编译多个文件,而且它不能自动编译相关的类。

目前,我的代码运行良好,但当我尝试编译接口类或抽象类时,它总是返回错误:

代码语言:javascript
复制
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

这是一个输入文件:

代码语言:javascript
复制
/**
 * 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);
}

以下是用于编译的代码片段:

代码语言:javascript
复制
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;
    }

每一个建议都是非常感谢的。任何代码示例都会很好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-31 22:13:22

List<Course>是Java1.4兼容的编译器--也就是在Java5中引入的it can't handle generics。第22行是List<Course>的开头行--泛型的使用,这是这个编译器不能处理的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16655577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档