- 浏览: 1304706 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (351)
- Java General (37)
- .net General (2)
- Linux Toy (55)
- Oracle (81)
- Mysql (11)
- Programer Career (12)
- Oh, my living ! (2)
- Shell Script (8)
- Web Service (0)
- Linux Server (22)
- Php/Python/Perl (3P) (2)
- Javascript General (5)
- Saleforce Apex Dev (2)
- Web General (5)
- Xen & VM tech. (17)
- PSP (13)
- OpenSolaris (34)
- php (1)
- RAI/flex/action script (16)
- asterisk/CTI (7)
- 交互设计 (6)
- English (3)
- Lucene (1)
最新评论
-
GuolinLee:
markmark
JVM调优总结 -Xms -Xmx -Xmn -Xss -
di1984HIT:
写的太好啊。
JVM调优总结 -Xms -Xmx -Xmn -Xss -
javajdbc:
javajdbc 写道
JVM调优总结 -Xms -Xmx -Xmn -Xss -
javajdbc:
...
JVM调优总结 -Xms -Xmx -Xmn -Xss -
alvin198761:
非常感谢,国外的被封杀了,你这里还有一份
How to Convert An Image-Based Guest To An LVM-Based Guest
There are a lot of documentation about this part in the Internet. All you have to do is basically creating an annotation class like below:
String info() default "";
}
And that’s it. Now it’s ready to use! Now you can put copyright information to your classes :) Since we didn’t define any @Target, you can use this annotation anywhere in your classes by default. If you want your annotation to be only available for class-wise or method-wise, you should define @Target annotation. Here is a little table of what options are available:
- @Target(ElementType.PACKAGE), package header
- @Target(ElementType.TYPE), class header
- @Target(ElementType.CONSTRUCTOR), constructor header
- @Target(ElementType.METHOD), method header
- @Target(ElementType.FIELD), for class fields only
- @Target(ElementType.PARAMATER), for method parameters only
- @Target(ElementType.LOCAL_VARIABLE), for local variables only
If you want your annotation to be available in more than one place, just use array syntax as in:
One thing you may already notice is annotations are interfaces, so you don’t implement anything in them.
How to Make Use of Your Custom Annotations?
Up to here, you can find lots of examples. Okaaay, now let’s do something useful :) For instance, let’s re-implement JUnit’s @Test annotation. As you guys already know, @Test annotation is a marker annotation. Basically it marks the method as test method. If you’re expecting any exceptions, you would set expect attribute in the annotation. You can try anything here, I’m just using this example since everyone knows how @Test annotation works.
First let’s define our annotation:
@Retention(RetentionPolicy.RUNTIME)
public @interface Test {
Class expected();
}
You might notice that I used @Retention. This annotation marks our annotation to be retained by JVM at runtime. This will allow us to use Java reflections later on.
Now we need to write our annotation parser class. This class will parse our annotation and trigger some other invocations related to what we want. Keep in mind that if you have more than one custom annotation, then it’s also wise to have separate parsers for each annotation you define. So I’ll create one for this! The basic idea behind the annotation parser is using Java reflections to access the annotation information/attributes etc. So here is an example parser for our @Test annotation:
public void parse(Class<?> clazz) throws Exception {
Method[] methods = clazz.getMethods();
int pass = 0;
int fail = 0;
for (Method method : methods) {
if (method.isAnnotationPresent(Test.class)) {
try {
method.invoke(null);
pass++;
} catch (Exception e) {
fail++;
}
}
}
}
}
That’s all you need. You parser is ready to use too. But wait a minute, we didn’t implement anything about the annotation attributes. This part is a bit tricky. Because you cannot directly access those attributes from the object graph. Luckily invocation helps us here. You can only access these attributes by invoking them. Sometimes you might need to cast the class to the annotation type too. I’m sure you’ll figure out when you see it:) Anyways here is a bit more logic to take our expected attribute into account:
// this is how you access to the attributes
Test test = method.getAnnotation(Test.class);
// we use Class type here because our attribute type
// is class. If it would be string, you'd use string
Class expected = test.expected();
try {
method.invoke(null);
pass++;
} catch (Exception e) {
if (Exception.class != expected) {
fail++;
} else {
pass++;
}
}
// ...
Now everything is ready to use. Below example demonstrates how you use Parser with your test classes:
public static void main(String [] args) {
TestAnnotationParser parser = new TestAnnotationParser();
parser.parse(MyTest.class);
// you can use also Class.forName
// to load from file system directly!
}
}
Yeah, I hope you enjoyed. Don’t hesitate to put some comments down if you’ve a better approach? Thanks! Here is the full parser class implementation:
public void parse(Class<?> clazz) throws Exception {
Method[] methods = clazz.getMethods();
int pass = 0;
int fail = 0;
for (Method method : methods) {
if (method.isAnnotationPresent(Test.class)) {
// this is how you access to the attributes
Test test = method.getAnnotation(Test.class);
Class expected = test.expected();
try {
method.invoke(null);
pass++;
} catch (Exception e) {
if (Exception.class != expected) {
fail++;
} else {
pass++;
}
}
}
}
}
}
发表评论
-
使用Spring 的封装的MailSender
2010-11-29 22:24 6667使用Spring 的封装的Ma ... -
有时候,SVN 上代码太多,而我们只想下载自己负责的那个部分进行修改,这时可以这样
2010-09-04 09:06 1239svn checkout <url_of_big_dir ... -
tomcat session replication on linux server.
2010-07-26 10:49 1166Specially, to turn on multicast ... -
Session lost when app. is redeployed (Solved)
2010-07-07 16:02 1227There is a workaround to this p ... -
jvm 5.0 GC 回收机制
2009-10-16 11:55 1729http://java.sun.com/docs/hotspo ... -
JAXB 深入学习<1>
2009-08-04 22:22 2320说白了就是一个api将 xml+schema->ja ... -
simple json lib for java
2009-08-04 21:57 3178有时候为了需要将一个对象或数组转成json string 给前 ... -
在servlet 上输出图片
2008-07-30 21:38 4252public void doGet(HttpServletRe ... -
用java 对 oracle 中的 image 存取
2008-07-30 21:35 1809package data; import java.io. ... -
有关 java 的 tnameserv的link
2008-07-15 22:39 2252http://java.sun.com/j2se/1.4.2/ ... -
SOAP and JDOM
2008-06-18 21:54 2116看完上一篇 blog: Web 服务搜 ... -
Java Reflection API 运用示例
2008-05-05 15:51 2336本文节选 ... -
将系统移植到Spring
2008-04-29 11:06 1450Spring已经是一个在Apache 2.0许可下发布的基础构 ... -
动态代理一例
2008-04-28 15:33 1186在之前的一篇关于Dec ... -
使用JAVA中的动态代理实现数据库连接池
2008-04-28 13:48 1443作者通过使用JAVA中的动 ... -
Have you known enough about DBCP?
2008-04-23 12:08 2226Have you known enough about DBC ... -
AX-RPC Evolves into Simpler, More Powerful JAX-WS
2008-03-23 15:40 3524s of version 2.0, JAX-RPC has b ... -
更改 Netbeans 界面的字体大小
2008-03-22 07:29 4640学习或者使用 Netbeans 的时候, 有时候觉得界面字体很 ... -
JSF+Spring+Hibernate的实例讲解
2008-03-20 16:41 2418我一直认为jsf必定会成为MS的 Net ... -
Struts+Spring+Hibernate练习(完整)
2008-03-20 16:17 2035工具: Eclipse3.2.1、MyEclipse5 ...
相关推荐
赠送jar包:swagger-annotations-2.1.2.jar; 赠送原API文档:swagger-annotations-2.1.2-javadoc.jar; 赠送源代码:swagger-annotations-2.1.2-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-2.1.2....
赠送jar包:swagger-annotations-1.5.20.jar; 赠送原API文档:swagger-annotations-1.5.20-javadoc.jar; 赠送源代码:swagger-annotations-1.5.20-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-...
赠送jar包:error_prone_annotations-2.10.0.jar; 赠送原API文档:error_prone_annotations-2.10.0-javadoc.jar; 赠送源代码:error_prone_annotations-2.10.0-sources.jar; 赠送Maven依赖信息文件:error_prone_...
Learn how to create interactive and visually aesthetic plots using the Bokeh package in Python Key Features A step by step approach to creating interactive plots with Bokeh Go from nstallation all ...
开发工具 jackson-annotations-2.8.6开发工具 jackson-annotations-2.8.6开发工具 jackson-annotations-2.8.6开发工具 jackson-annotations-2.8.6开发工具 jackson-annotations-2.8.6开发工具 jackson-annotations-...
赠送jar包:swagger-annotations-1.6.2.jar; 赠送原API文档:swagger-annotations-1.6.2-javadoc.jar; 赠送源代码:swagger-annotations-1.6.2-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-1.6.2....
无偿奉献代码
赠送jar包:audience-annotations-0.5.0.jar; 赠送原API文档:audience-annotations-0.5.0-javadoc.jar; 赠送源代码:audience-annotations-0.5.0-sources.jar; 赠送Maven依赖信息文件:audience-annotations-...
赠送jar包:swagger-annotations-1.5.24.jar; 赠送原API文档:swagger-annotations-1.5.24-javadoc.jar; 赠送源代码:swagger-annotations-1.5.24-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-...
annotations压缩文件,包含了更新的annotations文本,用于报错的情况下使用。
赠送jar包:jackson-annotations-2.9.0.jar; 赠送原API文档:jackson-annotations-2.9.0-javadoc.jar; 赠送源代码:jackson-annotations-2.9.0-sources.jar; 赠送Maven依赖信息文件:jackson-annotations-2.9.0....
赠送jar包:error_prone_annotations-2.10.0.jar; 赠送原API文档:error_prone_annotations-2.10.0-javadoc.jar; 赠送源代码:error_prone_annotations-2.10.0-sources.jar; 赠送Maven依赖信息文件:error_prone_...
use CDI and the CDI 2.0 to automatically manage the life cycle of your enterprise Java, Java EE, or Jakarta EE application’s beans using predefined scopes and define custom life cycles using scopes....
hibernate-annotations-3.4.0.GA hibernate-annotations-3.4.0.GA hibernate-annotations-3.4.0.GA
animal-sniffer-annotations
Following this, you will come across recipes that will help you to compose static maps, create heavily customized maps, and add specialized labels and annotations. Apart from this, the book will also...
Following this, you will come across recipes that will help you to compose static maps, create heavily customized maps, and add specialized labels and annotations. Apart from this, the book will also...
赠送jar包:annotations-13.0.jar; 赠送原API文档:annotations-13.0-javadoc.jar; 赠送源代码:annotations-13.0-sources.jar; 赠送Maven依赖信息文件:annotations-13.0.pom; 包含翻译后的API文档:...
赠送jar包:jackson-module-jaxb-annotations-2.2.3.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.2.3-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.2.3-sources.jar; 赠送Maven依赖...