Remove the support for HibernateOrmConfiguration annotation when collecting config properties

This commit is contained in:
marko-bekhta 2023-01-25 15:27:50 +01:00 committed by Steve Ebersole
parent d770ff1597
commit df1ebfe32a
5 changed files with 18 additions and 211 deletions

View File

@ -1,62 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.properties.processor;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
public final class AnnotationUtils {
private AnnotationUtils() {
}
public static boolean isIgnored(Element element) {
return findAnnotation( element, HibernateOrmConfiguration.class )
.flatMap( a -> a.attribute( "ignore", Boolean.class ) )
.orElse( false );
}
public static Optional<AnnotationAttributeHolder> findAnnotation(Element element, Class<?> annotation) {
for ( AnnotationMirror mirror : element.getAnnotationMirrors() ) {
if ( mirror.getAnnotationType().toString().equals( annotation.getName() ) ) {
return Optional.of( new AnnotationAttributeHolder( mirror ) );
}
}
return Optional.empty();
}
public static class AnnotationAttributeHolder {
private final AnnotationMirror annotationMirror;
private AnnotationAttributeHolder(AnnotationMirror annotationMirror) {
this.annotationMirror = annotationMirror;
}
public <T> Optional<T> attribute(String name, Class<T> klass) {
return annotationMirror.getElementValues().entrySet().stream()
.filter( entry -> entry.getKey().getSimpleName().contentEquals( name ) )
.map( entry -> klass.cast( entry.getValue().getValue() ) )
.findAny();
}
public <T> Optional<List<T>> multiAttribute(String name, Class<T> klass) {
return annotationMirror.getElementValues().entrySet().stream()
.filter( entry -> entry.getKey().getSimpleName().contentEquals( name ) )
.map( entry -> entry.getValue().getValue() )
.map( obj -> ( (List<?>) List.class.cast( obj ) ) )
.map( list -> list.stream().map( AnnotationValue.class::cast ).map( AnnotationValue::getValue )
.map( klass::cast ).collect( Collectors.toList() ) )
.findAny();
}
}
}

View File

@ -6,13 +6,11 @@
*/ */
package org.hibernate.orm.properties.processor; package org.hibernate.orm.properties.processor;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class ConfigurationProperty implements Comparable<ConfigurationProperty> { public class ConfigurationProperty implements Comparable<ConfigurationProperty> {
@ -107,21 +105,10 @@ public class ConfigurationProperty implements Comparable<ConfigurationProperty>
} }
public static class Key { public static class Key {
private final List<String> prefixes;
private final String key; private final String key;
public Key(List<String> prefixes, String key) { public Key(String key) {
this.key = key; this.key = key;
this.prefixes = prefixes;
}
public void overridePrefixes(String... prefixes) {
overridePrefixes( Arrays.asList( prefixes ) );
}
public void overridePrefixes(List<String> prefixes) {
this.prefixes.clear();
this.prefixes.addAll( prefixes );
} }
public boolean matches(Pattern pattern) { public boolean matches(Pattern pattern) {
@ -129,30 +116,12 @@ public class ConfigurationProperty implements Comparable<ConfigurationProperty>
} }
public List<String> resolvedKeys() { public List<String> resolvedKeys() {
if ( prefixes.isEmpty() ) { return Collections.singletonList( key );
return Collections.singletonList( key );
}
else {
return prefixes.stream()
.map( p -> p + key )
.collect( Collectors.toList() );
}
} }
@Override @Override
public String toString() { public String toString() {
return toString( "/" ); return key;
}
private String toString(String delimiter) {
if ( prefixes.isEmpty() ) {
return key;
}
else {
return prefixes.stream()
.map( p -> p + key )
.collect( Collectors.joining( delimiter ) );
}
} }
} }
} }

View File

