Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/net/vidageek/mirror/matcher/GetterMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
final public class GetterMatcher implements Matcher<Method> {

public boolean accepts(final Method element) {
return element.getName().startsWith("get") && (element.getParameterTypes().length == 0)
&& (!element.getReturnType().equals(void.class));
return (element.getParameterTypes().length == 0) && (!element.getReturnType().equals(void.class))
&& (element.getName().startsWith("get")
|| (element.getName().startsWith("is") && element.getReturnType() == boolean.class));
}
}
81 changes: 81 additions & 0 deletions src/test/java/net/vidageek/mirror/matcher/GetterMatcherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package net.vidageek.mirror.matcher;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.lang.reflect.Method;

import org.junit.Test;

public class GetterMatcherTest {

@Test
public void testGetter() throws Exception {
Method method = Bean.class.getMethod("isValid");
assertTrue(new GetterMatcher().accepts(method));
method = Bean.class.getMethod("getSomeFlag");
assertTrue(new GetterMatcher().accepts(method));
method = Bean.class.getMethod("getName");
assertTrue(new GetterMatcher().accepts(method));
}

@Test
public void testNotGetter() throws Exception {
Method method = Bean.class.getMethod("getSomething");
assertFalse(new GetterMatcher().accepts(method));
method = Bean.class.getMethod("getSomeOther", String.class);
assertFalse(new GetterMatcher().accepts(method));
method = Bean.class.getMethod("getSomeStrange", String.class);
assertFalse(new GetterMatcher().accepts(method));
method = Bean.class.getMethod("setValid", boolean.class);
assertFalse(new GetterMatcher().accepts(method));
method = Bean.class.getMethod("isSomeOtherFlag");
assertFalse(new GetterMatcher().accepts(method));
}

private static class Bean {
private boolean valid;
private Boolean someFlag;
private String name;

public Boolean isSomeOtherFlag() {
return someFlag;
}

public boolean isValid() {
return valid;
}

public void setValid(boolean valid) {
this.valid = valid;
}

public Boolean getSomeFlag() {
return someFlag;
}

public void setSomeFlag(Boolean someFlag) {
this.someFlag = someFlag;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void getSomething() {
}

public String getSomeOther(String thing) {
return thing;
}

public void getSomeStrange(String x) {

}

}
}