Reflection in inhertited classes - java

I use this method to remove Html code from my Strings in classes:
public void filterStrings() {
Field[] fields = this.getClass().getDeclaredFields();
if (fields == null) {
return;
}
for (Field f : fields) {
if (f.getType() == java.lang.String.class) {
try {
String value = (String) f.get(this);
f.set(this, methodToRemoveHtml(value));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
Works fine. Since I caught myself putting this method in many classes I use, I thought I'd let all those classes inherit from a BaseClass and implement the method only there.
But when I do this, I get a: java.lang.IllegalAccessException: access to field not allowed on every try.
Why is this happening and
How can I fix this?

I guess the fields are private, so they can only be accessed from code inside the class that contains them, and not a superclass.
You have to make them accessible by calling setAccessible(true); on them or making them public or protected.
for (Field f : fields) {
if (f.getType() == java.lang.String.class) {
try {
f.setAccessible(true); // make field accessible.
String value = (String) f.get(this);
// ...

May be you need to call:
f.setAccessible(true);

Related

What's the Java equivalent of this C# reflection method

I'm rewriting a framework originally done in C# into Java and this has me stumped. It's a method using reflection to return any property in a class that implements a particular interface using LINQ:
public List<TInterface> GetElementsOfType<TInterface>()
{
return GetType().GetProperties()
.Where(x => typeof(TInterface).IsAssignableFrom(x.PropertyType))
.Select(x => x.GetValue(this, null)).OfType<TInterface>()
.ToList();
}
I am trying to recreate in Java and so far have this. Not too sure if it's right though?:
public <T> List<?> GetElementsOfType(Class<T> klazz) {
Class<?> props = this.getClass();
List<T> elementsOfType = new ArrayList<>();
try {
Field fieldlist[] = props.getDeclaredFields();
for (Field aFieldlist : fieldlist) {
if (aFieldlist.getType().isAssignableFrom(klazz)){
elementsOfType.add((T)aFieldlist);
}
}
} catch (Exception e) {
return null;
}
return elementsOfType;
}
Update: Thanks everyone for your help. This seems to work for me although the unchecked cast causes a warning I had to suppress:
#SuppressWarnings("unchecked")
public <T> List<T> GetElementsOfType(Class<T> klazz) {
Class<?> props = this.getClass();
List<T> elementsOfType = new ArrayList<>();
try {
Field fieldlist[] = props.getDeclaredFields();
for (Field aFieldlist : fieldlist) {
if (klazz.isAssignableFrom(aFieldlist.getType())){
elementsOfType.add((T) aFieldlist.get(this));
}
}
} catch (Exception e) {
return elementsOfType;
}
return elementsOfType;
}
}
In Java, a property is not a field. A property is defined as a public non-static read method and, if it’s not a read-only property, a public non-static write method. An instance of a class with one or more such properties is known as a Java bean.
You can examine properties using the java.beans package, starting with Introspector:
try {
BeanInfo beanInfo = Introspector.getBeanInfo(props, Object.class);
for (PropertyDescriptor prop : beanInfo.getPropertyDescriptors()) {
if (klazz.isAssignableFrom(prop.getPropertyType())) {
elementsOfType.add(klazz.cast(prop.getReadMethod().invoke(this)));
}
}
} catch (IntrospectionException | ReflectiveOperationException e) {
throw new RuntimeException(e);
}
Because Java uses checked exceptions, you can’t easily use Streams to write LINQ-like code in this case. It’s possible, but unwieldy. And not worth the bother, considering the above code is fairly succinct.
(The reason for the Object.class in Introspector.getBeanInfo(klazz, Object.class) is that we don’t want properties of Object, namely the read-only class property of Object defined by Object.getClass().)

optimize java code and how to make it look good

I have a piece of code, it's always used but it looked very redundant , and what can I do for sidestepping redundancy.
if(CommonUtil.isNull(second.getProvince())) {
second.setProvince(first.getProvince());
}
if(CommonUtil.isNull(second.getCity())) {
second.setCity(first.getCity());
}
if(CommonUtil.isNull(second.getDistrict())) {
second.setDistrict(first.getDistrict());
}
if(CommonUtil.isNull(second.getAddress())) {
second.setAddress(first.getAddress());
}
........
You can write this method in your data classes and do null control of all fields with one line of code. My code advice is like below:
public boolean copyIfNull(Object o)
{
Class<?> clazz = this.getClass();
Field[] fields = clazz.getDeclaredFields();
for(Field field : fields)
{
try {
Object fieldValue = field.get(this);
if (fieldValue == null)
{
field.set(this, field.get(o));
return false;
}
}
catch (Exception e) {
System.err.println("Field value could not be obtained");
e.printStackTrace();
return false;
}
}
return true;
}
And you will call this method in main like that:
second.copyIfNull(first)
As your objects look like beans from afar, you might have a look at java.beans.Introspector and BeanInfo.
Roughly along the lines of:
BeanInfo bi = Introspector.getBeanInfo(MyObjectClass.class);
for(PropertyDescriptor p : bi.getPropertyDescriptors()) {
// perform null-check
// invoke read on source object via read method delivered by p.getReadMethod()
// write to target via method delivered by p.getWriteMethod()
}

How I can transfer atrributes values from a Objects to another

I want tranfers attributes values from a object that came from my Entity manager to a new object.
The return Object is always null
public class ReflectionUtil {
public static Object copyAttributesFromTo(Object a, Object b) throws IllegalArgumentException, IllegalAccessException {
Field[] fieldsFromFirstClass = a.getClass().getDeclaredFields();
Field[] fieldsFromSecondClass = b.getClass().getDeclaredFields();
for (Field currentFieldFromTheFirstClass : fieldsFromFirstClass) {
for (Field currentFieldFromTheSecondClass : fieldsFromSecondClass) {
String nameOfTheFirstField = currentFieldFromTheFirstClass.getName();
String nameOfTheSecondField = currentFieldFromTheSecondClass.getName();
if (!Modifier.isFinal(currentFieldFromTheFirstClass.getModifiers())) {//Dispensa os Final
if (!currentFieldFromTheFirstClass.isAnnotationPresent(Id.class)) {//Não sobescreve campo id
if (nameOfTheFirstField.equals(nameOfTheSecondField)) {
currentFieldFromTheFirstClass.setAccessible(true);
currentFieldFromTheSecondClass.setAccessible(true);
currentFieldFromTheSecondClass.get(b));
currentFieldFromTheFirstClass.set(a, currentFieldFromTheSecondClass.get(b));
}
}
}
}
}
return a;
}
}
In the Facade call I always have to put all the attribute values to new object
public void update(Profile object) {
dao.beginTransaction();
Profile persistedObject = dao.find(object.getId());
persistedObject.setName(object.getName());
dao.commitAndCloseTransaction();
}
So I think to create some like that
public void update(Profile object) {
dao.beginTransaction();
Profile persistedObject = dao.find(object.getId());
ReflectionUtil.copyAttributesFromTo(persistedObject , object);
dao.commitAndCloseTransaction();
}
really miss understood why u are using 2 loop? .. if the classes are same . u dont need to do it .. just do it in 1 loop .. and use fields get for obj which is holding data .. and use set for to set .. here is the more better way .. if the same object is required u can use generics .. and same object type will be required ( request return type )
public static <T> T copyAttributesFromTo(T value, T dataHolder) throws IllegalArgumentException, IllegalAccessException {
if (value == null || dataHolder == null) {
throw new IllegalArgumentException();
}
final Field[] fields = value.getClass().getDeclaredFields();
for (Field field : fields) {
if (!Modifier.isFinal(field.getModifiers())) {
field.setAccessible(true);
field.set(value, field.get(dataHolder));
}
}
return value;
}
The return Object is always null
It is impossible for the return object (i.e. whatever a contains when you return) to be null.
It is easy to see that the code does not change the reference a. There are no assignments to it in the method, so it cannot change.
The other possibility was that you called the method with a null value for a. But if you did that, the first line of the method calls a.getClass() and that will throw an NPE if a is null.
TL;DR - it is impossible.
So what does this mean?
Here are the most likely explanations:
You are mistaken that null is being returned. Perhaps the method is not being called? Perhaps, it is not returning?
Maybe you have misinterpreted the evidence in some other way. It is hard to know without seeing the code ... and the evidence.
Maybe you don't mean that the method is returning Object; i.e. I misunderstood the question. (Your problem description is pretty unambiguous though ...)
I make a change in the code and works to update for me
public static Object copyAttributesFromTo(Object a, Object b) throws IllegalArgumentException, IllegalAccessException {
Field[] fieldsFromFirstClass = a.getClass().getDeclaredFields();
Field[] fieldsFromSecondClass = b.getClass().getDeclaredFields();
//JSFMessageUtil.addMsgLog(JSFMessageUtil.matricula, ReflectionUtil.class.getCanonicalName(), "ReflectionUtil: Aqui");
for (Field currentFieldFromTheFirstClass : fieldsFromFirstClass) {
for (Field currentFieldFromTheSecondClass : fieldsFromSecondClass) {
Object nameOfTheFirstField = currentFieldFromTheFirstClass.getName();
Object nameOfTheSecondField = currentFieldFromTheSecondClass.getName();
if (!Modifier.isFinal(currentFieldFromTheFirstClass.getModifiers())) {//Dispensa os Final
//if (!currentFieldFromTheFirstClass.isAnnotationPresent(Id.class)) {//Não sobescreve campo id
if (nameOfTheFirstField.equals(nameOfTheSecondField)) {
currentFieldFromTheFirstClass.setAccessible(true);
currentFieldFromTheSecondClass.setAccessible(true);
//JSFMessageUtil.addMsgLog(JSFMessageUtil.matricula, ReflectionUtil.class.getCanonicalName(), "ReflectionUtil: " + currentFieldFromTheSecondClass.get(b));
currentFieldFromTheFirstClass.set(a, currentFieldFromTheSecondClass.get(b));
}
//}
}
}
}
return a;
}
The call
public void update(Aluno a) {
try {
Aluno aluno = new Aluno();//(Aluno) em.find(Aluno.class, a.getId());
em.getTransaction().begin();
//aluno.setNome(a.getNome());
ReflectionUtil.copyAttributesFromTo(aluno, a);
em.merge(aluno);
em.getTransaction().commit();
} catch (Exception e) {
System.out.println("Error " + e.getLocalizedMessage());
JOptionPane.showMessageDialog(null, e.getLocalizedMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
} finally {
em.close();
}
}

Get the class instance variables and print their values using reflection

I have a 2 POJO classes with getters and setters,now i am trying to get all the class instance variables of that class.
I got to know that we can use reflection how to do it?
This is my POJO Class which will extend my reflection class.
class Details{
private int age;
private String name;
}
Reflection class is like this:
class Reflection{
public String toString(){
return all the fields of that class
}
You could do something like this:
public void printFields(Object obj) throws Exception {
Class<?> objClass = obj.getClass();
Field[] fields = objClass.getFields();
for(Field field : fields) {
String name = field.getName();
Object value = field.get(obj);
System.out.println(name + ": " + value.toString());
}
}
This would only print the public fields, to print private fields use class.getDeclaredFields recursively.
Or if you would extend the class:
public String toString() {
try {
StringBuffer sb = new StringBuffer();
Class<?> objClass = this.getClass();
Field[] fields = objClass.getFields();
for(Field field : fields) {
String name = field.getName();
Object value = field.get(this);
sb.append(name + ": " + value.toString() + "\n");
}
return sb.toString();
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
Adding one more line to the above code. If you want to access the private properties of the class use below line
field.setAccessible(true);
ClassLoader classLoader = Main.class.getClassLoader();
try {
Class cls = classLoader.loadClass("com.example.Example");
Object clsObject = cls.newInstance();
Field[] fields = cls.getFields();
for (Field field : fields) {
String name = field.getName();
Object value = field.get(clsObject);
System.out.println("Name : "+name+" Value : "+value);
}
System.out.println(cls.getName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The resolution code or answer which mentioned above has one issue.
To access the value of a private varible their access type must be set to true
Field[] fields = objClass.getDeclaredFields();
for (Field field : fields) {
NotNull notNull = field.getAnnotation(NotNull.class);
field.setAccessible(true);
}
else it will throw java.lang.IllegalAccessException. Class Reflection can not access a member of class Details with modifiers "private"

Using reflection in Java to create a new instance with the reference variable type set to the new instance class name?

All the examples I look at for reflection show creating a new instance of an unknown implementation, and casting that implementation to it's interface. The issue with this is that now you can't call any new methods (only overrides) on the implementing class, as your object reference variable has the interface type. Here is what I have:
Class c = null;
try {
c = Class.forName("com.path.to.ImplementationType");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
InterfaceType interfaceType = null;
try {
interfaceType = (InterfaceType)c.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
If I only have a reference to "com.path.to.ImplementationType", and I don't know what that type might be (it is coming from a config file), then how can I use the class name to cast it to ImplementationType? Is this even possible?
This line seems to sum up the crux of your problem:
The issue with this is that now you can't call any new methods (only overrides) on the implementing class, as your object reference variable has the interface type.
You are pretty stuck in your current implementation, as not only do you have to attempt a cast, you also need the definition of the method(s) that you want to call on this subclass. I see two options:
1. As stated elsewhere, you cannot use the String representation of the Class name to cast your reflected instance to a known type. You can, however, use a String equals() test to determine whether your class is of the type that you want, and then perform a hard-coded cast:
try {
String className = "com.path.to.ImplementationType";// really passed in from config
Class c = Class.forName(className);
InterfaceType interfaceType = (InterfaceType)c.newInstance();
if (className.equals("com.path.to.ImplementationType") {
((ImplementationType)interfaceType).doSomethingOnlyICanDo();
}
} catch (Exception e) {
e.printStackTrace();
}
This looks pretty ugly, and it ruins the nice config-driven process that you have. I dont suggest you do this, it is just an example.
2. Another option you have is to extend your reflection from just Class/Object creation to include Method reflection. If you can create the Class from a String passed in from a config file, you can also pass in a method name from that config file and, via reflection, get an instance of the Method itself from your Class object. You can then call invoke(http://java.sun.com/javase/6/docs/api/java/lang/reflect/Method.html#invoke(java.lang.Object, java.lang.Object...)) on the Method, passing in the instance of your class that you created. I think this will help you get what you are after.
Here is some code to serve as an example. Note that I have taken the liberty of hard coding the params for the methods. You could specify them in a config as well, and would need to reflect on their class names to define their Class obejcts and instances.
public class Foo {
public void printAMessage() {
System.out.println(toString()+":a message");
}
public void printAnotherMessage(String theString) {
System.out.println(toString()+":another message:" + theString);
}
public static void main(String[] args) {
Class c = null;
try {
c = Class.forName("Foo");
Method method1 = c.getDeclaredMethod("printAMessage", new Class[]{});
Method method2 = c.getDeclaredMethod("printAnotherMessage", new Class[]{String.class});
Object o = c.newInstance();
System.out.println("this is my instance:" + o.toString());
method1.invoke(o);
method2.invoke(o, "this is my message, from a config file, of course");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException nsme){
nsme.printStackTrace();
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InstantiationException ie) {
ie.printStackTrace();
} catch (InvocationTargetException ite) {
ite.printStackTrace();
}
}
}
and my output:
this is my instance:Foo#e0cf70
Foo#e0cf70:a message
Foo#e0cf70:another message:this is my message, from a config file, of course
//====Single Class Reference used to retrieve object for fields and initial values. Performance enhancing only====
Class<?> reference = vector.get(0).getClass();
Object obj = reference.newInstance();
Field[] objFields = obj.getClass().getFields();
I'm not absolutely sure I got your question correctly, but it seems you want something like this:
Class c = null;
try {
c = Class.forName("com.path.to.ImplementationType");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
T interfaceType = null;
try {
interfaceType = (T) c.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Where T can be defined in method level or in class level, i.e. <T extends InterfaceType>
As an addendum to akf's answer you could use instanceof checks instead of String equals() calls:
String cname="com.some.vendor.Impl";
try {
Class c=this.getClass().getClassLoader().loadClass(cname);
Object o= c.newInstance();
if(o instanceof Spam) {
Spam spam=(Spam) o;
process(spam);
}
else if(o instanceof Ham) {
Ham ham = (Ham) o;
process(ham);
}
/* etcetera */
}
catch(SecurityException se) {
System.err.printf("Someone trying to game the system?%nOr a rename is in order because this JVM doesn't feel comfortable with: “%s”", cname);
se.printStackTrace();
}
catch(LinkageError le) {
System.err.printf("Seems like a bad class to this JVM: “%s”.", cname);
le.printStackTrace();
}
catch(RuntimeException re) {
// runtime exceptions I might have forgotten. Classloaders are wont to produce those.
re.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
Note the liberal hardcoding of some values. Anyways the main points are:
Use instanceof rather than equals(). If anything, it will co-operate better when refactoring.
Be sure to catch these runtime errors and security ones too.
You want to be able to pass in a Class and get a type-safe instance of that class? Try the following:
public static void main(String [] args) throws Exception {
String s = instanceOf(String.class);
}
public static <T> T instanceOf (Class<T> clazz) throws Exception {
return clazz.newInstance();
}
If you knew the Class of ImplementationType you could create an instance of it. So what you are trying to do is not possible.

Categories