@ -6,16 +6,11 @@
*/ */
package org.hibernate.orm.properties.processor; package org.hibernate.orm.properties.processor;
import static org.hibernate.orm.properties.processor.AnnotationUtils.findAnnotation;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.annotation.processing.Messager; import javax.annotation.processing.Messager;
@ -63,38 +58,16 @@ public class ConfigurationPropertyCollector {
if ( !processedTypes.contains( qualifiedName ) ) { if ( !processedTypes.contains( qualifiedName ) ) {
processedTypes.add( qualifiedName ); processedTypes.add( qualifiedName );
Optional<AnnotationUtils.AnnotationAttributeHolder> annotation = findAnnotation(
element, HibernateOrmConfiguration.class );
Optional<List<String>> classPrefix = annotation
.flatMap( a -> a.multiAttribute( "prefix", String.class ) );
Optional<String> title = annotation.flatMap( a -> a.attribute( "title", String.class ) );
Optional<String> anchorPrefix = annotation.flatMap( a -> a.attribute( "anchorPrefix", String.class ) );
for ( Element inner : elementUtils.getAllMembers( element ) ) { for ( Element inner : elementUtils.getAllMembers( element ) ) {
if ( inner.getKind().equals( ElementKind.FIELD ) && inner instanceof VariableElement ) { if ( inner.getKind().equals( ElementKind.FIELD ) && inner instanceof VariableElement ) {
processConstant( ( (VariableElement) inner ), classPrefix, title, anchorPrefix ); processConstant( ( (VariableElement) inner ) );
} }
} }
} }
} }
private void processConstant(VariableElement constant, Optional<List<String>> classPrefix, private void processConstant(VariableElement constant) {
Optional<String> classTitle, ConfigurationProperty.Key key = extractKey( constant );
Optional<String> classAnchorPrefix) {
Optional<AnnotationUtils.AnnotationAttributeHolder> annotation = findAnnotation(
constant, HibernateOrmConfiguration.class );
if ( annotation.flatMap( a -> a.attribute( "ignore", Boolean.class ) ).orElse( false ) ) {
return;
}
Optional<String> title = annotation.flatMap( a -> a.attribute( "title", String.class ) );
Optional<String> anchorPrefix = annotation.flatMap( a -> a.attribute( "anchorPrefix", String.class ) );
ConfigurationProperty.Key key = extractKey(
constant,
classPrefix,
annotation.flatMap( a -> a.multiAttribute( "prefix", String.class ) )
);
if ( !key.matches( ignoreKeys ) ) { if ( !key.matches( ignoreKeys ) ) {
properties.put( properties.put(
constant.getEnclosingElement().toString() + "#" + constant.getSimpleName().toString(), constant.getEnclosingElement().toString() + "#" + constant.getSimpleName().toString(),
@ -102,28 +75,15 @@ public class ConfigurationPropertyCollector {
.javadoc( extractJavadoc( constant ) ) .javadoc( extractJavadoc( constant ) )
.key( key ) .key( key )
.sourceClass( constant.getEnclosingElement().toString() ) .sourceClass( constant.getEnclosingElement().toString() )
.withModuleName( title.orElse( classTitle.orElse( this.title ) ) ) .withModuleName( this.title )
.withAnchorPrefix( anchorPrefix.orElse( classAnchorPrefix.orElse( this.anchor ) ) ) .withAnchorPrefix( this.anchor )
); );
} }
} }
private ConfigurationProperty.Key extractKey(VariableElement constant, Optional<List<String>> classPrefix, private ConfigurationProperty.Key extractKey(VariableElement constant) {
Optional<List<String>> constantPrefix) {
List<String> prefix;
if ( constantPrefix.isPresent() ) {
prefix = constantPrefix.get();
}
else if ( classPrefix.isPresent() ) {
prefix = classPrefix.get();
}
else {
prefix = Collections.emptyList();
}
return new ConfigurationProperty.Key( return new ConfigurationProperty.Key(
prefix,
Objects.toString( constant.getConstantValue(), "NOT_FOUND#" + constant.getSimpleName() ) Objects.toString( constant.getConstantValue(), "NOT_FOUND#" + constant.getSimpleName() )
); );
} }
@ -135,11 +95,13 @@ public class ConfigurationPropertyCollector {
enclosingClass.toString().replace( ".", File.separator ) + ".html" enclosingClass.toString().replace( ".", File.separator ) + ".html"
); );
String packagePath = packageElement( enclosingClass ).getQualifiedName().toString().replace( ".", File.separator ); String packagePath = packageElement( enclosingClass ).getQualifiedName().toString().replace(
".", File.separator );
Document javadoc = Jsoup.parse( docs.toFile() ); Document javadoc = Jsoup.parse( docs.toFile() );
org.jsoup.nodes.Element block = javadoc.selectFirst( "#" + constant.getSimpleName() + " + ul li.blockList"); org.jsoup.nodes.Element block = javadoc.selectFirst(
"#" + constant.getSimpleName() + " + ul li.blockList" );
if ( block != null ) { if ( block != null ) {
for ( org.jsoup.nodes.Element link : block.getElementsByTag( "a" ) ) { for ( org.jsoup.nodes.Element link : block.getElementsByTag( "a" ) ) {
String href = link.attr( "href" ); String href = link.attr( "href" );
@ -173,7 +135,10 @@ public class ConfigurationPropertyCollector {
} }
} }
catch (IOException e) { catch (IOException e) {
messager.printMessage( Diagnostic.Kind.NOTE, "Wasn't able to find rendered javadocs for " + constant + ". Trying to read plain javadoc comment." ); messager.printMessage(
Diagnostic.Kind.NOTE,
"Wasn't able to find rendered javadocs for " + constant + ". Trying to read plain javadoc comment."
);
return elementUtils.getDocComment( constant ); return elementUtils.getDocComment( constant );
} }
} }

