lundi 18 juin 2018

How to inove methods with annotation parameter prioritet

How to inove methods with annotation parameter prioritet?

Got testmethods, which i should invoke with java reflection:

public class Testmethods {

//There are eight method, should be invocen with value prioritet.
//First method(1), last method(8)

//first method
@Test(8)
public void method1(){
    System.out.println("test");
}
.
.
.
.
//last method
@Test(1)
public void method8(){
    System.out.println("test7");
}

Annottation class with int parametr prioritet:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Test {
    int value();
}

Try to invoce it with loop:

   private static void start(Class c) throws Exception {
        c = Testmethods.class;
        Method[] methods = c.getDeclaredMethods();

        int before = 0;
        int after = 0;
        int annoValue = 0;

        for (Method m : methods) {
            if (m.isAnnotationPresent(BeforeSuite.class)) {
                before++;
            }
            if (m.isAnnotationPresent(AfterSuite.class)) {
                after++;
            }

            if (m.isAnnotationPresent(BeforeSuite.class) && before > COUNT || m.isAnnotationPresent(AfterSuite.class) && after > COUNT) {
                throw new RuntimeException("more then one");
            }

            if (m.isAnnotationPresent(BeforeSuite.class) && m.getAnnotation(BeforeSuite.class).value() == 10) {
                m.invoke(c.newInstance());
            }
         }


//loop to invoce.  
//Loop invoce it in random order.       
            for (int i = 1; i < 9; i++) {
            if (methods[i].isAnnotationPresent(Test.class) && (methods[i].getAnnotation(Test.class).value() < i)) {
                methods[i].invoke(c.newInstance());
            }
        }
    }

But it's not work propertly.

How to inove methods with annotation parameter prioritet?





Aucun commentaire:

Enregistrer un commentaire