1

I hava a model in Java 1.6 similar to this example:

public class Animal {
  public String color;
}

public class Dog extends Animal {
  public Float height;
}

public class Bird extends Animal {
  public Integer wings;
}

Now I want cast from Animal to anyone child. I know that It's throw a runtime exception for forbidden cast. So I think that it's possible have a children only with the parent fields with the help of a constructor and java reflection. Example:

public class Animal {
  public String color;

  public Animal(Animal old){
    //Set all fields by reflection. but how!
  }
}

public class Dog extends Animal {
  public Float height;

  public Dog (Animal old){
     super(old);
  }
}

public class Bird extends Animal {
  public Integer wings;
  public Bird (Animal old){
     super(old);
  }
}

But how set all de parent fields with reflection?

SOLUTION BY Thinaesh (https://stackoverflow.com/a/17697270/1474638) Thanks!. I'm using Spring so only I need to do in the parent constructor the next:

public Animal(Animal old){
  super();
  BeanUtils.copyProperties(old, this);
}

  public Dog(Animal old){
  super(old);
}

public Bird(Animal old){
  super(old);
}
Community
  • 1
  • 1
earnaz
  • 335
  • 1
  • 4
  • 19
  • Side note: if you don't need them, use primitives instead of primitive wrapper objects – BackSlash Jul 17 '13 at 08:59
  • 1
    How is that supposed to work? You cannot set a ``height`` (a field from class Dog) to Animal, as there is no such field. – f1sh Jul 17 '13 at 09:03
  • @f1sh I don't want set children field's. I want a children object with the parent fields setted. Obviously that the children fields must have null values because in parent I don't have this fields. – earnaz Jul 17 '13 at 09:05
  • So you want to cast a Bird to a Dog? If you don't care about the class hierarchy you have, you can just have all Animals. BTW Don't use wrappers or float unless you have to. Use primitives and double instead. – Peter Lawrey Jul 17 '13 at 09:07
  • 1
    @earnaz what does that have to do with reflection? In the constructor ``Animal(Animal old)`` you simply state ``this.color = old.color;`` – f1sh Jul 17 '13 at 09:09
  • @PeterLawrey No! I wont a Dog or a Bird from an Animal. For example I have an Animal with black color, and I wan't to have a Dog with black color and Bird with black color from Animal class. – earnaz Jul 17 '13 at 09:10
  • @f1sh Yes I can do it, but the class Animal could change and I do not want to forget a field in the allocation. – earnaz Jul 17 '13 at 09:11
  • @earnaz all the fields of Animal are copied in the constructor of Animal, so if you change that class, you just change the constructor. – f1sh Jul 17 '13 at 09:25
  • @f1sh In my real class I have 15 constructor and 26 fields. If I change the class I do not want to be worried that someone forget a field – earnaz Jul 17 '13 at 09:31

2 Answers2

2

Try BeanUtils.copyProperties from Apache commons library.(Same thing available in Spring's BeanUtils class.

0

After getting some clarification through the comments to your question:

All you are doing here is copying values in a class's constructor. There is no need for reflection. Reflection should be avoided unless there is no other way.

f1sh
  • 11,489
  • 3
  • 25
  • 51