Java Reflection
In a project I was confronted with a reason to use Java Reflection. What I found was a way to dynamically scale a class to find out its variables, methods, etc. During this process fortunately we changed gears and moved away from the design which was forcing this way of thinking. I have, ever since, been looking into it more and more and trying to discover why it isn't more commonly used. Aside from the inherent complexity and detracting from readability I found a list of "draw backs" to using reflection which I will paste below:
http://java.sun.com/docs/books/tutorial/reflect/
Drawbacks of Reflection
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.
- Performance Overhead
- Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
- Security Restrictions
- Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.
- Exposure of Internals
- Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing
privatefields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.
Annotations
After starting my new job I have been working alot with Java Annotations (EJB) so below is a quick reference of the 4 Common Data ones. This information has been taken partly from the Java Documentation and from my own Experience using both JPA and Hibernate. Each of the examples below deals with 2 Classes which we will call class_1 and class_2 affectionately.
@OneToMany -> @ManyToOne
A class_1 can have many class_2
@OneToMany(targetEntity=com.class_2.class, cascade=ALL, fetch=FetchType.EAGER)
@JoinColumn(name="class_2_id")
private Set<class_2> c2s = new HashSet();<flip side>
@ManyToOne(targetEntity=com.class_1.class, cascade=ALL, fetch=FetchType.EAGER)
@JoinColumn(name="class_2_id", updatable=false, insertable=false)
private class_1 c1;
@OneToOne
A class_1 can have one class_2
private class_2 c2;
<flip side>
@OneToOne (targetEntity=struts.hibernate.model.class_1.class, fetch=FetchType.EAGER, mappedBy="c2")
private class_1 c1;
@ManyToMany
A class_1 can have many class_2 and a class_2 can have many class_1
*Note - This configuration uses a join table
@ManyToMany
@JoinTable(name="class_1_to_class_2"
joinColumns=
@JoinColumn(name="class_1_id", referencedColumnName="ID"),
inverseJoinColumns=
@JoinColumn(name="class_2_id", referencedColumnName="ID"))
private Set<class_2> c2s = new HashSet();<flip side>
@ManyToMany
@JoinTable(name="class_1_to_class_2"
joinColumns=
@JoinColumn(name="class_2_id", referencedColumnName="ID"),
inverseJoinColumns=
@JoinColumn(name="class_1_id", referencedColumnName="ID"))
private Set<class_1> c1s = new HashSet();