diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/DynamicType.java b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/DynamicType.java index 479696db7f2..edc0e6d2c8c 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/DynamicType.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/DynamicType.java @@ -951,8 +951,9 @@ interface Valuable extends FieldDefinition { * fields, the field's value is set to {@code 0} for {@code false} or {@code 1} for {@code true}. *
*- * A default value is set as a constant pool value and can only be set for {@code static} fields, Any other value must be set - * within a constructor. + * Important: A default value in a Java class file defines a field's value prior to the class's initialization. This value + * is only visible to code if the field is declared {@code static}. A default value can also be set for non-static fields where + * the value is not visible to code. The Java compiler only defines such values for {@code final} fields. *
* * @param value The value to define as a default value of the defined field. @@ -969,8 +970,9 @@ interface Valuable extends FieldDefinition { * values of {@code 0} and {@code 1} representing {@code false} and {@code true}. * *- * A default value is set as a constant pool value and can only be set for {@code static} fields, Any other value must be set - * within a constructor. + * Important: A default value in a Java class file defines a field's value prior to the class's initialization. This value + * is only visible to code if the field is declared {@code static}. A default value can also be set for non-static fields where + * the value is not visible to code. The Java compiler only defines such values for {@code final} fields. *
* * @param value The value to define as a default value of the defined field. @@ -984,8 +986,9 @@ interface Valuable extends FieldDefinition { * Defines the supplied {@code long} value as a default value of the previously defined or matched field. * *- * A default value is set as a constant pool value and can only be set for {@code static} fields, Any other value must be set - * within a constructor. + * Important: A default value in a Java class file defines a field's value prior to the class's initialization. This value + * is only visible to code if the field is declared {@code static}. A default value can also be set for non-static fields where + * the value is not visible to code. The Java compiler only defines such values for {@code final} fields. *
* * @param value The value to define as a default value of the defined field. @@ -999,8 +1002,9 @@ interface Valuable extends FieldDefinition { * Defines the supplied {@code float} value as a default value of the previously defined or matched field. * *- * A default value is set as a constant pool value and can only be set for {@code static} fields, Any other value must be set - * within a constructor. + * Important: A default value in a Java class file defines a field's value prior to the class's initialization. This value + * is only visible to code if the field is declared {@code static}. A default value can also be set for non-static fields where + * the value is not visible to code. The Java compiler only defines such values for {@code final} fields. *
* * @param value The value to define as a default value of the defined field. @@ -1014,8 +1018,10 @@ interface Valuable extends FieldDefinition { * Defines the supplied {@code double} value as a default value of the previously defined or matched field. * *- * A default value is set as a constant pool value and can only be set for {@code static} fields, Any other value must be set - * within a constructor. + *
+ * Important: A default value in a Java class file defines a field's value prior to the class's initialization. This value + * is only visible to code if the field is declared {@code static}. A default value can also be set for non-static fields where + * the value is not visible to code. The Java compiler only defines such values for {@code final} fields. *
* * @param value The value to define as a default value of the defined field. @@ -1029,8 +1035,9 @@ interface Valuable extends FieldDefinition { * Defines the supplied {@link String} value as a default value of the previously defined or matched field. * *- * A default value is set as a constant pool value and can only be set for {@code static} fields, Any other value must be set - * within a constructor. + * Important: A default value in a Java class file defines a field's value prior to the class's initialization. This value + * is only visible to code if the field is declared {@code static}. A default value can also be set for non-static fields where + * the value is not visible to code. The Java compiler only defines such values for {@code final} fields. *
* * @param value The value to define as a default value of the defined field. diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/TypeWriter.java b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/TypeWriter.java index 1e0ea9912fa..3bf89f9fda1 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/TypeWriter.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/TypeWriter.java @@ -1714,9 +1714,6 @@ public AnnotationVisitor visitTypeAnnotation(int typeReference, TypePath typePat @Override public FieldVisitor visitField(int modifiers, String name, String descriptor, String signature, Object defaultValue) { if (defaultValue != null) { - if ((modifiers & Opcodes.ACC_STATIC) == 0) { - throw new IllegalStateException("Cannot define a default value for non-static field " + name); - } Class> type; switch (descriptor.charAt(0)) { case 'Z': diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/TypeWriterDefaultTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/TypeWriterDefaultTest.java index 805f5b60d47..da5df3eb1d4 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/TypeWriterDefaultTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/TypeWriterDefaultTest.java @@ -136,15 +136,6 @@ public void testNonStaticFieldOnAnnotationAssertion() throws Exception { .make(); } - @Test(expected = IllegalStateException.class) - public void testNonStaticFieldWithConstantValue() throws Exception { - new ByteBuddy() - .subclass(Object.class) - .defineField(FOO, String.class) - .value(FOO) - .make(); - } - @Test(expected = IllegalStateException.class) public void testStaticFieldWithIncompatibleConstantValue() throws Exception { new ByteBuddy()