Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for serializing a model context #104

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.hibernate.models.jandex.internal;

import java.lang.annotation.Annotation;
import java.util.Map;

import org.hibernate.models.internal.AnnotationDescriptorRegistryStandard;
import org.hibernate.models.spi.AnnotationDescriptor;
Expand All @@ -24,4 +25,8 @@ protected <A extends Annotation> AnnotationDescriptor<A> buildAnnotationDescript
AnnotationDescriptor<? extends Annotation> containerDescriptor) {
return new JandexAnnotationDescriptorImpl<>( javaType, containerDescriptor, getModelBuildingContext() );
}

public Map<Class<? extends Annotation>, AnnotationDescriptor<? extends Annotation>> getDescriptorMap() {
return descriptorMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import java.util.List;

import org.hibernate.models.internal.ClassDetailsSupport;
import org.hibernate.models.internal.jdk.SerialJdkClassDetails;
import org.hibernate.models.internal.util.CollectionHelper;
import org.hibernate.models.serial.spi.SerialClassDetails;
import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.FieldDetails;
import org.hibernate.models.spi.MethodDetails;
Expand Down Expand Up @@ -276,4 +278,9 @@ private static List<TypeVariableDetails> determineTypeParameters(ClassInfo class
}
return result;
}

@Override
public SerialClassDetails toStorableForm() {
return new SerialJdkClassDetails( getName(), toJavaClass() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package org.hibernate.models.jandex.internal;

import java.util.Map;

import org.hibernate.models.UnknownClassException;
import org.hibernate.models.internal.AbstractClassDetailsRegistry;
import org.hibernate.models.internal.jdk.JdkBuilders;
Expand Down Expand Up @@ -50,4 +52,8 @@ protected ClassDetails createClassDetails(String name) {

throw new UnknownClassException( "Unable to resolve ClassDetails for `" + name + "`" );
}

protected Map<String, ClassDetails> getClassDetailsMap() {
return classDetailsMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.hibernate.models.jandex.spi.JandexModelBuildingContext;
import org.hibernate.models.jandex.spi.JandexValueConverter;
import org.hibernate.models.jandex.spi.JandexValueExtractor;
import org.hibernate.models.serial.internal.StorableContextImpl;
import org.hibernate.models.serial.spi.StorableContext;
import org.hibernate.models.spi.ClassLoading;
import org.hibernate.models.spi.RegistryPrimer;
import org.hibernate.models.spi.ValueTypeDescriptor;
Expand All @@ -22,6 +24,8 @@
import static org.hibernate.models.internal.ModelsClassLogging.MODELS_CLASS_LOGGER;

/**
* SourceModelBuildingContext implementation based on Jandex
*
* @author Steve Ebersole
*/
public class JandexModelBuildingContextImpl extends AbstractModelBuildingContext implements JandexModelBuildingContext {
Expand Down Expand Up @@ -96,4 +100,9 @@ public <V> JandexValueExtractor<V> getJandexValueExtractor(ValueTypeDescriptor<V
this
);
}

@Override
public StorableContext toStorableForm() {
return new StorableContextImpl( classDetailsRegistry.getClassDetailsMap(), descriptorRegistry.getDescriptorMap() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.models.serialization;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.hibernate.models.SerializationHelper;
import org.hibernate.models.SourceModelTestHelper;
import org.hibernate.models.serial.spi.StorableContext;
import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.SourceModelContext;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.models.internal.SimpleClassLoading.SIMPLE_CLASS_LOADING;

public class SimpleSerializationTests {
protected SourceModelBuildingContext createModelContext(Class<?>... classes) {
return SourceModelTestHelper.createBuildingContext( classes );
}

@Test
void serializeSimpleClass() {
final SourceModelBuildingContext buildingContext = createModelContext( SimpleClass.class );

final ClassDetails classDetails = buildingContext.getClassDetailsRegistry().findClassDetails( SimpleClass.class.getName() );
assertThat( classDetails ).isNotNull();

final StorableContext serialContext = buildingContext.toStorableForm();
final StorableContext clonedSerialContext = SerializationHelper.clone( serialContext );
assertThat( serialContext ).isNotSameAs( clonedSerialContext );

final SourceModelContext restored = clonedSerialContext.fromStorableForm( SIMPLE_CLASS_LOADING );
assertThat( buildingContext ).isNotSameAs( restored );
assertThat( buildingContext.getClassDetailsRegistry() ).isNotSameAs( restored.getClassDetailsRegistry() );
assertThat( buildingContext.getAnnotationDescriptorRegistry() ).isNotSameAs( restored.getAnnotationDescriptorRegistry() );

final ClassDetails cloneCassDetails = restored.getClassDetailsRegistry().findClassDetails( SimpleClass.class.getName() );
assertThat( cloneCassDetails ).isNotNull();
assertThat( classDetails ).isNotSameAs( cloneCassDetails );
}

@Test
void serializeSimpleClassWithMembers() {
final SourceModelBuildingContext buildingContext = createModelContext( SimpleClassWithMembers.class );

final ClassDetails classDetails = buildingContext.getClassDetailsRegistry().findClassDetails( SimpleClassWithMembers.class.getName() );
assertThat( classDetails ).isNotNull();
assertThat( classDetails.getFields() ).hasSize( 1 );
assertThat( classDetails.getMethods() ).hasSize( 3 );

final StorableContext serialContext = buildingContext.toStorableForm();
final StorableContext clonedSerialContext = SerializationHelper.clone( serialContext );
assertThat( serialContext ).isNotSameAs( clonedSerialContext );

final SourceModelContext restored = clonedSerialContext.fromStorableForm( SIMPLE_CLASS_LOADING );
assertThat( buildingContext ).isNotSameAs( restored );
assertThat( buildingContext.getClassDetailsRegistry() ).isNotSameAs( restored.getClassDetailsRegistry() );
assertThat( buildingContext.getAnnotationDescriptorRegistry() ).isNotSameAs( restored.getAnnotationDescriptorRegistry() );

final ClassDetails cloneCassDetails = restored.getClassDetailsRegistry().findClassDetails( SimpleClassWithMembers.class.getName() );
assertThat( cloneCassDetails ).isNotNull();
assertThat( classDetails ).isNotSameAs( cloneCassDetails );
assertThat( cloneCassDetails.getFields() ).hasSize( 1 );
assertThat( cloneCassDetails.getMethods() ).hasSize( 3 );
}

@Test
void serializeSimpleClassWithAnnotations() {
final SourceModelBuildingContext buildingContext = createModelContext( SimpleClassWithAnnotations.class );

final ClassDetails classDetails = buildingContext.getClassDetailsRegistry().findClassDetails( SimpleClassWithAnnotations.class.getName() );
assertThat( classDetails ).isNotNull();
assertThat( classDetails.getDirectAnnotationUsages() ).hasSize( 1 );
assertThat( classDetails.getFields() ).hasSize( 1 );
assertThat( classDetails.getFields().iterator().next().getDirectAnnotationUsages() ).hasSize( 1 );
assertThat( classDetails.getMethods() ).hasSize( 1 );
assertThat( classDetails.getMethods().iterator().next().getDirectAnnotationUsages() ).hasSize( 1 );

final StorableContext serialContext = buildingContext.toStorableForm();
final StorableContext clonedSerialContext = SerializationHelper.clone( serialContext );
assertThat( serialContext ).isNotSameAs( clonedSerialContext );

final SourceModelContext restored = clonedSerialContext.fromStorableForm( SIMPLE_CLASS_LOADING );
assertThat( restored ).isNotNull();
assertThat( buildingContext ).isNotSameAs( restored );
assertThat( buildingContext.getClassDetailsRegistry() ).isNotSameAs( restored.getClassDetailsRegistry() );
assertThat( buildingContext.getAnnotationDescriptorRegistry() ).isNotSameAs( restored.getAnnotationDescriptorRegistry() );

final ClassDetails cloneCassDetails = restored.getClassDetailsRegistry().findClassDetails( SimpleClassWithAnnotations.class.getName() );
assertThat( classDetails ).isNotSameAs( cloneCassDetails );
assertThat( cloneCassDetails.getDirectAnnotationUsages() ).hasSize( 1 );
assertThat( cloneCassDetails.getFields() ).hasSize( 1 );
assertThat( cloneCassDetails.getFields().iterator().next().getDirectAnnotationUsages() ).hasSize( 1 );
assertThat( cloneCassDetails.getMethods() ).hasSize( 1 );
assertThat( cloneCassDetails.getMethods().iterator().next().getDirectAnnotationUsages() ).hasSize( 1 );

}

public static class SimpleClass {
}

public static class SimpleClassWithMembers {
public int anInt;

public int getAnInt() {
return anInt;
}

public void setAnInt(int anInt) {
this.anInt = anInt;
}

public void doStuff() {
}
}

@Target({ ElementType.FIELD,ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AnAnnotation {
}

@AnAnnotation
public static class SimpleClassWithAnnotations {
@AnAnnotation
private int anInt;

@AnAnnotation
public int getAnInt() {
return anInt;
}

}
}
Loading
Loading