JSP编译成Servlet(五)JDT Compiler编译器

通过JSP编译器编译后生成了对应的java文件,接下去要把Java文件编译成class文件。对于这部分完全没有必要重新造轮子,常见的优秀编译工具有Eclipse JDT Java编译器和Ant编译器。Tomcat其实是同时支持两个编译器的,通过配置可以选择,而默认是使用Eclipse JDT编译器。

通过调用这些现成的编译器的API就可以方便地实现对java文件的编译,由于两个编译器功能基本一样,我们就挑默认编辑器看下它是如何进行编译的,下面仅看如何用Eclipse JDT编译器编译java文件。

Eclipse JDT提供了Compiler类用于编译,它的构造函数比较复杂,如下所示,其实就是实现自定义构造函数包含的参数即基本完成了编译工作。

public Compiler(

  INameEnvironment environment,

  IErrorHandlingPolicy policy,

  CompilerOptions options,

  final ICompilerRequestor requestor,

  IProblemFactory problemFactory) {

}

为了说明方便直接上一个简单的编译实现,如下:

public class JDTCompile {

 

    private static final File WORKDIR = new File("D:\\Program Files\\tomcat7\\work\\Catalina\\localhost\\test");

 

    public static void main(String[] args) {

 

        INameEnvironment nameEnvironment = new INameEnvironment() {

            public NameEnvironmentAnswer findType(final char[][] compoundTypeName) {

                return findType(join(compoundTypeName));

            }

 

            public NameEnvironmentAnswer findType(final char[] typeName, final char[][] packageName) {

                return findType(join(packageName) + "." + new String(typeName));

            }

 

            private NameEnvironmentAnswer findType(final String name) {

                File file = new File(WORKDIR, name.replace('.', '/') + ".java");

                if (file.isFile()) {

                    return new NameEnvironmentAnswer(new CompilationUnit(file), null);

                }

                try {

                    InputStream input =

                            this.getClass().getClassLoader().getResourceAsStream(name.replace(".", "/") + ".class");

                    if (input != null) {

                        byte[] bytes = IOUtils.toByteArray(input);

                        if (bytes != null) {

                            ClassFileReader classFileReader = new ClassFileReader(bytes, name.toCharArray(), true);

                            return new NameEnvironmentAnswer(classFileReader, null);

                        }

                    }

                } catch (ClassFormatException e) {

                    throw new RuntimeException(e);

                } catch (IOException e) {

                    throw new RuntimeException(e);

                }

                return null;

            }

 

            public boolean isPackage(char[][] parentPackageName, char[] packageName) {

                String name = new String(packageName);

                if (parentPackageName != null) {

                    name = join(parentPackageName) + "." + name;

                }

 

                File target = new File(WORKDIR, name.replace('.', '/'));

                return !target.isFile();

            }

 

            public void cleanup() {}

        };

 

        ICompilerRequestor compilerRequestor = new ICompilerRequestor() {

 

            public void acceptResult(CompilationResult result) {

                if (result.hasErrors()) {

                    for (IProblem problem : result.getErrors()) {

                        String className = new String(problem.getOriginatingFileName()).replace("/", ".");

                        className = className.substring(0, className.length() - 5);

                        String message = problem.getMessage();

                        if (problem.getID() == IProblem.CannotImportPackage) {

                            message = problem.getArguments()[0] + " cannot be resolved";

                        }

                        throw new RuntimeException(className + ":" + message);

                    }

                }

 

                ClassFile[] clazzFiles = result.getClassFiles();

                for (int i = 0; i < clazzFiles.length; i++) {

                    String clazzName = join(clazzFiles[i].getCompoundName());

                    File target = new File(WORKDIR, clazzName.replace(".", "/") + ".class");

                    try {

                        FileUtils.writeByteArrayToFile(target, clazzFiles[i].getBytes());

                    } catch (IOException e) {

                        throw new RuntimeException(e);

                    }

                }

            }

        };

 

        IProblemFactory problemFactory = new DefaultProblemFactory(Locale.ENGLISH);

        IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.exitOnFirstError();

 

        org.eclipse.jdt.internal.compiler.Compiler jdtCompiler =

                new org.eclipse.jdt.internal.compiler.Compiler(nameEnvironment, policy, getCompilerOptions(),

                        compilerRequestor, problemFactory);

 

        jdtCompiler

                .compile(new ICompilationUnit[] {new CompilationUnit(new File(WORKDIR, "org\\apache\\jsp\\HelloWorld_jsp.java"))});

    }

 

