Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the skills of Java8 dynamic agent

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the skills of Java8 dynamic agent". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Dynamic proxy (Dynamic proxies) is a feature introduced by Java 1.3.It is widely used in remote calls of J2EE. Given an abstract interface and a concrete implementation of this interface, you can make remote calls to this interface (for example, across JVM) by creating two additional classes. First, the corresponding interface is implemented on the source JVM, and the call details are serialized and transmitted over the network. Then, on the target JVM, get the details of the serialized call and assign it to the concrete class to call.

Without dynamic proxies and reflections, developers have to provide two classes for each remote interface. A dynamic proxy is a class generated at run time that implements one or more interfaces, and each method call in the interface is automatically converted to a method call provided by java.runtime.InvocationHandler:

Public interface InvocationHandler {Object invoke (Object proxy, Method method, Object [] args) throws Throwable;}

InvocationHandler decides how to handle the call and how to use valid information about the method at run time, including annotations, parameter types, and method return types. This allows you to implement a general logic to define the distribution of method calls. Once you have written an InvocationHandler, you can call the handler of the proxy class to complete the methods in all interfaces instead of writing a separate implementation for each interface.

Remote calls have become less popular in recent years because developers need to understand the essential differences in semantics and failure modes between method call distribution and web request delivery, but dynamic proxies remain in the language. In this article, I will discuss the role of dynamic proxies in other aspects. In the next article, we will discuss new implementation techniques for dynamic proxies, which are the result of the introduction of lambda expressions and default methods in Java 8.

Magic matcher

Over the years, I have been using a "Magic" object so that I can write concise streaming tests. I define an interface for "magic" and then use a dynamic proxy to achieve the target behavior. In particular, "magic builders" is used to generate test values during testing, and then "magic matchers" is used to express the results of asserted property tests. We only focus on matchers here.

We have a Person support class, which is a typical bean-- member variable that is private and exposed through the getter and setter methods.

Public class Person {private String name; private int age; / / insert getters and setters here}

Using a simple Hamcrest class, we have two ways to assert an instance of this class. One way is to extract each value separately and assert separately.

AssertThat (person.getName (), containsString ("Smith")); assertThat (person.getAge (), greaterThan (30))

Another way is to use the allOf and hasProperty methods to match the object as a whole by a set of expected values.

AssertThat (person, allOf (hasProperty ("name", containsString ("Smith")), hasProperty ("age", greaterThan (30))

This works well, but it doesn't help Hamcrest to describe overall matches and mismatches.

Expected: (hasProperty ("name", a string containing "Putey") and hasProperty ("age", a value greater than)) but: hasProperty ("age", a value greater than) property 'age' was less than

HasProperty matching is also very weak in type consistency detection: we can write hasProperty ("age", containsString ("Smith")) so that type checking is not rejected.

What we really want is a streaming API that can be used like this:

AssertThat (person, aPerson () .withName ("Arthur Putey") .withAge (greaterThan (43)

And can report wrong matches well and easily:

Expected: name: a string containing "Putey" age: a value greater than but: age: was less than

It's easy to write a custom matcher for the above functions, but you have to write it tediously many times. Fortunately, dynamic agents can help us solve the problem. First, we define a streaming interface that contains the following methods:

Interface PersonMatcher extends Matcher {PersonMatcher withName (String expected); PersonMatcher withName (Matcher

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report