Java proxy method invocation

How to «proxy» a method in Java

First off, I’m not sure how to best word my solution so if I seem to be babbling at times then please consider this. There is an interface in a library I wish to modify without touching the physical code,

public interface ProxiedPlayer < // .. other code public void setPermission(String permission, boolean state); >

I have written a third party library for handling permissions and having to hook into my API to edit permissions may be a step some developers do not want to take. So I ask that when setPermission is called is it possible to have it invoke my invoke the appropriate method in my library that will handle permission setting whilst ignoring the pre-programmed code or not? Here is the full interface I am attempting to proxy. I have looked into the Java Proxy class but it seems you need an instance of the object you’re trying to proxy in the first place. Given that the method can be called any time I do not believe this to be my solution but will happily stand corrected. I do not have control over instantiation of classes implementing the ProxiedPlayer interface. EDIT: Ignorant me, there several events that I can subscribe to where it is possible to get an instance of the player, would this be the appropriate place to attempt to proxy the method? One of these events is fired when a player joins the server and getting the instance of the player is possible. Would the Proxy code need to be called for every instance of the ProxiedPlayer interface or is it possible to simply proxy every invocation of the method in an easier way? My library is a plugin loaded after everything else that is essential has finished loading. Edit #2:

import net.md_5.bungee.api.connection.ProxiedPlayer; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class InvocationProxy implements InvocationHandler < @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable < ProxiedPlayer player = (ProxiedPlayer) proxy; if(method.getName().equals("setPermission")) < // Call my code here? >return method.invoke(player, args); > > 

Would something along the lines of what I have above work or am I barking up the wrong tree entirely?

Источник

Java proxy method invocation

Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods. To create a proxy for some interface Foo :

InvocationHandler handler = new MyInvocationHandler(. ); Class proxyClass = Proxy.getProxyClass(Foo.class.getClassLoader(), Foo.class); Foo f = (Foo) proxyClass.getConstructor(InvocationHandler.class). newInstance(handler);
Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(), new Class[] < Foo.class >, handler);
  • Proxy classes are public, final, and not abstract if all proxy interfaces are public.
  • Proxy classes are non-public, final, and not abstract if any of the proxy interfaces is non-public.
  • The unqualified name of a proxy class is unspecified. The space of class names that begin with the string «$Proxy» should be, however, reserved for proxy classes.
  • A proxy class extends java.lang.reflect.Proxy .
  • A proxy class implements exactly the interfaces specified at its creation, in the same order.
  • If a proxy class implements a non-public interface, then it will be defined in the same package as that interface. Otherwise, the package of a proxy class is also unspecified. Note that package sealing will not prevent a proxy class from being successfully defined in a particular package at runtime, and neither will classes already defined by the same class loader and the same package with particular signers.
  • Since a proxy class implements all of the interfaces specified at its creation, invoking getInterfaces on its Class object will return an array containing the same list of interfaces (in the order specified at its creation), invoking getMethods on its Class object will return an array of Method objects that include all of the methods in those interfaces, and invoking getMethod will find methods in the proxy interfaces as would be expected.
  • The Proxy.isProxyClass method will return true if it is passed a proxy class— a class returned by Proxy.getProxyClass or the class of an object returned by Proxy.newProxyInstance — and false otherwise.
  • The java.security.ProtectionDomain of a proxy class is the same as that of system classes loaded by the bootstrap class loader, such as java.lang.Object , because the code for a proxy class is generated by trusted system code. This protection domain will typically be granted java.security.AllPermission .
  • Each proxy class has one public constructor that takes one argument, an implementation of the interface InvocationHandler , to set the invocation handler for a proxy instance. Rather than having to use the reflection API to access the public constructor, a proxy instance can be also be created by calling the Proxy.newProxyInstance method, which combines the actions of calling Proxy.getProxyClass with invoking the constructor with an invocation handler.
Читайте также:  Python 3 сокращенный for

Methods Duplicated in Multiple Proxy Interfaces

When two or more interfaces of a proxy class contain a method with the same name and parameter signature, the order of the proxy class’s interfaces becomes significant. When such a duplicate method is invoked on a proxy instance, the Method object passed to the invocation handler will not necessarily be the one whose declaring class is assignable from the reference type of the interface that the proxy’s method was invoked through. This limitation exists because the corresponding method implementation in the generated proxy class cannot determine which interface it was invoked through. Therefore, when a duplicate method is invoked on a proxy instance, the Method object for the method in the foremost interface that contains the method (either directly or inherited through a superinterface) in the proxy class’s list of interfaces is passed to the invocation handler’s invoke method, regardless of the reference type through which the method invocation occurred. If a proxy interface contains a method with the same name and parameter signature as the hashCode , equals , or toString methods of java.lang.Object , when such a method is invoked on a proxy instance, the Method object passed to the invocation handler will have java.lang.Object as its declaring class. In other words, the public, non-final methods of java.lang.Object logically precede all of the proxy interfaces for the determination of which Method object to pass to the invocation handler. Note also that when a duplicate method is dispatched to an invocation handler, the invoke method may only throw checked exception types that are assignable to one of the exception types in the throws clause of the method in all of the proxy interfaces that it can be invoked through. If the invoke method throws a checked exception that is not assignable to any of the exception types declared by the method in one of the proxy interfaces that it can be invoked through, then an unchecked UndeclaredThrowableException will be thrown by the invocation on the proxy instance. This restriction means that not all of the exception types returned by invoking getExceptionTypes on the Method object passed to the invoke method can necessarily be thrown successfully by the invoke method.

