HHH-17772 add option to suppress JD metamodel

- also squash an error
- and improve report bug reporting
This commit is contained in:
Gavin King 2024-02-24 20:45:38 +01:00
parent 582d736062
commit 7e9b55ff92
15 changed files with 51 additions and 31 deletions

View File

@ -8,7 +8,7 @@ plugins {
id 'org.hibernate.build.xjc-jakarta'
}
description = 'Annotation Processor to generate JPA 2 static metamodel classes'
description = 'Hibernate compile-time tooling'
apply from: rootProject.file( 'gradle/published-java-module.gradle' )
apply plugin: 'version-injection'
@ -25,7 +25,6 @@ dependencies {
api jakartaLibs.jaxb
api jakartaLibs.validation
api jakartaLibs.annotation
api jakartaLibs.data
api libs.antlrRuntime
api libs.byteBuddy
@ -41,7 +40,8 @@ sourceSets.main {
compileTestJava {
options.compilerArgs += [
"-proc:none"
"-proc:none",
"-AsuppressJakartaDataMetamodel=true"
]
}
@ -63,7 +63,8 @@ if ( jdkVersions.test.release.asInt() >= 17 && jdkVersions.explicit ) {
languageVersion = jdkVersions.test.launcher
}
}
} else {
}
else {
sourceSets {
test {
java {

View File

@ -36,8 +36,8 @@ public final class ClassWriter {
public static void writeFile(Metamodel entity, Context context) {
try {
String metaModelPackage = entity.getPackageName();
// need to generate the body first, since this will also update the required imports which need to
// be written out first
// need to generate the body first, since this will also update
// the required imports which need to be written out first
String body = generateBody( entity, context ).toString();
FileObject fo = context.getProcessingEnvironment().getFiler().createSourceFile(

View File

@ -25,6 +25,7 @@ import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
@ -42,6 +43,7 @@ import static org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor.FULLY_ANNOTA
import static org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor.LAZY_XML_PARSING;
import static org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor.ORM_XML_OPTION;
import static org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor.PERSISTENCE_XML_OPTION;
import static org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor.SUPPRESS_JAKARTA_DATA_METAMODEL;
import static org.hibernate.jpamodelgen.util.Constants.*;
import static org.hibernate.jpamodelgen.util.TypeUtils.containsAnnotation;
import static org.hibernate.jpamodelgen.util.TypeUtils.getAnnotationMirror;
@ -77,7 +79,8 @@ import static org.hibernate.jpamodelgen.util.TypeUtils.isClassOrRecordType;
LAZY_XML_PARSING,
ADD_GENERATION_DATE,
ADD_GENERATED_ANNOTATION,
ADD_SUPPRESS_WARNINGS_ANNOTATION
ADD_SUPPRESS_WARNINGS_ANNOTATION,
SUPPRESS_JAKARTA_DATA_METAMODEL
})
public class JPAMetaModelEntityProcessor extends AbstractProcessor {
@ -123,6 +126,12 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
*/
public static final String ADD_SUPPRESS_WARNINGS_ANNOTATION = "addSuppressWarningsAnnotation";
/**
* Option to suppress generation of the Jakarta Data static metamodel,
* even when Jakarta Data is available on the build path.
*/
public static final String SUPPRESS_JAKARTA_DATA_METAMODEL = "suppressJakartaDataMetamodel";
private static final boolean ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS = false;
private Context context;
@ -167,10 +176,13 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
context.setAddGeneratedAnnotation( jakartaAnnotationPackage != null );
context.setAddDependentAnnotation( jakartaContextPackage != null );
context.setAddTransactionScopedAnnotation( jakartaTransactionsPackage != null );
context.setGenerateJakartaDataStaticMetamodel( jakartaDataPackage != null );
final Map<String, String> options = environment.getOptions();
boolean suppressJakartaData = parseBoolean( options.get( SUPPRESS_JAKARTA_DATA_METAMODEL ) );
context.setGenerateJakartaDataStaticMetamodel( !suppressJakartaData && jakartaDataPackage != null );
String setting = options.get( ADD_GENERATED_ANNOTATION );
if ( setting != null ) {
context.setAddGeneratedAnnotation( parseBoolean( setting ) );
@ -214,12 +226,19 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
createMetaModelClasses();
}
catch (Exception e) {
final StringBuffer b = new StringBuffer();
Arrays.stream(e.getStackTrace())
.forEach(stackTraceElement -> {
b.append(stackTraceElement);
b.append('\n');
});
final Throwable cause = e.getCause();
final String message =
cause != null && cause != e
? e.getMessage() + " caused by " + cause.getMessage()
: e.getMessage();
context.logMessage( Diagnostic.Kind.ERROR, "Error generating JPA metamodel: " + message );
context.logMessage( Diagnostic.Kind.ERROR, b.toString() );
}
}
return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;

View File

@ -70,7 +70,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
@Override
public String getMetaType() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override

View File

@ -69,7 +69,7 @@ public class AnnotationMetaPackage extends AnnotationMeta {
@Override
public @Nullable String getSupertypeName() {
throw new UnsupportedOperationException();
return null;
}
@Override
@ -157,7 +157,7 @@ public class AnnotationMetaPackage extends AnnotationMeta {
@Override
public String scope() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override

View File

@ -50,7 +50,7 @@ public class AnnotationMetaType implements MetaAttribute {
@Override
public String getAttributeNameDeclarationString() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override

View File

@ -122,7 +122,7 @@ public class DataAnnotationMetaAttribute implements MetaAttribute {
@Override
public String getAttributeNameDeclarationString(){
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override

View File

@ -114,12 +114,12 @@ public class DefaultConstructor implements MetaAttribute {
@Override
public String getAttributeNameDeclarationString() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override
public String getMetaType() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override

View File

@ -127,12 +127,12 @@ public class LifecycleMethod implements MetaAttribute {
@Override
public String getAttributeNameDeclarationString() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override
public String getMetaType() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override

View File

@ -37,7 +37,7 @@ class NameMetaAttribute implements MetaAttribute {
@Override
public String getAttributeDeclarationString() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override
@ -62,7 +62,7 @@ class NameMetaAttribute implements MetaAttribute {
@Override
public String getMetaType() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override

View File

@ -210,12 +210,12 @@ class NamedQueryMethod implements MetaAttribute {
@Override
public String getAttributeNameDeclarationString() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override
public String getMetaType() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override

View File

@ -151,12 +151,12 @@ public class RepositoryConstructor implements MetaAttribute {
@Override
public String getAttributeNameDeclarationString() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override
public String getMetaType() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override

View File

@ -83,7 +83,7 @@ public abstract class MockEntityPersister implements EntityPersister, Queryable,
@Override
public EntityMetamodel getEntityMetamodel() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override

View File

@ -401,7 +401,7 @@ public abstract class MockSessionFactory
@Override
public FastSessionServices getFastSessionServices() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@ -410,7 +410,7 @@ public abstract class MockSessionFactory
@Override
public RootGraphImplementor<?> findEntityGraphByName(String s) {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
static Class<?> toPrimitiveClass(Class<?> type) {
@ -746,7 +746,7 @@ public abstract class MockSessionFactory
@Override
public SqlStringGenerationContext getSqlStringGenerationContext() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override
@ -792,7 +792,7 @@ public abstract class MockSessionFactory
@Override
public <X> ManagedDomainType<X> findManagedType(Class<X> cls) {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override
@ -807,12 +807,12 @@ public abstract class MockSessionFactory
@Override
public <X> ManagedDomainType<X> managedType(Class<X> cls) {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override
public <X> EntityDomainType<X> entity(Class<X> cls) {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override

View File

@ -637,7 +637,7 @@ public class XmlMetaEntity implements Metamodel {
@Override
public String scope() {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException("operation not supported");
}
@Override