现在的位置: 首页 > 综合 > 正文

关于JDK6新特性资料

2013年10月05日 ⁄ 综合 ⁄ 共 26359字 ⁄ 字号 评论关闭
JDK6的新特性
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知道希望告诉我.

Java代码
  1. import java.awt.AWTException;   
  2. import java.awt.Desktop;   
  3. import java.awt.Image;   
  4. import java.awt.MenuItem;   
  5. import java.awt.PopupMenu;   
  6. import java.awt.SystemTray;   
  7. import java.awt.Toolkit;   
  8. import java.awt.TrayIcon;   
  9. import java.awt.event.ActionEvent;   
  10. import java.awt.event.ActionListener;   
  11. import java.io.File;   
  12. import java.io.IOException;   
  13. import java.net.URI;   
  14. import java.net.URISyntaxException;   
  15.   
  16. public class DesktopTrayTest{   
  17.     private static Desktop desktop;   
  18.     private static SystemTray st;   
  19.     private static PopupMenu pm;   
  20.        
  21.     public static void main( String[] args ) {   
  22.         if( Desktop.isDesktopSupported() ) {   
  23.             desktop = Desktop.getDesktop();   
  24.         }   
  25.         if( SystemTray.isSupported() ) {   
  26.             st = SystemTray.getSystemTray();   
  27.             Image image = Toolkit.getDefaultToolkit().createImage( "http://www.51ppt.com.cn/Article/Uploadphotos/200604/20064147333288.png" );   
  28.             createPopupMenu();   
  29.             TrayIcon ti = new TrayIcon( image, "Demo", pm );   
  30.             try{   
  31.                 st.add( ti );   
  32.             } catch( AWTException awte ) {   
  33.                 awte.printStackTrace();   
  34.             }   
  35.         }   
  36.     }   
  37.     public static void sendMail( String mail ) {   
  38.         if( desktop != null &&   
  39.             desktop.isSupported( Desktop.Action.MAIL ) ) {   
  40.             try {   
  41.                 desktop.mail( new URI( mail ) );   
  42.             } catch (IOException e) {   
  43.                 e.printStackTrace();   
  44.             } catch (URISyntaxException e) {   
  45.                 e.printStackTrace();   
  46.             }   
  47.         }   
  48.     }   
  49.     public static void openBrowser( String url ) {   
  50.         if( desktop != null &&   
  51.             desktop.isSupported( Desktop.Action.BROWSE )) {   
  52.             try {   
  53.                 desktop.browse( new URI( url ) );   
  54.             } catch (IOException e) {   
  55.                 e.printStackTrace();   
  56.             } catch (URISyntaxException e) {   
  57.                 e.printStackTrace();   
  58.             }   
  59.         }   
  60.     }   
  61.     public static void edit() {   
  62.         if( desktop != null &&   
  63.             desktop.isSupported( Desktop.Action.EDIT ) ) {   
  64.             File file = new File( "test.txt" );   
  65.             try {   
  66.                 if( file.exists() == false ) {   
  67.                     file.create();   
  68.                 }   
  69.                 desktop.edit( file );   
  70.             } catch( IOException ioe ) {   
  71.                 ioe.printStackTrace();   
  72.             }   
  73.         }   
  74.     }   
  75.     public static void createPopupMenu() {   
  76.         pm = new PopupMenu();   
  77.         MenuItem ob = new MenuItem( "Open url" );   
  78.         ob.addActionListener( new ActionListener() {   
  79.             public void actionPerformed( ActionEvent ae ) {   
  80.                 openBrowser( "http://blog.csdn.net/xumingming64398966" );   
  81.             }   
  82.         });   
  83.         MenuItem sm = new MenuItem( "Send Mail" );   
  84.         sm.addActionListener( new ActionListener() {   
  85.             public void actionPerformed( ActionEvent ae ) {   
  86.                 sendMail( "64398966@qq.com" );   
  87.             }   
  88.         });   
  89.         MenuItem ed = new MenuItem( "Edit" );   
  90.         ed.addActionListener( new ActionListener() {   
  91.             public void actionPerformed( ActionEvent ae ) {   
  92.                 edit();   
  93.             }   
  94.         });   
  95.         MenuItem ex = new MenuItem( "Exit" );   
  96.         ex.addActionListener( new ActionListener() {   
  97.             public void actionPerformed( ActionEvent ae ) {   
  98.                 System.exit( 0 );   
  99.             }   
  100.         });   
  101.         pm.add( ob );   
  102.         pm.add( sm );   
  103.         pm.add( ed );   
  104.         pm.addSeparator();   
  105.         pm.add( ex );   
  106.     }   
  107. }  

2.Console. JDK6中提供了java.io.Console类专用来访问基于字符的控制台设备. 你的程序如果要与Windows下的cmd或者Linux下的Terminal交互,就可以用Console类代劳. 但我们不总是能得到可用的Console, 一个JVM是否有可用的Console依赖于底层平台和JVM如何被调用. 如果JVM是在交互式命令行(比如Windows的cmd)中启动的,并且输入输出没有重定向到另外的地方,那么就可以得到一个可用的Console实例. 下面代码演示了Console类的用法:

 

