METAGEN-54 Cleaning up the code and making use of StringUtil.getPropertyName instead of adding a new helper method in XmlMetaEntity

This commit is contained in:
Hardy Ferentschik 2011-02-14 19:08:13 +01:00 committed by Strong Liu
parent 5bdd8c3311
commit e0e6fb0970
5 changed files with 95 additions and 48 deletions

View File

@ -15,7 +15,6 @@
* limitations under the License.
*/
package org.hibernate.jpamodelgen.xml;
import java.util.ArrayList;
@ -27,7 +26,9 @@ import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.*;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import org.hibernate.jpamodelgen.AccessTypeInformation;
@ -195,55 +196,50 @@ public class XmlMetaEntity implements MetaEntity {
continue;
}
if ( ElementKind.METHOD.equals(elem.getKind())) {
if (!propertyName.equals(determinePropertyName(elem))) {
continue;
}
}
else if(!elem.getSimpleName().toString().equals( propertyName )) {
continue;
}
String elementPropertyName = elem.getSimpleName().toString();
if ( elem.getKind().equals( ElementKind.METHOD ) ) {
elementPropertyName = StringUtil.getPropertyName( elementPropertyName );
}
DeclaredType type = determineDeclaredType(elem);
if (type != null) {
String types[] = new String[3];
determineTypes(propertyName, explicitTargetEntity, explicitMapKeyClass, type, types);
return types;
}
if ( !propertyName.equals( elementPropertyName ) ) {
continue;
}
DeclaredType type = determineDeclaredType( elem );
if ( type == null ) {
continue;
}
return determineTypes( propertyName, explicitTargetEntity, explicitMapKeyClass, type );
}
return null;
}
private DeclaredType determineDeclaredType(Element elem) {
DeclaredType type = null;
if (elem.asType() instanceof DeclaredType) {
type = ( ( DeclaredType ) elem.asType() );
}
else if (elem.asType() instanceof ExecutableType) {
ExecutableType executableType = (ExecutableType) elem.asType();
if (executableType.getReturnType() instanceof DeclaredType) {
DeclaredType declaredType = (DeclaredType) executableType.getReturnType();
type = declaredType;
}
}
return type;
}
private DeclaredType determineDeclaredType(Element elem) {
DeclaredType type = null;
if ( elem.asType() instanceof DeclaredType ) {
type = ( (DeclaredType) elem.asType() );
}
else if ( elem.asType() instanceof ExecutableType ) {
ExecutableType executableType = (ExecutableType) elem.asType();
if ( executableType.getReturnType() instanceof DeclaredType ) {
type = (DeclaredType) executableType.getReturnType();
}
}
return type;
}
/** Convert method name to property name: 'getSomething' to 'something'. */
private String determinePropertyName(Element elem) {
return elem.getSimpleName().subSequence(3,4).toString().toLowerCase()
+ elem.getSimpleName().subSequence(4,elem.getSimpleName().length());
}
private String[] determineTypes(String propertyName, String explicitTargetEntity, String explicitMapKeyClass, DeclaredType type) {
String[] types = new String[3];
determineTargetType( type, propertyName, explicitTargetEntity, types );
determineCollectionType( type, types );
if ( types[1].equals( "javax.persistence.metamodel.MapAttribute" ) ) {
determineMapType( type, explicitMapKeyClass, types );
}
return types;
}
private void determineTypes(String propertyName, String explicitTargetEntity, String explicitMapKeyClass, DeclaredType type, String types[]) {
determineTargetType( type, propertyName, explicitTargetEntity, types );
determineCollectionType( type, types );
if ( types[1].equals( "javax.persistence.metamodel.MapAttribute" ) ) {
determineMapType( type, explicitMapKeyClass, types );
}
}
private void determineMapType(DeclaredType type, String explicitMapKeyClass, String[] types) {
private void determineMapType(DeclaredType type, String explicitMapKeyClass, String[] types) {
if ( explicitMapKeyClass != null ) {
types[2] = explicitMapKeyClass;
}
@ -291,7 +287,7 @@ public class XmlMetaEntity implements MetaEntity {
String name = elem.getSimpleName().toString();
if ( ElementKind.METHOD.equals( elem.getKind() ) ) {
name = StringUtil.getPropertyName( name );
mirror = ( ( ExecutableElement ) elem ).getReturnType();
mirror = ( (ExecutableElement) elem ).getReturnType();
}
else {
mirror = elem.asType();
@ -388,9 +384,13 @@ public class XmlMetaEntity implements MetaEntity {
break;
}
}
for (org.hibernate.jpamodelgen.xml.jaxb.Embedded embedded : attributes.getEmbedded()) {
parseEmbedded( embedded);
}
}
private void parseEmbeddableAttributes(EmbeddableAttributes attributes) {
private void parseEmbeddableAttributes(EmbeddableAttributes attributes) {
if ( attributes == null ) {
return;
}
@ -452,6 +452,18 @@ public class XmlMetaEntity implements MetaEntity {
return false;
}
private void parseEmbedded(org.hibernate.jpamodelgen.xml.jaxb.Embedded embedded) {
XmlMetaSingleAttribute attribute;
ElementKind elementKind = getElementKind( embedded.getAccess() );
String type = getType( embedded.getName(), null, elementKind );
if ( type != null ) {
attribute = new XmlMetaSingleAttribute( this, embedded.getName(), type );
members.add( attribute );
}
}
private String determineExplicitTargetEntity(String targetClass) {
String explicitTargetClass = targetClass;
if ( explicitTargetClass != null ) {
@ -569,10 +581,8 @@ public class XmlMetaEntity implements MetaEntity {
private ElementKind getElementKind(org.hibernate.jpamodelgen.xml.jaxb.AccessType accessType) {
// if no explicit access type was specified in xml we use the entity access type
if ( accessType == null ) {
// return TypeUtils.getElementKindForAccessType( accessTypeInfo.getDefaultAccessType());
return TypeUtils.getElementKindForAccessType( accessTypeInfo.getAccessType() );
}
if ( org.hibernate.jpamodelgen.xml.jaxb.AccessType.FIELD.equals( accessType ) ) {
return ElementKind.FIELD;
}

View File

@ -0,0 +1,6 @@
package org.hibernate.jpamodelgen.test.xmlonly;
public class Option {
private Long id;
private Period period;
}

View File

@ -0,0 +1,8 @@
package org.hibernate.jpamodelgen.test.xmlonly;
import java.util.Date;
public class Period {
private Date start;
private Date end;
}

View File

@ -50,6 +50,13 @@ public class XmlOnlyTest extends CompilationTest {
assertPresenceOfFieldInMetamodelFor(Tire.class, "car", "Type should be inferred from field");
}
@Test
public void testMetaModelGeneratedForEmbeddable() {
assertPresenceOfFieldInMetamodelFor(Option.class, "period", "Embedded expected");
assertPresenceOfFieldInMetamodelFor(Period.class, "start", "Embedded expected");
assertPresenceOfFieldInMetamodelFor(Period.class, "end", "Embedded expected");
}
@Override
protected String getPackageNameOfCurrentTest() {
return XmlOnlyTest.class.getPackage().getName();

View File

@ -63,5 +63,21 @@
</attributes>
</entity>
<entity class="Option" access="FIELD">
<attributes>
<id name="id"/>
<embedded name="period" />
</attributes>
</entity>
<embeddable class="Period" access="FIELD">
<attributes>
<basic name="start">
<temporal>DATE</temporal>
</basic>
<basic name="end">
<temporal>DATE</temporal>
</basic>
</attributes>
</embeddable>
</entity-mappings>