使用Bytebuddy拦截setter

时间:2017-11-03 20:40:14

标签: java byte-buddy

让我有一个像这样的界面:

public interface User extends Element {

    String getName();

    String getPassword();

}

和类似的实现类:

public class BaseUser implements User {

    @Override
    public String getId() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("Set name to " + name);
    }

    public void setPassword(String password) {
        this.password = password;
    }

    private String id;
    private String name;
    private String password;

}

现在我想使用bytebuddy创建一个拦截器/代理,它将调用捕获到setter,存储更改的值并调用实际方法。

最后,我想“询问”被叫setter的拦截器/代理以及更改的值。

我也考虑了教程,但到目前为止我找不到有效的解决方案。也许有人可以帮助我。

这是拦截器:

public class GenericInterceptor implements InvocationHandler {

    @Override
    @RuntimeType
    public Object invoke(@This Object proxy, @Origin Method method, @AllArguments Object[] args) throws Throwable {
        if (isSetter(method, args)) {
            intercept(proxy, method, args);
        }
        return method.invoke(proxy, args);
    }

}

这是我目前的'测试'代码:

public static void main(String[] args) {
    final ByteBuddy bb = new ByteBuddy();
    final GenericInterceptor interceptor = new GenericInterceptor();

    bb.subclass(BaseUser.class)
            .method(isDeclaredBy(BaseUser.class).and(isSetter()))
            .intercept(MethodDelegation.to(interceptor))
            .make()
            .load(BaseUser.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);

    final BaseUser user = new BaseUser();
    user.setName("my name");
}

编辑:

public interface Element {
    String getId();
}

public class GenericInterceptor<T extends Element> {

    public GenericInterceptor(Class<T> type) {
        this.type = type;
    }

    public Map<String, Object> getChanges(T obj) {
        final String id = obj.getId();
        return changes.get(id);
    }

    @RuntimeType
    public void invoke(@This T proxy, @Origin Method method, @AllArguments Object[] args) throws Throwable {
        System.out.println("invoke " + method.getName() + " " + Arrays.toString(args));

        intercept(proxy, method, args);
    }

    private Object getCurrentValue(T proxy, final Field field) {
        try {
            return field.get(proxy);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            return null;
        }
    }

    private Field getSetterField(Method setter) {
        final String setterName = setter.getName();

        Field f = assignedFields.get(setterName);
        if (f != null) return f;

        final String fieldName = Character.toLowerCase(setterName.charAt(3)) + setterName.substring(4);
        try {
            f = type.getDeclaredField(fieldName);
            if (f == null) return null;
            f.setAccessible(true);
            assignedFields.put(setterName, f);
            return f;
        } catch (NoSuchFieldException | SecurityException e) {
            return null;
        }
    }

    private void intercept(T proxy, Method setter, Object[] args) {
        final Field field = getSetterField(setter);
        if (field == null)
            return;

        final Object currentValue = getCurrentValue(proxy, field);
        final Object newValue = args[0];

        System.out.println("Set from " + currentValue + " to " + newValue);

        final String id = proxy.getId();
        Map<String, Object> changeMap = changes.get(id);
        if (changeMap == null) {
            changeMap = new HashMap<>();
        }
        changeMap.put(field.getName(), currentValue);
        changes.put(id, changeMap);
    }

    private final Map<String, Field> assignedFields = new HashMap<>();
    private final Map<String, Map<String, Object>> changes = new LinkedHashMap<>();
    private final Class<T> type;

}

1 个答案:

答案 0 :(得分:2)

您可以使用 MethodDelegation.to(...)。andThen(SuperMethodCall.INSTANCE)调用orignal方法。

public class ByteBuddyTest {

  public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, InstantiationException {
      GenericInterceptor interceptor = new GenericInterceptor ();

      Class<?> clazz = new ByteBuddy()
        .subclass(BaseUser.class)
        .method(ElementMatchers.isDeclaredBy(BaseUser.class).and(ElementMatchers.isSetter()))
            .intercept(SuperMethodCall.INSTANCE.andThen(MethodDelegation.to(interceptor))))
        .make()
        .load(ByteBuddyTest.class.getClassLoader())
        .getLoaded();

      BaseUser user1 = (BaseUser) clazz.getConstructors()[0].newInstance();
      BaseUser user2 = (BaseUser) clazz.getConstructors()[0].newInstance();
      user1.setName("user1");
      user1.setPassword("password1");
      user2.setName("user2");
      user2.setPassword("password2");

      System.out.println(interceptor.getInterceptedValue("user1", "name"));
      System.out.println(interceptor.getInterceptedValue("user1", "password"));
      System.out.println(interceptor.getInterceptedValue("user2", "name"));
      System.out.println(interceptor.getInterceptedValue("user2", "password"));

      user1.setPassword("password2");
      user1.setPassword("password3");
  }

  public static class GenericInterceptor {
      private Map<String, Object> interceptedValuesMap = new HashMap();

      public void set(String obj, @This User user, @Origin Method setter) {
        // assume that user name is unique so we can use it as a key in values map.
        // or define equals/hashcode in GenericUser object and use it as a key directly
        String setterName = setter.getName();
        String propertyName = setterName.substring(3, setterName.length()).toLowerCase();
        String key = user.getName() + "_" + propertyName;

        System.out.println("Setting " + propertyName + " to " + obj);
        System.out.println("Previous value " + interceptedValuesMap.get(key));

        interceptedValuesMap.put(key, obj);
      }

    public Object getInterceptedValue(String userName, String fieldName) {
        return interceptedValuesMap.get(userName + "_" + fieldName);
    }
  }

  public static interface User {

      String getName();

      String getPassword();

  }

  public static class BaseUser implements User {

      @Override
      public String getName() {
          return name;
      }

      @Override
      public String getPassword() {
          return password;
      }

      public void setName(String name) {
          this.name = name;
      }

      public void setPassword(String password) {
          this.password = password;
      }

      private String name;
      private String password;

  }
}