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:
parent
5bdd8c3311
commit
e0e6fb0970
|
@ -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,21 +196,21 @@ public class XmlMetaEntity implements MetaEntity {
|
|||
continue;
|
||||
}
|
||||
|
||||
if ( ElementKind.METHOD.equals(elem.getKind())) {
|
||||
if (!propertyName.equals(determinePropertyName(elem))) {
|
||||
continue;
|
||||
String elementPropertyName = elem.getSimpleName().toString();
|
||||
if ( elem.getKind().equals( ElementKind.METHOD ) ) {
|
||||
elementPropertyName = StringUtil.getPropertyName( elementPropertyName );
|
||||
}
|
||||
}
|
||||
else if(!elem.getSimpleName().toString().equals( propertyName )) {
|
||||
|
||||
if ( !propertyName.equals( elementPropertyName ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DeclaredType type = determineDeclaredType( elem );
|
||||
if (type != null) {
|
||||
String types[] = new String[3];
|
||||
determineTypes(propertyName, explicitTargetEntity, explicitMapKeyClass, type, types);
|
||||
return types;
|
||||
if ( type == null ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return determineTypes( propertyName, explicitTargetEntity, explicitMapKeyClass, type );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -222,25 +223,20 @@ public class XmlMetaEntity implements MetaEntity {
|
|||
else if ( elem.asType() instanceof ExecutableType ) {
|
||||
ExecutableType executableType = (ExecutableType) elem.asType();
|
||||
if ( executableType.getReturnType() instanceof DeclaredType ) {
|
||||
DeclaredType declaredType = (DeclaredType) executableType.getReturnType();
|
||||
type = 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 void determineTypes(String propertyName, String explicitTargetEntity, String explicitMapKeyClass, DeclaredType type, String types[]) {
|
||||
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 determineMapType(DeclaredType type, String explicitMapKeyClass, String[] types) {
|
||||
|
@ -388,6 +384,10 @@ public class XmlMetaEntity implements MetaEntity {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (org.hibernate.jpamodelgen.xml.jaxb.Embedded embedded : attributes.getEmbedded()) {
|
||||
parseEmbedded( embedded);
|
||||
}
|
||||
}
|
||||
|
||||
private void parseEmbeddableAttributes(EmbeddableAttributes attributes) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
package org.hibernate.jpamodelgen.test.xmlonly;
|
||||
|
||||
public class Option {
|
||||
private Long id;
|
||||
private Period period;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.hibernate.jpamodelgen.test.xmlonly;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Period {
|
||||
private Date start;
|
||||
private Date end;
|
||||
}
|
|
@ -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();
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue