0

I want to loop through the Fields of a class, like Class.getDeclaredFields() but now I want to check if one Field is a HashMap and if so it should remove one Value of it. How can I reach this? I am clueless, but trying

This is my code so far: (It makes use of the Spigot API)

@EventHandler public void onInvClose(InventoryCloseEvent e) { Player p = (Player) e.getPlayer(); List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(Core.core); for(RegisteredListener l : listeners) { Field[] fields = l.getClass().getDeclaredFields(); for(Field field : fields) { // Check if field is a HashMap if(field.getType().isAssignableFrom( HashMap.class )) { // Remove Player p from field } } } } 
2

2 Answers 2

1

var map = (Map<Player, YourValueObject>)field.get(l); Should work and then just remove by key with p. Though I cannot try that on my phone right now.

Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately var map is not valid Java.
It did not take the < > and what´s in it without formatting finding the right type of apostrophe is not that easy on phones. xD Also var is totally a thing in Java10.
But I could try Map<?,?> :P
Ok, I have overseen the var Java 10 construct. Thanks for pointing that out.
If you dont have java 10 just use the same as you did in the cast in place of var.
0

You can use field.get(objectInstance) to get particular field of particular instance and then remove specific entry from that map.

Detailed steps would be as follows:

Step 1: Take all the declared fields by calling getDeclaredFields()

Step 2: For each field returned compare the type with HashMap by using equals method.

Step 3: If field type matches with HashMap, then get the value by calling get(objectInstace)and remove the not required value from map.

Please refer the complete example below;

jshell> class Player{ ...> Object someObject; ...> HashMap<Integer,String> someMap; ...> { ...> someMap = new HashMap<>(); ...> someMap.put(1,"Player"); ...> } ...> } | modified class Player jshell> jshell> jshell> Player player = new Player(); player ==> Player@71423665 jshell> java.lang.reflect.Field[] fields = player.getClass().getDeclaredFields(); fields ==> Field[2] { java.lang.Object Player.someObject, ja ... l.HashMap Player.someMap } jshell> for(java.lang.reflect.Field field : fields) { ...> // Check if field is a HashMap ...> if(field.getType().equals( HashMap.class)) { ...> ...> HashMap<Integer,String> data = (HashMap<Integer,String>)(field.get(player)); ...> data.remove(1); ...> System.out.println("Data left in map is "+data.size()); ...> } ...> } Data left in map is 0 

2 Comments

Could you describe the get(objectInstance) method in more detail? So I can remember it better :P
@RicheeNektar I have shared the example as well in my answer. You can call this function from field variable and pass the instance of whom you want the value to be retrieved, so in your case, objectInstance will be l

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.