Skip to content

Commit

Permalink
Showing 6 changed files with 60 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1516,11 +1516,9 @@ public Resolved resolve(TypeDescription instrumentedType, MethodDescription inst
* {@inheritDoc}
*/
public InstrumentedType prepare(InstrumentedType instrumentedType) {
return instrumentedType
.withField(new FieldDescription.Token(name,
Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_VOLATILE | Opcodes.ACC_SYNTHETIC,
fieldType.asGenericType()))
.withInitializer(new LoadedTypeInitializer.ForStaticField(name, value));
return instrumentedType.withAuxiliaryField(new FieldDescription.Token(name,
Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_VOLATILE | Opcodes.ACC_SYNTHETIC,
fieldType.asGenericType()), value);
}
}

Original file line number Diff line number Diff line change
@@ -899,6 +899,7 @@ interface Factory extends InstrumentedType.Prepareable {
*/
ArgumentProvider make(Implementation.Target implementationTarget);
}

/**
* An argument loader that loads the {@code null} value onto the operand stack.
*/
@@ -1510,11 +1511,9 @@ public Factory(Object value) {
* {@inheritDoc}
*/
public InstrumentedType prepare(InstrumentedType instrumentedType) {
return instrumentedType
.withField(new FieldDescription.Token(name,
Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(value.getClass())))
.withInitializer(new LoadedTypeInitializer.ForStaticField(name, value));
return instrumentedType.withAuxiliaryField(new FieldDescription.Token(name,
Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(value.getClass())), value);
}

/**
@@ -2091,6 +2090,7 @@ public StackManipulation toStackManipulation(MethodDescription invokedMethod, As
);
}
}

/**
* A factory for invoking a static method or a self-declared method.
*/
@@ -2275,11 +2275,9 @@ protected Factory(Object target, TypeDescription.Generic fieldType) {
* {@inheritDoc}
*/
public InstrumentedType prepare(InstrumentedType instrumentedType) {
return instrumentedType
.withField(new FieldDescription.Token(name,
Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_VOLATILE | Opcodes.ACC_SYNTHETIC,
fieldType))
.withInitializer(new LoadedTypeInitializer.ForStaticField(name, target));
return instrumentedType.withAuxiliaryField(new FieldDescription.Token(name,
Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_VOLATILE | Opcodes.ACC_SYNTHETIC,
fieldType), target);
}

/**
@@ -3005,6 +3003,7 @@ interface Factory {
*/
TerminationHandler make(TypeDescription instrumentedType);
}

/**
* Simple termination handler implementations.
*/
Original file line number Diff line number Diff line change
@@ -1079,16 +1079,9 @@ protected WithInstance(String fieldName,
* {@inheritDoc}
*/
public InstrumentedType prepare(InstrumentedType instrumentedType) {
if (!instrumentedType.getDeclaredFields().filter(named(fieldName).and(fieldType(fieldType.asErasure()))).isEmpty()) {
throw new IllegalStateException("Field with name " + fieldName
+ " and type " + fieldType.asErasure()
+ " already declared by " + instrumentedType);
}
return instrumentedType
.withField(new FieldDescription.Token(fieldName,
Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_VOLATILE | Opcodes.ACC_SYNTHETIC,
fieldType))
.withInitializer(new LoadedTypeInitializer.ForStaticField(fieldName, target));
return instrumentedType.withAuxiliaryField(new FieldDescription.Token(fieldName,
Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_VOLATILE | Opcodes.ACC_SYNTHETIC,
fieldType), target);
}

@Override
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@ protected static InstrumentedType.WithFlexibleName makePlainInstrumentedType() {
Collections.<TypeVariableToken>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<FieldDescription.Token>emptyList(),
Collections.<String, Object>emptyMap(),
Collections.<MethodDescription.Token>emptyList(),
Collections.<RecordComponentDescription.Token>emptyList(),
Collections.<AnnotationDescription>emptyList(),
@@ -190,6 +191,42 @@ public void testWithFieldOfInstrumentedTypeAsArray() throws Exception {
assertThat(fieldDescription.getDeclaringType(), sameInstance((TypeDescription) instrumentedType));
}

@Test
@SuppressWarnings("unchecked")
public void testWithAuxiliaryField() throws Exception {
TypeDescription.Generic fieldType = mock(TypeDescription.Generic.class);
when(fieldType.accept(Mockito.any(TypeDescription.Generic.Visitor.class))).thenReturn(fieldType);
TypeDescription rawFieldType = mock(TypeDescription.class);
when(fieldType.asErasure()).thenReturn(rawFieldType);
when(rawFieldType.getName()).thenReturn(FOO);
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
Object value = mock(Object.class);
instrumentedType = instrumentedType.withAuxiliaryField(new FieldDescription.Token(BAR, Opcodes.ACC_PUBLIC, fieldType), value);
assertThat(instrumentedType.withAuxiliaryField(new FieldDescription.Token(BAR, Opcodes.ACC_PUBLIC, fieldType), value), sameInstance(instrumentedType));
assertThat(instrumentedType.getDeclaredFields().size(), is(1));
FieldDescription.InDefinedShape fieldDescription = instrumentedType.getDeclaredFields().get(0);
assertThat(fieldDescription.getType(), is(fieldType));
assertThat(fieldDescription.getModifiers(), is(Opcodes.ACC_PUBLIC));
assertThat(fieldDescription.getName(), is(BAR));
assertThat(fieldDescription.getDeclaringType(), sameInstance((TypeDescription) instrumentedType));
assertThat(instrumentedType.getLoadedTypeInitializer().isAlive(), is(true));
}

@Test(expected = IllegalStateException.class)
public void testWithAuxiliaryFieldConflict() throws Exception {
TypeDescription.Generic fieldType = mock(TypeDescription.Generic.class);
when(fieldType.accept(Mockito.any(TypeDescription.Generic.Visitor.class))).thenReturn(fieldType);
TypeDescription rawFieldType = mock(TypeDescription.class);
when(fieldType.asErasure()).thenReturn(rawFieldType);
when(rawFieldType.getName()).thenReturn(FOO);
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
instrumentedType
.withAuxiliaryField(new FieldDescription.Token(BAR, Opcodes.ACC_PUBLIC, fieldType), mock(Object.class))
.withAuxiliaryField(new FieldDescription.Token(BAR, Opcodes.ACC_PUBLIC, fieldType), mock(Object.class));
}

@Test
@SuppressWarnings("unchecked")
public void testWithMethod() throws Exception {
@@ -1525,6 +1562,7 @@ public void testTypeVariableOutOfScopeIsErased() throws Exception {
Collections.<TypeVariableToken>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<FieldDescription.Token>emptyList(),
Collections.<String, Object>emptyMap(),
Collections.singletonList(new MethodDescription.Token("foo",
Opcodes.ACC_BRIDGE,
TypeDescription.Generic.VOID,
Original file line number Diff line number Diff line change
@@ -42,6 +42,11 @@ public void testFieldToken() throws Exception {
new InstrumentedType.Frozen(TypeDescription.STRING, LoadedTypeInitializer.NoOp.INSTANCE).withField(mock(FieldDescription.Token.class));
}

@Test(expected = IllegalStateException.class)
public void testAuxiliaryFieldToken() throws Exception {
new InstrumentedType.Frozen(TypeDescription.STRING, LoadedTypeInitializer.NoOp.INSTANCE).withAuxiliaryField(mock(FieldDescription.Token.class), mock(Object.class));
}

@Test(expected = IllegalStateException.class)
public void testMethodToken() throws Exception {
new InstrumentedType.Frozen(TypeDescription.STRING, LoadedTypeInitializer.NoOp.INSTANCE).withMethod(mock(MethodDescription.Token.class));
Original file line number Diff line number Diff line change
@@ -1096,6 +1096,7 @@ public void testVisibilityExtension() throws Exception {
Collections.<TypeVariableToken>emptyList(),
Collections.<TypeDescription.Generic>singletonList(TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(VisibilityExtension.class)),
Collections.<FieldDescription.Token>emptyList(),
Collections.<String, Object>emptyMap(),
Collections.<MethodDescription.Token>emptyList(),
Collections.<RecordComponentDescription.Token>emptyList(),
Collections.<AnnotationDescription>emptyList(),
@@ -1135,6 +1136,7 @@ public void testOrphanedBridge() throws Exception {
Collections.<TypeVariableToken>emptyList(),
Collections.<TypeDescription.Generic>emptyList(),
Collections.<FieldDescription.Token>emptyList(),
Collections.<String, Object>emptyMap(),
Collections.singletonList(new MethodDescription.Token("foo",
Opcodes.ACC_BRIDGE,
TypeDescription.Generic.VOID,

0 comments on commit 2ca57be

Please sign in to comment.