HHH-16633 add CDI @Dependent annotation if in build path

This commit is contained in:
Gavin King 2023-07-10 13:31:30 +02:00
parent 24db891e84
commit 447fa30a89
8 changed files with 53 additions and 7 deletions

View File

@ -100,10 +100,12 @@ public final class ClassWriter {
final StringWriter sw = new StringWriter();
try ( PrintWriter pw = new PrintWriter(sw) ) {
if ( context.addDependentAnnotation() && entity.isInjectable() ) {
pw.println( writeDependentAnnotation( entity ) );
}
if ( entity.getElement() instanceof TypeElement ) {
pw.println( writeStaticMetaModelAnnotation( entity ) );
}
if ( context.addGeneratedAnnotation() ) {
pw.println( writeGeneratedAnnotation( entity, context ) );
}
@ -239,7 +241,12 @@ public final class ClassWriter {
return "@SuppressWarnings({ \"deprecation\", \"rawtypes\" })";
}
private static String writeDependentAnnotation(Metamodel entity) {
return "@" + entity.importType( "jakarta.enterprise.context.Dependent" );
}
private static String writeStaticMetaModelAnnotation(Metamodel entity) {
return "@" + entity.importType( "jakarta.persistence.metamodel.StaticMetamodel" ) + "(" + entity.getSimpleName() + ".class)";
return "@" + entity.importType( "jakarta.persistence.metamodel.StaticMetamodel" )
+ "(" + entity.getSimpleName() + ".class)";
}
}

View File

@ -66,6 +66,7 @@ public final class Context {
*/
private Boolean fullyXmlConfigured;
private boolean addInjectAnnotation = false;
private boolean addDependentAnnotation = false;
private boolean addNonnullAnnotation = false;
private boolean addGeneratedAnnotation = true;
private boolean addGenerationDate;
@ -122,6 +123,14 @@ public final class Context {
this.addInjectAnnotation = addInjectAnnotation;
}
public boolean addDependentAnnotation() {
return addDependentAnnotation;
}
public void setAddDependentAnnotation(boolean addDependentAnnotation) {
this.addDependentAnnotation = addDependentAnnotation;
}
public boolean addNonnullAnnotation() {
return addNonnullAnnotation;
}

View File

@ -117,10 +117,14 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
final PackageElement jakartaAnnotationPackage =
context.getProcessingEnvironment().getElementUtils()
.getPackageElement( "jakarta.annotation" );
final PackageElement jakartaContextPackage =
context.getProcessingEnvironment().getElementUtils()
.getPackageElement( "jakarta.enterprise.context" );
context.setAddInjectAnnotation( jakartaInjectPackage != null );
context.setAddNonnullAnnotation( jakartaAnnotationPackage != null );
context.setAddGeneratedAnnotation( jakartaAnnotationPackage != null );
context.setAddDependentAnnotation( jakartaContextPackage != null );
final Map<String, String> options = environment.getOptions();

View File

@ -221,6 +221,11 @@ public class AnnotationMetaEntity extends AnnotationMeta {
return dao;
}
@Override
public boolean isInjectable() {
return dao;
}
@Override
public String toString() {
return new StringBuilder()

View File

@ -138,4 +138,9 @@ public class AnnotationMetaPackage extends AnnotationMeta {
boolean belongsToDao() {
return false;
}
@Override
public boolean isInjectable() {
return false;
}
}

View File

@ -18,7 +18,7 @@ public class DaoConstructor implements MetaAttribute {
private final String constructorName;
private final String methodName;
private final String returnTypeName;
private final boolean inject;
private final boolean addInjectAnnotation;
private final boolean addNonnullAnnotation;
public DaoConstructor(
@ -26,13 +26,13 @@ public class DaoConstructor implements MetaAttribute {
String constructorName,
String methodName,
String returnTypeName,
boolean inject,
boolean addInjectAnnotation,
boolean addNonnullAnnotation) {
this.annotationMetaEntity = annotationMetaEntity;
this.constructorName = constructorName;
this.methodName = methodName;
this.returnTypeName = returnTypeName;
this.inject = inject;
this.addInjectAnnotation = addInjectAnnotation;
this.addNonnullAnnotation = addNonnullAnnotation;
}
@ -55,8 +55,9 @@ public class DaoConstructor implements MetaAttribute {
declaration
.append(annotationMetaEntity.importType(returnTypeName))
.append(" entityManager;")
.append("\n")
.append(inject ? "\n@" + annotationMetaEntity.importType("jakarta.inject.Inject") : "")
.append("\n");
inject( declaration );
declaration
.append("\npublic ")
.append(constructorName)
.append("(");
@ -79,6 +80,14 @@ public class DaoConstructor implements MetaAttribute {
return declaration.toString();
}
private void inject(StringBuilder declaration) {
if ( addInjectAnnotation ) {
declaration
.append("\n@")
.append(annotationMetaEntity.importType("jakarta.inject.Inject"));
}
}
private void notNull(StringBuilder declaration) {
if ( addNonnullAnnotation ) {
declaration

View File

@ -36,4 +36,6 @@ public interface Metamodel extends ImportContext {
Context getContext();
boolean isImplementation();
boolean isInjectable();
}

View File

@ -622,4 +622,9 @@ public class XmlMetaEntity implements Metamodel {
public boolean isImplementation() {
return false;
}
@Override
public boolean isInjectable() {
return false;
}
}