@Override public Size apply( MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) { StackManipulation thisReference = MethodVariableAccess.REFERENCE.loadOffset(0); FieldList<?> fieldList = instrumentedType.getDeclaredFields(); StackManipulation[] fieldLoading = new StackManipulation[fieldList.size()]; int index = 0; for (FieldDescription fieldDescription : fieldList) { fieldLoading[index] = new StackManipulation.Compound( thisReference, MethodVariableAccess.forType(fieldDescription.getType().asErasure()) .loadOffset(instrumentedMethod.getParameters().get(index).getOffset()), FieldAccess.forField(fieldDescription).putter()); index++; } StackManipulation.Size stackSize = new StackManipulation.Compound( thisReference, MethodInvocation.invoke(ConstructorCall.INSTANCE.objectTypeDefaultConstructor), new StackManipulation.Compound(fieldLoading), MethodReturn.VOID) .apply(methodVisitor, implementationContext); return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize()); }
/** * Returns a method handle for a getter of the given field. * * @param fieldDescription The field to represent. * @return A method handle for a getter of the given field. */ public static MethodHandle ofSetter(FieldDescription fieldDescription) { return new MethodHandle( HandleType.ofSetter(fieldDescription), fieldDescription.getDeclaringType().asRawType(), fieldDescription.getInternalName(), TypeDescription.VOID, Collections.singletonList(fieldDescription.getType().asRawType())); }
/** * Returns a method handle for a setter of the given field. * * @param fieldDescription The field to represent. * @return A method handle for a setter of the given field. */ public static MethodHandle ofGetter(FieldDescription fieldDescription) { return new MethodHandle( HandleType.ofGetter(fieldDescription), fieldDescription.getDeclaringType().asRawType(), fieldDescription.getInternalName(), fieldDescription.getType().asRawType(), Collections.<TypeDescription>emptyList()); }
@Test public void testGenericField() throws Exception { DynamicType.Unloaded<?> unloaded = new ByteBuddy().redefine(GenericField.class).make(); Class<?> type = unloaded .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); FieldDescription createdField = new FieldDescription.ForLoadedField(type.getDeclaredField(FOO)); FieldDescription originalField = new FieldDescription.ForLoadedField(GenericField.class.getDeclaredField(FOO)); assertThat(createdField.getType(), is(originalField.getType())); }
@Override public boolean equals(Object other) { if (this == other) return true; if (!(other instanceof Resolved)) return false; Resolved resolved = (Resolved) other; return fieldDescription.equals(resolved.fieldDescription); }
@Test public void testNoMatch() throws Exception { when(fieldDescription.asToken(ElementMatchers.is(instrumentedType))).thenReturn(otherToken); assertThat( new LatentMatcher.ForFieldToken(token).resolve(instrumentedType).matches(fieldDescription), is(false)); }
@Override public int hashCode() { return fieldDescription.hashCode(); }
/** * Extracts a handle type for a setter of the given field. * * @param fieldDescription The field for which to create a setter handle. * @return The corresponding handle type. */ protected static HandleType ofSetter(FieldDescription fieldDescription) { return fieldDescription.isStatic() ? PUT_STATIC_FIELD : PUT_FIELD; }
/** * Returns a method type for a getter of the given field. * * @param fieldDescription The field to extract a getter type for. * @return The type of a getter for the given field. */ public static MethodType ofGetter(FieldDescription fieldDescription) { return new MethodType( fieldDescription.getType().asRawType(), Collections.<TypeDescription>emptyList()); }
/** * Returns a method type for a setter of the given field. * * @param fieldDescription The field to extract a setter type for. * @return The type of a setter for the given field. */ public static MethodType ofSetter(FieldDescription fieldDescription) { return new MethodType( TypeDescription.VOID, Collections.singletonList(fieldDescription.getType().asRawType())); }