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;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class ConfigurationProperty implements Comparable<ConfigurationProperty> {
@ -107,21 +105,10 @@ public class ConfigurationProperty implements Comparable<ConfigurationProperty>
}
public static class Key {
private final List<String> prefixes;
private final String key;
public Key(List<String> prefixes, String key) {
public Key(String 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) {
@ -129,30 +116,12 @@ public class ConfigurationProperty implements Comparable<ConfigurationProperty>
}
public List<String> resolvedKeys() {
if ( prefixes.isEmpty() ) {
return Collections.singletonList( key );
}
else {
return prefixes.stream()
.map( p -> p + key )
.collect( Collectors.toList() );
}
return Collections.singletonList( key );
}
@Override
public String toString() {
return toString( "/" );
}
private String toString(String delimiter) {
if ( prefixes.isEmpty() ) {
return key;
}
else {
return prefixes.stream()
.map( p -> p + key )
.collect( Collectors.joining( delimiter ) );
}
return key;
}
}
}

View File

@ -6,16 +6,11 @@
*/
package org.hibernate.orm.properties.processor;
import static org.hibernate.orm.properties.processor.AnnotationUtils.findAnnotation;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.processing.Messager;
@ -63,38 +58,16 @@ public class ConfigurationPropertyCollector {
if ( !processedTypes.contains( 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 ) ) {
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,
Optional<String> classTitle,
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 ) )
);
private void processConstant(VariableElement constant) {
ConfigurationProperty.Key key = extractKey( constant );
if ( !key.matches( ignoreKeys ) ) {
properties.put(
constant.getEnclosingElement().toString() + "#" + constant.getSimpleName().toString(),
@ -102,28 +75,15 @@ public class ConfigurationPropertyCollector {
.javadoc( extractJavadoc( constant ) )
.key( key )
.sourceClass( constant.getEnclosingElement().toString() )
.withModuleName( title.orElse( classTitle.orElse( this.title ) ) )
.withAnchorPrefix( anchorPrefix.orElse( classAnchorPrefix.orElse( this.anchor ) ) )
.withModuleName( this.title )
.withAnchorPrefix( this.anchor )
);
}
}
private ConfigurationProperty.Key extractKey(VariableElement constant, Optional<List<String>> classPrefix,
Optional<List<String>> constantPrefix) {
List<String> prefix;
if ( constantPrefix.isPresent() ) {
prefix = constantPrefix.get();
}
else if ( classPrefix.isPresent() ) {
prefix = classPrefix.get();
}
else {
prefix = Collections.emptyList();
}
private ConfigurationProperty.Key extractKey(VariableElement constant) {
return new ConfigurationProperty.Key(
prefix,
Objects.toString( constant.getConstantValue(), "NOT_FOUND#" + constant.getSimpleName() )
);
}
@ -135,11 +95,13 @@ public class ConfigurationPropertyCollector {
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() );
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 ) {
for ( org.jsoup.nodes.Element link : block.getElementsByTag( "a" ) ) {
String href = link.attr( "href" );
@ -173,7 +135,10 @@ public class ConfigurationPropertyCollector {
}
}
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 );
}
}

View File

@ -7,8 +7,6 @@
package org.hibernate.orm.properties.processor;
import static org.hibernate.orm.properties.processor.AnnotationUtils.isIgnored;
import java.nio.file.Path;
import java.util.Optional;
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() ) {
beforeExit();
}
@ -108,8 +93,7 @@ public class ConfigurationPropertyProcessor extends AbstractProcessor {
}
private void process(ConfigurationPropertyCollector propertyCollector, Element element) {
if ( !isIgnored( element ) && !ignore.map( p -> p.matcher( element.toString() ).matches() ).orElse(
Boolean.FALSE ) ) {
if ( !ignore.map( p -> p.matcher( element.toString() ).matches() ).orElse( Boolean.FALSE ) ) {
propertyCollector.visitType( (TypeElement) element );
}
}
@ -118,9 +102,4 @@ public class ConfigurationPropertyProcessor extends AbstractProcessor {
return ( element.getKind().equals( ElementKind.CLASS ) || element.getKind().equals( ElementKind.INTERFACE ) ) &&
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 "";
}