0

My previous post was not very clear, sorry for that. I will try to give a better example of what I am trying to do.

I have an Java application that will load .class files and runs them in a special enviroment (the Java app has built-in functions) Note: This is not a library.

That Java application will then display an applet, and I want to modify the variables in the applet.

The main class of the applet is called 'client'. The Java application will load the applet by creating an new instance of class 'client'.

I already got access to the 'client' class. the Java application will put the applet in a variable:

Applet client = (Applet) loadedClientClass.newInstance();

So I did this:

Class<?> class_client = client.getClass();

I can now read and set the fields but the 'client' class will call a funation of an other class, like this:

otherClass.someVoid(false);

And if I try something like:

class_client.getDeclaredMethod("otherClass.someVoid",boolean.class);

It will fail, saying that the function can not be found.

'otherClass' is the direct class name, it is not a reference to a new instance of the class as far as I know.

Is there any way to get 'otherClass.someVoid'?

3
  • this is not valid java code, what exactly are you trying to do? Commented Jul 14, 2011 at 8:20
  • Actually, I tried to make the class simple. This is not the actual class. (but it should behave the same) Commented Jul 14, 2011 at 8:26
  • you can not have code in the class body, it will not compile. show us more realistic example of what you're trying to do. Commented Jul 14, 2011 at 8:53

3 Answers 3

1

You're using getDeclaredMethod like a static method (expecting it to return methods from any class), but it only returns method from the class itself. Here's how you can call otherClass.someVoid(false).

Class<?> otherClass = Class.forName("com.xyz.OtherClass"); // Get the class
Method method = otherClass.getDeclaredMethod("someVoid", boolean.class);

// If the method is an Class (ie static) method, invoke it on the Class:
method.invoke(otherClass, false);

// If the method is an instance (ie non-static) method, invoke it on an instance of the Class:
Object otherInstance = otherClass.newInstance(); // Get an instance of other class - this approach assumes there is a default constructor
method.invoke(otherInstance, false);
Sign up to request clarification or add additional context in comments.

Comments

0

If the class isn't initialized, the var someInteger doesn't exist. It's a member variable, so it only exists inside of instances of the class. So, you can't change it since it's doesn't exist. Now, if you made it a static variable, then you could change it.

1 Comment

What if the class was initialised? Could I do it then?
0

Is there any way to change 'otherClass.someInteger' through the 'mainClass' class?

No.

But you can get it via OtherClass' class via Class.forName:

Class<?> theOtherClazz = Class.forName("OtherClass");

And then get the methods via theOtherClazz.getDeclaredMethod

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.