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