Problem with IllegalAccessException

So I’m working on making a game with plugin support, and today I encountered a problem. The game uses two .jar file, VillagePlus.jar which is the game it self, while VillageContent.jar is the plugin I’m testing on.

This is the message I get when I try to run one of my test plugins

[quote]java.lang.IllegalAccessException: Class Village.Plugin can not access a member of class villagecontent.VillageContent$1 with modifiers “public”
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:101)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:295)
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:287)
at java.lang.reflect.Method.invoke(Method.java:476)
at Village.Plugin.runMethod(Plugin.java:150)
at Village.Screen.lambda$render$5(Screen.java:118)
at Village.Screen$$Lambda$12/286652023.accept(Unknown Source)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1359)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at Village.Screen.render(Screen.java:118)
at Yemto.screen.ScreenFragment.render(ScreenFragment.java:123)
at Village.Game.render(Game.java:190)
at Village.Game.run(Game.java:160)
at java.lang.Thread.run(Thread.java:745)
[/quote]
Here is the plugin

package villagecontent;

import Village.Game;
import Village.Plugins.*;
import Village.Plugins.TriggerEvent.RenderEvent;
import java.awt.*;

public class VillageContent implements VillagePlugin, VillageListener {
    
    @Override
    public void onEnable(){
       Game.registerListener(new VillageListener(){
           @VillageEvent
            public void onRenderEvent(RenderEvent event){
                Graphics2D g2 = event.getGraphics2D();
                g2.setColor(Color.WHITE);
                g2.drawString("Testing mod", 700, 700);
            }
       }, this);
    }

    @Override
    public void onDisable() {
        
    }
}

This is just a simple method to save the Listener into a HashSet inside the plugin object.

public static void registerListener(VillageListener listener, VillagePlugin plugin){
        Game.plugins.stream().filter(p -> p.getMain().equals(plugin)).forEach(p -> p.addListener(listener));
    }

My run method method

protected void runMethod(String method, Class argumentClass, Object... object){
        if(!isEnabled())
            return;
        try{
            for(VillageListener l : listeners){
                if(l == null)
                    continue;
                Method m = argumentClass != null ? l.getClass().getMethod(method, argumentClass) : l.getClass().getMethod(method);
                if(m.isAnnotationPresent(VillageEvent.class)){
                    m.invoke(l, object);
                }
            }
        }catch(NoSuchMethodException | NoClassDefFoundError e){
            //These aren't important as long as they don't cause significant performance issues.
        }catch(SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e){
            e.printStackTrace();
            enable = false;
        }
    }

I have no real idea what to do, the only thing I can think of is I somehow need to load the villagecontent.VillageContent$1 into the game.

I finally manage to solve the issue, I just added m.setAccessible(true); to the function

protected void runMethod(String method, Class argumentClass, Object... object){
        if(!isEnabled())
            return;
        try{
            for(VillageListener l : listeners){
                if(l == null)
                    continue;
                Method m = argumentClass != null ? l.getClass().getMethod(method, argumentClass) : l.getClass().getMethod(method);
                m.setAccessible(true);
                if(m.isAnnotationPresent(VillageEvent.class))
                    m.invoke(l, object);
            }
        }catch(NoSuchMethodException | NoClassDefFoundError e){
            //These aren't important as long as they don't cause significant performance issues.
        }catch(SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e){
            e.printStackTrace();
            enable = false;
        }
    }

You are calling a public method on a private (anonymous inner) class. This is obviously not (by default) allowed, forcing you to (indeed) acquire access through Method.setAccessible(true).

It might work if you called clazz.getMethod(…) – where clazz would refer to the interface declaring the public method (if any).