Skip to content

Commit

Permalink
Fix #127
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrodoc authored and mkarneim committed Dec 29, 2016
1 parent 6afccb1 commit 4d87dc1
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
Expand All @@ -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
*/
Expand All @@ -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}
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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}).
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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}
*/
Expand All @@ -361,7 +359,7 @@ public boolean isUpperBoundToObject(TypeParameterElement typeParamEl) {

/**
* Returns <code>true</code> if the given string is a valid Java identifier.
*
*
* @param string the string
* @return <code>true</code> if the given string is a valid Java identifier
*/
Expand All @@ -382,7 +380,7 @@ public boolean isValidJavaIdentifier(String string) {
* Returns <code>true</code> if the given string is a valid Java package name.
* <p>
* This does not check if the package exists.
*
*
* @param string the string
* @return <code>true</code> if the given string is a valid Java package name.
*/
Expand All @@ -405,7 +403,8 @@ public Collection<? extends Element> findAnnotatedElements(Collection<TypeElemen
return result;
}

private void findAnnotatedElements(List<Element> result, Element element, Class<?> annotationType) {
private void findAnnotatedElements(List<Element> result, Element element,
Class<?> annotationType) {
switch (element.getKind()) {
case CLASS:
TypeElement typeEl = (TypeElement) element;
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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) {}
}
Original file line number Diff line number Diff line change
@@ -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) {}
}
Loading

0 comments on commit 4d87dc1

Please sign in to comment.