diff --git a/jcodemodel/src/main/java/com/helger/jcodemodel/AbstractJClass.java b/jcodemodel/src/main/java/com/helger/jcodemodel/AbstractJClass.java index b5bae1e5..639045c9 100644 --- a/jcodemodel/src/main/java/com/helger/jcodemodel/AbstractJClass.java +++ b/jcodemodel/src/main/java/com/helger/jcodemodel/AbstractJClass.java @@ -2,7 +2,7 @@ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved. - * Portions Copyright 2013-2026 Philip Helger + contributors + * Portions Copyright 2013-2025 Philip Helger + contributors * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development @@ -353,6 +353,7 @@ public AbstractJClass narrowAny () @NonNull public JAnnotatedClass annotated (@NonNull final Class extends Annotation> aClazz) { + ValueEnforcer.notNull (aClazz, "Clazz"); return annotated (owner ().ref (aClazz)); } @@ -392,9 +393,35 @@ public JAnnotatedClass annotated (@NonNull final Class extends Annotation> aC @NonNull public JAnnotatedClass annotated (@NonNull final AbstractJClass aClazz) { + ValueEnforcer.notNull (aClazz, "Clazz"); return new JAnnotatedClass (this, new JAnnotationUse (aClazz)); } + /** + * Creates a new type with a type-use annotation (JSR 308, Java 8+). + *
+ * This overload accepts a pre-configured {@link JAnnotationUse}, allowing annotations + * with parameters: + *
+ * JAnnotationUse sizeAnnotation = new JAnnotationUse(codeModel.ref(Size.class));
+ * sizeAnnotation.param("min", 1).param("max", 10);
+ * AbstractJClass annotatedString = stringClass.annotated(sizeAnnotation);
+ * // Generates: @Size(min = 1, max = 10) String
+ *
+ *
+ * @param aAnnotation
+ * The pre-configured annotation to apply as a type-use annotation.
+ * @return A new {@link JAnnotatedClass} wrapping this class with the annotation.
+ * @since 4.2.0
+ * @see #annotated(Class) for simple annotations without parameters
+ */
+ @NonNull
+ public JAnnotatedClass annotated (@NonNull final JAnnotationUse aAnnotation)
+ {
+ ValueEnforcer.notNull (aAnnotation, "Annotation");
+ return new JAnnotatedClass (this, aAnnotation);
+ }
+
/**
* @return If this class is parameterized, the type parameters of the given index.
*/
diff --git a/jcodemodel/src/main/java/com/helger/jcodemodel/JAnnotatedClass.java b/jcodemodel/src/main/java/com/helger/jcodemodel/JAnnotatedClass.java
index 374b8d6c..ef1cc749 100644
--- a/jcodemodel/src/main/java/com/helger/jcodemodel/JAnnotatedClass.java
+++ b/jcodemodel/src/main/java/com/helger/jcodemodel/JAnnotatedClass.java
@@ -69,8 +69,18 @@
* Example: {@code private List<@NotNull String> items;}
*
* - * Use {@link AbstractJClass#annotated(Class)} or {@link AbstractJClass#annotated(AbstractJClass)} - * to create instances of this class. + * Annotations with parameters: + *
+ * For annotations that require parameters, use {@link AbstractJClass#annotated(JAnnotationUse)}: + *
+ * JAnnotationUse sizeAnnotation = new JAnnotationUse(codeModel.ref(Size.class));
+ * sizeAnnotation.param("min", 1).param("max", 10);
+ * AbstractJClass annotatedString = stringClass.annotated(sizeAnnotation);
+ * // Generates: @Size(min = 1, max = 10) String
+ *
+ *
+ * Use {@link AbstractJClass#annotated(Class)}, {@link AbstractJClass#annotated(AbstractJClass)},
+ * or {@link AbstractJClass#annotated(JAnnotationUse)} to create instances of this class.
*
* @since 4.2.0
*/
@@ -150,6 +160,15 @@ public JAnnotatedClass annotated (@NonNull final AbstractJClass aClazz)
return new JAnnotatedClass (m_aBasis, newAnnotations);
}
+ @Override
+ @NonNull
+ public JAnnotatedClass annotated (@NonNull final JAnnotationUse aAnnotation)
+ {
+ final List