JDK6的新特性之一_Desktop类和SystemTray类
JDK6的新特性之七_用Console开发控制台程序
JDK6的新特性之三_理解StAX
JDK6的新特性之九_CommonAnnotations
JDK6的新特性之二_使用JAXB2来实现对象与XML之间的映射
JDK6的新特性之五_轻量级HttpServer
JDK6的新特性之八_嵌入式数据库Derby
JDK6的新特性之六_插入式注解处理API
JDK6的新特性之十_Web服务元数据
JDK6的新特性之十一_更简单强大的JAX-WS
JDK6的新特性之十三_JTable的排序和过滤
JDK6的新特性之十二_脚本语言支持
JDK6的新特性之四_使用Compiler API
JDK6的新特性之一_Desktop类和SystemTray类
JDK6.0发布有段时间了,新的JDK也有不少新的特性,我去网上搜集了一下,列在下面和大家一起学习.
1.Desktop和SystemTray. 在JDK6中 ,AWT新增加了两个类:Desktop和SystemTray,前者可以用来打开系统默认浏览器浏览指定的URL,打开系统默认邮件客户端给指定的邮箱发邮件,用默认应用程序打开或编辑文件(比如,用记事本打开以txt为后缀名的文件),用系统默认的打印机打印文档;后者可以用来在系统托盘区创建一个托盘程序。
我随便找了几张图,在Tray里面都是空的,没有图,可能是图太大,有xdjm知道希望告诉我.
- import java.awt.AWTException;
- import java.awt.Desktop;
- import java.awt.Image;
- import java.awt.MenuItem;
- import java.awt.PopupMenu;
- import java.awt.SystemTray;
- import java.awt.Toolkit;
- import java.awt.TrayIcon;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- public class DesktopTrayTest{
- private static Desktop desktop;
- private static SystemTray st;
- private static PopupMenu pm;
- public static void main( String[] args ) {
- if( Desktop.isDesktopSupported() ) {
- desktop = Desktop.getDesktop();
- }
- if( SystemTray.isSupported() ) {
- st = SystemTray.getSystemTray();
- Image image = Toolkit.getDefaultToolkit().createImage( "http://www.51ppt.com.cn/Article/Uploadphotos/200604/20064147333288.png" );
- createPopupMenu();
- TrayIcon ti = new TrayIcon( image, "Demo", pm );
- try{
- st.add( ti );
- } catch( AWTException awte ) {
- awte.printStackTrace();
- }
- }
- }
- public static void sendMail( String mail ) {
- if( desktop != null &&
- desktop.isSupported( Desktop.Action.MAIL ) ) {
- try {
- desktop.mail( new URI( mail ) );
- } catch (IOException e) {
- e.printStackTrace();
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- }
- }
- public static void openBrowser( String url ) {
- if( desktop != null &&
- desktop.isSupported( Desktop.Action.BROWSE )) {
- try {
- desktop.browse( new URI( url ) );
- } catch (IOException e) {
- e.printStackTrace();
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- }
- }
- public static void edit() {
- if( desktop != null &&
- desktop.isSupported( Desktop.Action.EDIT ) ) {
- File file = new File( "test.txt" );
- try {
- if( file.exists() == false ) {
- file.create();
- }
- desktop.edit( file );
- } catch( IOException ioe ) {
- ioe.printStackTrace();
- }
- }
- }
- public static void createPopupMenu() {
- pm = new PopupMenu();
- MenuItem ob = new MenuItem( "Open url" );
- ob.addActionListener( new ActionListener() {
- public void actionPerformed( ActionEvent ae ) {
- openBrowser( "http://blog.csdn.net/xumingming64398966" );
- }
- });
- MenuItem sm = new MenuItem( "Send Mail" );
- sm.addActionListener( new ActionListener() {
- public void actionPerformed( ActionEvent ae ) {
- sendMail( "64398966@qq.com" );
- }
- });
- MenuItem ed = new MenuItem( "Edit" );
- ed.addActionListener( new ActionListener() {
- public void actionPerformed( ActionEvent ae ) {
- edit();
- }
- });
- MenuItem ex = new MenuItem( "Exit" );
- ex.addActionListener( new ActionListener() {
- public void actionPerformed( ActionEvent ae ) {
- System.exit( 0 );
- }
- });
- pm.add( ob );
- pm.add( sm );
- pm.add( ed );
- pm.addSeparator();
- pm.add( ex );
- }
- }
import java.awt.AWTException; import java.awt.Desktop; import java.awt.Image; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.Toolkit; import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; public class DesktopTrayTest{ private static Desktop desktop; private static SystemTray st; private static PopupMenu pm; public static void main( String[] args ) { if( Desktop.isDesktopSupported() ) { desktop = Desktop.getDesktop(); } if( SystemTray.isSupported() ) { st = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit().createImage( "http://www.51ppt.com.cn/Article/Uploadphotos/200604/20064147333288.png" ); createPopupMenu(); TrayIcon ti = new TrayIcon( image, "Demo", pm ); try{ st.add( ti ); } catch( AWTException awte ) { awte.printStackTrace(); } } } public static void sendMail( String mail ) { if( desktop != null && desktop.isSupported( Desktop.Action.MAIL ) ) { try { desktop.mail( new URI( mail ) ); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } } } public static void openBrowser( String url ) { if( desktop != null && desktop.isSupported( Desktop.Action.BROWSE )) { try { desktop.browse( new URI( url ) ); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } } } public static void edit() { if( desktop != null && desktop.isSupported( Desktop.Action.EDIT ) ) { File file = new File( "test.txt" ); try { if( file.exists() == false ) { file.create(); } desktop.edit( file ); } catch( IOException ioe ) { ioe.printStackTrace(); } } } public static void createPopupMenu() { pm = new PopupMenu(); MenuItem ob = new MenuItem( "Open url" ); ob.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { openBrowser( "http://blog.csdn.net/xumingming64398966" ); } }); MenuItem sm = new MenuItem( "Send Mail" ); sm.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { sendMail( "64398966@qq.com" ); } }); MenuItem ed = new MenuItem( "Edit" ); ed.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { edit(); } }); MenuItem ex = new MenuItem( "Exit" ); ex.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { System.exit( 0 ); } }); pm.add( ob ); pm.add( sm ); pm.add( ed ); pm.addSeparator(); pm.add( ex ); } }
2.Console. JDK6中提供了java.io.Console类专用来访问基于字符的控制台设备. 你的程序如果要与Windows下的cmd或者Linux下的Terminal交互,就可以用Console类代劳. 但我们不总是能得到可用的Console, 一个JVM是否有可用的Console依赖于底层平台和JVM如何被调用. 如果JVM是在交互式命令行(比如Windows的cmd)中启动的,并且输入输出没有重定向到另外的地方,那么就可以得到一个可用的Console实例. 下面代码演示了Console类的用法:
- import java.io.Console;
- public class ConsoleTest {
- public static void main( String[] args ) {
- Console console = System.console();
- if( console != null ) {
- String user = new String( console.readLine( "Enter User:", new Object[ 0 ] ) );
- String pwd = new String( console.readPassword( "Enter Password:", new Object[ 0 ] ));
- console.printf( "User name is:%s", new Object[]{user} );
- console.printf( "Password is:%s", new Object[]{pwd} );
- } else {
- System.out.println( "No Console!" );
- }
- }
- }
import java.io.Console; public class ConsoleTest { public static void main( String[] args ) { Console console = System.console(); if( console != null ) { String user = new String( console.readLine( "Enter User:", new Object[ 0 ] ) ); String pwd = new String( console.readPassword( "Enter Password:", new Object[ 0 ] )); console.printf( "User name is:%s", new Object[]{user} ); console.printf( "Password is:%s", new Object[]{pwd} ); } else { System.out.println( "No Console!" ); } } }
你如果是在一个IDE中如eclipse, netbeans中运行你将得到:
No Console!
因为只有在命令行中才能得到Console对象。
3.Compiler API. 现在我们可以用JDK6 的Compiler API(JSR 199)去动态编译Java源文件,Compiler API结合反射功能就可以实现动态的产生Java代码并编译执行这些代码,有点动态语言的特征。这个特性对于某些需要用到动态编译的应用程序相当有用,比如JSP Web Server,当我们手动修改JSP后,是不希望需要重启Web Server才可以看到效果的,这时候我们就可以用Compiler API来实现动态编译JSP文件,当然,现在的JSP Web Server也是支持JSP热部署的,现在的JSP Web Server通过在运行期间通过Runtime.exec或ProcessBuilder来调用javac来编译代码,这种方式需要我们产生另一个进程去做编译工作,不够优雅而且容易使代码依赖与特定的操作系统;Compiler API通过一套易用的标准的API提供了更加丰富的方式去做动态编译,而且是跨平台的。 下面代码演示了Compiler API的使用:
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Iterator;
- import javax.tools.JavaCompiler;
- import javax.tools.JavaFileObject;
- import javax.tools.StandardJavaFileManager;
- import javax.tools.ToolProvider;
- public class CompilerAPITest {
- private final static String srcFileName = "Test.java";
- private final static String classFileName = "Test.class";
- private final static String className = "Test";
- public static void main( String[] args ) {
- JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
- if( compiler == null ) {
- System.err.println( "Compiler is null!" );
- return;
- }
- StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null );
- generateJavaClass();
- Iterable < ? extends JavaFileObject> sourceFiles = fileManager.getJavaFileObjects( new String[]{ srcFileName } );
- compiler.getTask( null, fileManager, null, null, null, sourceFiles ).call();
- try {
- fileManager.close();
- Class.forName( className ).newInstance();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- public static void generateJavaClass() {
- try {
- FileWriter rw = new FileWriter( srcFileName );
- BufferedWriter bw = new BufferedWriter( rw );
- bw.write( "public class " + className + " {" );
- bw.newLine();
- bw.write( "public " + className + "() {");
- bw.newLine();
- bw.write( "System.out.println( 'you are in the constructor of Class Test' );" );
- bw.write( "}" );
- bw.newLine();
- bw.write( "}" );
- bw.flush();
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; import javax.tools.JavaCompiler; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.ToolProvider; public class CompilerAPITest { private final static String srcFileName = "Test.java"; private final static String classFileName = "Test.class"; private final static String className = "Test"; public static void main( String[] args ) { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if( compiler == null ) { System.err.println( "Compiler is null!" ); return; } StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null ); generateJavaClass(); Iterable < ? extends JavaFileObject> sourceFiles = fileManager.getJavaFileObjects( new String[]{ srcFileName } ); compiler.getTask( null, fileManager, null, null, null, sourceFiles ).call(); try { fileManager.close(); Class.forName( className ).newInstance(); } catch (IOException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static void generateJavaClass() { try { FileWriter rw = new FileWriter( srcFileName ); BufferedWriter bw = new BufferedWriter( rw ); bw.write( "public class " + className + " {" ); bw.newLine(); bw.write( "public " + className + "() {"); bw.newLine(); bw.write( "System.out.println( 'you are in the constructor of Class Test' );" ); bw.write( "}" ); bw.newLine(); bw.write( "}" ); bw.flush(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } }
JDK1.6.0新特性详解与代码示例
JDK6.0发布有段时间了,新的JDK也有不少新的特性,我去网上搜集了一下,列在下面和大家一起学习.
1.Desktop和SystemTray. 在JDK6中 ,AWT新增加了两个类:Desktop和SystemTray,前者可以用来打开系统默认浏览器浏览指定的URL,打开系统默认邮件客户端给指定的邮箱发邮件,用默认应用程序打开或编辑文件(比如,用记事本打开以txt为后缀名的文件),用系统默认的打印机打印文档;后者可以用来在系统托盘区创建一个托盘程序。
我随便找了几张图,在Tray里面都是空的,没有图,可能是图太大,有xdjm知道希望告诉我.
import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class DesktopTrayTest {
private static Desktop desktop;
private static SystemTray st;
private static PopupMenu pm;
public static void main( String[] args ) {
if( Desktop.isDesktopSupported() ) {
desktop = Desktop.getDesktop();
}
if( SystemTray.isSupported() ) {
st = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().createImage( "http://www.51ppt.com.cn/Article/Uploadphotos/200604/20064147333288.png" );
createPopupMenu();
TrayIcon ti = new TrayIcon( image, "Demo", pm );
try{
st.add( ti );
} catch( AWTException awte ) {
awte.printStackTrace();
}
}
}
public static void sendMail( String mail ) {
if( desktop != null &&
desktop.isSupported( Desktop.Action.MAIL ) ) {
try {
desktop.mail( new URI( mail ) );
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
public static void openBrowser( String url ) {
if( desktop != null &&
desktop.isSupported( Desktop.Action.BROWSE )) {
try {
desktop.browse( new URI( url ) );
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
public static void edit() {
if( desktop != null &&
desktop.isSupported( Desktop.Action.EDIT ) ) {
File file = new File( "test.txt" );
try {
if( file.exists() == false ) {
file.create();
}
desktop.edit( file );
} catch( IOException ioe ) {
ioe.printStackTrace();
}
}
}
public static void createPopupMenu() {
pm = new PopupMenu();
MenuItem ob = new MenuItem( "Open url" );
ob.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
openBrowser( "http://blog.csdn.net/xumingming64398966" );
}
});
MenuItem sm = new MenuItem( "Send Mail" );
sm.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
sendMail( "64398966@qq.com" );
}
});
MenuItem ed = new MenuItem( "Edit" );
ed.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
edit();
}
});
MenuItem ex = new MenuItem( "Exit" );
ex.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
System.exit( 0 );
}
});
pm.add( ob );
pm.add( sm );
pm.add( ed );
pm.addSeparator();
pm.add( ex );
}
}
2.Console. JDK6中提供了java.io.Console类专用来访问基于字符的控制台设备. 你的程序如果要与Windows下的cmd或者Linux下的Terminal交互,就可以用Console类代劳. 但我们不总是能得到可用的Console, 一个JVM是否有可用的Console依赖于底层平台和JVM如何被调用. 如果JVM是在交互式命令行(比如Windows的cmd)中启动的,并且输入输出没有重定向到另外的地方,那么就可以得到一个可用的Console实例. 下面代码演示了Console类的用法:
import java.io.Console;
public class ConsoleTest {
public static void main( String[] args ) {
Console console = System.console();
if( console != null ) {
String user = new String( console.readLine( "Enter User:", new Object[ 0 ] ) );
String pwd = new String( console.readPassword( "Enter Password:", new Object[ 0 ] ));
console.printf( "User name is:%s", new Object[]{user} );
console.printf( "Password is:%s", new Object[]{pwd} );
} else {
System.out.println( "No Console!" );
}
}
}
你如果是在一个IDE中如eclipse, netbeans中运行你将得到:
No Console!
因为只有在命令行中才能得到Console对象。
3.Compiler API. 现在我们可以用JDK6 的Compiler API(JSR 199)去动态编译Java源文件,Compiler API结合反射功能就可以实现动态的产生Java代码并编译执行这些代码,有点动态语言的特征。这个特性对于某些需要用到动态编译的应用程序相当有用,比如JSP Web Server,当我们手动修改JSP后,是不希望需要重启Web Server才可以看到效果的,这时候我们就可以用Compiler API来实现动态编译JSP文件,当然,现在的JSP Web Server也是支持JSP热部署的,现在的JSP Web Server通过在运行期间通过Runtime.exec或ProcessBuilder来调用javac来编译代码,这种方式需要我们产生另一个进程去做编译工作,不够优雅而且容易使代码依赖与特定的操作系统;Compiler API通过一套易用的标准的API提供了更加丰富的方式去做动态编译,而且是跨平台的。 下面代码演示了Compiler API的使用:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class CompilerAPITest {
private final static String srcFileName = "Test.java";
private final static String classFileName = "Test.class";
private final static String className = "Test";
public static void main( String[] args ) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if( compiler == null ) {
System.err.println( "Compiler is null!" );
return;
}
StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null );
generateJavaClass();
Iterable < ? extends JavaFileObject> sourceFiles = fileManager.getJavaFileObjects( new String[]{ srcFileName } );
compiler.getTask( null, fileManager, null, null, null, sourceFiles ).call();
try {
fileManager.close();
Class.forName( className ).newInstance();
} catch (IOException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void generateJavaClass() {
try {
FileWriter rw = new FileWriter( srcFileName );
BufferedWriter bw = new BufferedWriter( rw );
bw.write( "public class " + className + " {" );
bw.newLine();
bw.write( "public " + className + "() {");
bw.newLine();
bw.write( "System.out.println( 'you are in the constructor of Class Test' );" );
bw.write( "}" );
bw.newLine();
bw.write( "}" );
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我在运行这个例子的时候发现ToolProvider.getSystemJavaCompiler得到的是NULL,后来上网一查,原来是一个Bug!链接如下:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6477844
Closed, not reproducible
那为什么我一直在reproduce阿?
4.Http Server API. JDK6提供了一个简单的Http Server API,据此我们可以构建自己的嵌入式Http Server,它支持Http和Https协议,提供了HTTP1.1的部分实现,没有被实现的那部分可以通过扩展已有的Http Server API来实现,程序员必须自己实现HttpHandler接口,HttpServer会调用HttpHandler实现类的回调方法来处理客户端请求,在这里,我们把一个Http请求和它的响应称为一个交换,包装成HttpExchange类,HttpServer负责将HttpExchange传给 HttpHandler实现类的回调方法.下面代码演示了怎样创建自己的Http Server .
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.InetSocketAddress;
- import com.sun.net.httpserver.HttpExchange;
- import com.sun.net.httpserver.HttpHandler;
- import com.sun.net.httpserver.HttpServer;
- public class HttpServerAPITest {
- private static int count = 0;
- public static void main( String[] args ) {
- try {
- HttpServer hs = HttpServer.create( new InetSocketAddress( 8888 ), 0 );
- hs.createContext( "/", new MyHandler() );
- hs.createContext( "/java", new MyHandler() );
- hs.setExecutor( null );
- hs.start();
- System.out.println( "---begin---" );
- System.out.println( "Listening on " + hs.getAddress() );
- } catch( IOException ioe ) {
- ioe.printStackTrace();
- }
- }
- static class MyHandler implements HttpHandler {
- public void handle( HttpExchange he ) throws IOException {
- System.out.println( "Request " + count++ );
- System.out.println( he.getHttpContext().getPath() );
- InputStream is = he.getRequestBody();
- String response = "<font color='blue'>Happy Spring Festerval</font>";
- he.sendResponseHeaders( 200, response.length() );
- OutputStream os = he.getResponseBody();
- os.write( response.getBytes() );
- os.close();
- }
- }
- }
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetSocketAddress; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; public class HttpServerAPITest { private static int count = 0; public static void main( String[] args ) { try { HttpServer hs = HttpServer.create( new InetSocketAddress( 8888 ), 0 ); hs.createContext( "/", new MyHandler() ); hs.createContext( "/java", new MyHandler() ); hs.setExecutor( null ); hs.start(); System.out.println( "---begin---" ); System.out.println( "Listening on " + hs.getAddress() ); } catch( IOException ioe ) { ioe.printStackTrace(); } } static class MyHandler implements HttpHandler { public void handle( HttpExchange he ) throws IOException { System.out.println( "Request " + count++ ); System.out.println( he.getHttpContext().getPath() ); InputStream is = he.getRequestBody(); String response = "<font color='blue'>Happy Spring Festerval</font>"; he.sendResponseHeaders( 200, response.length() ); OutputStream os = he.getResponseBody(); os.write( response.getBytes() ); os.close(); } } }
5.对脚本语言的支持如: ruby, groovy, javascript.
- import java.io.FileReader;
- import javax.script.Invocable;
- import javax.script.ScriptEngine;
- import javax.script.ScriptEngineManager;
- public class ScriptTest {
- public static void main( String[] args ) {
- ScriptEngineManager manager = new ScriptEngineManager();
- ScriptEngine engine = manager.getEngineByName( "ECMAScript" );
- try {
- engine.eval( new FileReader( "C:/test.js" ) );
- Invocable invocableEngine = (Invocable)engine;
- Object ret = invocableEngine.invokeFunction( "test", null );
- System.out.println( "The result is :" + (Double)ret );
- } catch( Exception e ) {
- e.printStackTrace();
- }
- }
- }
- test.js如下:
- function test(){
- return Math.round( 11.2 );
- }
import java.io.FileReader; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class ScriptTest { public static void main( String[] args ) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName( "ECMAScript" ); try { engine.eval( new FileReader( "C:/test.js" ) ); Invocable invocableEngine = (Invocable)engine; Object ret = invocableEngine.invokeFunction( "test", null ); System.out.println( "The result is :" + (Double)ret ); } catch( Exception e ) { e.printStackTrace(); } } } test.js如下: function test(){ return Math.round( 11.2 ); }
6.插入式注解处理API(Pluggable Annotation Processing API),插入式注解处理API(JSR 269)提供一套标准API来处理Annotations.JSR 269用Annotation Processor在编译期间而不是运行期间处理Annotation, Annotation Processor相当于编译器的一个插件,所以称为插入式注解处理.如果Annotation Processor处理Annotation时(执行process方法)产生了新的Java代码,编译器会再调用一次Annotation Processor,如果第二次处理还有新代码产生,就会接着调用Annotation Processor,直到没有新代码产生为止.每执行一次process()方法被称为一个"round",这样整个Annotation processing过程可以看作是一个round的序列.
举个例子:们想建立一套基于Annotation的单元测试框架(如TestNG),在测试类里面用Annotation来标识测试期间需要执行的测试方法,如下所示:
- @TestMethod
- public void testCheckName(){
- //do something here
- }
@TestMethod public void testCheckName(){ //do something here }
这时我们就可以用JSR 269提供的API来处理测试类,根据Annotation提取出需要执行的测试方法.
再举个例子: 下面我用代码演示如何来用JSR 269提供的API来处理Annotations和读取Java源文件的元数据(metadata)
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import javax.annotation.processing.AbstractProcessor;
- import javax.annotation.processing.RoundEnvironment;
- import javax.annotation.processing.SupportedAnnotationTypes;
- import javax.annotation.processing.SupportedSourceVersion;
- import javax.lang.model.SourceVersion;
- import javax.lang.model.element.AnnotationMirror;
- import javax.lang.model.element.AnnotationValue;
- import javax.lang.model.element.Element;
- import javax.lang.model.element.ExecutableElement;
- import javax.lang.model.element.TypeElement;
- import javax.lang.model.util.ElementFilter;
- import javax.tools.Diagnostic.Kind;
- @SupportedAnnotationTypes( "ToBeTested" )
- @SupportedSourceVersion( SourceVersion.RELEASE_6 )
- public class MyAnnotationProcessor extends AbstractProcessor {
- private void note( String msg ) {
- processingEnv.getMessager().printMessage( Kind.NOTE, msg );
- }
- public boolean process( Set< ? extends TypeElement > annotations, RoundEnvironment roundEnv ) {
- for( TypeElement te : annotations ) {
- note( "annotation: " + te.toString() );
- }
- Set< ? extends Element > elements = roundEnv.getRootElements();
- for( Element e : elements ) {
- List< ? extends Element > enclosedElems = e.getEnclosedElements();
- List< ? extends ExecutableElement > ees = ElementFilter.methodsIn( enclosedElems );
- for( ExecutableElement ee : ees ) {
- note( "Executable Element Name: " + ee.getSimpleName() );
- List< ? extends AnnotationMirror > as = ee.getAnnotationMirrors();
- note( " as: " + as );
- for( AnnotationMirror am : as ){
- Map< ? extends ExecutableElement, ? extends AnnotationValue > map = am.getElementValues();
- Set< ? extends ExecutableElement > ks = map.keySet();
- for( ExecutableElement k : ks ) {
- AnnotationValue av = map.get( k );
- note("----"+ee.getSimpleName()+"."+k.getSimpleName()+"="+av.getValue());
- }
- }
- }
- }
- return false;
- }
- }
- public class Testing {
- @ToBeTested(group="A")
- public void m1(){
- }
- @ToBeTested(group="B",owner="QQ")
- public void m2(){
- }
- }
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- @Retention( RetentionPolicy.RUNTIME )
- @Target( ElementType.METHOD )
- public @interface ToBeTested {
- String owner() default "Chinajash";
- String group();
- }