Repository: raphw/byte-buddy
Issue: Using MethodCall onField with field locator pointing to different class fails

```java
DynamicType.Loaded<Object> loaded = new ByteBuddy()
        .subclass(Object.class)
        .invokable(isTypeInitializer())
        .intercept(MethodCall.invoke(named("println").and(takesArguments(Object.class)))
            .onField("out", new FieldLocator.ForExactType.Factory(TypeDescription.ForLoadedType.of(System.class))).with(""))
        .make()
        .load(Object.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
loaded.getLoaded().getDeclaredConstructor().newInstance();
```

The code above fails with the error below:

```
java.lang.VerifyError: Local variable table overflow
Exception Details:
  Location:
    net/bytebuddy/renamed/java/lang/Object$ByteBuddy$XtBtrrHJ.<clinit>()V @0: aload_0
  Reason:
    Local index 0 is invalid
  Bytecode:
    0x0000000: 2ab2 000f 1211 b600 17b1     
```

Relevant bytecode:
```
  static <clinit>()V
    ALOAD 0
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    LDC ""
    INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/Object;)V
    RETURN
    MAXSTACK = 3
    MAXLOCALS = 0
}
```


Changed files:
byte-buddy-dep/src/test/java/net/bytebuddy/implementation/MethodCallTest.java, byte-buddy-dep/src/main/java/net/bytebuddy/implementation/MethodCall.java

diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/MethodCall.java b/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/MethodCall.java
index 9b06dc57a6..6c241dc565 100644
--- a/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/MethodCall.java
+++ b/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/MethodCall.java
@@ -1770,9 +1770,10 @@ public class MethodCall implements Implementation.Composable {
                 if (!stackManipulation.isValid()) {
                     throw new IllegalStateException("Cannot invoke " + invokedMethod + " on " + resolution.getField());
                 }
-                return new StackManipulation.Compound(invokedMethod.isStatic()
+                return new StackManipulation.Compound(invokedMethod.isStatic() || resolution.getField().isStatic()
                         ? StackManipulation.Trivial.INSTANCE
-                        : MethodVariableAccess.loadThis(), FieldAccess.forField(resolution.getField()).read(), stackManipulation);
+                        : MethodVariableAccess.loadThis(),
+                        FieldAccess.forField(resolution.getField()).read(), stackManipulation);
             }
 
             @Override
diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/implementation/MethodCallTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/implementation/MethodCallTest.java
index a92ee06c90..eec5b5a728 100644
--- a/byte-buddy-dep/src/test/java/net/bytebuddy/implementation/MethodCallTest.java
+++ b/byte-buddy-dep/src/test/java/net/bytebuddy/implementation/MethodCallTest.java
@@ -7,6 +7,7 @@ import net.bytebuddy.description.modifier.Visibility;
 import net.bytebuddy.description.type.TypeDescription;
 import net.bytebuddy.dynamic.DynamicType;
 import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
+import net.bytebuddy.dynamic.scaffold.FieldLocator;
 import net.bytebuddy.implementation.bytecode.StackManipulation;
 import net.bytebuddy.implementation.bytecode.assign.Assigner;
 import net.bytebuddy.implementation.bytecode.constant.TextConstant;
@@ -276,6 +277,38 @@ public class MethodCallTest {
                 .make();
     }
 
+    @Test
+    public void testStaticOnStaticFieldFromAnotherClass() throws Exception {
+        DynamicType.Loaded<Object> loaded = new ByteBuddy()
+                .subclass(Object.class)
+                .invokable(isTypeInitializer())
+                .intercept(MethodCall.invoke(named("println").and(takesArguments(Object.class)))
+                    .onField("out", new FieldLocator.ForExactType.Factory(TypeDescription.ForLoadedType.of(System.class))).with(""))
+                .make()
+                .load(Object.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
+        assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
+        assertThat(loaded.getLoaded().getDeclaredMethods().length, is(0));
+        assertThat(loaded.getLoaded().getDeclaredFields().length, is(0));
+        Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
+        assertThat(instance, instanceOf(Object.class));
+    }
+
+    @Test
+    public void testOnStaticFieldFromAnotherClass() throws Exception {
+        DynamicType.Loaded<Object> loaded = new ByteBuddy()
+                .subclass(Object.class)
+                .defineMethod("foo", void.class)
+                .intercept(MethodCall.invoke(named("println").and(takesArguments(Object.class)))
+                    .onField("out", new FieldLocator.ForExactType.Factory(TypeDescription.ForLoadedType.of(System.class))).with("fooCall"))
+                .make()
+                .load(Object.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
+        assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
+        assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
+        assertThat(loaded.getLoaded().getDeclaredFields().length, is(0));
+        Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
+        assertThat(instance, instanceOf(Object.class));
+    }
+
     @Test
     public void testOnFieldUsingMatcher() throws Exception {
         DynamicType.Loaded<MethodCallOnField> loaded = new ByteBuddy()