码迷,mamicode.com
首页 > 编程语言 > 详细

Java6的新特性

时间:2017-09-24 15:22:11      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:ssi   objects   gets   包括   gre   api   mode   基本使用   system   

原文出处:xixicat

 

本文梳理了下java6的新特性,相对于java5而言,java6的特性显得少些,分量也不那么重,相当于java5是windows xp,java6有点像vista。

特性列表

  • JSR223脚本引擎

  • JSR199--Java Compiler API

  • JSR269--Pluggable Annotation Processing API

  • 支持JDBC4.0规范

  • JAX-WS 2.0规范

1、JSR223脚本引擎

Scripting for the Java Platform

  • 基本使用

public class BasicScripting {
    public void greet() throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        //支持通过名称、文件扩展名、MIMEtype查找
        ScriptEngine engine = manager.getEngineByName("JavaScript");
//        engine = manager.getEngineByExtension("js");
//        engine = manager.getEngineByMimeType("text/javascript");
        if (engine == null) {
            throw new RuntimeException("找不到JavaScript语言执行引擎。");
        }
        engine.eval("println(‘Hello!‘);");
    }
    public static void main(String[] args) {
        try {
            new BasicScripting().greet();
        } catch (ScriptException ex) {
            Logger.getLogger(BasicScripting.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
  • 绑定上下文

public class ScriptContextBindings extends JsScriptRunner {
    public void scriptContextBindings() throws ScriptException {
        ScriptEngine engine = getJavaScriptEngine();
        ScriptContext context = engine.getContext();
        Bindings bindings1 = engine.createBindings();
        bindings1.put("name", "Alex");
        context.setBindings(bindings1, ScriptContext.GLOBAL_SCOPE);
        Bindings bindings2 = engine.createBindings();
        bindings2.put("name", "Bob");
        context.setBindings(bindings2, ScriptContext.ENGINE_SCOPE);
        engine.eval("println(name);");
    }
    public void useScriptContextValues() throws ScriptException {
        ScriptEngine engine = getJavaScriptEngine();
        ScriptContext context = engine.getContext();
        Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
        bindings.put("name", "Alex");
        engine.eval("println(name);");
    }
    public void attributeInBindings() throws ScriptException {
        ScriptEngine engine = getJavaScriptEngine();
        ScriptContext context = engine.getContext();
        context.setAttribute("name", "Alex", ScriptContext.GLOBAL_SCOPE);
        engine.eval("println(name);");
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ScriptException {
        ScriptContextBindings scb = new ScriptContextBindings();
        scb.scriptContextBindings();
        scb.useScriptContextValues();
        scb.attributeInBindings();
    }
}

2、JSR199--Java Compiler API

public class JavaCompilerAPICompiler {
    public void compile(Path src, Path output) throws IOException {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
            Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(src.toFile());
            Iterable<String> options = Arrays.asList("-d", output.toString());
            JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, compilationUnits);
            boolean result = task.call();
        }
    }
}

3、JSR269--Pluggable Annotation Processing API

一部分是进行注解处理的javax.annotation.processing,另一部分是对程序的静态结构进行建模的javax.lang.model

4、其他

  • 支持JDBC4.0规范

  • JAX-WS 2.0规范(包括JAXB 2.0)

  • 轻量级HttpServer

参考

Java6的新特性

标签:ssi   objects   gets   包括   gre   api   mode   基本使用   system   

原文地址:http://www.cnblogs.com/huangwenjie/p/7587063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!