2014-11-08 46 views
2

谁能帮助转换下面的JAVA到DART ...注释/元数据中镖郎

我想明白注释,无法找到足够的DART文档,发现这个JAVA例子here;试图将其转换成DART但未能:(

Test.java

package com.mkyong.test.core; 
    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) //can use in method only. 
    public @interface Test { 

     //should ignore this test? 
     public boolean enabled() default true; 
    } 

TesterInfo.java

package com.mkyong.test.core; 

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.TYPE) //on class level 
public @interface TesterInfo { 

public enum Priority { 
    LOW, MEDIUM, HIGH 
} 

Priority priority() default Priority.MEDIUM; 

String[] tags() default ""; 

    String createdBy() default "Mkyong"; 
    String lastModified() default "03/01/2014"; 

} 

TestExample.java

package com.mkyong.test; 

import com.mkyong.test.core.Test; 
import com.mkyong.test.core.TesterInfo; 
import com.mkyong.test.core.TesterInfo.Priority; 

@TesterInfo(
priority = Priority.HIGH, 
createdBy = "mkyong.com", 
tags = {"sales","test" } 
) 
public class TestExample { 

@Test 
void testA() { 
    if (true) 
    throw new RuntimeException("This test always failed"); 
} 

@Test(enabled = false) 
void testB() { 
    if (false) 
    throw new RuntimeException("This test always passed"); 
} 

@Test(enabled = true) 
void testC() { 
    if (10 > 1) { 
    // do nothing, this test always passed. 
    } 
} 

} 

RunTest.java

package com.mkyong.test; 

import java.lang.annotation.Annotation; 
import java.lang.reflect.Method; 

    import com.mkyong.test.core.Test; 
    import com.mkyong.test.core.TesterInfo; 

    public class RunTest { 

    public static void main(String[] args) throws Exception { 

    System.out.println("Testing..."); 

    int passed = 0, failed = 0, count = 0, ignore = 0; 

    Class<TestExample> obj = TestExample.class; 

    // Process @TesterInfo 
    if (obj.isAnnotationPresent(TesterInfo.class)) { 

    Annotation annotation = obj.getAnnotation(TesterInfo.class); 
    TesterInfo testerInfo = (TesterInfo) annotation; 

    System.out.printf("%nPriority :%s", testerInfo.priority()); 
    System.out.printf("%nCreatedBy :%s", testerInfo.createdBy()); 
    System.out.printf("%nTags :"); 

    int tagLength = testerInfo.tags().length; 
    for (String tag : testerInfo.tags()) { 
     if (tagLength > 1) { 
      System.out.print(tag + ", "); 
     } else { 
      System.out.print(tag); 
     } 
     tagLength--; 
    } 

    System.out.printf("%nLastModified :%s%n%n", testerInfo.lastModified()); 

    } 

    // Process @Test 
    for (Method method : obj.getDeclaredMethods()) { 

    // if method is annotated with @Test 
    if (method.isAnnotationPresent(Test.class)) { 

     Annotation annotation = method.getAnnotation(Test.class); 
     Test test = (Test) annotation; 

     // if enabled = true (default) 
     if (test.enabled()) { 

      try { 
      method.invoke(obj.newInstance()); 
      System.out.printf("%s - Test '%s' - passed %n", ++count, method.getName()); 
      passed++; 
      } catch (Throwable ex) { 
      System.out.printf("%s - Test '%s' - failed: %s %n", ++count, method.getName(), ex.getCause()); 
      failed++; 
      } 

     } else { 
      System.out.printf("%s - Test '%s' - ignored%n", ++count, method.getName()); 
      ignore++; 
     } 

    } 

    } 
    System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n", count, passed, failed, ignore); 

    } 
} 

的输出上面应该是:

测试...

优先级:高 CreatedBy:mkyong.com 标签:销售,测试 上次更改时间:2014年3月1日

1 - 测试 '种皮' - 失败:了java.lang.RuntimeException:此测试总是失败 2 - 测试 'TESTC' - 通过 3 - 测试 'TESTB' - 忽略

结果:共有3条,通过:1,失败1,忽略1

回答

2

我没有在代码仔细看,它做什么,但注释

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) //can use in method only. 

在Dart中没有相应的东西。在飞镖中,每个常数值都可以作为批注在每个允许注释的地方应用。

一些问题/关于飞镖

检查出的标签dart-mirrors更多。

有关在飞镖中编写单元测试的信息https://www.dartlang.org/articles/dart-unit-tests/