View File

@ -7,8 +7,6 @@
package org.hibernate.orm.properties.processor; package org.hibernate.orm.properties.processor;
import static org.hibernate.orm.properties.processor.AnnotationUtils.isIgnored;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@ -83,19 +81,6 @@ public class ConfigurationPropertyProcessor extends AbstractProcessor {
} }
} }
// means we might have some inner classes that we also wanted to consider for config property processing
// so let's see if we need to process any:
for ( TypeElement annotation : annotations ) {
if ( annotation.getQualifiedName().contentEquals( HibernateOrmConfiguration.class.getName() ) ) {
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith( annotation );
for ( Element element : elements ) {
if ( isTypeElement( element ) ) {
process( propertyCollector, element );
}
}
}
}
if ( roundEnv.processingOver() ) { if ( roundEnv.processingOver() ) {
beforeExit(); beforeExit();
} }
@ -108,8 +93,7 @@ public class ConfigurationPropertyProcessor extends AbstractProcessor {
} }
private void process(ConfigurationPropertyCollector propertyCollector, Element element) { private void process(ConfigurationPropertyCollector propertyCollector, Element element) {
if ( !isIgnored( element ) && !ignore.map( p -> p.matcher( element.toString() ).matches() ).orElse( if ( !ignore.map( p -> p.matcher( element.toString() ).matches() ).orElse( Boolean.FALSE ) ) {
Boolean.FALSE ) ) {
propertyCollector.visitType( (TypeElement) element ); propertyCollector.visitType( (TypeElement) element );
} }
} }
@ -118,9 +102,4 @@ public class ConfigurationPropertyProcessor extends AbstractProcessor {
return ( element.getKind().equals( ElementKind.CLASS ) || element.getKind().equals( ElementKind.INTERFACE ) ) && return ( element.getKind().equals( ElementKind.CLASS ) || element.getKind().equals( ElementKind.INTERFACE ) ) &&
element.getSimpleName().toString().endsWith( "Settings" ); element.getSimpleName().toString().endsWith( "Settings" );
} }
private boolean isTypeElement(Element element) {
return element instanceof TypeElement;
}
} }

View File

@ -1,44 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.properties.processor;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Marker annotation to define overrides configured by annotation processor configuration.
*/
@Retention(RetentionPolicy.SOURCE)
@Target({ ElementType.TYPE, ElementType.FIELD })
public @interface HibernateOrmConfiguration {
/**
* Set to {@code true} in case we have a {@code *Settings} class that we want to ignore in config processing.
* Also works on a field leve. Setting it to {@code true} on field level will not include that particular constant.
* Can be useful to skip prefix definitions etc.
*/
boolean ignore() default false;
/**
* Overrides a prefix provided by annotation processor configuration. If set on class level - all constants from that class will
* use this prefix. If set on field level - that particular constant will use the configured prefix and will ignore the
* one set by annotation processor configuration or at class level.
*/
String[] prefix() default "";
/**
* Used to group properties in sections and as a title of that grouped section.
*/
String title() default "";
/**
* Used as part of generated anchor links to provide uniqueness.
*/
String anchorPrefix() default "";
}