From 92e559981b011b2614e65961948c5ce4cce55733 Mon Sep 17 00:00:00 2001 From: Philip Helger Date: Sat, 20 Oct 2018 08:04:43 +0200 Subject: [PATCH] Added new `JAnnotationUse` method overloads that automatically pass `value` as the annotation parameter name (issue #64) --- README.md | 2 + .../com/helger/jcodemodel/JAnnotationUse.java | 320 ++++++++++++++---- .../jcodemodel/AnnotationUseFuncTest.java | 5 +- 3 files changed, 265 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 6db9c05c..a5da2889 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,11 @@ A site with the links to the [API docs](http://phax.github.io/jcodemodel/) etc. * v3.2.0 - work in progress * Introduced class `JCMWriter` that should be used to emit the outgoing Java files. This replaces `codemodel.build` and offers a more consistent API. Most existing method remain existing and deprecated and just forward to `JCMWriter`. + * Instead of `cm.build (...)` use `new JCMWriter (cm).build (...)` * Extracted `IJFormatter` interface for better separation of concerns. `JFormatter` was moved to a sub-package * `ProgressCodeWriter` no longer needs an explicit `PrintStream` but a `ProgressCodeWriter.IProgressTracker` instead. * Default charset for Java classes is now `UTF-8`. + * Added new `JAnnotationUse` method overloads that automatically pass `value` as the annotation parameter name (issue #64) * v3.1.0 - 2018-08-22 * Added ` AbstractJType._new()` * Change return types of special `JBlock` methods to `void` to avoid chaining (issue #62) - incompatible change! diff --git a/src/main/java/com/helger/jcodemodel/JAnnotationUse.java b/src/main/java/com/helger/jcodemodel/JAnnotationUse.java index d332e71c..5bc97efd 100644 --- a/src/main/java/com/helger/jcodemodel/JAnnotationUse.java +++ b/src/main/java/com/helger/jcodemodel/JAnnotationUse.java @@ -207,15 +207,29 @@ private JAnnotationUse _addValue (@Nonnull final String sName, return this; } + /** + * Adds a member value pair to this annotation using the name + * {@value #SPECIAL_KEY_VALUE}. + * + * @param bValue + * The value for this annotation + * @return this for chaining + * @since 3.2.0 + */ + @Nonnull + public JAnnotationUse param (final boolean bValue) + { + return param (SPECIAL_KEY_VALUE, bValue); + } + /** * Adds a member value pair to this annotation * * @param sName * The simple name for this annotation * @param bValue - * The boolean value for this annotation - * @return The {@link JAnnotationUse}. More member value pairs can be added to - * it using the same or the overloaded methods. + * The value for this annotation + * @return this for chaining */ @Nonnull public JAnnotationUse param (@Nonnull final String sName, final boolean bValue) @@ -230,15 +244,29 @@ public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final bo return this; } + /** + * Adds a member value pair to this annotation using the name + * {@value #SPECIAL_KEY_VALUE}. + * + * @param nValue + * The value for this annotation + * @return this for chaining + * @since 3.2.0 + */ + @Nonnull + public JAnnotationUse param (final byte nValue) + { + return param (SPECIAL_KEY_VALUE, nValue); + } + /** * Adds a member value pair to this annotation * * @param sName * The simple name for this annotation * @param nValue - * The byte member value for this annotation - * @return The {@link JAnnotationUse}. More member value pairs can be added to - * it using the same or the overloaded methods. + * The value for this annotation + * @return this for chaining */ @Nonnull public JAnnotationUse param (@Nonnull final String sName, final byte nValue) @@ -253,15 +281,29 @@ public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final by return this; } + /** + * Adds a member value pair to this annotation using the name + * {@value #SPECIAL_KEY_VALUE}. + * + * @param cValue + * The value for this annotation + * @return this for chaining + * @since 3.2.0 + */ + @Nonnull + public JAnnotationUse param (final char cValue) + { + return param (SPECIAL_KEY_VALUE, cValue); + } + /** * Adds a member value pair to this annotation * * @param sName * The simple name for this annotation * @param cValue - * The char member value for this annotation - * @return The {@link JAnnotationUse}. More member value pairs can be added to - * it using the same or the overloaded methods. + * The value for this annotation + * @return this for chaining */ @Nonnull public JAnnotationUse param (@Nonnull final String sName, final char cValue) @@ -276,15 +318,29 @@ public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final ch return this; } + /** + * Adds a member value pair to this annotation using the name + * {@value #SPECIAL_KEY_VALUE}. + * + * @param dValue + * The value for this annotation + * @return this for chaining + * @since 3.2.0 + */ + @Nonnull + public JAnnotationUse param (final double dValue) + { + return param (SPECIAL_KEY_VALUE, dValue); + } + /** * Adds a member value pair to this annotation * * @param sName * The simple name for this annotation * @param dValue - * The double member value for this annotation - * @return The {@link JAnnotationUse}. More member value pairs can be added to - * it using the same or the overloaded methods. + * The value for this annotation + * @return this for chaining */ @Nonnull public JAnnotationUse param (@Nonnull final String sName, final double dValue) @@ -299,15 +355,29 @@ public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final do return this; } + /** + * Adds a member value pair to this annotation using the name + * {@value #SPECIAL_KEY_VALUE}. + * + * @param fValue + * The value for this annotation + * @return this for chaining + * @since 3.2.0 + */ + @Nonnull + public JAnnotationUse param (final float fValue) + { + return param (SPECIAL_KEY_VALUE, fValue); + } + /** * Adds a member value pair to this annotation * * @param sName * The simple name for this annotation * @param fValue - * The float member value for this annotation - * @return The {@link JAnnotationUse}. More member value pairs can be added to - * it using the same or the overloaded methods. + * The value for this annotation + * @return this for chaining */ @Nonnull public JAnnotationUse param (@Nonnull final String sName, final float fValue) @@ -322,87 +392,143 @@ public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final fl return this; } + /** + * Adds a member value pair to this annotation using the name + * {@value #SPECIAL_KEY_VALUE}. + * + * @param nValue + * The value for this annotation + * @return this for chaining + * @since 3.2.0 + */ + @Nonnull + public JAnnotationUse param (final int nValue) + { + return param (SPECIAL_KEY_VALUE, nValue); + } + /** * Adds a member value pair to this annotation * * @param sName * The simple name for this annotation * @param nValue - * The long member value for this annotation - * @return The {@link JAnnotationUse}. More member value pairs can be added to - * it using the same or the overloaded methods. + * The value for this annotation + * @return this for chaining */ @Nonnull - public JAnnotationUse param (@Nonnull final String sName, final long nValue) + public JAnnotationUse param (@Nonnull final String sName, final int nValue) { return _addValue (sName, wrap (nValue)); } @Nonnull - public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final long... aValues) + public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final int... aValues) { paramArray (sName).params (aValues); return this; } + /** + * Adds a member value pair to this annotation using the name + * {@value #SPECIAL_KEY_VALUE}. + * + * @param nValue + * The value for this annotation + * @return this for chaining + * @since 3.2.0 + */ + @Nonnull + public JAnnotationUse param (final long nValue) + { + return param (SPECIAL_KEY_VALUE, nValue); + } + /** * Adds a member value pair to this annotation * * @param sName * The simple name for this annotation * @param nValue - * The short member value for this annotation - * @return The {@link JAnnotationUse}. More member value pairs can be added to - * it using the same or the overloaded methods. + * The value for this annotation + * @return this for chaining */ @Nonnull - public JAnnotationUse param (@Nonnull final String sName, final short nValue) + public JAnnotationUse param (@Nonnull final String sName, final long nValue) { return _addValue (sName, wrap (nValue)); } @Nonnull - public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final short... aValues) + public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final long... aValues) { paramArray (sName).params (aValues); return this; } + /** + * Adds a member value pair to this annotation using the name + * {@value #SPECIAL_KEY_VALUE}. + * + * @param nValue + * The value for this annotation + * @return this for chaining + * @since 3.2.0 + */ + @Nonnull + public JAnnotationUse param (final short nValue) + { + return param (SPECIAL_KEY_VALUE, nValue); + } + /** * Adds a member value pair to this annotation * * @param sName * The simple name for this annotation * @param nValue - * The int member value for this annotation - * @return The {@link JAnnotationUse}. More member value pairs can be added to - * it using the same or the overloaded methods. + * The value for this annotation + * @return this for chaining */ @Nonnull - public JAnnotationUse param (@Nonnull final String sName, final int nValue) + public JAnnotationUse param (@Nonnull final String sName, final short nValue) { return _addValue (sName, wrap (nValue)); } @Nonnull - public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final int... aValues) + public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final short... aValues) { paramArray (sName).params (aValues); return this; } + /** + * Adds a member value pair to this annotation using the name + * {@value #SPECIAL_KEY_VALUE}. + * + * @param sValue + * The value for this annotation + * @return this for chaining + * @since 3.2.0 + */ + @Nonnull + public JAnnotationUse param (@Nonnull final String sValue) + { + return param (SPECIAL_KEY_VALUE, sValue); + } + /** * Adds a member value pair to this annotation * * @param sName * The simple name for this annotation * @param sValue - * The String member value for this annotation - * @return The {@link JAnnotationUse}. More member value pairs can be added to - * it using the same or the overloaded methods. + * The value for this annotation + * @return this for chaining */ @Nonnull - public JAnnotationUse param (@Nonnull final String sName, final String sValue) + public JAnnotationUse param (@Nonnull final String sName, @Nonnull final String sValue) { return _addValue (sName, wrap (sValue)); } @@ -414,15 +540,29 @@ public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final St return this; } + /** + * Adds a member value pair to this annotation using the name + * {@value #SPECIAL_KEY_VALUE}. + * + * @param aValue + * The value for this annotation + * @return this for chaining + * @since 3.2.0 + */ + @Nonnull + public JAnnotationUse param (@Nonnull final Enum aValue) + { + return param (SPECIAL_KEY_VALUE, aValue); + } + /** * Adds a member value pair to this annotation * * @param sName * The simple name for this annotation * @param aValue - * The enum class which is member value for this annotation - * @return The {@link JAnnotationUse}. More member value pairs can be added to - * it using the same or the overloaded methods. + * The value for this annotation + * @return this for chaining */ @Nonnull public JAnnotationUse param (@Nonnull final String sName, @Nonnull final Enum aValue) @@ -437,15 +577,29 @@ public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final En return this; } + /** + * Adds a member value pair to this annotation using the name + * {@value #SPECIAL_KEY_VALUE}. + * + * @param aValue + * The value for this annotation + * @return this for chaining + * @since 3.2.0 + */ + @Nonnull + public JAnnotationUse param (@Nonnull final JEnumConstant aValue) + { + return param (SPECIAL_KEY_VALUE, aValue); + } + /** * Adds a member value pair to this annotation * * @param sName * The simple name for this annotation * @param aValue - * The {@link JEnumConstant} which is member value for this annotation - * @return The {@link JAnnotationUse}. More member value pairs can be added to - * it using the same or the overloaded methods. + * The value for this annotation + * @return this for chaining */ @Nonnull public JAnnotationUse param (@Nonnull final String sName, @Nonnull final JEnumConstant aValue) @@ -461,7 +615,29 @@ public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final JE } /** - * Adds a member value pair to this annotation This can be used for e.g to + * Adds a member value pair to this annotation using the name + * {@value #SPECIAL_KEY_VALUE}. This can be used for e.g to specify + * + *
+   * @Anno(value=Integer.class);
+   * 
+ * + * For adding a value of Class<? extends Annotation> + * {@link #annotationParam(String, Class)} + * + * @param aValue + * The value for this annotation + * @return this for chaining + * @since 3.2.0 + */ + @Nonnull + public JAnnotationUse param (@Nonnull final Class aValue) + { + return param (SPECIAL_KEY_VALUE, aValue); + } + + /** + * Adds a member value pair to this annotation. This can be used for e.g to * specify * *
@@ -474,9 +650,8 @@ public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final JE
    * @param sName
    *        The simple name for this annotation param
    * @param aValue
-   *        The class type of the param
-   * @return The {@link JAnnotationUse}. More member value pairs can be added to
-   *         it using the same or the overloaded methods.
+   *        The value for this annotation
+   * @return this for chaining
    */
   @Nonnull
   public JAnnotationUse param (@Nonnull final String sName, @Nonnull final Class  aValue)
@@ -491,6 +666,21 @@ public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final Cl
     return this;
   }
 
+  /**
+   * Adds a member value pair to this annotation using the name
+   * {@value #SPECIAL_KEY_VALUE}.
+   *
+   * @param aValue
+   *        The value for this annotation
+   * @return this for chaining
+   * @since 3.2.0
+   */
+  @Nonnull
+  public JAnnotationUse param (@Nonnull final AbstractJType aValue)
+  {
+    return param (SPECIAL_KEY_VALUE, aValue);
+  }
+
   /**
    * Adds a member value pair to this annotation based on the type represented
    * by the given {@link AbstractJType}
@@ -498,9 +688,8 @@ public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final Cl
    * @param sName
    *        The simple name for this annotation param
    * @param aValue
-   *        the {@link AbstractJType} representing the actual type
-   * @return The {@link JAnnotationUse}. More member value pairs can be added to
-   *         it using the same or the overloaded methods.
+   *        The value for this annotation
+   * @return this for chaining
    */
   @Nonnull
   public JAnnotationUse param (@Nonnull final String sName, @Nonnull final AbstractJType aValue)
@@ -515,16 +704,29 @@ public JAnnotationUse paramArray (@Nonnull final String sName, @Nonnull final Ab
     return this;
   }
 
+  /**
+   * Adds a member value pair to this annotation using the name
+   * {@value #SPECIAL_KEY_VALUE}.
+   *
+   * @param aValue
+   *        The value for this annotation
+   * @return this for chaining
+   * @since 3.2.0
+   */
+  @Nonnull
+  public JAnnotationUse param (@Nonnull final IJExpression aValue)
+  {
+    return param (SPECIAL_KEY_VALUE, aValue);
+  }
+
   /**
    * Adds a member value pair to this annotation.
    *
    * @param sName
    *        The simple name for this annotation
    * @param aValue
-   *        The {@link IJExpression} which provides the content value for this
-   *        annotation
-   * @return The {@link JAnnotationUse}. More member value pairs can be added to
-   *         it using the same or the overloaded methods.
+   *        The value for this annotation
+   * @return this for chaining
    */
   @Nonnull
   public JAnnotationUse param (@Nonnull final String sName, @Nonnull final IJExpression aValue)
@@ -556,7 +758,7 @@ public JAnnotationArrayMember paramArray (@Nonnull final String sName)
   }
 
   /**
-   * Adds a member value pair to this annotation For adding class values as
+   * Adds a member value pair to this annotation for adding class values as
    * param
    *
    * @see #param(String, Class)
@@ -564,8 +766,7 @@ public JAnnotationArrayMember paramArray (@Nonnull final String sName)
    *        The simple name for this annotation
    * @param aValue
    *        The annotation class which is member value for this annotation
-   * @return The {@link JAnnotationUse}. More member value pairs can be added to
-   *         it using the same or the overloaded methods.
+   * @return this for chaining
    */
   @Nonnull
   public JAnnotationUse annotationParam (@Nonnull final String sName,
@@ -575,7 +776,7 @@ public JAnnotationUse annotationParam (@Nonnull final String sName,
   }
 
   /**
-   * Adds a member value pair to this annotation For adding class values as
+   * Adds a member value pair to this annotation for adding class values as
    * param
    *
    * @see #param(String, Class)
@@ -583,15 +784,14 @@ public JAnnotationUse annotationParam (@Nonnull final String sName,
    *        The simple name for this annotation
    * @param aValue
    *        The annotation class which is member value for this annotation
-   * @return The {@link JAnnotationUse}. More member value pairs can be added to
-   *         it using the same or the overloaded methods.
+   * @return this for chaining
    */
   @Nonnull
   public JAnnotationUse annotationParam (@Nonnull final String sName, @Nonnull final AbstractJClass aValue)
   {
-    final JAnnotationUse annotationUse = new JAnnotationUse (aValue);
-    _addValue (sName, annotationUse);
-    return annotationUse;
+    final JAnnotationUse aAnnotationUse = new JAnnotationUse (aValue);
+    _addValue (sName, aAnnotationUse);
+    return aAnnotationUse;
   }
 
   @Nonnegative
diff --git a/src/test/java/com/helger/jcodemodel/AnnotationUseFuncTest.java b/src/test/java/com/helger/jcodemodel/AnnotationUseFuncTest.java
index 5340467c..6b832784 100644
--- a/src/test/java/com/helger/jcodemodel/AnnotationUseFuncTest.java
+++ b/src/test/java/com/helger/jcodemodel/AnnotationUseFuncTest.java
@@ -106,7 +106,7 @@ interface XmlElementW extends IJAnnotationWriter 
    *   }
    * }
    * 
- * + * * @throws JClassAlreadyExistsException * internally only */ @@ -166,7 +166,8 @@ public void testMain () throws JClassAlreadyExistsException // adding an annotation as a member value pair final JAnnotationUse myuse = aUse.annotationParam ("foo", Target.class); myuse.param ("junk", 7); + myuse.param ("value-value"); - CodeModelTestsHelper.parseCodeModel (cm); + CodeModelTestsHelper.printCodeModel (cm); } }