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

Abel Cepeda
Abel Cepeda
4,661 Points

Challenge task 4 of 4

I having problems in the last challenge, of this Java Annotations, can someone help me?

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[] dog() default {};
}
SocialNetworkTest.java
public class SocialNetworkTest {
  public void testTweet() {

  }
  @Test(
    desc = "Hello iam Ai",
    dog = {"hello iam a only a dog"})
  public void testInsta() { 

  }
@Test(desc = "you need a shower", dog = {"you need to learn how to handle a bone"})
  public void testFacebook() { 

  }
@Test(desc ="no", dog = {"need food"})
  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) {
    clazz = SocialNetworkTest.class;
    Method[] methods = clazz.getDeclaredMethods();
    for(Method m: methods){
      System.out.printf("name: %s%n", m.getName());
      System.out.printf("\t# params: %s%n", m.getParameterCount());
    }

    return 0;
  }
}

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Abel;

With your code I get the following error:


Bummer! Your method returns 0. Do you have a loop that iterates over the declared methods of clazz, keeping a count of the ones where isAnnotationPresent is true?


It looks like we need to declare a counter variable and then use a for loop to loop through each method of clazz. If a given method is indeed annotated we should increment our counter and in the end return our counter, correct?

See if that gets you headed in the right direction and post back if you have further questions. You are so close to completing the course I don't want to give it away too soon. :smile:

Happy coding,
Ken

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 counter=0; clazz = SocialNetworkTest.class; Method[] methods = clazz.getDeclaredMethods(); for(Method m: methods){ boolean pre=m.isAnnotationPresent(Test.class); if(pre){ counter++; } System.out.printf("name: %s%n", m.getName()); System.out.printf("\t# params: %s%n", m.getParameterCount()); }

    return counter; } }

Bummer: Your method is not returning the correct number. Do you have a loop that iterates over the declared methods of clazz, keeping a count of the ones where isAnnotationPresent is true