Skip to content

Commit

Permalink
#74 - Add the ability to display a ClassDetails (including its Member…
Browse files Browse the repository at this point in the history
…Details) with its AnnotationUsages
  • Loading branch information
sebersole committed Apr 26, 2024
1 parent b158650 commit e32410b
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.AttributeDescriptor;
import org.hibernate.models.spi.RenderingCollector;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.ValueTypeDescriptor;
import org.hibernate.models.spi.ValueWrapper;
Expand Down Expand Up @@ -42,6 +43,16 @@ public V createValue(
return valueWrapper.wrap( defaultValue, context );
}

@Override
public void render(RenderingCollector collector, String name, Object attributeValue) {
collector.addLine( "%s = %s", name, attributeValue );
}

@Override
public void render(RenderingCollector collector, Object attributeValue) {
collector.addLine( "%s = %s", attributeValue );
}

@Override
public String toString() {
return String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.Collections;
import java.util.List;

import org.hibernate.models.internal.jandex.ArrayValueExtractor;
import org.hibernate.models.internal.jandex.ArrayValueWrapper;
import org.hibernate.models.internal.util.CollectionHelper;
import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.AttributeDescriptor;
import org.hibernate.models.spi.RenderingCollector;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.ValueExtractor;
import org.hibernate.models.spi.ValueTypeDescriptor;
Expand Down Expand Up @@ -132,4 +131,28 @@ public Object unwrap(List<V> value) {
}
return result;
}

@Override
public void render(RenderingCollector collector, String name, Object attributeValue) {
//noinspection unchecked
final List<V> values = (List<V>) attributeValue;

collector.addLine( "%s = {", name );
collector.indent( 2 );
values.forEach( (value) -> elementTypeDescriptor.render( collector, value ) );
collector.unindent( 2 );
collector.addLine( "}" );
}

@Override
public void render(RenderingCollector collector, Object attributeValue) {
//noinspection unchecked
final List<V> values = (List<V>) attributeValue;

collector.addLine( "{" );
collector.indent( 2 );
values.forEach( (value) -> elementTypeDescriptor.render( collector, value ) );
collector.unindent( 2 );
collector.addLine( "}" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.hibernate.models.internal.jandex.ClassValueExtractor;
import org.hibernate.models.internal.jandex.ClassValueWrapper;
import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.RenderingCollector;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.ValueExtractor;
import org.hibernate.models.spi.ValueWrapper;
Expand Down Expand Up @@ -55,4 +56,14 @@ public ValueExtractor<Annotation, ClassDetails> createJdkExtractor(SourceModelBu
public Object unwrap(ClassDetails value) {
return value.toJavaClass();
}

@Override
public void render(RenderingCollector collector, String name, Object attributeValue) {
super.render( collector, name, ( (ClassDetails) attributeValue ).getName() );
}

@Override
public void render(RenderingCollector collector, Object attributeValue) {
super.render( collector, ( (ClassDetails) attributeValue ).getName() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.hibernate.models.internal.jdk.AnnotationDescriptorImpl;
import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.AnnotationUsage;
import org.hibernate.models.spi.RenderingCollector;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.ValueExtractor;
import org.hibernate.models.spi.ValueWrapper;
Expand Down Expand Up @@ -109,6 +110,20 @@ public ValueExtractor<Annotation, AnnotationUsage<A>> resolveJdkExtractor(Source
return jdkExtractor;
}

@Override
public void render(RenderingCollector collector, String name, Object attributeValue) {
//noinspection unchecked
final AnnotationUsage<A> nested = (AnnotationUsage<A>) attributeValue;
nested.renderAttributeValue( name, collector );
}

@Override
public void render(RenderingCollector collector, Object attributeValue) {
//noinspection unchecked
final AnnotationUsage<A> nested = (AnnotationUsage<A>) attributeValue;
nested.render( collector );
}

@Override
public String toString() {
return String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.hibernate.models.internal.jandex.StringValueExtractor;
import org.hibernate.models.internal.jandex.StringValueWrapper;
import org.hibernate.models.internal.jdk.PassThruExtractor;
import org.hibernate.models.spi.RenderingCollector;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.ValueExtractor;
import org.hibernate.models.spi.ValueWrapper;
Expand Down Expand Up @@ -59,4 +60,14 @@ public ValueExtractor<Annotation, String> createJdkExtractor(SourceModelBuilding
public Object unwrap(String value) {
return value;
}

@Override
public void render(RenderingCollector collector, String name, Object attributeValue) {
super.render( collector, name, "\"" + attributeValue + "\"" );
}

@Override
public void render(RenderingCollector collector, Object attributeValue) {
super.render( collector, attributeValue );
}
}
19 changes: 19 additions & 0 deletions src/main/java/org/hibernate/models/spi/AnnotationUsage.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,23 @@ default void render(RenderingCollector collector) {
collector.addLine( ")" );
}
}

default void renderAttributeValue(String name, RenderingCollector collector) {
final List<AttributeDescriptor<?>> attributes = getAnnotationDescriptor().getAttributes();
if ( attributes.isEmpty() ) {
collector.addLine( "%s = @%s", name, getAnnotationType().getName() );
}
else {
collector.addLine( "%s = @%s(", name, getAnnotationType().getName() );
collector.indent( 2 );
attributes.forEach( (attribute) -> attribute.getTypeDescriptor().render(
collector,
attribute.getName(),
getAttributeValue( attribute.getName() )
) );

collector.unindent( 2 );
collector.addLine( ")" );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ public interface ValueTypeDescriptor<V> {
default void render(RenderingCollector collector, String name, Object attributeValue) {
collector.addLine( "%s=%s", name, "..." );
}

void render(RenderingCollector collector, Object attributeValue);
}

0 comments on commit e32410b

Please sign in to comment.