From 5618ea53e39e47e1da5e21081cb9823048c8ac92 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Tue, 29 Oct 2013 17:39:35 +0100 Subject: [PATCH] METAGEN-104 Making sure that determination of bean properties takes method name and return type into consideration --- .../JPAMetaModelEntityProcessor.java | 2 +- .../MetaAttributeGenerationVisitor.java | 4 +- .../jpamodelgen/util/StringUtil.java | 54 ++++++++++++------- .../hibernate/jpamodelgen/util/TypeUtils.java | 2 +- .../jpamodelgen/test/StringUtilTest.java | 17 +++--- .../jpamodelgen/test/hashcode/HashEntity.java | 4 ++ 6 files changed, 50 insertions(+), 33 deletions(-) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java index e5073e244a..b68798ea4b 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java @@ -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; } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java index 0fd543f9cc..96d8147fea 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java @@ -173,7 +173,7 @@ public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor6= 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 ) ); } } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java index 47bdefbdc7..01eff80a95 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java @@ -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; } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/StringUtilTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/StringUtilTest.java index 35b27f7314..7956c6c509 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/StringUtilTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/StringUtilTest.java @@ -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" ) ); } } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/hashcode/HashEntity.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/hashcode/HashEntity.java index 53d6dcde24..8e155bd422 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/hashcode/HashEntity.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/hashcode/HashEntity.java @@ -42,6 +42,10 @@ public class HashEntity { this.id = id; } + public Boolean goo() { + return null; + } + @Override public boolean equals(Object o) { if ( this == o ) {