Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Annotations Writing Your Own Annotation Putting It All Together

ALBERT QERIMI
ALBERT QERIMI
49,872 Points

Having truoble to pass any suggestion what i am missing

Having truoble to pass any suggestion what i am missing

Test.java
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD}) 
public @interface Test { 
   int num() default 1;
    boolean enabled() default true;
   String name() default "albert";
}
SocialNetworkTest.java
public class SocialNetworkTest {
  public void testTweet() {

  }
  @Test(enabled = false , name = "AAA" , num = 13)
  public void testInsta() { 

  }
  @Test(enabled = false , name = "aaA" , num = 23)
  public void testFacebook() { 

  }
  @Test(enabled = false , name = "A2A" , num = 43)
  public void testPinterest() { 

  }

  public void testSnapchat() { 

  }
}
TestAnalyzer.java
import java.lang.reflect.Method;

public class TestAnalyzer {
  /**
   *  Counts the number of methods in the class given by `clazz` that have been annotated
   *  with the @Test annotation.
   */
  public static int getNumAnnotatedMethods(Class<?> clazz) {
      int count = 0;
    for (Method m : clazz.getMethods()) { 
        if (m.hasAnnotation()) {
          count++;
          }  
        }
        return count;
  }
}

1 Answer

Kristian Gausel
Kristian Gausel
14,661 Points
import java.lang.reflect.Method;
public class TestAnalyzer {
  /**
   *  Counts the number of methods in the class given by `clazz` that have been annotated
   *  with the @Test annotation.
   */
  public static int getNumAnnotatedMethods(Class<?> clazz) {
      int count = 0;
    for (Method m : clazz.getMethods()) { 
        if (m.isAnnotationPresent(Test.class)) {
          count++;
          }  
        }
        return count;
  }
}

thanks