Home » Tag Archives: Reflection (page 3)

Tag Archives: Reflection

Processing Java Annotations Using Reflection

In my previous article covering Java Annotations, I outlined a recent use case and provided you with some examples of custom annotations and how they might be used. In this article, I’m going to take that a step further and give you a few examples of custom annotations and how you would process these custom annotations using the Java Reflection API. ...

Read More »

Using Reflection for Testing

I am working on a presentation about the ‘Single Responsibility Principle’, based on my previous post. It take most of my time. In the meantime, I want to share a sample code of how I use to test inner fields in my classes. I am doing it for a special case of testing, which is more of an integration test. ...

Read More »

Implementing dynamic proxies – a comparison

Sometimes there is the need to intercept certain method calls in order to execute your own logic everytime the intercepted method is called. If you are not within in Java EE’s CDI world and don’t want to use AOP frameworks like aspectj, you have a simple and similar effective alternative. Since version 1.5 the JDK comes with the class java.lang.reflect.Proxy ...

Read More »

Java Reflection Tutorial

In this tutorial, I mainly write some examples to introduce what Java reflection can do. Hopefully, it can give you an overview of this concept. Please leave your comment for suggestions. What is Reflection? In brief, reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime. This concept is sometimes ...

Read More »

Java’s Reflection API

If you have ever asked yourself questions like these: – “How do I invoke a method, having only it’s name in a String?” – “How do I list all the attributes in a class dynamically?” – “How do I write a method that resets the state of any given object to default values?” Then you have probably already heared of ...

Read More »

Duck typing in Java ? Well, not exactly

According to Wikipedia duck typing is: style of dynamic typing in which an object’s methods and properties determine the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface In simplier words When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call ...

Read More »

Creating a Java Dynamic Proxy

Java Dynamic proxy mechanism provides an interesting way to create proxy instances. The steps to create a dynamic proxy is a little tedious though, consider a proxy to be used for auditing the time taken for a method call for a service instance – public interface InventoryService { public Inventory create(Inventory inventory); public List<Inventory> list(); public Inventory findByVin(String vin); public ...

Read More »

Calling private Java methods publicly?

We Java developers, known 4 access modifiers in Java: private, protected, public, and package. Well, except for the private, the last three, can be called from outside of the class by inheritance, same package or from the instance. Now, the common question, can private be called publicly (from outside class)? well the answer is NO and YES. No when you ...

Read More »