Is there a simple way through reflections to test if a Java interface has been proxied?
Use Proxy.isProxyClass()
Proxy.isProxyClass(yourInterface.getClass())
Look at the implementation: Check this.getClass().getName() for generic names or this.getClass().isAssignableFrom(Class<?>) if you have a specific proxy in mind.
Proxy.isProxyClass() might also help (depending on the method used for implementing the proxy.
Related
I've just upgraded some projects from Java 6 to Java 8 and one class was implementing Connection interface. Now Connection interface seems to have more methods but I don't want to implement all missing methods, for example: Connection.getSchema(). Connection.getConnectionTimeOut() and so on.
How I should deal with this issue due to the fact I don't want to make my class abstract or I should implement all the missing methods?
If the Connection interface does not implement Defaultmethods for the given new Methodes you will have to implement them. If they are not used in your Applicationcontext you might be able to just implement them empty. While that will be a quick fix for your problem i would not recommend that because maybe late some other class will have to use these Functions.
You don't have any choice but to implement them.
However, if your custom implementation of Connection is not designed to be general purpose, then you could get away with dummy implementations like this:
public String getSchema() {
throw new UnsupportedOperationException("Connection::getSchema");
}
And if you discover that you do need some of these methods, you can go back and implement them properly.
Making the class abstract won't help. That just puts the problem off to a subclass ... where you will need to implement the methods.
I want to listen on method calls in order to attach additional behavior dynamically around the call. I've already done it on JUnit methods with a custom annotation and runner. I'm trying to do it on a standard java application.
The main idea is to do:
#Override
public void beforeInvoke (Object self, Method m, Object[] args){
Object[] newargs = modifyArgs (args);
m.invoke (self, newargs);
}
It's just an abstract idea, I don't have any concrete example, but I'm curious if it's possible in java.
I've found some approaches:
java.lang.reflect.Proxy.newProxyInstance(...)
where a proxy is defined for an interface only (but not used to decorate concrete classes). It seems similar to injection pattern and it's a different concern.
Another approach here using a factory pattern with the ProxyFactory class. This other solution requires explicit calls to create() method to produce object proxies listening on method invocations. So, if you bypass it by using natural constructors of your classes, it's not working. It's very constraining if you must explicit a call to a factory each time you have to create an object.
There is a way to do it with transparency ?
Like Proxy.newProxyInstance() but working also on concrete classes ?
Thanks.
Well,this is commonly seen with Spring Framework and Aspect Oriented Programming. Since you delegate your constructor calls to Spring, it is quite easy for Spring to put a proxy in place to intercept calls to the actual objects.
As far as I can tell, the only way to intercept calls is to use a proxy. Either in the way you mentioned or using Spring and AOP.
I think cglib let you instrument concrete classes.
As far as I know there is no easy way to intercept method calls that are called on a concrete class.
As mentioned you could manipulate the bytecode during compilation (as Used in AOP) or at class loading time (as used from cglib).
Another product to instrument Classes would be jmockit (http://jmockit.org/). Usually I would use this special kind of black magic only in testing environments and not in an productive environment.
Another way you could go is Annotation Processing. It work's during compiling process. You have to write a Processor which will walk through your source code and generate source-code that contains the original code plus the enhanced method-calls you need.
Depending on how much source-code you have to enhance, this method might be a good idea, but in general it is a lot of work.
Here's a link (https://deors.wordpress.com/2011/10/08/annotation-processors/).
Despite usually it's used in combination with annotations, this is not a strict requirement.
For example I have proxy that is proxing interface CashManagment, toString of this proxy is somepackage.CashManagment:Stateless. I would like to know what real classes is being used by this proxy.
I think its EJB. CashManagment has more than one implementation.
As far as I know, java.lang.reflect.Proxy — I'm assuming that's what you're talking about — does not offer any way of discovering what the underlying proxied object is, or its class for that matter. It might be obtainable by using reflection, but that would be dependent on the runtime implementation and thus not a good idea.
If you're using Spring and its proxy mechanisms, then perhaps there is a way to obtain this.
You could introduce a function getType() in the interface, which returns the type of the implementation.
I'm looking for a solution to create an interface in runtime. I don't really know if this is possible in anyway.
Problem:
I've got a OSGi service which publishes a Map<String,String> where the key defines an action for this service. I want to publish this service directly as Hessianservice with spring but for this, I need to delcare an interface. Now I would like to create this interface at runtime.
It's possible to create interfaces dynamically for example by generating it with a bytecode manipulation library such as ASM. But it won't be possible to use that interface, because no code can be compiled against it (unless you generate dynamically also the code which uses it).
What is it that you are trying to do?
You can't really do that (unless you involve byte-code maniuplation/creation and I don't think that's the best path).
What good would a dynamically created interface do if you have nothing that could access that interface?
Or in other words: nothing can compile against a dynamically created interface (since it doesn't exist at compile-time, obviously). So who would be using it?
Picked the following answer from another question. The example actually writes a new class, so may be this will help you.
JDK6 has a Java compiler API. However, it's not necessarily very easy to use.
A quick google pulled up this example usage.
Interfaces and classes exist solely to help compilers find possible bugs. If you want to make this interface at runtime, you have no compiler, co it won't find you the bugs, so why do you need this interface?
In such a situation just publish implementation of some generic interface, which can look like:
interface GenericInterface {
Object invokeMethod(String name, Object... arguments);
}
That's the only interface you need, and you can create it at compile time! Only implementations of it you may need to create at runtime, eg with java.lang.reflect.Proxy
My code takes an interface as input but only excercises a couple of the interface's methods (often, just getters).
When testing the code, I'd love to define an anonymous inner class that returns the test data. But what do I do about all the other methods that the interface requires?
I could use my IDE to auto-generate a stub for the interface but that seems fairly code-heavy.
What is the easiest way to stub the two methods I care about and none of the methods I don't?
If you are using JUnit to test, use Mocks instead of stubs.
Read Martin Fowler's seminal article "Mocks Aren't Stubs"
I recommend the EasyMock framework, it works like a charm automatically Mocking your interface using reflection. It is a bit more advanced than the code samples in Fowler's article, especially when you use the unitils library to wrap EasyMock, so the syntax will be much simpler than that in the article. Also, if you don't have an interface, but you want to mock a concrete class, EasyMock has a class extension.
Check out JMock.
http://www.jmock.org/
Write an "Adapter Class" and overwrite only the methods you care.
class MyAdapter extends MyClass {
public void A() {
}
...
}
I believe the classical way is to make an abstract class with empty methods.
At least, that's how Sun did for MouseListener, creating MouseAdapter to ease the use of these events.
EasyMock or JMock are definitely the winners. I haven't used JMock, but I know with EasyMock you can setup the Mock object according to a testing script and it will return certain values in certain situations or points during your test. It's pretty easy to learn and get running, generally in less than an hour.