METAGEN-104 Making sure that determination of bean properties takes method name and return type into consideration
This commit is contained in:
parent
b98c45f904
commit
5618ea53e3
|
@ -328,7 +328,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
|
|||
}
|
||||
|
||||
String string = element.getSimpleName().toString();
|
||||
if ( !StringUtil.isPropertyName( string ) ) {
|
||||
if ( !StringUtil.isProperty( string, TypeUtils.toTypeString( t.getReturnType() ) ) ) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
|
|
|
@ -173,7 +173,7 @@ public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor6<Annotatio
|
|||
}
|
||||
|
||||
String string = p.getSimpleName().toString();
|
||||
if ( !StringUtil.isPropertyName( string ) ) {
|
||||
if ( !StringUtil.isProperty( string, TypeUtils.toTypeString( t.getReturnType() ) ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -186,7 +186,7 @@ public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor6<Annotatio
|
|||
|| TypeUtils.containsAnnotation( element, Constants.ONE_TO_ONE )
|
||||
|| TypeUtils.containsAnnotation( element, Constants.MANY_TO_ONE )
|
||||
|| TypeUtils.containsAnnotation( element, Constants.EMBEDDED_ID )
|
||||
|| TypeUtils.containsAnnotation( element, Constants.ID )) {
|
||||
|| TypeUtils.containsAnnotation( element, Constants.ID ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -56,37 +56,36 @@ public final class StringUtil {
|
|||
return fqcn.substring( fqcn.lastIndexOf( NAME_SEPARATOR ) + 1 );
|
||||
}
|
||||
|
||||
public static boolean isPropertyName(String name) {
|
||||
if ( name == null ) {
|
||||
public static boolean isProperty(String methodName, String returnTypeAsString) {
|
||||
if ( methodName == null || "void".equals( returnTypeAsString ) ) {
|
||||
return false;
|
||||
}
|
||||
return checkPropertyName( name, PROPERTY_PREFIX_GET )
|
||||
|| checkPropertyName( name, PROPERTY_PREFIX_IS )
|
||||
|| checkPropertyName( name, PROPERTY_PREFIX_HAS );
|
||||
|
||||
if ( isValidPropertyName( methodName, PROPERTY_PREFIX_GET ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean checkPropertyName(String name, String prefix) {
|
||||
if ( isValidPropertyName( methodName, PROPERTY_PREFIX_IS ) || isValidPropertyName( methodName, PROPERTY_PREFIX_HAS ) ) {
|
||||
return isBooleanGetter( returnTypeAsString );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isBooleanGetter(String type) {
|
||||
return "Boolean".equals( type ) || "java.lang.Boolean".equals( type );
|
||||
}
|
||||
|
||||
private static boolean isValidPropertyName(String name, String prefix) {
|
||||
if ( !name.startsWith( prefix ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// the name has to start with the prefix and have at least one more character
|
||||
if ( name.length() < prefix.length() + 1 ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( !Character.isUpperCase( name.charAt( prefix.length() ) ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return name.length() >= prefix.length() + 1;
|
||||
}
|
||||
|
||||
public static String getPropertyName(String name) {
|
||||
if ( !isPropertyName( name ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String tmp = name;
|
||||
if ( name.startsWith( PROPERTY_PREFIX_GET ) ) {
|
||||
tmp = name.replaceFirst( PROPERTY_PREFIX_GET, "" );
|
||||
|
@ -97,7 +96,22 @@ public final class StringUtil {
|
|||
else if ( name.startsWith( PROPERTY_PREFIX_HAS ) ) {
|
||||
tmp = name.replaceFirst( PROPERTY_PREFIX_HAS, "" );
|
||||
}
|
||||
return tmp.substring( 0, 1 ).toLowerCase() + tmp.substring( 1 );
|
||||
return decapitalize( tmp );
|
||||
}
|
||||
|
||||
public static String decapitalize(String string) {
|
||||
if ( string == null || string.isEmpty() || startsWithSeveralUpperCaseLetters( string ) ) {
|
||||
return string;
|
||||
}
|
||||
else {
|
||||
return string.substring( 0, 1 ).toLowerCase() + string.substring( 1 );
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean startsWithSeveralUpperCaseLetters(String string) {
|
||||
return string.length() > 1 &&
|
||||
Character.isUpperCase( string.charAt( 0 ) ) &&
|
||||
Character.isUpperCase( string.charAt( 1 ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -434,7 +434,7 @@ public final class TypeUtils {
|
|||
}
|
||||
|
||||
String string = p.getSimpleName().toString();
|
||||
if ( !StringUtil.isPropertyName( string ) ) {
|
||||
if ( !StringUtil.isProperty( string, TypeUtils.toTypeString( t.getReturnType() ) ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -36,21 +36,20 @@ import static org.junit.Assert.assertTrue;
|
|||
public class StringUtilTest {
|
||||
@Test
|
||||
public void testIsPropertyName() {
|
||||
assertTrue( StringUtil.isPropertyName( "getFoo" ) );
|
||||
assertTrue( StringUtil.isPropertyName( "isFoo" ) );
|
||||
assertTrue( StringUtil.isPropertyName( "hasFoo" ) );
|
||||
assertTrue( StringUtil.isProperty( "getFoo", "java.lang.Object" ) );
|
||||
assertTrue( StringUtil.isProperty( "isFoo", "Boolean" ) );
|
||||
assertTrue( StringUtil.isProperty( "hasFoo", "java.lang.Boolean" ) );
|
||||
|
||||
assertFalse( StringUtil.isPropertyName( "getfoo" ) );
|
||||
assertFalse( StringUtil.isPropertyName( "isfoo" ) );
|
||||
assertFalse( StringUtil.isPropertyName( "hasfoo" ) );
|
||||
assertFalse( StringUtil.isProperty( "isfoo", "void" ) );
|
||||
assertFalse( StringUtil.isProperty( "hasfoo", "java.lang.Object" ) );
|
||||
|
||||
assertFalse( StringUtil.isPropertyName( "" ) );
|
||||
assertFalse( StringUtil.isPropertyName( null ) );
|
||||
assertFalse( StringUtil.isProperty( "", "java.lang.Object" ) );
|
||||
assertFalse( StringUtil.isProperty( null, "java.lang.Object" ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "METAGEN-76")
|
||||
public void testHashCodeNotAProperty() {
|
||||
assertFalse( StringUtil.isPropertyName( "hashCode" ) );
|
||||
assertFalse( StringUtil.isProperty( "hashCode", "Integer" ) );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,10 @@ public class HashEntity {
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
public Boolean goo() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
|
|
Loading…
Reference in New Issue