Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to understand java reflect

Shulou Source: shulou.com Published: 2022-06-01 00:26:47 10月21日 Update

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

Tags: Attributes that is more knowledge articles parts practical important irrelevant one line things number memory fields objects work meetings efficiency arrays articles time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia Apple Docker MySQL