Skip to content

Commit

Permalink
PLANNER-766 Correct error message
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Mar 22, 2017
1 parent 60e4c4e commit 8648254
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static boolean hasGetterMethod(Class<?> containingClass, String propertyN
/**
* @param containingClass never null
* @param propertyName never null
* @return true if that getter exists
* @return sometimes null
*/
public static Method getGetterMethod(Class<?> containingClass, String propertyName) {
String getterName = PROPERTY_ACCESSOR_PREFIX_GET
Expand All @@ -128,6 +128,28 @@ public static Method getGetterMethod(Class<?> containingClass, String propertyNa
return null;
}

/**
* @param containingClass never null
* @param fieldName never null
* @return true if that field exists
*/
public static boolean hasField(Class<?> containingClass, String fieldName) {
return getField(containingClass, fieldName) != null;
}

/**
* @param containingClass never null
* @param fieldName never null
* @return sometimes null
*/
public static Field getField(Class<?> containingClass, String fieldName) {
try {
return containingClass.getField(fieldName);
} catch (NoSuchFieldException e) {
return null;
}
}

/**
* @param containingClass never null
* @param propertyType never null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,28 +374,29 @@ public Collection<VariableDescriptor<Solution_>> getDeclaredVariableDescriptors(
}

public String buildInvalidVariableNameExceptionMessage(String variableName) {
if (!ReflectionHelper.hasGetterMethod(entityClass, variableName)) {
if (!ReflectionHelper.hasGetterMethod(entityClass, variableName)
&& !ReflectionHelper.hasField(entityClass, variableName)) {
String exceptionMessage = "The variableName (" + variableName
+ ") for entityClass (" + entityClass
+ ") does not exists as a property (getter/setter) on that class.\n"
+ ") does not exists as a getter or field on that class.\n"
+ "Check the spelling of the variableName (" + variableName + ").";
if (variableName.length() >= 2
&& !Character.isUpperCase(variableName.charAt(0))
&& Character.isUpperCase(variableName.charAt(1))) {
String correctedVariableName = variableName.substring(0, 1).toUpperCase()
+ variableName.substring(1);
exceptionMessage += " It probably needs to be correctedVariableName ("
+ correctedVariableName + ") instead because the JavaBeans spec states" +
" the first letter should be a upper case if the second is upper case.";
String correctedVariableName = variableName.substring(0, 1).toUpperCase() + variableName.substring(1);
exceptionMessage += "Maybe it needs to be correctedVariableName (" + correctedVariableName
+ ") instead, if it's a getter, because the JavaBeans spec states that "
+ "the first letter should be a upper case if the second is upper case.";
}
return exceptionMessage;
}
return "The variableName (" + variableName
+ ") for entityClass (" + entityClass
+ ") exists as a property (getter/setter) on that class,"
+ " but not as an annotated as a planning variable.\n"
+ "Check if your planning entity's getter has the annotation "
+ PlanningVariable.class.getSimpleName() + " (or a shadow variable annotation).";
+ ") exists as a getter or field on that class,"
+ " but isn't in the planning variables (" + effectiveVariableDescriptorMap.keySet() + ").\n"
+ (Character.isUpperCase(variableName.charAt(0)) ? "Maybe the variableName (" + variableName + ") should start with a lowercase.\n" : "")
+ "Maybe your planning entity's getter or field lacks a " + PlanningVariable.class.getSimpleName()
+ " annotation or a shadow variable annotation.";
}

public boolean hasAnyChainedGenuineVariables() {
Expand Down

0 comments on commit 8648254

Please sign in to comment.