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

Declan Nolan
Declan Nolan
2,706 Points

I'm a little lost on what to do

I've done the first three steps but the final step is giving me trouble can anyone help me? In the TestAnalyzer class, implement (complete) the getNumAnnotatedMethods method. The details of how it should function are given in the comment above the method.

Test.java
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;
import java.lang.annotation.ElementType;

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

  String desc() default "";

  String[] params() default {};

}
SocialNetworkTest.java
public class SocialNetworkTest {
  public void testTweet() {

  }

  @Test( desc = "Insert desc here", params = {"insert", "insert 2"} )
  public void testInsta() { 

  }
    @Test(
      desc = "Insert desc here",
      params = {"insert", "insert 2"}
  )
  public void testFacebook() { 

  }

    @Test(
      desc = "Insert desc here",
      params = {"insert", "insert 2"}
  )
  public void testPinterest() { 

  }

  public void testSnapchat() { 

  }
}
TestAnalyzer.java
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) {
    return 0;  
  }
}

1 Answer

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

Hey there Declan Nolan ! 👋

This is just like what was shown in the previous video before this quiz. Here is what I did to get it to pass, but I'd recommend revisiting that video as well.

I made the necessary imports, then created a counter variable which is set to 0 initially. I then looped over the methods, checked if the current method has that annotation, and if so, increment the counter 👍

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

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 method : clazz.getDeclaredMethods()) {
      if (method.isAnnotationPresent(Test.class)) {
        count++;
      }
    }

    return count;
  }
}