Читайте также:  Пример footer html css

Field Summary

Constructor Summary

Constructs a new Proxy instance from a subclass (typically, a dynamic proxy class) with the specified value for its invocation handler.

Method Summary

Returns the java.lang.Class object for a proxy class given a class loader and an array of interfaces.

Returns true if and only if the specified class was dynamically generated to be a proxy class using the getProxyClass method or the newProxyInstance method.

Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.

Источник

Динамический прокси Java: что это и как им пользоваться?

Ну что ж до Нового года и старта десятого потока «Разработчик Java» осталось совсем шуть-шуть. Так что у нас остался один открытый урок, который мы подготавливаем для публикации и сегодняшняя заметка, из которой вы узнаете о динамическом прокси Java: что это такое, когда и как его использовать в коде.

Прокси — это шаблон проектирования. Мы создаем и используем его для добавления и изменения функционала уже существующих классов. В таком случае, прокси-объект применяется вместо исходного. Обычно он использует тот же метод, что и оригинальный, и в Java прокси-классы расширяют исходные. Прокси может вызвать метод исходного объекта, так как у него есть дескриптор оригинала.

Таким образом, прокси-классы удобно реализуют многие вещи:

  • логирование старта и остановки метода;
  • дополнительную проверку аргументов;
  • имитацию поведения исходного класса;
  • реализацию отложенной инициализации затратных ресурсов;

Все это происходит без изменений оригинального кода класса. Полный список не ограничивается примерами выше, они лишь его малая часть.

На практике, прокси-класс напрямую не реализует функционал. Следуя принципу единственной ответственности, прокси-класс непосредственно выполняет только проксирование, а изменение поведения реализуется в обработчиках. При вызове прокси-объекта вместо исходного, сам прокси решает, вызвать ли оригинальный метод или какие-то обработчики. Обработчик может выполнить как собственную задачу, так и обратиться к оригинальному методу.

Читайте также:  Astra linux special edition java

Хоть шаблон прокси применяется не только для создания прокси-объекта и класса в среде выполнения, в Java это особенно интересная тема. В этой статье я фокусируюсь именно на таких прокси.

Это сложная тема, которая требует использования класса отражения, или манипулирования байт-кодом, или компиляции Java-кода, сгенерированного динамически. А может всего и сразу. Чтобы новый класс не был доступен в качестве байт-кода во время исполнения, потребуются сгенерированный байт-код и загрузчик классов для загрузки байт-кода. Для создания байт-кода, используйте cglib, bytebuddy или встроенный компилятор Java.

Важность разделения ответственностей, в нашем случае, становится ясна, стоит лишь подумать о прокси-классах и вызываемых ими обработчиках. Прокси-класс генерируется во время выполнения, но вызванные им обработчики могут быть добавлены в обычный исходный код и скомпилированы с остальной программой.

Как этим пользоваться в нашем коде?

Самое простое — использовать java.lang.reflect.Proxy , который является частью JDK. Этот класс может создать прокси-класс или напрямую его инстанс. Пользоваться прокси, встроенным в Java, очень просто. Все что нужно — реализовать java.lang.InvocationHandler , чтобы прокси-объект мог его вызывать. Интерфейс InvocationHandler крайне прост и содержит только один метод: invoke() . При его вызове, аргументы содержат проксируемый оригинальный объект, вызванный метод (как отражение объекта Method ) и массив объектов исходных аргументов. Фрагмент кода ниже демонстрирует применение:

package proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class JdkProxyDemo < interface If < void originalMethod(String s); >static class Original implements If < public void originalMethod(String s) < System.out.println(s); >> static class Handler implements InvocationHandler < private final If original; public Handler(If original) < this.original = original; >public Object invoke(Object proxy, Method method, Object[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException < System.out.println("BEFORE"); method.invoke(original, args); System.out.println("AFTER"); return null; >> public static void main(String[] args)< Original original = new Original(); Handler handler = new Handler(original); If f = (If) Proxy.newProxyInstance(If.class.getClassLoader(), new Class[] < If.class >, handler); f.originalMethod("Hallo"); > >

Для вызова оригинального метода исходного объекта, обработчику необходим доступ к нему. Что не предоставлено реализацией прокси Java. Вам понадобится самостоятельно передать аргумент инстансу обработчика в коде. (Обратите внимание на объект (обычно с названием proxy), который передается в качестве аргумента вызываемому обработчику. Это прокси-объект, который отражение Java генерирует динамически, а не тот объект, что мы хотим проксировать.) Таким образом, вы можете использовать как отдельные объекты-обработчики для каждого исходного класса, так и общий объект, который знает, как вызвать оригинальный объект, если для этого вообще есть какой-либо метод.

В особом случае, вы можете создать обработчик вызова и прокси интерфейса без оригинального объекта. Более того, класс для реализации интерфейса в исходном коде — не требуется. Его реализует динамически созданный прокси-класс.

Если же проксируемый класс не реализует интерфейс, стоит задуматься об использовании какой-либо иной реализации прокси.

Ждём ваши комментарии и вопросы. Как всегда или тут, или можно зайти к Виталию на день открытых дверей.

Источник

Оцените статью