只有一套父母破房子的低端北京土著是怎么活下(转载)

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 {

}

posted @ 2020-07-11 16:22  ukyo--碳水化合物  阅读(312)  评论(0)    收藏  举报