Skip to content

Commit

Permalink
Do not trigger validation for constant attribute in non-static fields…
Browse files Browse the repository at this point in the history
… as they are actually addable in byte code but only invisible to the runtime.
  • Loading branch information
raphw committed Apr 23, 2016
1 parent 24fe646 commit 4227434
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
31 changes: 19 additions & 12 deletions byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/DynamicType.java
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,9 @@ interface Valuable<U> extends FieldDefinition<U> {
* fields, the field's value is set to {@code 0} for {@code false} or {@code 1} for {@code true}.
* </p>
* <p>
* 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.
* <b>Important</b>: 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.
* </p>
*
* @param value The value to define as a default value of the defined field.
Expand All @@ -969,8 +970,9 @@ interface Valuable<U> extends FieldDefinition<U> {
* values of {@code 0} and {@code 1} representing {@code false} and {@code true}.
* </p>
* <p>
* 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.
* <b>Important</b>: 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.
* </p>
*
* @param value The value to define as a default value of the defined field.
Expand All @@ -984,8 +986,9 @@ interface Valuable<U> extends FieldDefinition<U> {
* Defines the supplied {@code long} value as a default value of the previously defined or matched field.
* </p>
* <p>
* 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.
* <b>Important</b>: 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.
* </p>
*
* @param value The value to define as a default value of the defined field.
Expand All @@ -999,8 +1002,9 @@ interface Valuable<U> extends FieldDefinition<U> {
* Defines the supplied {@code float} value as a default value of the previously defined or matched field.
* </p>
* <p>
* 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.
* <b>Important</b>: 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.
* </p>
*
* @param value The value to define as a default value of the defined field.
Expand All @@ -1014,8 +1018,10 @@ interface Valuable<U> extends FieldDefinition<U> {
* Defines the supplied {@code double} value as a default value of the previously defined or matched field.
* </p>
* <p>
* 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.
* <p>
* <b>Important</b>: 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.
* </p>
*
* @param value The value to define as a default value of the defined field.
Expand All @@ -1029,8 +1035,9 @@ interface Valuable<U> extends FieldDefinition<U> {
* Defines the supplied {@link String} value as a default value of the previously defined or matched field.
* </p>
* <p>
* 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.
* <b>Important</b>: 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.
* </p>
*
* @param value The value to define as a default value of the defined field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 4227434

Please sign in to comment.