Java代码
  1. import java.io.Console;   
  2.   
  3. public class ConsoleTest {   
  4.     public static void main( String[] args ) {   
  5.         Console console = System.console();   
  6.         if( console != null ) {   
  7.             String user = new String( console.readLine( "Enter User:"new Object[ 0 ] ) );   
  8.             String pwd = new String( console.readPassword( "Enter Password:"new Object[ 0 ] ));   
  9.             console.printf( "User name is:%s"new Object[]{user} );   
  10.             console.printf( "Password is:%s"new Object[]{pwd} );   
  11.         } else {   
  12.             System.out.println( "No Console!" );   
  13.         }   
  14.     }   
  15. }  

 

你如果是在一个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的使用:

 

Java代码
  1. import java.io.BufferedWriter;   
  2. import java.io.FileWriter;   
  3. import java.io.IOException;   
  4. import java.util.Iterator;   
  5.   
  6. import javax.tools.JavaCompiler;   
  7. import javax.tools.JavaFileObject;   
  8. import javax.tools.StandardJavaFileManager;   
  9. import javax.tools.ToolProvider;   
  10.   
  11. public class CompilerAPITest {   
  12.     private final static String srcFileName = "Test.java";   
  13.     private final static String classFileName = "Test.class";   
  14.     private final static String className = "Test";   
  15.        
  16.     public static void main( String[] args ) {   
  17.         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();   
  18.         if( compiler == null ) {   
  19.             System.err.println( "Compiler is null!" );   
  20.             return;   
  21.         }   
  22.         StandardJavaFileManager fileManager = compiler.getStandardFileManager( nullnullnull );   
  23.         generateJavaClass();   
  24.            
  25.         Iterable < ? extends JavaFileObject> sourceFiles = fileManager.getJavaFileObjects( new String[]{ srcFileName } );   
  26.         compiler.getTask( null, fileManager, nullnullnull, sourceFiles ).call();   
  27.         try {   
  28.             fileManager.close();   
  29.             Class.forName( className ).newInstance();   
  30.         } catch (IOException e) {   
  31.             e.printStackTrace();   
  32.         } catch (InstantiationException e) {   
  33.             e.printStackTrace();   
  34.         } catch (IllegalAccessException e) {   
  35.             e.printStackTrace();   
  36.         } catch (ClassNotFoundException e) {   
  37.             e.printStackTrace();   
  38.         }   
  39.     }   
  40.        
  41.     public static void generateJavaClass() {   
  42.         try {   
  43.             FileWriter rw = new FileWriter( srcFileName );   
  44.             BufferedWriter bw = new BufferedWriter( rw );   
  45.             bw.write( "public class " + className + " {" );   
  46.             bw.newLine();   
  47.                
  48.             bw.write( "public " + className + "() {");   
  49.             bw.newLine();   
  50.             bw.write( "System.out.println( 'you are in the constructor of Class Test' );" );   
  51.             bw.write( "}" );   
  52.             bw.newLine();   
  53.                
  54.             bw.write( "}" );   
  55.             bw.flush();   
  56.             bw.close();   
  57.         } catch (IOException e) {   
  58.             e.printStackTrace();   
  59.         }   
  60.     }   
  61. }  

 

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 .

Java代码
  1. import java.io.IOException;   
  2. import java.io.InputStream;   
  3. import java.io.OutputStream;   
  4. import java.net.InetSocketAddress;   
  5.   
  6. import com.sun.net.httpserver.HttpExchange;   
  7. import com.sun.net.httpserver.HttpHandler;   
  8. import com.sun.net.httpserver.HttpServer;   
  9.   
  10. public class HttpServerAPITest {   
  11.     private static int count = 0;   
  12.     public static void main( String[] args ) {   
  13.         try {   
  14.             HttpServer hs = HttpServer.create( new InetSocketAddress( 8888 ), 0 );   
  15.             hs.createContext( "/"new MyHandler() );   
  16.             hs.createContext( "/java"new MyHandler() );   
  17.             hs.setExecutor( null );   
  18.             hs.start();   
  19.             System.out.println( "---begin---" );   
  20.             System.out.println( "Listening on " + hs.getAddress() );   
  21.         } catch( IOException ioe ) {   
  22.             ioe.printStackTrace();   
  23.         }   
  24.     }   
  25.     static class MyHandler implements HttpHandler {   
  26.         public void handle( HttpExchange he ) throws IOException {   
  27.             System.out.println( "Request " + count++  );   
  28.             System.out.println( he.getHttpContext().getPath() );   
  29.                
  30.             InputStream is = he.getRequestBody();   
  31.             String response = "<font color='blue'>Happy Spring Festerval</font>";   
  32.             he.sendResponseHeaders( 200, response.length() );   
  33.             OutputStream os = he.getResponseBody();   
  34.             os.write( response.getBytes() );   
  35.             os.close();   
  36.         }   
  37.     }   
  38. }  

 

