0

I am making a game engine and I'm currently working on the component-part of the program. I want the user to be able to provide a special render method to the components so therefore I am using reflection and some other stuff. As the title suggests, this throws a ClassCastException and I can't figure out why. Here's the code:

public class LComponent {
    public Vector pos, size;
    private Class renderClass = getClass();
    private Method renderMethod;

    public LComponent(Vector pos, Vector size) {
        try {
            renderMethod = renderClass.getDeclaredMethod("defaultRender",
                    Graphics.class);
            renderMethod.setAccessible(true);
        } catch (NoSuchMethodException | SecurityException e) {
            e.printStackTrace();
        }
    }

    public void render(Graphics g) {
        try {
            renderMethod.invoke(renderClass, g);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void defaultRender(Graphics g) {
        g.drawRect((int) pos.getX(), (int) pos.getY(), (int) size.getX(),
                (int) size.getY());
    }
}
4
  • can you also please attach the stacktrace? Commented May 8, 2015 at 11:20
  • Do you have a debugger? This is easy to fix with the right information. Commented May 8, 2015 at 11:21
  • What are you trying to achieve with this, that can't be achieved with simple overriding of methods? Commented May 8, 2015 at 11:33
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Commented May 8, 2015 at 11:54

1 Answer 1

1

instead of

renderMethod.invoke(renderClass);

you need

renderMethod.invoke(this, g);

Although you could write all this without refection much simpler.


I want the class to be class to be customizable

I suggest you use an interface

interface Renderable {
    void render(Graphics g);
}

public class LComponent implements Renderable {
    // can be anything which implements Renderable
    final Renderable renderable; // initialise in the constructor

    public void render(Graphics g) {
         renderable.redner(g);
    }

}

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

3 Comments

But I want the class to be class to be customizable
@Loovjo You have to have an instance to call the method on. If you make the instance implement an interface, you can call this method on any instance via the interface without reflection. Generally speaking, you don't need reflection unless you are calling code you don't have control over.
@Loovjo Yes, that is why I gave an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.