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 trialShaun Wetzel
9,085 PointsStruggling with Putting it all together challenge task 4 of 4 in java Annotation course
First 3 tasks i completed, and i attempted the 4th task but am a little lost on what to do.
My attempt is what i saw during the course videos and what i had on the course workspaces.
Task goal In the TestAnalyzer class, implement (complete) the getNumAnnotatedMethods method. The details of how it should function are given in the comment above the method.
output.html error messages
./TestAnalyzer.java:8: error: cannot find symbol
int annotatedMethodCount = clazz.methods().length;
^
symbol: method methods()
location: variable clazz of type Class
./TestAnalyzer.java:9: error: cannot find symbol
int actualMethodCount = clazz.getMethodCount();
^
symbol: method getMethodCount()
location: variable clazz of type Class
2 errors
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 eleOne() default "";
String[] eleTwo() default {};
}
public class SocialNetworkTest {
@Test(eleOne = "Test tweet", eleTwo = {"display the test tweets"})
public void testTweet() {
}
@Test(eleOne = "test insta", eleTwo = {"display insta tests"})
public void testInsta() {
}
@Test(eleOne = "testing facebook", eleTwo = {"display the facebook tests"})
public void testFacebook() {
}
public void testPinterest() {
}
public void testSnapchat() {
}
}
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
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 numAnnotated = 0;
int annotatedMethodCount = clazz.methods().length;
int actualMethodCount = clazz.getMethodCount();
if (annotatedMethodCount < actualMethodCount) {
numAnnotated = actualMethodCount - annotatedMethodCount;
}
return numAnnotated;
}
}
3 Answers
Shaun Wetzel
9,085 PointsStill haven't found an answer.
busongomwembe
30,682 Pointsfacing same problem
Bramyn Payne
19,589 PointsBramyn Payne
19,589 PointsI think that you are making things a little complex.
The way that I solved the problem was to initialize an integer with a value of 0. Then I looped through the declared methods using
clazz.getDeclaredMethods
. Then I checked to see if the method was annotated usingmethod.isAnnotationPresent(Test.class)
. If this was true, I just incremented the count and returned that at the end.Hopefully, that helps anybody going forward.