5.对脚本语言的支持如: ruby, groovy, javascript.

Java代码
  1. import java.io.FileReader;   
  2.   
  3. import javax.script.Invocable;   
  4. import javax.script.ScriptEngine;   
  5. import javax.script.ScriptEngineManager;   
  6.   
  7. public class ScriptTest {   
  8.     public static void main( String[] args ) {   
  9.         ScriptEngineManager manager = new ScriptEngineManager();   
  10.         ScriptEngine engine = manager.getEngineByName( "ECMAScript" );   
  11.         try {   
  12.             engine.eval( new FileReader( "C:/test.js" ) );   
  13.             Invocable invocableEngine = (Invocable)engine;   
  14.             Object ret = invocableEngine.invokeFunction( "test"null );   
  15.             System.out.println( "The result is :" + (Double)ret );   
  16.         } catch( Exception e ) {   
  17.             e.printStackTrace();   
  18.         }   
  19.     }   
  20. }   
  21.   
  22. test.js如下:   
  23.   
  24. function test(){   
  25.     return Math.round( 11.2 );   
  26. }  

 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来标识测试期间需要执行的测试方法,如下所示:

Java代码
  1. @TestMethod  
  2.  public void testCheckName(){   
  3.        //do something here   
  4.  }  

 

这时我们就可以用JSR 269提供的API来处理测试类,根据Annotation提取出需要执行的测试方法.

再举个例子: 下面我用代码演示如何来用JSR 269提供的API来处理Annotations和读取Java源文件的元数据(metadata)

Java代码
  1. import java.util.List;   
  2. import java.util.Map;   
  3. import java.util.Set;   
  4.   
  5. import javax.annotation.processing.AbstractProcessor;   
  6. import javax.annotation.processing.RoundEnvironment;   
  7. import javax.annotation.processing.SupportedAnnotationTypes;   
  8. import javax.annotation.processing.SupportedSourceVersion;   
  9. import javax.lang.model.SourceVersion;   
  10. import javax.lang.model.element.AnnotationMirror;   
  11. import javax.lang.model.element.AnnotationValue;   
  12. import javax.lang.model.element.Element;   
  13. import javax.lang.model.element.ExecutableElement;   
  14. import javax.lang.model.element.TypeElement;   
  15. import javax.lang.model.util.ElementFilter;   
  16. import javax.tools.Diagnostic.Kind;   
  17.   
  18. @SupportedAnnotationTypes"ToBeTested" )   
  19. @SupportedSourceVersion( SourceVersion.RELEASE_6 )   
  20. public class MyAnnotationProcessor extends AbstractProcessor {   
  21.     private void note( String msg ) {   
  22.         processingEnv.getMessager().printMessage( Kind.NOTE, msg );   
  23.     }   
  24.     public boolean process( Set< ? extends TypeElement > annotations, RoundEnvironment roundEnv ) {   
  25.         for( TypeElement te : annotations ) {   
  26.             note( "annotation: " + te.toString() );   
  27.         }   
  28.         Set< ? extends Element > elements = roundEnv.getRootElements();   
  29.         for( Element e : elements ) {   
  30.             List< ? extends Element > enclosedElems = e.getEnclosedElements();   
  31.             List< ? extends ExecutableElement > ees = ElementFilter.methodsIn( enclosedElems );   
  32.             for( ExecutableElement ee : ees ) {   
  33.                 note( "Executable Element Name: " + ee.getSimpleName() );   
  34.                 List< ? extends AnnotationMirror > as = ee.getAnnotationMirrors();   
  35.                 note( " as: " + as );    
  36.                 for( AnnotationMirror am : as ){   
  37.                     Map< ? extends ExecutableElement, ? extends AnnotationValue > map = am.getElementValues();   
  38.                     Set< ? extends ExecutableElement > ks = map.keySet();   
  39.                     for( ExecutableElement k : ks ) {   
  40.                         AnnotationValue av = map.get( k );   
  41.                         note("----"+ee.getSimpleName()+"."+k.getSimpleName()+"="+av.getValue());   
  42.                     }   
  43.                 }   
  44.             }   
  45.         }   
  46.         return false;   
  47.     }   
  48. }   
  49.   
  50.     
  51.   
  52.   
  53. public class Testing {   
  54.     @ToBeTested(group="A")   
  55.     public void m1(){   
  56.     }   
  57.     @ToBeTested(group="B",owner="QQ")   
  58.     public void m2(){   
  59.     }       
  60. }   
  61.   
  62.     
  63.   
  64. import java.lang.annotation.ElementType;   
  65. import java.lang.annotation.Retention;   
  66. import java.lang.annotation.RetentionPolicy;   
  67. import java.lang.annotation.Target;   
  68.   
  69. @Retention( RetentionPolicy.RUNTIME )   
  70. @Target( ElementType.METHOD )   
  71. public @interface ToBeTested {   
  72.     String owner() default "Chinajash";   
  73.     String group();   
  74. }  

抱歉!评论已关闭.