Skip to content

Commit

Permalink
#601 issue fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszwalacik committed Nov 23, 2017
1 parent 6f68669 commit 6bce1ad
Show file tree
Hide file tree
Showing 21 changed files with 237 additions and 149 deletions.
Expand Up @@ -6,7 +6,7 @@
* @author Pawel Cierpiatka <pawel.cierpiatka@gmail.com>
*/
public enum JaversExceptionCode {
CLASS_EXTRACTION_ERROR(JaversException.BOOTSTRAP_ERROR + "Don't know how to extract Class from type '%s'.") ,
CLASS_EXTRACTION_ERROR(JaversException.BOOTSTRAP_ERROR + "Can't extract Class from Type '%s'.") ,

COMMITTING_TOP_LEVEL_VALUES_NOT_SUPPORTED("Committing top-level %ss like '%s' is not supported. You can commit only Entity or ValueObject instance."),

Expand Down Expand Up @@ -62,6 +62,8 @@ public enum JaversExceptionCode {

MANAGED_CLASS_MAPPING_ERROR("given javaClass '%s' is mapped to %s, expected %s"),

CLASS_MAPPING_ERROR("given javaClass '%s' is mapped to %s, expected %s"),

MALFORMED_CHANGE_TYPE_FIELD("no such Change type - '%s'"),

MALFORMED_ENTRY_CHANGE_TYPE_FIELD("no such EntryChange type - '%s'"),
Expand Down
Expand Up @@ -31,8 +31,12 @@ private void println(String text) {
out.append(text + "\n");
}

private void print(String text) {
out.append(text);
}

public String build() {
println("}");
print("}");
return out.toString();
}
}
Expand Up @@ -20,7 +20,7 @@ public CustomToNativeAppenderAdapter(CustomPropertyComparator<T, C> delegate, Cl

@Override
public boolean supports(JaversType propertyType) {
return propertyType.getBaseJavaClass().equals(propertyJavaClass);
return propertyType.getBaseJavaType().equals(propertyJavaClass);
}

@Override
Expand Down
Expand Up @@ -10,22 +10,31 @@
* @author bartosz walacik
*/
public class ValueObjectDefinition extends ClientsClassDefinition {
private final boolean defaultType;

/**
* Simple recipe for ValueObject
*/
public ValueObjectDefinition(Class<?> valueObject) {
super(valueObject);
this.defaultType = false;
}

/**
* Recipe for ValueObject with ignoredProperties
*/
public ValueObjectDefinition(Class<?> valueObject, List<String> ignoredProperties) {
super(valueObject, ignoredProperties);
this.defaultType = false;
}

ValueObjectDefinition(ClientsClassDefinitionBuilder builder) {
ValueObjectDefinition(ValueObjectDefinitionBuilder builder) {
super(builder);
this.defaultType = builder.isDefault();

}

public boolean isDefault() {
return defaultType;
}
}
Expand Up @@ -14,7 +14,9 @@
* @since 1.4
* @author bartosz.walacik
*/
public class ValueObjectDefinitionBuilder extends ClientsClassDefinitionBuilder<ValueObjectDefinitionBuilder>{
public class ValueObjectDefinitionBuilder extends ClientsClassDefinitionBuilder<ValueObjectDefinitionBuilder> {
private boolean defaultType;

private ValueObjectDefinitionBuilder(Class valueObject) {
super(valueObject);
}
Expand All @@ -23,8 +25,17 @@ public static ValueObjectDefinitionBuilder valueObjectDefinition(Class<?> valueO
return new ValueObjectDefinitionBuilder(valueObject);
}

public ValueObjectDefinitionBuilder defaultType() {
this.defaultType = true;
return this;
}

@Override
public ValueObjectDefinition build() {
return new ValueObjectDefinition(this);
}

public boolean isDefault() {
return defaultType;
}
}
@@ -0,0 +1,57 @@
package org.javers.core.metamodel.type;

import org.javers.common.reflection.ReflectionUtil;
import org.javers.common.validation.Validate;
import java.lang.reflect.Type;
import java.util.Optional;

import static org.javers.common.validation.Validate.argumentIsNotNull;

/**
* Class or ParametrizedType
*/
abstract class ClassType extends JaversType {

private final Class baseJavaClass;

ClassType(Type baseJavaType) {
this(baseJavaType, Optional.empty());
}

ClassType(Type baseJavaType, Optional<String> name) {
this(baseJavaType, name, 0);
}

ClassType(Type baseJavaType, Optional<String> name, int expectedArgs) {
super(baseJavaType, name, expectedArgs);
Validate.argumentIsNotNull(name);
this.baseJavaClass = ReflectionUtil.extractClass(baseJavaType);
}

@Override
public boolean canBePrototype() {
return true;
}

@Override
public boolean isInstance(Object cdo) {
argumentIsNotNull(cdo);
return baseJavaClass.isAssignableFrom(cdo.getClass());
}

/**
* Type for JSON representation.
*
* For Values it's simply baseJavaType.
*
* For ManagedTypes (references to Entities and ValueObjects) it's GlobalId
* because JaVers serializes references in the 'dehydrated' form.
*/
protected Type getRawDehydratedType() {
return baseJavaClass;
}

public Class getBaseJavaClass() {
return baseJavaClass;
}
}
Expand Up @@ -22,7 +22,7 @@
*
* @author bartosz walacik
*/
public class CustomType extends JaversType {
public class CustomType extends ClassType {
public CustomType(Type baseJavaType) {
super(baseJavaType);
}
Expand Down
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

/**
* Type for JSON representation. Generic version of {@link JaversType#getRawDehydratedType()}
* Type for JSON representation. Generic version of {@link ClassType#getRawDehydratedType()}
*
* @author bartosz.walacik
*/
Expand All @@ -22,13 +22,12 @@ class DehydratedTypeFactory {

//recursive
public Type build(Type givenType){
final JaversType javersType = mapper.getJaversType(givenType);
final ClassType javersType = mapper.getJaversClassType(givenType);

//for Generics, we have list of type arguments to dehydrate
if (javersType.isGenericType()) {
Type rawType = javersType.getBaseJavaClass();
List<Type> actualDehydratedTypeArguments = extractAndDehydrateTypeArguments(javersType);
return new ParametrizedDehydratedType(rawType, actualDehydratedTypeArguments);
return new ParametrizedDehydratedType(javersType.getBaseJavaClass(), actualDehydratedTypeArguments);
}

if (javersType instanceof ArrayType){
Expand Down
Expand Up @@ -47,7 +47,9 @@ public class EntityType extends ManagedType {

@Override
EntityType spawn(ManagedClass managedClass, Optional<String> typeName) {
return new EntityType(managedClass, idProperty, typeName);
//when spawning from prototype, prototype.idProperty and child.idProperty are different objects
//with (possibly) different return types, so we need to update Id pointer
return new EntityType(managedClass, managedClass.getProperty(idProperty.getName()), typeName);
}

public Type getIdPropertyGenericType() {
Expand Down Expand Up @@ -79,7 +81,7 @@ public JaversProperty getIdProperty() {
public Object getIdOf(Object instance) {
Validate.argumentIsNotNull(instance);

if (!getBaseJavaClass().isInstance(instance)) {
if (!isInstance(instance)) {
throw new JaversException(JaversExceptionCode.NOT_INSTANCE_OF, getName(), instance.getClass().getName());
}

Expand Down
Expand Up @@ -11,10 +11,10 @@
* Collection or Array or Map
* @author bartosz walacik
*/
public abstract class EnumerableType extends JaversType {
public abstract class EnumerableType extends ClassType {

public EnumerableType(Type baseJavaType, int expectedArgs) {
super(baseJavaType, Optional.<String>empty(), expectedArgs);
EnumerableType(Type baseJavaType, int expectedArgs) {
super(baseJavaType, Optional.empty(), expectedArgs);
}

/**
Expand Down
Expand Up @@ -7,13 +7,9 @@
*
* @author bartosz.walacik
*/
public class IgnoredType extends JaversType {
public class IgnoredType extends ClassType {

public IgnoredType(Type baseJavaType) {
super(baseJavaType);
}

IgnoredType(Class<?> baseJavaClass) {
super(baseJavaClass);
}
}

0 comments on commit 6bce1ad

Please sign in to comment.