- 浏览: 728387 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (419)
- 杂软粉墨 (2)
- 创意灵感 (3)
- 经验记录 (137)
- 开源轨迹 (2)
- sip-communicator (2)
- 闲侃杂谈 (8)
- 问题交流 (24)
- 概念模式 (32)
- 难点备案 (5)
- JwChat (1)
- 中国象棋 (1)
- 教育探索 (6)
- 英语研究 (58)
- 星际争霸 (1)
- 电信知识 (1)
- 软件架构 (3)
- 哲学探索 (26)
- 算法灵魂 (8)
- 近视探索 (6)
- 数学数学 (3)
- 牛角钻尖 (23)
- 至强文言 (3)
- 数据结构 (1)
- 宇宙物理 (2)
- 网络架构 (3)
- 游戏领域 (4)
- 图形处理 (2)
- 修炼之路 (8)
- 读书天地 (20)
- 编解乱码 (2)
- 概念探索 (8)
- 格物致知 (1)
- 其它语言 (1)
- 测试领域 (3)
- 文化风流 (1)
- JQuery (1)
- 網頁領域 (1)
- Unix/Linux (1)
- Inside JVM (1)
- 异常分析 (1)
最新评论
-
suyujie:
引用
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
iamzhoug37:
您能说一下"局部变量不受文本顺序限制" 是 ...
声明前为什么能赋值却不能输出,都是使用
最近迷读Effective Java 2nd Edition,其中的ITEM 35: PREFER ANNOTATIONS TO NAMING PATTERNS,也就是用annotations来替代过去的使用名称模式的习惯,代码摆上:
// Annotation type with a parameter import java.lang.annotation.*; /** * Indicates that the annotated method is a test method that must throw the * designated exception to succeed. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface ExceptionTest { Class<? extends Exception> value(); } // Program containing annotations with a parameter public class Sample2 { @ExceptionTest(ArithmeticException.class) public static void m1() { // Test should pass int i = 0; i = i / i; } @ExceptionTest(ArithmeticException.class) public static void m2() { // Should fail (wrong exception) int[] a = new int[0]; int i = a[1]; } @ExceptionTest(ArithmeticException.class) public static void m3() { } // Should fail (no exception) } // Program to process marker annotations import java.lang.reflect.*; public class RunTests { public static void main(String[] args) throws Exception { int tests = 0; int passed = 0; Class<?> testClass = Class.forName("Sample2"); for (Method m : testClass.getDeclaredMethods()) { if (m.isAnnotationPresent(ExceptionTest.class)) { tests++; try { m.invoke(null); System.out.printf("Test %s failed: no exception%n", m); } catch (InvocationTargetException wrappedEx) { Throwable exc = wrappedEx.getCause(); Class<? extends Exception> excType = m.getAnnotation(ExceptionTest.class).value(); // note the value is ArithmeticException.class // and test if the caused exception is the instance of the excType if (excType.isInstance(exc)) { passed++; } else { System.out.printf("Test %s failed: expected %s, got %s%n", m, excType.getName(), exc); } } catch (Exception exc) { System.out.println("INVALID @Test: " + m); } } } System.out.printf("Passed: %d, Failed: %d%n", passed, tests - passed); } }
ExceptionTest是带参的annotation
测试类RunTests主要是测试Sample2类中哪个方法抛出通过annotation参数指定的异常,是则passed,否则failed
结果如下:
Test public static void Sample2.m2() failed: expected java.lang.ArithmeticException, got java.lang.ArrayIndexOutOfBoundsException: 1
Test public static void Sample2.m3() failed: no exception
Passed: 1, Failed: 2
发表评论
-
关于方法访问控制符protected
2012-11-29 10:38 1198http://bbs.csdn.net/topics/3902 ... -
一个基本问题关于引用的
2012-05-15 10:20 1077问: int a = 1; Integer b = new ... -
我對面向對象和過程的理解。
2012-05-02 08:30 1011我的一些理解。 面向过程,是对客观现象的描述,感觉是有一个上 ... -
stack and heap
2012-01-13 23:17 1011我觉得是根据应用方式 和本身特性 才将内存分区的,目的是提 ... -
program experience conclusion
2011-07-11 15:35 10141. check parameters for validit ... -
PreparedStatement's possible designated parameter
2011-04-29 13:45 939though it's nearly impossible t ... -
clean Log4j
2011-04-12 11:19 1026import org.apache.log4j.BasicCo ... -
about abstract class
2011-04-02 10:34 809yes, we do know abstract class ... -
cvs operations on linux
2011-03-25 09:40 959http://www.linuxhowtos.org/Syst ... -
regex to exchange two parts
2011-03-24 17:09 1010public class Test { public ... -
About the database locking
2011-03-09 11:02 903http://en.wikipedia.org/wiki/Lo ... -
how to send soap message in java
2011-03-08 10:29 1854import java.io.BufferedReader; ... -
About ShutDownDemo
2011-03-07 15:02 921public class ShutdownDemo { p ... -
How do you know if an explicit object casting is needed
2011-02-24 16:33 1121通俗来讲,不可能将一只是猫的动物强转为狗 再说Graphic ... -
有关MimeUtility
2011-02-24 13:11 3298import java.io.UnsupportedEncod ... -
C#连接sql server 2008的一件2事
2011-02-24 09:01 2112once upon a time, i came upon o ... -
Shadowing, Overriding, Hiding and Obscuring
2011-02-22 15:15 1097当子类属性与父类属性重叠时 这种叫法上是shadowi ... -
JAXP usage
2011-02-16 16:07 1052import java.io.ByteArrayInputSt ... -
运行一个类,如果classpath中路径带空格就加双引号
2011-02-11 11:25 2762注意是这样加: java -cp .;"d:\my ... -
关于ClassPath中的current directory
2011-01-28 16:40 1115Given: 1. package com.company. ...
相关推荐
Item 39: Prefer annotations to naming patterns Item 40: Consistently use the Override annotation Item 41: Use marker interfaces to define types 7 Lambdas and Streams Item 42: Prefer lambdas to ...
Berger_Goulding_Rice-Do small businesses still prefer community banks-JBF2014
var prefer = req.prefer; if (prefer.handling === 'strict') { // strict handling } else if (prefer.handling === 'lenient') { // lenient handling } if (prefer.respondAsync) { // client prefers ...
Item 22: Prefer Defining and Implementing Interfaces to Inheritance 129 Item 23: Understand How Interface Methods Differ from Virtual Methods 139 Item 24: Express Callbacks with Delegates 143 Item 25...
Naming General Naming Rules File Names Type Names Variable Names Constant Names Function Names Namespace Names Enumerator Names Macro Names Exceptions to Naming Rules Comments Comment Style File ...
prefer V-ing/N to V-ing/N 結構︰主詞+prefer+動名詞/名詞…to+動名詞/名詞… 說明︰此句型意為“寧可…而不願…;喜歡…而不喜歡…”。<動名詞>的位置可以換成<名詞>。 In the U.S., people prefer waiting ...
developing enterprise architectures using JavaScript, others prefer not to use it at all. When the hype is over, most will probably agree that the middle way is the wisest way to go for most ...
Some people prefer to have a GPS tracker as part of their mobile phone plan as opposed to having an auxiliary one attached to the inside of their vehicle. Whichever method you choose is nothing more ...
Minikube and Docker for Mac or Windows should undoubtedly be there for those who prefer to "play" with Docker locally. AWS is the biggest hosting provider so Kubernetes Operations (kops) must be ...
Of course, if you’d prefer to just get into the meat of the project that we’ll be building, and start learning how to use all the new bells and whistles that HTML5 and CSS3 bring to the table, you ...
启用首选:安全的HTTP标头 Chrome扩展程序启用prefer: safe HTTP标头。 更多信息: :
例 Some students prefer to study alone. Others prefer to study with a group of students. Which do you prefer? Use specific reasons and examples to support your answer. 参考分析: 可以采取it depends的...
专享:prefer的用法__(全).pdf
传单首选画布使用meteorhacks:inject-initial软件包注入L_PREFER_CANVAS = true; 选项,然后加载您喜欢的传单包。 这被添加到您的文档头中: <head> [removed]L_PREFER_CANVAS = true;[removed] ... style...
Everyone wants to build a data-driven organization. It’s a popular phrase and there are plenty of books, journals, and ... I prefer to start by highlighting organizations that use data effectively
前端开源库-coffeelint-prefer-double-quotescofeelint更喜欢双引号,自定义cofeelint规则更喜欢双引号
often prefer more to less. As a result, the ones mentioned above have become major parts of the book. There are also several unusual short subjects, such as the Todd- Coxeter algorithm and the ...