Changing a method of an instance from another class in java - java

Im trying to change the method of just one instance from another class. Is this possible, and if so how can I do it?
Im trying to rewrite the entire method durring runtime. I only want the method to be changed for one instance of the class, and all the other instance's methods should stay the same. I hope that clears up some confusion

No this is not possible.
Method definitions are stored by class, not instance (and are immutable anyway). One thing you can do is to store a callable object per instance and call that.

I think you need to provide more details and perhaps some sample code to get a sufficient answer. That being said, if class Foo extends class Bar, class Foo can override a method of class Bar. Is that what you're trying to do?

Related

Java - Creating a class to dynamically determine if user has access to the calling method

I have tried doing a search for this but I fear I may not be wording what I want to do very well.
Currently, we have about a hundred action classes in our application with each determining if a user has access to it. I would like to make a class that can figure out the calling method, what permissions are required for it, and if the user has those permissions. Unfortunately, I don't really know how to even get started with this as each class may have slightly different requirements.
I'm happy to add more explanation if needed but as I said, I'm not sure I'm wording what I'm trying to do very well so if anyone has a better way of putting it that gets me some google results or a link to a related question here that's already been answered, I know I'd appreciate it.
current permissions checks look like below. This is a simple implementation, there are usually multiple profile checks in one if block.
If (scc.getUser().getCurrentProfile().getSystemAdmin() != 1) {
logIllegalAccess(log);
break;
}
IMHO the most elegant solution would make use of annotation processing. The idea is that you would annotate action classes with a custom annotation, something like:
#RequiredPermission(Permissions.SYSADM)
class ActionA {
public ActionA newInstance() {
return new ActionA_Gen(new ActionA());
}
private ActionA() {...}
...
}
Action classes would have to have a newInstance() method to be used to create instances instead of calling new. The method would create an instance of a class by the same name with _Gen extension. This class would have one method for each method in the original action class, which would perform a permission check and call the corresponding method in the original class instance that was passed to its constructor.
The _Gen class would be generated by an annotation processor.
Note that by using reflection it might be possible to move the newInstance() method in a common superclass.

Make 2 classes share the same instance of an object in Java

I was wondering if anyone could help me. I am stuck with something that seems simple. Say I initialize a Student object with the values of name, age and address in one class. How can I share THAT INSTANCE with another class, eg enable methods in the second class to make changes to the instance, thereby affecting the first class etc.
I was thinking of using JFrames where a new frame would popup and a button would affect values on the first frame.
Thank you
mutators can help you there. or, the singleton pattern, if you want to go "hard-knox".
it's a pattern that allows only one single instance of a class in a jvm to exist.
just look at this
in java if you pass reference of an object to a method and do any changes to that object property, it will change the actual object properties(call by reference)
also if you want to have only single object of a class that needs to be shared across other classes create a singleton pattern

How to search for instances of a class using eclipse?

I want to find instances of a class and modify some of the properties of one of the instance. But i cannot find a way to break in a block of code that references that class, so that it may appear in the variables view. What i'm thinking is to pause the jvm and the search for instances of MyClass but i can't find a tool to do this. Someone got an idea ?
I would open the call-hierarchie of the constructor of this class. Mark the constructor, left click, call hierarchie. That will give you a list wherever the object gets instantiated.

How to instantiate a class that has a constructor requiring an interface object

I am trying to use the Interactive Brokers Java API to see if I can do some algorithmic trading (on paper initially). I want to call a method called ReqMktDepth() which is in a class called EClientSocket.
The EClientSocket constructor requires an object of type AnyWrapper to be passed, and AnyWrapper is an interface not a concrete class. In theory how do I go about passing an AnyWrapper class to the EClientSocket constructor.
You need to create a class that implements AnyWrapper (using the "implements" keyword) and then you must provide the definitions for any methods defined by that interface.
Here's one simple tutorial:
http://www.uweb.ucsb.edu/~cdecuir/Polymorphism.html
You can either create your own class which implements AnyWrapper interface as Bobby suggests. or Use any other class(present in the library/jar/namespace) which already extends from AnyWrapper interface like the EWrapper, class which already has an implementation of AnyWrapper.
see -> http://www.interactivebrokers.com/php/apiUsersGuide/apiguide/java/eclientsocket.htm
You should probably use some class in that API you use which implements the AnyWrapper interface. You could have a look into the JavaDoc of that API or use your IDE's features (something like show type hierarchy) to find out which classes implement AnyWrapper, and pass one of them.
Several other answers have pointed out that you can create an instance of AnyWrapper by either implementing it yourself or by finding an existing class and passing in an instance of that class.
However it seems to me that what you are doing is not likely to succeed. You are trying to call a method whose argument is completely unknown to you. You need to read the documentation about that method and find out what the AnyWrapper is for and how it will be used. Maybe there just needs to be something provided, but maybe AnyWrapper has some responsibility that the EClientSocket needs.
This kind of programming by trial and error can lead to some serious problems down the road. For one thing, certain methods are not safe to call unless other safeguards are taken. Certain methods have major performance or security implications. In this case I think you really need to find out what it is you're trying to do before you figure out how to do it.

In Java, given an object, is it possible to override one of the methods?

I have an object of class A. I want to override one of the methods of that class. Can this be done?
More specifically, I have an object that is being injected into a field. I need to override one of the methods, before I can use it.
I am trying to see if Reflection could help solve the problem. Note that the method that I am trying override does not dependent on private variables of that class.
Look into Dynamic Proxy classes.
Yes.
Overriding means writing a new class, compiling it, changing the injection to use the new class, and packaging it with the rest of your app. Of course it can be done, but I don't see why you'd want reflection.
If you want this to be a dynamic thing, you're talking about aspect-oriented programming.
Assuming you are being given the object and so cannot subclass it: You can write a proxy. Forward on all the methods as is with the exception of the one you want to override. Of course no other reference to that original object will use the proxy. In particular if the object itself internally calls methods on itself then that will not use the "overridden" method.
Alternatively you could rewrite the code that calls the method, or modifies the implementing class, using AOP-style hacks.
Probably you want to have a careful think about your design.
CGLIB should be able to help you to achieve what you're trying to do. Check out Enhancer class.

Categories

Resources