- 浏览: 597432 次
- 性别:
- 来自: 安徽
文章分类
最新评论
-
jianxin1009:
你说的答案是对的
Re: 一道微软的面试题(年薪40万) -
thrillerzw:
应该相信爱情
忽然相信爱情。。。 -
weng:
jaxchow 写道mikeandmore 写道我觉得国外之所 ...
国外项目的代码质量真是高啊 -
cinhoo:
怎么也应该叫大蛇蛋才对嘛
lazypy 这个小蛇蛋不错 -
rosolio:
这个怎么解决?
Hibernate Annotation的中@ManyToOne和CascadeType问题
以下几个Java官方文档描述了一些java区别于其他语言的特殊性
反射
http://java.sun.com/javase/6/docs/technotes/guides/reflection/index.html
Proxy
java.sun.com/javase/6/docs/technotes/guides/reflection/proxy.html
Annotation
java.sun.com/javase/6/docs/technotes/guides/language/annotations.html
Annotations
Many APIs require a fair amount of boilerplate code. For example, in order to write a JAX-RPC web service, you must provide a paired interface and implementation. This boilerplate could be generated automatically by a tool if the program were “decorated” with annotations indicating which methods were remotely accessible.
很多的API需要相当数量的样板代码。例如,为了写一个JAX-RPC web service,你必须提供一组配对的接口和实现。如果你在那些远程访问的methods上“装饰”annotaions的话,那么你就可以通过一个工具来自动生成(generated automatically)这些样板。<o:p></o:p>
Other APIs require “side files” to be maintained in parallel with programs. For example JavaBeans requires a BeanInfo
class to be maintained in parallel with a bean, and Enterprise JavaBeans (EJB) requires a deployment descriptor. It would be more convenient and less error-prone if the information in these side files were maintained as annotations in the program itself.
还有一些API需要"额外文件"来保持代码的同步。例如JavaBeans需要一个BeanInfo类来与一个Bean平行,EJB需要一个部署描述 。如果这些“额外文件”是用Annotaion在各自的代码上描述的话,将会更加的方便,并且减少错误。<o:p></o:p>
The Java platform has always had various ad hoc annotation mechanisms. For example the transient
modifier is an ad hoc annotation indicating that a field should be ignored by the serialization subsystem, and the @deprecated
javadoc tag is an ad hoc annotation indicating that the method should no longer be used. As of release 5.0, the platform has a general purpose annotation (also known as metadata) facility that permits you to define and use your own annotation types. The facility consists of a syntax for declaring annotation types, a syntax for annotating declarations, APIs for reading annotations, a class file representation for annotations, and an annotation processing tool.
Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program. Annotations can be read from source files, class files, or reflectively at run time.
说明不直接影响程序的语义,但它们确实改善了工具和Library的编程方式,它可以反过来影响程序的运行。 Annotation可以从源文件,类文件,在运行时间用反射来读取。
Annotations complement javadoc tags. In general, if the markup is intended to affect or produce documentation, it should probably be a javadoc tag; otherwise, it should be an annotation.
Typical application programmers will never have to define an annotation type, but it is not hard to do so. Annotation type declarations are similar to normal interface declarations. An at-sign (@
) precedes the interface
keyword. Each method declaration defines an element of the annotation type. Method declarations must not have any parameters or a throws
clause. Return types are restricted to primitives, String
, Class
, enums, annotations, and arrays of the preceding types. Methods can have default values. Here is an example annotation type declaration:
典型应用程序员不需要定义一个Annotation类型(Annotation Type),但定义Annotation类型并不难。定义Annotation类型与定义Interface很像。at符号(@)后紧跟一个interface关键字,每个方法定义一个Annotation类型的元素。方法描述不可以有任何参数和throws描述。返回类型仅限基本类型、String、Class、枚举类型、Annotations、还有之前这些类型对应的数组类型。方法可以有默认值。这里是一个定义Annotaion Type的例子
/** * Describes the Request-For-Enhancement(RFE) that led * to the presence of the annotated API element. */ public @interface RequestForEnhancement { int id(); String synopsis(); String engineer() default "[unassigned]"; String date() default "[unimplemented]"; }
Once an annotation type is defined, you can use it to annotate declarations. An annotation is a special kind of modifier, and can be used anywhere that other modifiers (such as public
, static
, or final
) can be used. By convention, annotations precede other modifiers. Annotations consist of an at-sign (@
) followed by an annotation type and a parenthesized list of element-value pairs. The values must be compile-time constants. Here is a method declaration with an annotation corresponding to the annotation type declared above:
一旦Annotaion类型定义好了,你就可以使用它来注解(annotate)描述信息了。Annotaion是一种特殊的修饰词(modifier),任何其他修饰词(如:public,static,或final)可以使用的地方都可以使用Annotaion。按照惯例,Annotaion会放在其他修饰词前面。at符合(@
) 紧跟Annotaion类型和一组用括号括起的元素-值。值必须是一个编译时常量(compile-time constants)。这里是一个使用上面的Annotaion类型来注解一个函数的例子。
@RequestForEnhancement( id = 2868724, synopsis = "Enable time-travel", engineer = "Mr. Peabody", date = "4/1/3007" ) public static void travelThroughTime(Date destination) { ... }
An annotation type with no elements is termed a marker annotation type, for example:
没有元素的Annotation类型被称为标记,例如
/** * Indicates that the specification of the annotated API element * is preliminary and subject to change. */ public @interface Preliminary { }
It is permissible to omit the parentheses in marker annotations, as shown below:
@Preliminary public class TimeTravel { ... }
In annotations with a single element, the element should be named value
, as shown below:
如果annotation只有一个元素,那这个元素应该被命名为value,如下:
/** * Associates a copyright notice with the annotated API element. */ public @interface Copyright { String value(); }
It is permissible to omit the element name and equals sign (=
) in a single-element annotation whose element name is value
, as shown below:
单一元素命名为value的Annotaion允许省略元素名和等号(=),如下:
@Copyright("2002 Yoyodyne Propulsion Systems") public class OscillationOverthruster { ... }
To tie it all together, we'll build a simple annotation-based test framework. First we need a marker annotation type to indicate that a method is a test method, and should be run by the testing tool:
import java.lang.annotation.*; /** * Indicates that the annotated method is a test method. * This annotation should be used only on parameterless static methods. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Test { }
Note that the annotation type declaration is itself annotated. Such annotations are called meta-annotations. The first (@Retention(RetentionPolicy.RUNTIME)
) indicates that annotations with this type are to be retained by the VM so they can be read reflectively at run-time. The second (@Target(ElementType.METHOD)
) indicates that this annotation type can be used to annotate only method declarations.
Here is a sample program, some of whose methods are annotated with the above interface:
public class Foo { @Test public static void m1() { } public static void m2() { } @Test public static void m3() { throw new RuntimeException("Boom"); } public static void m4() { } @Test public static void m5() { } public static void m6() { } @Test public static void m7() { throw new RuntimeException("Crash"); } public static void m8() { } }
Here is the testing tool:
import java.lang.reflect.*; public class RunTests { public static void main(String[] args) throws Exception { int passed = 0, failed = 0; for (Method m : Class.forName(args[0]).getMethods()) { if (m.isAnnotationPresent(Test.class)) { try { m.invoke(null); passed++; } catch (Throwable ex) { System.out.printf("Test %s failed: %s %n", m, ex.getCause()); failed++; } } } System.out.printf("Passed: %d, Failed %d%n", passed, failed); } }
The tool takes a class name as a command line argument and iterates over all the methods of the named class attempting to invoke each method that is annotated with the Test
annotation type (defined above). The reflective query to find out if a method has a Test
annotation is highlighted in green. If a test method invocation throws an exception, the test is deemed to have failed, and a failure report is printed. Finally, a summary is printed showing the number of tests that passed and failed. Here is how it looks when you run the testing tool on the Foo
program (above):
$ java RunTests Foo Test public static void Foo.m3() failed: java.lang.RuntimeException: Boom Test public static void Foo.m7() failed: java.lang.RuntimeException: Crash Passed: 2, Failed 2
While this testing tool is clearly a toy, it demonstrates the power of annotations and could easily be extended to overcome its limitations. <!----><!---->
发表评论
-
jvm调优经验
2010-04-23 17:50 962转自一个没写地址的网站,所以我也写他的网址了,肯定不是他们原创 ... -
jvm优化配置
2010-04-23 17:38 1106JVM优化配置《一》 OOM ... -
国外项目的代码质量真是高啊
2010-03-27 02:19 2643近期有幸承接到了一个 ... -
django做的那么好,我还有必要维护做java版lightmtv的吗?
2009-03-22 21:07 2205我一直想做web框架,后来做了lightmtv。 做的终于有 ... -
AOP不就是HOOK嘛
2008-10-07 16:20 1235AOP就是HOOK 炒概念,大忽悠 -
Hibernate各模块之间应注意兼容性
2008-09-10 16:11 1472http://www.hibernate.org/6.htm ... -
让runjettyrun可以修改运行中的项目
2008-07-30 16:53 1916最近使用runjettyrun 替代了jettylaucher ... -
你认为java适合开发互联网网站吗?
2008-07-21 13:44 4141我这里说的互联网网站指的是拥有很大并发访问量的互联网网站,并且 ... -
Person对象中"姓-名"的设计及中英习惯不同的问题
2008-06-25 11:42 1461以前一直用 FirstName,LastName来进行设计,一 ... -
Maven-Ant-Task 使用手记
2008-04-15 15:51 7644Maven的依赖管理的确是个好东西。但在集成构建方面,Ant显 ... -
对象关联关系
2008-03-04 15:03 2633双向关联,lazy Global Refrence Key, ... -
给国人的CTE的一些建议
2008-01-17 10:45 2808非常好的项目,首先他是中国人开发的,太棒了! http://w ... -
Hibernate ? ActiveRecord ? pretty code!
2008-01-09 22:44 2347看到了段代码,贴出来聊聊 public class Per ... -
Holder模式
2007-04-28 15:33 1930程序经常需要用到配置文件,但我们又并不想关心何时读取配置文件, ... -
获取java heap 大小
2007-04-09 14:28 2605The heap is the area in memory ... -
运行web项目的几种方法
2007-04-02 16:56 40241. 使用tomcat plugin,eclipse 插件,可 ... -
也论java加壳
2007-03-18 14:15 13055在C世界,对代码进行加 ... -
如何取得某个包下面的所有的Class对象
2007-03-17 14:18 4241查看了javadoc,google了,没有找到好的答案,有个例 ... -
请大家试用,疯狂POJO,crazyPOJO-0.0.1
2007-03-15 23:14 4418什么是crazyPOJO? 是一个基于jdk5 annotat ... -
修改了Tapestry5的源码,完全支持中文了
2007-03-15 22:25 6182Tapestry5最新check下来的代码,中文问题还没有解决 ...
相关推荐
包含翻译后的API文档:annotations-13.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.jetbrains:annotations:13.0; 标签:jetbrains、annotations、中文文档、jar包、java; 使用方法:解压翻译后的API文档...
包含翻译后的API文档:jackson-annotations-2.10.2-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:com.fasterxml.jackson.core:jackson-annotations:2.10.2; 标签:fasterxml、jackson、core、...
包含翻译后的API文档:jackson-annotations-2.7.8-javadoc-API文档-中文(简体)版.zip; Maven坐标:com.fasterxml.jackson.core:jackson-annotations:2.7.8; 标签:fasterxml、jackson、core、annotations、中文...
**Python 3.7.3 文档官方中文版** Python是一种高级编程语言,以其简洁、易读的语法和强大的功能而受到广泛欢迎。Python 3.7.3是该语言的一个稳定版本,发布于2019年04月09日。这个文档集合包含了多个格式,包括PDF...
中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【***.jar Maven依赖信息(可用于项目pom.xml).txt】 ...
此文档旨在为中文用户理解Hibernate Annotations提供帮助,并且它是一个协作式的翻译项目,在网络上由多个贡献者共同完成。随着Hibernate框架的持续发展与更新,该文档也会随之更新,以确保内容的时效性和准确性。 ...
这个“JAVA API官方文档中文版”压缩包包含了完整的Java API的中文翻译,对于Java开发人员和学习者来说,是一份极其重要的参考资料。 文档通常会按照Java的不同版本进行更新,确保覆盖最新的特性和功能。Java API...
中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【***.jar Maven依赖信息(可用于项目pom.xml).txt】 ...
中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【***.jar Maven依赖信息(可用于项目pom.xml).txt】 ...
中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【***.jar Maven依赖信息(可用于项目pom.xml).txt】 ...
中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【***.jar Maven依赖信息(可用于项目pom.xml).txt】 ...
#### Annotations 注解允许用户将非识别信息附加到对象上。这些信息对对象的属性没有直接的影响,但可以用于工具和库。 #### Kubernetes用户指南:应用程序管理 用户指南提供了如何使用Kubernetes进行应用管理的...
中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【***.jar Maven依赖信息(可用于项目pom.xml).txt】 ...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方中文手册文档包括了Python 3.7的语法、内置函数、模块、异常、类型、高级主题等所有方面,覆盖了从基础语法到高级特性的详尽解释。对于初学者,可以从基础部分开始,理解变量、控制流、函数等概念;对于有经验的...
中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【***.jar Maven依赖信息(可用于项目pom.xml).txt】 ...
"Python 3.7官方文档"是学习和参考这个版本的关键资源,特别是对于初学者和开发者来说。 CHM(Microsoft Compiled HTML Help)文件是一种常见的帮助文档格式,它将HTML页面编译成单一的离线文件,便于快速查找和...
Python3.7官方文档是学习和理解Python编程语言最新版本的关键资源。这个文档详尽地阐述了Python 3.7的特性和改进,为开发者提供了深入的指导和参考资料。Python是一种广泛使用的高级编程语言,以其易读性、简洁的...
Python3.2.3官方文档的中文版使得中国地区的开发者能够更方便地阅读和学习Python,避免了语言障碍,提高了学习效率。 Python 3.2的主要改进和新特性包括: 1. **元类(Metaclasses)**:Python 3.2引入了更清晰的...