首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >让Javac来接我的班

让Javac来接我的班
EN

Stack Overflow用户
提问于 2014-06-19 18:10:46
回答 1查看 381关注 0票数 0

问题的背景:

创建一个包含具有不存在的超类的类的java文件;让javac解析类,但不编译它;编程地生成超类(这意味着没有类文件);最后让javac成功地生成一个类文件。

这是CompilerAPITest.java

代码语言:javascript
运行
复制
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Locale;
import java.util.logging.Logger;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;    
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

/**
* A test class to test dynamic compilation API.
*
*/
public class CompilerAPITest {
    static final Logger logger = Logger.getLogger(CompilerAPITest.class.getName()) ;

    public void doCompilation (){
        File[] files = {new File("/home/guestu/GeneratedClass.java")};

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

        StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);
        /* Prepare a list of compilation units (java source code file objects) to input to  compilation task*/
        Iterable<? extends JavaFileObject> compilationUnits = stdFileManager.getJavaFileObjects(files);

        /*Create a diagnostic controller, which holds the compilation problems*/
        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
        /*Create a compilation task from compiler by passing in the required input objects prepared above*/
        CompilationTask compilerTask = compiler.getTask(null, stdFileManager, diagnostics, null, null, compilationUnits) ;

        boolean status = compilerTask.call();
        if (!status){//If compilation error occurs
            /*Iterate through each compilation problem and print it*/
            for (Diagnostic diagnostic : diagnostics.getDiagnostics()){
                System.out.format("Error on line %d in %s", diagnostic.getLineNumber(), diagnostic);
            } 
    }
    try {
       stdFileManager.close() ;//Close the file manager
    } catch (IOException e) {
 }

//Here is Helloworld.java
public class Helloworld extends GeneratedClass{
    public static void main(String[] args){
        System.out.println("Hello");

    }

}

//And here is GeneratedClass.java
package com.sun.tools.javac.parser;

import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCModifiers;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Names;

/* This class programmatically produces the superclass "GeneratedClass" with signature public class GeneratedClass{} */

public class GeneratedClass {
    TreeMaker maker;
    Names names;
    public GeneratedClass(ParserFactory fac) {
        maker = fac. F;
        names = fac.names;
    }

    public JCCompilationUnit getTree() {
        ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
        defs.append(makeClassDecl());
        return maker.TopLevel(List.<JCAnnotation>nil(), null, defs.toList());
    }

    /* This is a class decalaration node for public class GeneratedClass{}*/
    protected JCClassDecl makeClassDecl() {
        JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
        Name name = names.fromString("GeneratedClass");
        return maker.ClassDef(mods, name, List.<JCTypeParameter>nil(), null, List.<JCExpression>nil(), List.<JCTree>nil());
    }
}

我现在的挑战

我以编程方式生成了一个类文件(GeneratedClass)。我希望它能被java编译器用来编译Helloworld.java。我尝试过上面的方法,但是Helloworld没有编译,因为它不能获取GeneratedClass。有什么办法可以做到吗?耽误您时间,实在对不起。

EN

回答 1

Stack Overflow用户

发布于 2014-06-19 18:17:49

我想你必须定制文件管理器。但是,您的任务似乎非常适合于注释处理器。代码将被解析,如果缺少某些类,则完全没有问题。解析代码的模型被传递给注释处理器,注释处理器可以做任何选择。甚至还有一些编写生成类的基础设施。看见

http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/Processor.html

http://deors.wordpress.com/2011/10/08/annotation-processors/

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

https://stackoverflow.com/questions/24313533

复制
相关文章

相似问题

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