    public static CompilerOptions getCompilerOptions() {

        Map settings = new HashMap();

        String javaVersion = CompilerOptions.VERSION_1_7;

        settings.put(CompilerOptions.OPTION_Source, javaVersion);

        settings.put(CompilerOptions.OPTION_Compliance, javaVersion);

        return new CompilerOptions(settings);

    }

 

    private static class CompilationUnit implements ICompilationUnit {

 

        private File file;

 

        public CompilationUnit(File file) {

            this.file = file;

        }

 

        public char[] getContents() {

            try {

                return FileUtils.readFileToString(file).toCharArray();

            } catch (IOException e) {

                throw new RuntimeException(e);

            }

        }

 

        public char[] getMainTypeName() {

            return file.getName().replace(".java", "").toCharArray();

        }

 

        public char[][] getPackageName() {

            String fullPkgName = this.file.getParentFile().getAbsolutePath().replace(WORKDIR.getAbsolutePath(), "");

            fullPkgName = fullPkgName.replace("/", ".").replace("\\", ".");

            if (fullPkgName.startsWith("."))

                fullPkgName = fullPkgName.substring(1);

            String[] items = fullPkgName.split("[.]");

            char[][] pkgName = new char[items.length][];

            for (int i = 0; i < items.length; i++) {

                pkgName[i] = items[i].toCharArray();

            }

            return pkgName;

        }

 

        public boolean ignoreOptionalProblems() {

            return false;

        }

 

        public char[] getFileName() {

            return this.file.getName().toCharArray();

        }

    }

 

    private static String join(char[][] chars) {

        StringBuilder sb = new StringBuilder();

        for (char[] item : chars) {

            if (sb.length() > 0) {

                sb.append(".");

            }

            sb.append(item);

        }

        return sb.toString();

    }

 

}

为了更好理解,我们根据构造函数的参数分别看看,

INameEnvironment接口,主要需要实现的方法是findTypeisPackagefindType是帮助JDT找到相应的java源文件或者class字节码,根据传进来的包名和类名去寻找。例如传入“java.lang.String”或“org.apache.jsp.HelloWorld_jsp”则分别要找到JDK自带的String字节码及tomcat中编译的HelloWorld_jsp.java文件。接着按要求封装这些对象返回JDT规定的NameEnvironmentAnswer对象。而isPackage则提供是否是包的判断。

IErrorHandlingPolicy接口,用于描述错误策略,可直接使用DefaultErrorHandlingPolicies.exitOnFirstError(),表示第一个错误就退出编译。

CompilerOptions对象,指定编译时的一些参数,例如这里指定编译的Java版本为1.7

ICompilerRequestor接口,它只有一个acceptResult方法,这个方法用于处理编译后的结果,如果包含了错误信息则抛异常,否则则把编译成功的字节码写到指定路径的HelloWorld_jsp.class文件中,即生成字节码。

IProblemFactory接口,主要是控制编译错误信息的格式。

所有Compiler构造函数需要的参数对象都已经具备,传入这些参数后创建一个Compiler对象,然后调用compile方法即可对指定的java文件进行编译。这里完成了HelloWorld_jsp.java的编译,结果生成了HelloWorld_jsp.class字节码。实际的tomcat中基本也是类似这样使用JDT实现servlet的编译,但它使用的某些策略可能不相同,例如使用DefaultErrorHandlingPolicies.proceedWithAllProblems()作为错误策略。

通过这两章节“从JSPServlet”及“从Servletclass字节码”,我们已经清楚tomcatJSP编译处理的整个过程了,先根据JSP语法解析生成类似xxxx.javaServlet,然后再通过Eclipse JDTxxxx.java编译,最后生成了JVM能识别的class字节码。

你可能感兴趣的:(java,tomcat,jsp,compiler,jdt)