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

How to understand java reflect

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

What this article shares with you is about how to understand java reflect. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet ()

Class selectorImplClass =

Class.forName ("sun.nio.ch.SelectorImpl", false, ClassLoader.getSystemClassLoader ())

/ / Ensure the current selector implementation is what we can instrument.

If (! selectorImplClass.isAssignableFrom (selector.getClass () {

Return selector

}

Field selectedKeysField = selectorImplClass.getDeclaredField ("selectedKeys")

Field publicSelectedKeysField = selectorImplClass.getDeclaredField ("publicSelectedKeys")

SelectedKeysField.setAccessible (true)

PublicSelectedKeysField.setAccessible (true)

SelectedKeysField.set (selector, selectedKeySet)

PublicSelectedKeysField.set (selector, selectedKeySet)

SelectedKeys = selectedKeySet

Take a look at the first line, SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet (); what is this thing for? instead of the selectKeys field in the default implementation of selector, the implementation is very powerful. We'll see later.

Skip the unimportant part and go straight to this part.

Field selectedKeysField = selectorImplClass.getDeclaredField ("selectedKeys")

Field publicSelectedKeysField = selectorImplClass.getDeclaredField ("publicSelectedKeys")

Get 2 field in order to forget the value set in the object selector.

SelectedKeysField.setAccessible (true)

PublicSelectedKeysField.setAccessible (true)

SelectedKeysField.set (selector, selectedKeySet)

PublicSelectedKeysField.set (selector, selectedKeySet)

Then it is to set the value to access the attribute, and modify the two attributes of selector (selectedKeys and publicSelectedKeys), set the value to the selectedKeySet from the previous new, and then let selectedKeys = selectedKeySet. When selector executes select, it will modify the attribute value in selectedKeys.

Then take a look at what is really going on in SelectedSelectionKeySet:

Final class SelectedSelectionKeySet extends AbstractSet {

Private SelectionKey [] keysA

Private int keysASize

Private SelectionKey [] keysB

Private int keysBSize

Private boolean isA = true

SelectedSelectionKeySet () {

KeysA = new SelectionKey [1024]

KeysB = keysA.clone ()

}

@ Override

Public boolean add (SelectionKey o) {

If (o = = null) {

Return false

}

If (isA) {

Int size = keysASize

KeysA [size + +] = o

KeysASize = size

If (size = = keysA.length) {

DoubleCapacityA ()

}

} else {

Int size = keysBSize

KeysB [size + +] = o

KeysBSize = size

If (size = = keysB.length) {

DoubleCapacityB ()

}

}

Return true

}

Private void doubleCapacityA () {

SelectionKey [] newKeysA = new SelectionKey [keysA.length

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

Servers

Wechat

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

12
Report