diff --git a/src/main/java/net/karneim/pojobuilder/analysis/JavaModelAnalyzerUtil.java b/src/main/java/net/karneim/pojobuilder/analysis/JavaModelAnalyzerUtil.java
index 97a3546..b70a7ac 100644
--- a/src/main/java/net/karneim/pojobuilder/analysis/JavaModelAnalyzerUtil.java
+++ b/src/main/java/net/karneim/pojobuilder/analysis/JavaModelAnalyzerUtil.java
@@ -54,7 +54,7 @@ public NoType getVoidType() {
/**
* Returns the classname (without any package qualifier) of the given type element.
- *
+ *
* @param typeElem the type element
* @return the classname of the given type element
*/
@@ -70,7 +70,7 @@ public String getClassname(TypeElement typeElem) {
/**
* Returns the Java package the given type (or it's outer type) belongs to.
- *
+ *
* @param type the type
* @return the Java package the given type belongs to
*/
@@ -80,7 +80,7 @@ public String getPackage(DeclaredType type) {
/**
* Returns the Java package the given type element (or it's outer type) belongs to.
- *
+ *
* @param typeElem the type element
* @return the Java package the given type element belongs to
*/
@@ -97,7 +97,7 @@ public String getPackage(TypeElement typeElem) {
/**
* Returns the top-level Java class that contains the given element.
- *
+ *
* @param elem the element
* @return the top-level Java class that contains the given element
*/
@@ -110,7 +110,7 @@ public TypeElement getCompilationUnit(Element elem) {
/**
* Returns true if the given element is accessible for the given builder.
- *
+ *
* @param el the element
* @param builderM the builder
* @return true if the given element is accessible
@@ -134,7 +134,7 @@ public boolean isAccessibleForBuilder(Element el, BuilderM builderM) {
/**
* Returns true if the given element is marked with a 'static' modifier.
- *
+ *
* @param el the element
* @return true if the given element is marked with a 'static' modifier
*/
@@ -144,35 +144,33 @@ public boolean isStatic(Element el) {
/**
* Returns true if the given element is a Setter-method.
- *
+ *
* @param el the element
* @return true if the given element is a Setter-method
*/
public boolean isSetterMethod(ExecutableElement el) {
String methodName = el.getSimpleName().toString();
- TypeMirror retType = el.getReturnType();
return methodName.startsWith(SET) && methodName.length() > SET.length()
- && retType.getKind() == VOID && el.getParameters().size() == 1;
+ && el.getParameters().size() == 1;
}
/**
* Returns true if the given element is a Getter-method.
- *
+ *
* @param el the element
* @return true if the given element is a Getter-method
*/
public boolean isGetterMethod(ExecutableElement el) {
String methodName = el.getSimpleName().toString();
TypeMirror retType = el.getReturnType();
- return ((methodName.startsWith(GET) && methodName.length() > GET.length()) || (methodName
- .startsWith(IS) && methodName.length() > IS.length()))
- && retType.getKind() != VOID
- && el.getParameters().size() == 0;
+ return ((methodName.startsWith(GET) && methodName.length() > GET.length())
+ || (methodName.startsWith(IS) && methodName.length() > IS.length()))
+ && retType.getKind() != VOID && el.getParameters().size() == 0;
}
/**
* Returns whether the given element is directly declared in {@link Object}.
- *
+ *
* @param el the element
* @return true if the element is declared in {@link Object}
*/
@@ -187,7 +185,7 @@ public boolean isDeclaredInObject(Element el) {
/**
* Returns the name of the property that is accessed by the given [G|S]etter method.
- *
+ *
* @param methodEl the method element
* @return the name of the property
*/
@@ -207,14 +205,14 @@ public String getPropertyName(ExecutableElement methodEl) {
name = firstCharToLowerCase(name);
return name;
} else {
- throw new IllegalArgumentException(String.format("Not a setter or getter method name: %s!",
- name));
+ throw new IllegalArgumentException(
+ String.format("Not a setter or getter method name: %s!", name));
}
}
/**
* Returns a copy of the given text where the first character is a lower case letter.
- *
+ *
* @param text the text
* @return a copy of the text with first letter lower case
*/
@@ -227,7 +225,7 @@ private String firstCharToLowerCase(String text) {
/**
* Returns the effective type of the given element when is is viewed as a member of the given
* owner type.
- *
+ *
* @param ownerType the owner type
* @param element the element
* @return the effective type of the given element
@@ -245,7 +243,7 @@ public TypeMirror getType(DeclaredType ownerType, Element element) {
/**
* Returns true, if the given type element has a method called "build" with no parameters and
* which has an actual return type that is compatible with the given return type.
- *
+ *
* @param typeElement the type element
* @param requiredReturnType the required return type (maybe {@link NoType})
* @return true, if the type element has a build method
@@ -258,7 +256,7 @@ public boolean hasBuildMethod(TypeElement typeElement, TypeMirror requiredReturn
* Returns true, if the given type element has a method with the given name and has an actual
* return type that is compatible with the given return type, and has an actual parameter that is
* compatible with the given parameter type.
- *
+ *
* @param typeElement the type element
* @param name the required name of the method
* @param requiredReturnType the required return type (maybe {@link NoType}).
@@ -306,7 +304,7 @@ public boolean hasMethod(TypeElement typeElement, String name, TypeMirror requir
/**
* Returns true if the given type element defines a public no-args constructor.
- *
+ *
* @param typeEl Type element.
* @return true if the given type element defines a public no-args constructor
*/
@@ -327,7 +325,7 @@ public boolean hasPublicNoArgsConstructor(TypeElement typeEl) {
/**
* Returns true if the given typeElement is a subtype of the given type parameter's upper bound.
- *
+ *
* @param typeElement the type element
* @param typeParamEl the type parameter element
* @return true if the given typeElement is a subtype of the given type parameter's upper bound
@@ -344,7 +342,7 @@ public boolean matchesUpperBound(TypeElement typeElement, TypeParameterElement t
/**
* Returns true if the given type parameter has an upper bound of type {@link Object}.
- *
+ *
* @param typeParamEl the type parameter
* @return true if the given type parameter has an upper bound of type {@link Object}
*/
@@ -361,7 +359,7 @@ public boolean isUpperBoundToObject(TypeParameterElement typeParamEl) {
/**
* Returns true
if the given string is a valid Java identifier.
- *
+ *
* @param string the string
* @return true
if the given string is a valid Java identifier
*/
@@ -382,7 +380,7 @@ public boolean isValidJavaIdentifier(String string) {
* Returns true
if the given string is a valid Java package name.
*
* This does not check if the package exists.
- *
+ *
* @param string the string
* @return true
if the given string is a valid Java package name.
*/
@@ -405,7 +403,8 @@ public Collection extends Element> findAnnotatedElements(Collection result, Element element, Class> annotationType) {
+ private void findAnnotatedElements(List result, Element element,
+ Class> annotationType) {
switch (element.getKind()) {
case CLASS:
TypeElement typeEl = (TypeElement) element;
@@ -450,10 +449,8 @@ public static String uncapitalize(final String str) {
return str;
}
- return new StringBuilder(strLen)
- .append(Character.toLowerCase(firstChar))
- .append(str.substring(1))
- .toString();
+ return new StringBuilder(strLen).append(Character.toLowerCase(firstChar))
+ .append(str.substring(1)).toString();
}
}
diff --git a/src/test/java/net/karneim/pojobuilder/analysis/with/setter/JavaModelAnalyzer_WithSetter_Test.java b/src/test/java/net/karneim/pojobuilder/analysis/with/setter/JavaModelAnalyzer_WithSetter_Test.java
new file mode 100644
index 0000000..2c5e739
--- /dev/null
+++ b/src/test/java/net/karneim/pojobuilder/analysis/with/setter/JavaModelAnalyzer_WithSetter_Test.java
@@ -0,0 +1,92 @@
+package net.karneim.pojobuilder.analysis.with.setter;
+
+import static javax.lang.model.element.Modifier.PUBLIC;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.Test;
+
+import net.karneim.pojobuilder.analysis.Input;
+import net.karneim.pojobuilder.analysis.Output;
+import net.karneim.pojobuilder.analysis.with.AnalysisTestSupport;
+import net.karneim.pojobuilder.model.PropertyListM.Key;
+import net.karneim.pojobuilder.model.PropertyM;
+
+public class JavaModelAnalyzer_WithSetter_Test extends AnalysisTestSupport {
+
+ @Test
+ public void testAnalyze_public_void_setMyProperty_Object() {
+ // Given:
+ Class> pojoClass = PojoWithPublicVoidObjectSetter.class;
+ Input input = inputFor(pojoClass);
+ // When:
+ Output output = underTest.analyze(input);
+ // Then:
+ assertThat(output).isNotNull();
+ Key key = new Key("myProperty", "java.lang.Object");
+ PropertyM property = output.getBuilderModel().getProperties().get(key);
+ assertThat(property).isNotNull();
+ assertThat(property.getSetterMethod()).isNotNull();
+ assertThat(property.getSetterMethod().getDeclaringClass().getName())
+ .isEqualTo(pojoClass.getName());
+ assertThat(property.getSetterMethod().getModifiers()).containsOnly(PUBLIC);
+ assertThat(property.getSetterMethod().getName()).isEqualTo("setMyProperty");
+ }
+
+ @Test
+ public void testAnalyze_public_void_setMyProperty_primitive() {
+ // Given:
+ Class> pojoClass = PojoWithPublicVoidPrimitiveSetter.class;
+ Input input = inputFor(pojoClass);
+ // When:
+ Output output = underTest.analyze(input);
+ // Then:
+ assertThat(output).isNotNull();
+ Key key = new Key("myProperty", "int");
+ PropertyM property = output.getBuilderModel().getProperties().get(key);
+ assertThat(property).isNotNull();
+ assertThat(property.getSetterMethod()).isNotNull();
+ assertThat(property.getSetterMethod().getDeclaringClass().getName())
+ .isEqualTo(pojoClass.getName());
+ assertThat(property.getSetterMethod().getModifiers()).containsOnly(PUBLIC);
+ assertThat(property.getSetterMethod().getName()).isEqualTo("setMyProperty");
+ }
+
+ @Test
+ public void testAnalyze_public_Object_setMyProperty_Object() {
+ // Given:
+ Class> pojoClass = PojoWithPublicObjectObjectSetter.class;
+ Input input = inputFor(pojoClass);
+ // When:
+ Output output = underTest.analyze(input);
+ // Then:
+ assertThat(output).isNotNull();
+ Key key = new Key("myProperty", "java.lang.Object");
+ PropertyM property = output.getBuilderModel().getProperties().get(key);
+ assertThat(property).isNotNull();
+ assertThat(property.getSetterMethod()).isNotNull();
+ assertThat(property.getSetterMethod().getDeclaringClass().getName())
+ .isEqualTo(pojoClass.getName());
+ assertThat(property.getSetterMethod().getModifiers()).containsOnly(PUBLIC);
+ assertThat(property.getSetterMethod().getName()).isEqualTo("setMyProperty");
+ }
+
+ @Test
+ public void testAnalyze_public_primitive_setMyProperty_Object() {
+ // Given:
+ Class> pojoClass = PojoWithPublicPrimitiveObjectSetter.class;
+ Input input = inputFor(pojoClass);
+ // When:
+ Output output = underTest.analyze(input);
+ // Then:
+ assertThat(output).isNotNull();
+ Key key = new Key("myProperty", "java.lang.Object");
+ PropertyM property = output.getBuilderModel().getProperties().get(key);
+ assertThat(property).isNotNull();
+ assertThat(property.getSetterMethod()).isNotNull();
+ assertThat(property.getSetterMethod().getDeclaringClass().getName())
+ .isEqualTo(pojoClass.getName());
+ assertThat(property.getSetterMethod().getModifiers()).containsOnly(PUBLIC);
+ assertThat(property.getSetterMethod().getName()).isEqualTo("setMyProperty");
+ }
+
+}
diff --git a/src/testdata/java/net/karneim/pojobuilder/analysis/with/setter/PojoWithPublicObjectObjectSetter.java b/src/testdata/java/net/karneim/pojobuilder/analysis/with/setter/PojoWithPublicObjectObjectSetter.java
new file mode 100644
index 0000000..6f7f2f0
--- /dev/null
+++ b/src/testdata/java/net/karneim/pojobuilder/analysis/with/setter/PojoWithPublicObjectObjectSetter.java
@@ -0,0 +1,10 @@
+package net.karneim.pojobuilder.analysis.with.setter;
+
+import net.karneim.pojobuilder.GeneratePojoBuilder;
+
+@GeneratePojoBuilder
+public class PojoWithPublicObjectObjectSetter {
+ public Object setMyProperty(Object myProperty) {
+ return null;
+ }
+}
diff --git a/src/testdata/java/net/karneim/pojobuilder/analysis/with/setter/PojoWithPublicPrimitiveObjectSetter.java b/src/testdata/java/net/karneim/pojobuilder/analysis/with/setter/PojoWithPublicPrimitiveObjectSetter.java
new file mode 100644
index 0000000..cc41ed3
--- /dev/null
+++ b/src/testdata/java/net/karneim/pojobuilder/analysis/with/setter/PojoWithPublicPrimitiveObjectSetter.java
@@ -0,0 +1,10 @@
+package net.karneim.pojobuilder.analysis.with.setter;
+
+import net.karneim.pojobuilder.GeneratePojoBuilder;
+
+@GeneratePojoBuilder
+public class PojoWithPublicPrimitiveObjectSetter {
+ public int setMyProperty(Object myProperty) {
+ return 0;
+ }
+}
diff --git a/src/testdata/java/net/karneim/pojobuilder/analysis/with/setter/PojoWithPublicVoidObjectSetter.java b/src/testdata/java/net/karneim/pojobuilder/analysis/with/setter/PojoWithPublicVoidObjectSetter.java
new file mode 100644
index 0000000..a53344c
--- /dev/null
+++ b/src/testdata/java/net/karneim/pojobuilder/analysis/with/setter/PojoWithPublicVoidObjectSetter.java
@@ -0,0 +1,8 @@
+package net.karneim.pojobuilder.analysis.with.setter;
+
+import net.karneim.pojobuilder.GeneratePojoBuilder;
+
+@GeneratePojoBuilder
+public class PojoWithPublicVoidObjectSetter {
+ public void setMyProperty(Object myProperty) {}
+}
diff --git a/src/testdata/java/net/karneim/pojobuilder/analysis/with/setter/PojoWithPublicVoidPrimitiveSetter.java b/src/testdata/java/net/karneim/pojobuilder/analysis/with/setter/PojoWithPublicVoidPrimitiveSetter.java
new file mode 100644
index 0000000..912fed2
--- /dev/null
+++ b/src/testdata/java/net/karneim/pojobuilder/analysis/with/setter/PojoWithPublicVoidPrimitiveSetter.java
@@ -0,0 +1,8 @@
+package net.karneim.pojobuilder.analysis.with.setter;
+
+import net.karneim.pojobuilder.GeneratePojoBuilder;
+
+@GeneratePojoBuilder
+public class PojoWithPublicVoidPrimitiveSetter {
+ public void setMyProperty(int myProperty) {}
+}
diff --git a/src/testdata/java/samples/FileBuilder.java b/src/testdata/java/samples/FileBuilder.java
index 4abf90b..a040845 100644
--- a/src/testdata/java/samples/FileBuilder.java
+++ b/src/testdata/java/samples/FileBuilder.java
@@ -9,6 +9,14 @@ public class FileBuilder
protected FileBuilder self;
protected String value$path$java$lang$String;
protected boolean isSet$path$java$lang$String;
+ protected long value$lastModified$long;
+ protected boolean isSet$lastModified$long;
+ protected boolean value$writable$boolean;
+ protected boolean isSet$writable$boolean;
+ protected boolean value$readable$boolean;
+ protected boolean isSet$readable$boolean;
+ protected boolean value$executable$boolean;
+ protected boolean isSet$executable$boolean;
/**
* Creates a new {@link FileBuilder}.
@@ -29,6 +37,54 @@ public FileBuilder withPath(String value) {
return self;
}
+ /**
+ * Sets the default value for the {@link File#lastModified} property.
+ *
+ * @param value the default value
+ * @return this builder
+ */
+ public FileBuilder withLastModified(long value) {
+ this.value$lastModified$long = value;
+ this.isSet$lastModified$long = true;
+ return self;
+ }
+
+ /**
+ * Sets the default value for the {@link File#writable} property.
+ *
+ * @param value the default value
+ * @return this builder
+ */
+ public FileBuilder withWritable(boolean value) {
+ this.value$writable$boolean = value;
+ this.isSet$writable$boolean = true;
+ return self;
+ }
+
+ /**
+ * Sets the default value for the {@link File#readable} property.
+ *
+ * @param value the default value
+ * @return this builder
+ */
+ public FileBuilder withReadable(boolean value) {
+ this.value$readable$boolean = value;
+ this.isSet$readable$boolean = true;
+ return self;
+ }
+
+ /**
+ * Sets the default value for the {@link File#executable} property.
+ *
+ * @param value the default value
+ * @return this builder
+ */
+ public FileBuilder withExecutable(boolean value) {
+ this.value$executable$boolean = value;
+ this.isSet$executable$boolean = true;
+ return self;
+ }
+
/**
* Returns a clone of this builder.
*
@@ -62,6 +118,18 @@ public FileBuilder but() {
public File build() {
try {
File result = FileFactory.createFile(value$path$java$lang$String);
+ if (isSet$lastModified$long) {
+ result.setLastModified(value$lastModified$long);
+ }
+ if (isSet$writable$boolean) {
+ result.setWritable(value$writable$boolean);
+ }
+ if (isSet$readable$boolean) {
+ result.setReadable(value$readable$boolean);
+ }
+ if (isSet$executable$boolean) {
+ result.setExecutable(value$executable$boolean);
+ }
return result;
} catch (RuntimeException ex) {
throw ex;