只有一套父母破房子的低端北京土著是怎么活下(转载)
https://bbs.pku.edu.cn/v2/post-read.php?bid=468&threadid=17731252
package com.example.annotationdemo.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
//表示该注解会保留在class文件中
@Target(ElementType.METHOD)
//表示该注解只能用于方法
public @interface MultipleTest {
int a() default 0;
int b() default 0;
}
package com.example.annotationdemo.annotation;
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 SingleTest {
int value() default 0;
}
package com.example.annotationdemo.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Target限定只能用在方法上 METHOD
* * - 包
* * - 类
* * - 接口
* * - 方法
* * - 构造器
* * - 成员变量
* * - 局部变量/形参变量/类型参数
*/
@Target(ElementType.METHOD)
/**
* Retention表示该注解会保留在class文件中
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface Test {
}
package com.example.annotationdemo;
import com.example.annotationdemo.annotation.MultipleTest;
public class MultipleDemo {
@MultipleTest(a=1,b=1)
public static void m1(int a,int b){
if(a+b<0){
throw new RuntimeException("Crash");
}
}
@MultipleTest
public static void m2(int a,int b){
//全部采用默认值
if(a+b<0){
throw new RuntimeException("Broken");
}
}
@MultipleTest(b=-2,a=1)
public static void m3(int a,int b){
if(a+b<0){
throw new RuntimeException("Boom");
}
}
}
package com.example.annotationdemo;
import com.example.annotationdemo.annotation.MultipleTest;
import java.lang.reflect.Method;
public class MultipleMain {
public static void main(String[] args) throws Exception{
int passed = 0, failed = 0;
String className = "com.example.annotationdemo.MultipleDemo";
for(Method m:Class.forName(className).getMethods()){
if(m.isAnnotationPresent(MultipleTest.class)){
System.out.println(m.getName());
MultipleTest st = m.getAnnotation(MultipleTest.class);
try {
m.invoke(null,st.a(),st.b());
passed++;
} catch (Throwable e) {
System.out.printf("Test %s failed: %s %n",m,e.getCause());
failed++;
}
}
}
System.out.printf("Passed: %d, Failed %d%n",passed,failed);
}
}
package com.example.annotationdemo;
public class SingleTestDemo {
}
本文来自博客园,作者:ukyo--碳水化合物,转载请注明原文链接:https://www.cnblogs.com/ukzq/p/13284101.html

浙公网安备 33010602011771号